Skip to content

Commit 96b3872

Browse files
committed
v.0.1.0-beta.3
Features: - Mapping of uninitialized properties in the model; - Custom mapping of references; - Ability to ignore auto-mapped properties; - Refactored name of mapping method 'add' to 'autoMapper'; - Added 'mapper' mapping method for manual mapping; - Refactor name of the mapping method from 'mapper' column to 'column';
1 parent 22aba95 commit 96b3872

12 files changed

+229
-265
lines changed

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.2",
3+
"version": "0.1.0-beta.3",
44
"description": "Library to assist in creating and maintaining SQL commands.",
55
"main": "./src/index.js",
66
"types": "./src/index.d.ts",

src/core/enums/field-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export enum FieldType {
2-
STRING,
2+
STRING = 1,
33
NUMBER,
44
BOOLEAN,
55
DATE,

src/database-helper.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,52 @@
11
import { DatetimeUtils } from "./datetime-utils";
2-
import { ValueType, ValueTypeToParse } from "./core/utils";
2+
import { Utils, ValueType, ValueTypeToParse } from "./core/utils";
33
import * as moment from "moment";
44
import { FieldType } from "./core/enums/field-type";
55
import { ColumnType } from "./core/enums/column-type";
66
import { DatabaseBuilderError } from "./core/errors";
77

88
export class DatabaseHelper {
99

10+
public isTypeSimpleByType(type: FieldType): boolean {
11+
return type !== FieldType.OBJECT && type !== FieldType.FUNCTION && type !== FieldType.ARRAY;
12+
}
13+
1014
public isTypeSimple(value: ValueTypeToParse): boolean {
1115
const type = this.getType(value);
12-
return type !== FieldType.OBJECT && type !== FieldType.FUNCTION && type !== FieldType.ARRAY;
16+
return this.isTypeSimpleByType(type);
17+
}
18+
19+
public isTypeIgnoredInMapperByType(type: FieldType): boolean {
20+
return type === FieldType.ARRAY;
1321
}
1422

1523
public isTypeIgnoredInMapper(value: ValueTypeToParse): boolean {
1624
const type = this.getType(value);
17-
return type === FieldType.ARRAY;
25+
return this.isTypeIgnoredInMapperByType(type);
1826
}
1927

20-
public getType(value: ValueTypeToParse): FieldType {
21-
const valueFormatted = this.preFormatValue(value);
22-
const tipo = typeof valueFormatted;
23-
switch (tipo) {
28+
public getFieldType<T>(type: string | (new () => T), constructorName?: string) {
29+
const typeCase: string = (Utils.isString(type) ? type as string : (type as new () => void).name).toLowerCase();
30+
switch (typeCase) {
2431
case "string":
2532
return FieldType.STRING;
2633
case "number":
2734
return FieldType.NUMBER;
2835
case "boolean":
2936
return FieldType.BOOLEAN;
3037
case "object":
31-
// tratar date como inteiro
32-
if (
33-
valueFormatted.constructor.name === "Date"
34-
|| valueFormatted.constructor.name === "Moment"
35-
) {
36-
return FieldType.DATE;
37-
}
38-
if (Array.isArray(valueFormatted)) {
39-
return FieldType.ARRAY;
38+
if (constructorName) {
39+
// tratar date como inteiro
40+
if (
41+
constructorName === "Date"
42+
||
43+
constructorName === "Moment"
44+
) {
45+
return FieldType.DATE;
46+
}
47+
if (constructorName === "Array") {
48+
return FieldType.ARRAY;
49+
}
4050
}
4151
// serializar todos os objetos
4252
return FieldType.OBJECT;
@@ -45,10 +55,22 @@ export class DatabaseHelper {
4555
case "undefined":
4656
return FieldType.NULL;
4757
default:
48-
throw new DatabaseBuilderError(`type: '${tipo}', value: '${valueFormatted}' não configurado!`);
58+
if (
59+
!Utils.isString(type) &&
60+
type.constructor.length === 0 ? new (type as (new () => T))() : {} instanceof Object
61+
) {
62+
return FieldType.OBJECT;
63+
}
64+
throw new DatabaseBuilderError(`type: '${type}', constructor name: '${constructorName}' não configurado!`);
4965
}
5066
}
5167

68+
public getType(value: ValueTypeToParse): FieldType {
69+
const valueFormatted = this.preFormatValue(value);
70+
const tipo = typeof valueFormatted;
71+
return this.getFieldType(tipo, valueFormatted ? valueFormatted.constructor.name : void 0);
72+
}
73+
5274
public parseToColumnType(type: FieldType): ColumnType {
5375
switch (type) {
5476
case FieldType.STRING:

src/ddl/ddl-columns-builder.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,5 @@ export class DdlColumnsBuilder<T> extends ColumnsBaseBuilder<DdlColumnsBuilder<T
4040
throw new DatabaseBuilderError(`Mapper '${this.metadata.newable.name}', column '${column.name}' of type 'NULL' not supported!`);
4141
}
4242
return `${column.name} ${Utils.parseColumnType(column.type)}${column.isKeyColumn ? ` NOT NULL PRIMARY KEY` : ""}${column.isAutoIncrement ? ` AUTOINCREMENT` : ""}`;
43-
// return `${column.name} ${Utils.parseColumnType(column.type)}${column.isAutoIncrement ? ` AUTOINCREMENT` : ""}`;
4443
}
4544
}

src/mapper/mapper-base.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,44 @@ export class MapperBase implements GetMapper {
1818
) {
1919
}
2020

21-
// public mapper(
22-
// readOnly?: boolean,
23-
// settings: MapperSettingsModel = this._defaultSettings,
24-
// ...defaultsMapper: Array<new () => any>
25-
// ): MapperBase {
26-
// defaultsMapper.forEach(mapper => {
27-
// this.add(mapper, readOnly, settings);
28-
// });
29-
// return this;
30-
// }
31-
3221
/**
33-
* Added mapper for column
22+
* Auto Mapper Table for Model, primary key and all column initialized in model class
3423
* @param newable Type Model
3524
* @param keyColumn Expression primary key
3625
* @param isAutoIncrement If primary key is autoincrement, default 'false'
3726
* @param readOnly if column is readonly, default 'false'
3827
* @param settings settings mapper, default settings construtor
3928
*/
40-
public add<T>(
29+
public autoMapper<T>(
4130
newable: new () => T,
4231
keyColumn: Expression<T>,
4332
isAutoIncrement?: boolean,
4433
readOnly?: boolean,
45-
settings: MapperSettingsModel = this._defaultSettings,
46-
// advancedMapper: (metadata: MetadataTable<T>) => void = void 0
34+
settings: MapperSettingsModel = this._defaultSettings
4735
): MetadataTable<T> {
48-
// ): MapperBase {
4936
const metadata = new MetadataTable(newable, this._databaseHelper, this, readOnly)
5037
.key(keyColumn, isAutoIncrement)
5138
.autoMapper(
5239
settings.references,
5340
settings.referencesId,
5441
settings.referencesIdRecursive
5542
);
56-
// if (advancedMapper) {
57-
// advancedMapper(metadata);
58-
// }
5943
this.push(metadata);
6044
return metadata;
61-
// return this;
45+
}
46+
47+
/**
48+
* Mapper Table for Model
49+
* @param newable Type Model
50+
* @param readOnly if column is readonly, default 'false'
51+
*/
52+
public mapper<T>(
53+
newable: new () => T,
54+
readOnly?: boolean
55+
): MetadataTable<T> {
56+
const metadata = new MetadataTable(newable, this._databaseHelper, this, readOnly);
57+
this.push(metadata);
58+
return metadata;
6259
}
6360

6461
public has<T>(tKey: (new () => T) | string): boolean {
@@ -86,7 +83,10 @@ export class MapperBase implements GetMapper {
8683
}
8784

8885
private push(metadataTable: MetadataTable<any>): void {
89-
this._mappers.set(metadataTable.instance.constructor.name, metadataTable);
86+
if (this.has(metadataTable.newable.name)) {
87+
throw new DatabaseBuilderError(`Duplicate mapper: '${metadataTable.newable.name}'`);
88+
}
89+
this._mappers.set(metadataTable.newable.name, metadataTable);
9090
}
9191

9292
private resolveKey<T>(tKey: (new () => T) | string): string {

0 commit comments

Comments
 (0)