Skip to content

Commit 4336300

Browse files
committed
Implementado possibilidade de mapeamento hasMany, que pode fazer manipulações por cascata,
1 parent 0b80094 commit 4336300

67 files changed

Lines changed: 1472 additions & 659 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "database-builder",
3-
"version": "0.1.0-beta.16",
3+
"version": "0.1.0-rc.1",
44
"description": "Library to assist in creating and maintaining SQL commands.",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",

src/core/columns-base-builder.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { MetadataTable } from "../metadata-table";
21
import { ExpressionOrColumn, Utils, ValueTypeToParse } from "./utils";
32
import { MapperTable } from "../mapper-table";
43
import { Column } from "./column";
@@ -103,10 +102,5 @@ export abstract class ColumnsBaseBuilder<
103102
);
104103
}
105104
}
106-
// for (const key in mapper.dependencies) {
107-
// if (mapper.dependencies.hasOwnProperty(key)) {
108-
// const dependency = mapper.dependencies[key];
109-
// }
110-
// }
111105
}
112106
}

src/core/columns-values-builder.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Column } from "./column";
77
import { FieldType } from "./enums/field-type";
88
import { ColumnsCompiled } from "./columns-compiled";
99
import { MapperTable } from "../mapper-table";
10+
import { ColumnType } from "./enums/column-type";
1011

1112
export abstract class ColumnsValuesBuilder<
1213
T, TThis extends ColumnsValuesBuilder<T, TThis>>
@@ -32,11 +33,12 @@ export abstract class ColumnsValuesBuilder<
3233
): TThis {
3334
switch (primaryKeyType) {
3435
case PrimaryKeyType.Assigned:
35-
if (!value) {
36+
if (value === void 0) {
3637
throw new DatabaseBuilderError("Primary key to be informed when generation strategy is 'Assigned'!");
3738
}
39+
break;
3840
case PrimaryKeyType.Guid:
39-
if (!value && this.allowGenerateKey()) {
41+
if (value === void 0 && this.allowGenerateKey()) {
4042
// gerar GUID
4143
value = Utils.GUID();
4244
// set value GUID in model
@@ -89,10 +91,12 @@ export abstract class ColumnsValuesBuilder<
8991
};
9092
result.keyColumns = this.columns.filter(x => !!x.primaryKeyType).map(x => x.name);
9193
this.columns.forEach((column) => {
92-
const columnName = this.columnFormat(column);
93-
if (columnName !== void 0) {
94-
result.columns.push(columnName);
95-
result.params.push(column.value);
94+
if (this.isAddColumn(column)) {
95+
const columnName = this.columnFormat(column);
96+
if (columnName !== void 0) {
97+
result.columns.push(columnName);
98+
result.params.push(column.value);
99+
}
96100
}
97101
});
98102
return result;
@@ -102,7 +106,18 @@ export abstract class ColumnsValuesBuilder<
102106
return false;
103107
}
104108

105-
protected abstract columnFormat(column: Column): string;
109+
protected isAddColumn(column: Column): boolean {
110+
// is table reference/list
111+
const columnType = Utils.parseColumnType(column.type);
112+
if (columnType === ColumnType.TABLE_REFERENCE) {
113+
return false;
114+
}
115+
return true;
116+
}
117+
118+
protected columnFormat(column: Column): string {
119+
return column.name;
120+
}
106121

107122
private getValueByExpression<TReturn>(expression: ExpressionOrColumn<TReturn, T>): TReturn {
108123
return Utils.getValue(this.modelToSave, expression);

src/core/crud-compiled.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ParamType } from "./utils";
1+
// import { ParamType } from "./utils";
22

3-
export interface CrudCompiled {
4-
sql: string;
5-
params: ParamType[];
6-
}
3+
// export interface CrudCompiled {
4+
// sql: string;
5+
// params: ParamType[];
6+
// }

src/core/executable-builder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ export class ExecutableBuilder {
1313
compiled: QueryCompiled[],
1414
database: DatabaseBase,
1515
): Promise<DatabaseResult[]> {
16-
this.log(compiled);
16+
// this.log(compiled);
1717
return this.executorLinked(compiled, [], database);
1818
// return this.executeSql(database, compiled);
1919
}
2020

2121
private executeSql(
2222
database: DatabaseBase, compiled: QueryCompiled,
2323
): Promise<DatabaseResult> {
24+
this.log(compiled);
2425
if ((database as DatabaseObject).addTransaction) {
2526
return (database as DatabaseObject).executeSql(
2627
compiled.query,

src/core/key-utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { MapperTable } from "./../mapper-table";
22
import { MapperColumn } from "../mapper-column";
33
import { PrimaryKeyType } from "./enums/primary-key-type";
4-
import { Utils } from "./utils";
5-
import * as lodash from "lodash";
4+
import { ModelUtils } from "./model-utils";
65

76
export class KeyUtils {
87

98
public static setKey(mapperTable: MapperTable, model: any, keyValue: any): void {
109
// model[this.primaryKeyMapper(metadata).fieldReference] = keyValue;
11-
lodash.set(model, this.primaryKeyMapper(mapperTable).fieldReference, keyValue);
10+
ModelUtils.set(model, this.primaryKeyMapper(mapperTable).fieldReference, keyValue);
11+
// lodash.set(model, this.primaryKeyMapper(mapperTable).fieldReference, keyValue);
1212
}
1313

1414
public static getKey(mapperTable: MapperTable, model: any): any {
15-
return Utils.getValue(model, this.primaryKeyMapper(mapperTable).fieldReference);
15+
return ModelUtils.get(model, this.primaryKeyMapper(mapperTable).fieldReference);
16+
// return Utils.getValue(model, this.primaryKeyMapper(mapperTable).fieldReference);
1617
}
1718

1819
public static primaryKeyType(mapperTable: MapperTable): PrimaryKeyType {

src/core/model-utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as lodash from "lodash";
2+
3+
export class ModelUtils {
4+
5+
public static set(model: any, property: string, keyValue: any): void {
6+
lodash.set(model, property, keyValue);
7+
}
8+
9+
public static get(model: any, property: string): any {
10+
return lodash.get(model, property);
11+
}
12+
}

src/core/query-compilable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { QueryCompiled } from "./query-compiled";
22

33
export interface QueryCompilable {
4-
compile(): QueryCompiled[];
4+
compile(): QueryCompiled;
55
}

src/core/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ export class Utils {
226226
return new ProjectionCompiled(this.getColumn(projection as ExpressionOrColumn<TReturn, T>));
227227
}
228228

229+
public static getFieldExpression<T>(expression: Expression<T>): string {
230+
return this.getExpressionUtils().getColumnByExpression(expression, ".");
231+
}
232+
229233
public static getValue<TReturn, T>(instance: any, expression: ExpressionOrColumn<TReturn, T>): TReturn {
230234
return this.expressionOrColumn(expression) === ExpressionOrColumnEnum.Expression
231235
? this.getExpressionUtils().getValueByExpression(instance, expression as Expression<T>)

src/crud/crud-base-builder.ts

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,63 @@
11
import { WhereBuilder } from "./where-builder";
22
import { ColumnsValuesBuilder } from "../core/columns-values-builder";
33
import { ColumnsCompiled } from "../core/columns-compiled";
4-
import { QueryCompiled } from "../core/query-compiled";
5-
import { CrudCompiled } from "../core/crud-compiled";
6-
import { WhereCompiled } from "./where-compiled";
74
import { MapperTable } from "../mapper-table";
5+
import { SqlBaseBuilder } from "./sql-base-builder";
6+
import { QueryCompiled } from "../core/query-compiled";
87

9-
let NEXT_VALUE_ALIAS: number = 0;
8+
// let NEXT_VALUE_ALIAS: number = 0;
109

11-
export abstract class CrudBaseBuilder<T, TColumnsBuilder extends ColumnsValuesBuilder<T, TColumnsBuilder>> {
10+
export abstract class CrudBaseBuilder<
11+
T,
12+
TColumnsBuilder extends ColumnsValuesBuilder<T, TColumnsBuilder>
13+
> extends SqlBaseBuilder<T> {
1214

13-
protected _tablename: string;
15+
// protected _tablename: string;
1416

15-
private _columnsCompiled: ColumnsCompiled = { columns: [], params: [], keyColumns: [] };
16-
private _whereCompiled: WhereCompiled = { where: "", params: [] };
17+
private _columnsCompiled: ColumnsCompiled = {
18+
columns: [],
19+
params: [],
20+
keyColumns: []
21+
};
22+
// private _whereCompiled: WhereCompiled = { where: "", params: [] };
1723

1824
constructor(
19-
protected readonly _typeT: new () => T,
20-
// protected metadata: MetadataTable<T>,
21-
protected mapperTable: MapperTable,
22-
protected readonly _alias: string = void 0,
25+
typeT: new () => T,
26+
mapperTable: MapperTable,
27+
alias: string = void 0,
2328
) {
24-
this._tablename = _typeT.name;
25-
if (!this._alias) {
26-
this._alias = this.createUniqueAlias(this.defaultAlias(_typeT));
27-
}
29+
super(typeT, mapperTable, alias);
30+
// this._tablename = _typeT ? _typeT.name : mapperTable.tableName;
31+
// if (!this._alias) {
32+
// this._alias = this.createUniqueAlias(this.defaultAlias(this._tablename));
33+
// }
2834
}
2935

30-
// public getMetadata(): MetadataTable<T> {
31-
// return this.metadata;
36+
// public getMapper(): MapperTable {
37+
// return this.mapperTable;
3238
// }
3339

34-
public getMapper(): MapperTable {
35-
return this.mapperTable;
36-
}
37-
38-
public hasAlias(alias: string): boolean {
39-
if (this._alias === alias) {
40-
return true;
41-
}
42-
return false;
43-
}
40+
// public hasAlias(alias: string): boolean {
41+
// if (this._alias === alias) {
42+
// return true;
43+
// }
44+
// return false;
45+
// }
4446

4547
public compile(): QueryCompiled {
4648
const compiledBase = this.buildBase();
4749
return {
48-
params: compiledBase.params.concat(this._whereCompiled.params),
49-
query: `${compiledBase.sql}${this._whereCompiled.where}`,
50+
params: compiledBase.params.concat(this.whereCompiled.params),
51+
query: `${compiledBase.query}${this.whereCompiled.where}`,
5052
};
5153
}
54+
// public compile(): QueryCompiled {
55+
// const compiledBase = this.buildBase();
56+
// return {
57+
// params: compiledBase.params.concat(this._whereCompiled.params),
58+
// query: `${compiledBase.sql}${this._whereCompiled.where}`,
59+
// };
60+
// }
5261

5362
protected getColumnsCompiled(): ColumnsCompiled {
5463
if (!this._columnsCompiled.columns.length) {
@@ -77,32 +86,32 @@ export abstract class CrudBaseBuilder<T, TColumnsBuilder extends ColumnsValuesBu
7786
return instanceReturn;
7887
}
7988

80-
protected abstract buildBase(): CrudCompiled;
89+
// protected abstract buildBase(): CrudCompiled;
8190

8291
protected abstract setDefaultColumns(): void;
8392

8493
public abstract getModel(): T;
8594

86-
private compileWhere(compiled: WhereCompiled) {
87-
if (compiled.where.length) {
88-
this._whereCompiled.where += `${(this._whereCompiled.where.length ? " AND " : " WHERE ")}${compiled.where}`;
89-
this._whereCompiled.params = this._whereCompiled.params.concat(compiled.params);
90-
}
91-
}
95+
// private compileWhere(compiled: WhereCompiled) {
96+
// if (compiled.where.length) {
97+
// this._whereCompiled.where += `${(this._whereCompiled.where.length ? " AND " : " WHERE ")}${compiled.where}`;
98+
// this._whereCompiled.params = this._whereCompiled.params.concat(compiled.params);
99+
// }
100+
// }
92101

93-
private defaultAlias(typeT: new () => T) {
94-
if (typeT.name.length > 3) {
95-
return typeT.name.substring(0, 3);
96-
}
97-
return typeT.name;
98-
}
102+
// private defaultAlias(tableName: string) {
103+
// if (tableName.length > 3) {
104+
// return tableName.substring(0, 3);
105+
// }
106+
// return tableName;
107+
// }
99108

100-
private createUniqueAlias(aliasProposed: string): string {
101-
if (this.hasAlias(aliasProposed)) {
102-
return this.createUniqueAlias(`${aliasProposed}${NEXT_VALUE_ALIAS++}`);
103-
}
104-
return aliasProposed;
105-
}
109+
// private createUniqueAlias(aliasProposed: string): string {
110+
// if (this.hasAlias(aliasProposed)) {
111+
// return this.createUniqueAlias(`${aliasProposed}${NEXT_VALUE_ALIAS++}`);
112+
// }
113+
// return aliasProposed;
114+
// }
106115

107116
private compileColumns(compiled: ColumnsCompiled) {
108117
if (compiled.columns.length) {

0 commit comments

Comments
 (0)