Skip to content

Commit 38bfa60

Browse files
committed
Fixed bug, in insert generation with sub-level property, did not retrieve the value of the object correctly.
1 parent c0b02cf commit 38bfa60

File tree

5 files changed

+147
-6
lines changed

5 files changed

+147
-6
lines changed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
{
22
"name": "database-builder",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "Framework to assist in database manipulation (DDL and CRUD)",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",
77
"scripts": {
8-
"test": " tsc & mocha src/**/*.spec.js",
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",
911
"test-debug": "mocha -r ts-node/register src/**/*.spec.ts --debug-brk"
1012
},
1113
"dependencies": {
1214
"moment": "2.20.1",
13-
"lambda-expression": "0.1.1"
15+
"lambda-expression": "0.1.2"
1416
},
1517
"devDependencies": {
1618
"@types/chai": "^4.1.1",

src/core/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ export class Utils {
116116
return isNameColumn.test(column);
117117
}
118118

119+
public static normalizeSqlString(inputSql: string): string{
120+
return inputSql.replace(/\s+/g, ' ').trim();
121+
}
122+
119123
private static getExpressionUtils(): ExpressionUtils {
120124
return this._expressionUtils = this._expressionUtils ? this._expressionUtils : new ExpressionUtils();
121125
}

src/crud/insert/insert-builder.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Utils } from './../../core/utils';
12
import { InsertColumnsBuilder } from './insert-columns-builder';
23
import { MetadataTable } from "./../../metadata-table";
34
import { CrudBaseBuilder } from "../crud-base-builder";
@@ -27,9 +28,11 @@ export class InsertBuilder<T> extends CrudBaseBuilder<T, InsertColumnsBuilder<T>
2728

2829
return {
2930
params: this.getColumnsCompiled().params,
30-
sql: `INSERT INTO ${this._tablename}
31-
(${this.getColumnsCompiled().columns.join(", ")})
32-
VALUES (${parameterValues.join(", ")})`,
31+
sql: Utils.normalizeSqlString(
32+
`INSERT INTO ${this._tablename}
33+
(${this.getColumnsCompiled().columns.join(", ")})
34+
VALUES (${parameterValues.join(", ")})`
35+
),
3336
};
3437
}
3538

src/test/mappers-table.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Regiao } from './models/regiao';
2+
import { SubRegiao } from './models/sub-regiao';
3+
import { Uf } from './models/uf';
4+
import { Cidade } from './models/cidade';
5+
import { Cliente } from './models/cliente';
6+
import { MetadataTable } from './../metadata-table';
7+
import { Classificacao } from './models/classificacao';
8+
import { DatabaseHelper } from './../database-helper';
9+
import { GetMapper } from "../index";
10+
11+
12+
export class MappersTable implements GetMapper {
13+
14+
private _databaseHelper: DatabaseHelper = new DatabaseHelper()
15+
16+
17+
public classificacaoMapper =
18+
new MetadataTable(Classificacao, this._databaseHelper)
19+
.autoMapper(false, true, false);
20+
public clienteMapper =
21+
new MetadataTable(Cliente, this._databaseHelper)
22+
.autoMapper(false, true, false);
23+
public cidadeMapper =
24+
new MetadataTable(Cidade, this._databaseHelper)
25+
.autoMapper(false, true, false);
26+
public ufMapper =
27+
new MetadataTable(Uf, this._databaseHelper)
28+
.autoMapper(false, true, false);
29+
public subRegiaoMapper =
30+
new MetadataTable(SubRegiao, this._databaseHelper)
31+
.autoMapper(false, true, false);
32+
public regiaoMapper =
33+
new MetadataTable(Regiao, this._databaseHelper)
34+
.autoMapper(false, true, false);
35+
36+
private _mappersKeyValue: Map<string, MetadataTable<any>> = new Map([
37+
this.createEntry(this.classificacaoMapper),
38+
this.createEntry(this.clienteMapper),
39+
this.createEntry(this.cidadeMapper),
40+
this.createEntry(this.ufMapper),
41+
this.createEntry(this.subRegiaoMapper),
42+
this.createEntry(this.regiaoMapper),
43+
]);
44+
45+
/**
46+
* Find mapper metadata by key
47+
*
48+
* @public
49+
* @param {string} tKey
50+
* @returns {MetadataTable}
51+
* @memberof MappersTable
52+
*/
53+
public getMapper<T>(tKey: new () => T): MetadataTable<T> {
54+
return this._mappersKeyValue.get(tKey.name);
55+
}
56+
57+
public forEachMapper(
58+
callbackfn: (
59+
value: MetadataTable<any>,
60+
key: string,
61+
map: Map<string, MetadataTable<any>>
62+
) => void,
63+
thisArg?: any
64+
) {
65+
this._mappersKeyValue.forEach(callbackfn);
66+
}
67+
68+
/**
69+
* Find mapper metadata by key and merge with new instance of data
70+
*
71+
* @param {string} tKey
72+
* @param {*} newInstance
73+
* @returns {MetadataTable}
74+
* @memberof MappersTable
75+
* */
76+
public getMapperMerge<T>(tKey: new () => T, newInstance: any): MetadataTable<T> {
77+
let mapper = this.getMapper(tKey);
78+
if (!mapper) {
79+
throw `Mapper to ${tKey.name} not found!`;
80+
}
81+
let metadataCopy: MetadataTable<T> = Object.assign({}, mapper);
82+
metadataCopy.instance = Object.assign(metadataCopy.instance, newInstance);
83+
return metadataCopy;
84+
}
85+
86+
private createEntry(metadataTable: MetadataTable<any>): [string, MetadataTable<any>] {
87+
return [metadataTable.instance.constructor.name, metadataTable];
88+
}
89+
}

src/test/test.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import { Regiao } from './models/regiao';
2+
import { SubRegiao } from './models/sub-regiao';
3+
import { Uf } from './models/uf';
4+
import { Classificacao } from './models/classificacao';
15
import { ProjectionCompiled } from './../crud/projection-compiled';
26
import { Projection } from './../crud/enums/projection';
37
import { Query } from './../crud/query/query';
48
import { Cliente } from './models/cliente';
59
import { expect, assert } from "chai";
610
import { Cidade } from './models/cidade';
711
import { Operator } from '../crud/enums/operator';
12+
import { Crud, Insert } from '../index';
13+
import { MappersTable } from './mappers-table';
814

915
describe("Query method", () => {
1016

@@ -137,4 +143,41 @@ describe("Query method", () => {
137143

138144
// TODO: query from query
139145

146+
147+
});
148+
149+
describe("Mapper", () => {
150+
let clienteToSave = <Cliente>{
151+
id: 1,
152+
razaoSocial: "Razão",
153+
apelido: "Apelido",
154+
cidade: <Cidade>{
155+
id: 2,
156+
nome: "Cidade",
157+
uf: <Uf>{
158+
id: "SC",
159+
nome: "Santa Catarina"
160+
},
161+
subRegiao: <SubRegiao>{
162+
id: 4,
163+
nome: "Sub Região",
164+
regiao: <Regiao>{
165+
id: 5,
166+
nome: "Região"
167+
}
168+
},
169+
},
170+
classificacao: <Classificacao>{
171+
id: 3,
172+
descricao: "Top"
173+
},
174+
desativo: false
175+
};
176+
177+
it("Test mapper insert", () => {
178+
const result = new Insert(Cliente, clienteToSave, new MappersTable().clienteMapper).compile();
179+
expect(result.params.toString()).to.equal([1, 'Razão', 'Apelido', false, 2, 3].toString());
180+
expect(result.query).to.equal("INSERT INTO Cliente (id, razaoSocial, apelido, desativo, cidade_id, classificacao_id) VALUES (?, ?, ?, ?, ?, ?)");
181+
});
182+
140183
});

0 commit comments

Comments
 (0)