Skip to content

Commit 648d79c

Browse files
committed
v.0.1.0-beta.9
1 parent e1048de commit 648d79c

46 files changed

Lines changed: 518 additions & 157 deletions

Some content is hidden

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

BREAKING-CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#Breaking Changes
2+
3+
##Find by 'BREAKING-CHANGE:' in code:
4+

package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
{
22
"name": "database-builder",
3-
"version": "0.1.0-beta.6",
3+
"version": "0.1.0-beta.9",
44
"description": "Library to assist in creating and maintaining SQL commands.",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",
77
"scripts": {
8-
"test": "tsc & mocha src/**/*.spec.js",
9-
"test-grep": "tsc & mocha src/**/*.spec.js --grep",
10-
"test-single": "tsc & mocha src/**/*.spec.js --grep Mapper --debug-brk",
11-
"test-debug": "mocha -r ts-node/register src/**/*.spec.ts --debug-brk",
8+
"test": "tsc & mocha src/test/**/*.spec.js",
9+
"test-grep": "tsc & mocha src/test/**/*.spec.js --grep",
10+
"test-single": "tsc & mocha src/test/**/*.spec.js --grep Mapper --debug-brk",
11+
"test-debug": "mocha -r ts-node/register src/test/**/*.spec.ts --debug-brk",
1212
"publish-npm": "tsc & npm publish"
1313
},
1414
"dependencies": {
1515
"lambda-expression": ">=0.1.3",
16+
"lodash": "^4.17.11",
1617
"moment": ">=2.22.0",
1718
"uuid": ">=3.3.2"
1819
},
1920
"devDependencies": {
2021
"@types/chai": "^4.1.1",
22+
"@types/lodash": "^4.14.116",
2123
"@types/mocha": "^2.2.46",
24+
"@types/sqlite3": "^3.1.3",
2225
"@types/uuid": "^3.4.4",
2326
"chai": "^4.1.2",
2427
"mocha": "^5.0.0",
28+
"sqlite3": "^4.0.2",
2529
"ts-node": "^4.1.0",
2630
"tslint": "^5.9.1",
27-
"typescript": "2.8.1"
31+
"typescript": "3.0.3"
2832
},
2933
"keywords": [
3034
"orm",

src/core/columns-base-builder.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,23 @@ export abstract class ColumnsBaseBuilder<
3030
column: string,
3131
type: FieldType,
3232
primaryKeyType: PrimaryKeyType
33-
// isKeyColumn: boolean,
34-
// isAutoIncrement: boolean
3533
): TThis {
3634
this.columns.push({
3735
name: column,
3836
type,
3937
primaryKeyType
40-
// isKeyColumn,
41-
// isAutoIncrement
4238
} as TColumn);
4339
return this.getInstance();
4440
}
4541

4642
public set<TReturn extends ValueTypeToParse>(
4743
expression: ExpressionOrColumn<TReturn, T>,
4844
primaryKeyType: PrimaryKeyType
49-
// isKeyColumn: boolean,
50-
// isAutoIncrement: boolean
5145
): TThis {
5246
return this.setColumn(
5347
Utils.getColumn(expression),
5448
Utils.getType(this.metadata.instance, expression),
5549
primaryKeyType
56-
// isKeyColumn,
57-
// isAutoIncrement
5850
);
5951
}
6052

@@ -68,7 +60,6 @@ export abstract class ColumnsBaseBuilder<
6860
if (this.columns.hasOwnProperty(key)) {
6961
const column = this.columns[key];
7062
if (column.primaryKeyType) {
71-
// if (column.isKeyColumn) {
7263
result.keyColumns.push(column.name);
7364
}
7465
result.columns.push(this.columnFormat(column));
@@ -79,7 +70,6 @@ export abstract class ColumnsBaseBuilder<
7970

8071
protected isCompositeKey(): boolean {
8172
return this.metadata.mapperTable.columns.filter(x => !!x.primaryKeyType).length > 1;
82-
// return this.metadata.mapperTable.columns.filter(x => x.isKeyColumn === true).length > 1;
8373
}
8474

8575
protected abstract columnFormat(column: TColumn): string;
@@ -91,8 +81,6 @@ export abstract class ColumnsBaseBuilder<
9181
value: ValueTypeToParse,
9282
fieldType: FieldType,
9383
primaryKeyType: PrimaryKeyType
94-
// isKeyColumn: boolean,
95-
// isAutoIncrement: boolean
9684
): TThis;
9785

9886
private setAllColumns(mapper: MapperTable, modelWithValue: T): void {
@@ -104,8 +92,6 @@ export abstract class ColumnsBaseBuilder<
10492
Utils.getValue(modelWithValue, column.fieldReference),
10593
column.fieldType,
10694
column.primaryKeyType
107-
// column.isKeyColumn,
108-
// column.isAutoIncrement
10995
);
11096
}
11197
}

src/core/columns-values-builder.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { KeyUtils } from "./key-utils";
12
import { PrimaryKeyType } from "./enums/primary-key-type";
23
import { MetadataTable } from "../metadata-table";
34
import { ExpressionOrColumn, Utils, ValueTypeToParse } from "./utils";
@@ -22,21 +23,19 @@ export abstract class ColumnsValuesBuilder<
2223
value: ValueTypeToParse,
2324
fieldType: FieldType,
2425
primaryKeyType?: PrimaryKeyType
25-
// isKeyColumn?: boolean,
26-
// isAutoIncrement?: boolean
2726
): TThis {
2827
// verificar se é GUID, se for gerar um valor para o mesmo
29-
if (primaryKeyType === PrimaryKeyType.Guid) {
28+
if (primaryKeyType === PrimaryKeyType.Guid && !value) {
3029
// gerar GUID
3130
value = Utils.GUID();
31+
// set value GUID in model
32+
KeyUtils.setKey(this.metadata, this.modelToSave, value);
3233
}
3334
this.columns.push({
3435
name: column,
3536
type: fieldType,
3637
value: Utils.getValueType(value, fieldType),
3738
primaryKeyType
38-
// isKeyColumn,
39-
// isAutoIncrement
4039
});
4140
return this.getInstance();
4241
}
@@ -45,31 +44,23 @@ export abstract class ColumnsValuesBuilder<
4544
expression: ExpressionOrColumn<TReturn, T>,
4645
value: TReturn,
4746
primaryKeyType?: PrimaryKeyType
48-
// isKeyColumn?: boolean,
49-
// isAutoIncrement?: boolean
5047
): TThis {
5148
return this.setColumnValue(
5249
Utils.getColumn(expression),
5350
value,
5451
Utils.getType(value),
5552
primaryKeyType
56-
// isKeyColumn,
57-
// isAutoIncrement
5853
);
5954
}
6055

6156
public set<TReturn extends ValueTypeToParse>(
6257
expression: ExpressionOrColumn<TReturn, T>,
6358
primaryKeyType?: PrimaryKeyType
64-
// isKeyColumn?: boolean,
65-
// isAutoIncrement?: boolean
6659
): TThis {
6760
return this.setValue(
6861
expression,
6962
this.getValueByExpression(expression),
7063
primaryKeyType
71-
// isKeyColumn,
72-
// isAutoIncrement
7364
);
7465
}
7566

src/core/key-utils.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { MapperColumn } from "../mapper-column";
2+
import { PrimaryKeyType } from "./enums/primary-key-type";
3+
import { Utils } from "./utils";
4+
import * as lodash from "lodash";
5+
import { MetadataTable } from "../metadata-table";
6+
7+
export class KeyUtils {
8+
9+
public static setKey<T>(metadata: MetadataTable<T>, model: any, keyValue: any): void {
10+
// model[this.primaryKeyMapper(metadata).fieldReference] = keyValue;
11+
lodash.set(model, this.primaryKeyMapper(metadata).fieldReference, keyValue);
12+
}
13+
14+
public static getKey<T>(metadata: MetadataTable<T>, model: any): any {
15+
return Utils.getValue(model, this.primaryKeyMapper(metadata).fieldReference);
16+
}
17+
18+
public static primaryKeyType<T>(metadata: MetadataTable<T>): PrimaryKeyType {
19+
return this.primaryKeyMapper(metadata).primaryKeyType;
20+
}
21+
22+
public static isCompositeKey<T>(metadata: MetadataTable<T>): boolean {
23+
return this.primaryKeysMapper(metadata).length > 1;
24+
}
25+
26+
public static primaryKeyMapper<T>(metadata: MetadataTable<T>): MapperColumn {
27+
return this.primaryKeysMapper(metadata).find(_ => true);
28+
}
29+
30+
public static primaryKeysMapper<T>(metadata: MetadataTable<T>): MapperColumn[] {
31+
return metadata.mapperTable.columns.filter(x => !!x.primaryKeyType);
32+
}
33+
}

src/core/projections-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Projection } from "../crud/enums/projection";
22
import { ExpressionOrColumn, TypeProjection, Utils } from "./utils";
3-
import { ProjectionCompiled } from "..";
43
import { ProjectionsUtils } from "./projections-utils";
4+
import { ProjectionCompiled } from "../crud/projection-compiled";
55

66
export class ProjectionsHelper<T> {
77

src/core/projections-utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Projection } from "../crud/enums/projection";
22
import { ExpressionOrColumn, Utils } from "./utils";
3-
import { ProjectionCompiled } from "..";
4-
3+
import { ProjectionCompiled } from "../crud/projection-compiled";
54
export class ProjectionsUtils<T> {
65

76
public static readonly WILDCARD = "*";

src/core/query-compilable.ts

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

33
export interface QueryCompilable {
44
compile(): QueryCompiled;

src/core/row-result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MapperTable } from "../mapper-table";
22
import { ExpressionOrColumn, Utils } from "./utils";
3-
import { DatabaseHelper } from "..";
43
import { FieldType } from "./enums/field-type";
4+
import { DatabaseHelper } from "../database-helper";
55

66
export class RowResult<T> {
77
private _databaseHelper: DatabaseHelper;

src/crud/crud-base-builder.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ColumnsCompiled } from "../core/columns-compiled";
44
import { QueryCompiled } from "../core/query-compiled";
55
import { CrudCompiled } from "../core/crud-compiled";
66
import { WhereCompiled } from "./where-compiled";
7+
import { MetadataTable } from "../metadata-table";
78

89
let NEXT_VALUE_ALIAS: number = 0;
910

@@ -16,6 +17,7 @@ export abstract class CrudBaseBuilder<T, TColumnsBuilder extends ColumnsValuesBu
1617

1718
constructor(
1819
protected readonly _typeT: new () => T,
20+
protected metadata: MetadataTable<T>,
1921
protected readonly _alias: string = void 0,
2022
) {
2123
this._tablename = _typeT.name;
@@ -24,6 +26,10 @@ export abstract class CrudBaseBuilder<T, TColumnsBuilder extends ColumnsValuesBu
2426
}
2527
}
2628

29+
public getMetadata(): MetadataTable<T> {
30+
return this.metadata;
31+
}
32+
2733
public hasAlias(alias: string): boolean {
2834
if (this._alias === alias) {
2935
return true;
@@ -39,6 +45,30 @@ export abstract class CrudBaseBuilder<T, TColumnsBuilder extends ColumnsValuesBu
3945
};
4046
}
4147

48+
// public setKeyByModel(keyValue: any): void {
49+
// (this.getModel() as any)[this.primaryKeyMapper().fieldReference] = keyValue;
50+
// }
51+
52+
// public getKeyByModel(): any {
53+
// return Utils.getValue(this.getModel(), this.primaryKeyMapper().fieldReference);
54+
// }
55+
56+
// public primaryKeyType(): PrimaryKeyType {
57+
// return this.primaryKeyMapper().primaryKeyType;
58+
// }
59+
60+
// public isCompositeKey(): boolean {
61+
// return this.primaryKeysMapper().length > 1;
62+
// }
63+
64+
// public primaryKeyMapper(): MapperColumn {
65+
// return this.primaryKeysMapper().find(_ => true);
66+
// }
67+
68+
// public primaryKeysMapper(): MapperColumn[] {
69+
// return this.metadata.mapperTable.columns.filter(x => !!x.primaryKeyType);
70+
// }
71+
4272
protected getColumnsCompiled(): ColumnsCompiled {
4373
if (!this._columnsCompiled.columns.length) {
4474
this.setDefaultColumns();
@@ -70,6 +100,8 @@ export abstract class CrudBaseBuilder<T, TColumnsBuilder extends ColumnsValuesBu
70100

71101
protected abstract setDefaultColumns(): void;
72102

103+
public abstract getModel(): T;
104+
73105
private compileWhere(compiled: WhereCompiled) {
74106
if (compiled.where.length) {
75107
this._whereCompiled.where += `${(this._whereCompiled.where.length ? " AND " : " WHERE ")}${compiled.where}`;

0 commit comments

Comments
 (0)