Skip to content

Commit 20b95f1

Browse files
committed
#276 - Incluir exceção para PK Assigned que não tiver valor informado no insert.
1 parent 54a2d43 commit 20b95f1

5 files changed

Lines changed: 31 additions & 37 deletions

File tree

src/core/columns-values-builder.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DatabaseBuilderError } from "./errors";
12
import { KeyUtils } from "./key-utils";
23
import { PrimaryKeyType } from "./enums/primary-key-type";
34
import { MetadataTable } from "../metadata-table";
@@ -24,15 +25,22 @@ export abstract class ColumnsValuesBuilder<
2425
fieldType: FieldType,
2526
primaryKeyType?: PrimaryKeyType
2627
): TThis {
27-
if (
28-
primaryKeyType === PrimaryKeyType.Guid
29-
&& !value
30-
&& this.allowGenerateKey()
31-
) {
32-
// gerar GUID
33-
value = Utils.GUID();
34-
// set value GUID in model
35-
KeyUtils.setKey(this.metadata, this.modelToSave, value);
28+
switch (primaryKeyType) {
29+
case PrimaryKeyType.Assigned:
30+
if (!value) {
31+
throw new DatabaseBuilderError("Primary key to be informed when generation strategy is 'Assigned'!");
32+
}
33+
case PrimaryKeyType.Guid:
34+
if (!value && this.allowGenerateKey()) {
35+
// gerar GUID
36+
value = Utils.GUID();
37+
// set value GUID in model
38+
KeyUtils.setKey(this.metadata, this.modelToSave, value);
39+
}
40+
break;
41+
case PrimaryKeyType.AutoIncrement:
42+
default:
43+
break;
3644
}
3745
this.columns.push({
3846
name: column,
@@ -74,7 +82,6 @@ export abstract class ColumnsValuesBuilder<
7482
params: [],
7583
};
7684
result.keyColumns = this.columns.filter(x => !!x.primaryKeyType).map(x => x.name);
77-
// result.keyColumns = this.columns.filter(x => x.isKeyColumn).map(x => x.name);
7885
this.columns.forEach((column) => {
7986
const columnName = this.columnFormat(column);
8087
if (columnName !== void 0) {

src/core/errors.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11

22
export class DatabaseBuilderError extends Error {
33
constructor(message: string) {
4-
super();
5-
this.message = message;
4+
super(message);
5+
// Maintains proper stack trace for where our error was thrown (only available on V8)
6+
if (Error.captureStackTrace) {
7+
Error.captureStackTrace(this, DatabaseBuilderError);
8+
}
69
}
710
}

src/crud/crud-base-builder.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ export abstract class CrudBaseBuilder<T, TColumnsBuilder extends ColumnsValuesBu
4545
};
4646
}
4747

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-
7248
protected getColumnsCompiled(): ColumnsCompiled {
7349
if (!this._columnsCompiled.columns.length) {
7450
this.setDefaultColumns();

src/test/insert.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DatabaseBuilderError } from './../core/errors';
12
import { Pedido } from "./models/pedido";
23
import { Marca } from "./models/marca";
34
import { CondicaoPagamento } from "./models/condicao-pagamento";
@@ -34,6 +35,13 @@ describe("Insert", () => {
3435
expect(result.query).to.equal("INSERT INTO Regiao (codeImport, nome) VALUES (?, ?)");
3536
});
3637

38+
it("Regiao (primary key Assigned) key not informed!", () => {
39+
const sql = new Insert(Regiao, {
40+
nome: "Sul"
41+
} as Regiao, mapper.get(Regiao));
42+
expect(() => sql.compile()).to.throw("Primary key to be informed when generation strategy is 'Assigned'!");
43+
});
44+
3745
it("SubRegiao", () => {
3846
const result = new Insert(SubRegiao, ObjectToTest.subRegiao, mapper.get(SubRegiao)).compile();
3947
expect(result.params.toString()).to.equal([

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// "allowJs": true, /* Allow javascript files to be compiled. */
1212
// "checkJs": true, /* Report errors in .js files. */
1313
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
14-
"declaration": true, /* Generates corresponding '.d.ts' file. */
14+
"declaration": false, /* Generates corresponding '.d.ts' file. */
1515
// "sourceMap": true, /* Generates corresponding '.map' file. */
1616
// "outFile": "./", /* Concatenate and emit output to single file. */
1717
// "outDir": "./", /* Redirect output structure to the directory. */

0 commit comments

Comments
 (0)