Skip to content

Commit 22aba95

Browse files
committed
0.1.0-beta.2
1 parent 1efe381 commit 22aba95

File tree

12 files changed

+52
-36
lines changed

12 files changed

+52
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ $> npm install
163163
2. Use following commands based on what you'd like to do:
164164

165165
```shell
166-
$> npm test # runs test suite once and exit.
166+
$> npm run test # runs test suite once and exit.
167167
```
168168

169169
3. Have you found a bug, or want to develop a new feature?

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

src/crud/delete/delete-columns-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export class DeleteColumnsBuilder<T> extends ColumnsValuesBuilder<T, DeleteColum
99
}
1010

1111
protected columnFormat(column: Column): string {
12-
throw new DatabaseBuilderError("Method not supported.");
12+
throw new DatabaseBuilderError(`Mapper '${this.metadata.newable.name}', method not supported.`);
1313
}
1414
}

src/crud/query/query-builder-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export abstract class QueryBuilderBase<T, TQuery extends QueryBuilderBase<T, TQu
6666
alias = this.createUniqueAlias(this.defaultAlias(_typeT));
6767
}
6868
if (this.hasAlias(alias)) {
69-
throw new DatabaseBuilderError(`Alias: ${alias} já está sendo utilizado nesse contexto de query
69+
throw new DatabaseBuilderError(`Mapper '${this._typeT.name}', alias: ${alias} já está sendo utilizado nesse contexto de query
7070
(query: ${this.compile().query}).`);
7171
}
7272
this._alias = alias;

src/database-helper.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export class DatabaseHelper {
5252
public parseToColumnType(type: FieldType): ColumnType {
5353
switch (type) {
5454
case FieldType.STRING:
55+
case FieldType.ARRAY:
5556
case FieldType.OBJECT:
5657
return ColumnType.TEXT;
5758
case FieldType.DATE:
@@ -80,6 +81,7 @@ export class DatabaseHelper {
8081
public databaseToValue(columnValue: any, fieldType: FieldType) {
8182
switch (fieldType) {
8283
case FieldType.OBJECT:
84+
case FieldType.ARRAY:
8385
return JSON.parse(columnValue);
8486
case FieldType.DATE:
8587
return DatetimeUtils.databaseToDatetime(columnValue);
@@ -94,6 +96,7 @@ export class DatabaseHelper {
9496
const type = value !== void 0 ? this.getType(value) : fieldType;
9597
switch (type) {
9698
case FieldType.OBJECT:
99+
case FieldType.ARRAY:
97100
return JSON.stringify(value);
98101
case FieldType.DATE:
99102
return DatetimeUtils.datetimeToDatabase(value as moment.Moment);

src/ddl/ddl-columns-builder.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ export class DdlColumnsBuilder<T> extends ColumnsBaseBuilder<DdlColumnsBuilder<T
2929
protected columnFormat(column: Column): string {
3030
if (this.isCompositeKey()) {
3131
if (column.isAutoIncrement) {
32-
throw new DatabaseBuilderError("Auto increment not work to composite id");
32+
throw new DatabaseBuilderError(`Mapper '${this.metadata.newable.name}', auto increment not work to composite id`);
3333
}
3434
return `${column.name} ${Utils.parseColumnType(column.type)}`;
3535
}
3636
if (column.isAutoIncrement && !column.isKeyColumn) {
37-
throw new DatabaseBuilderError("Auto increment not work in column not primary key");
37+
throw new DatabaseBuilderError(`Mapper '${this.metadata.newable.name}', auto increment not work in column not primary key`);
38+
}
39+
if (column.type === FieldType.NULL) {
40+
throw new DatabaseBuilderError(`Mapper '${this.metadata.newable.name}', column '${column.name}' of type 'NULL' not supported!`);
3841
}
3942
return `${column.name} ${Utils.parseColumnType(column.type)}${column.isKeyColumn ? ` NOT NULL PRIMARY KEY` : ""}${column.isAutoIncrement ? ` AUTOINCREMENT` : ""}`;
4043
// return `${column.name} ${Utils.parseColumnType(column.type)}${column.isAutoIncrement ? ` AUTOINCREMENT` : ""}`;

src/mapper/mapper-base.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export class MapperBase implements GetMapper {
2929
// return this;
3030
// }
3131

32+
/**
33+
* Added mapper for column
34+
* @param newable Type Model
35+
* @param keyColumn Expression primary key
36+
* @param isAutoIncrement If primary key is autoincrement, default 'false'
37+
* @param readOnly if column is readonly, default 'false'
38+
* @param settings settings mapper, default settings construtor
39+
*/
3240
public add<T>(
3341
newable: new () => T,
3442
keyColumn: Expression<T>,

src/metadata-table.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export class MetadataTable<T> {
2020
public newable: new () => T,
2121
private _databaseHelper: DatabaseHelper,
2222
private _getMapper: GetMapper,
23-
public readOnly: boolean = false,
24-
// public keyColumn: string | string[] = "key",
23+
public readOnly: boolean = false
2524
) {
2625
this.instance = new newable();
2726
this.mapperTable = new MapperTable(newable.name);
@@ -46,7 +45,7 @@ export class MetadataTable<T> {
4645
referencesIdRecursive: boolean = true
4746
): MetadataTable<T> {
4847
if (this.keyColumns().length === 0) {
49-
throw new DatabaseBuilderError("No column as key was informed to the Mapper");
48+
throw new DatabaseBuilderError(`Mapper '${this.newable.name}', no column as key was informed to the Mapper`);
5049
}
5150
this.autoMapperColumns(references, referencesId, referencesIdRecursive);
5251
this._autoMapperCalled = true;
@@ -58,7 +57,7 @@ export class MetadataTable<T> {
5857
isAutoIncrement?: boolean
5958
): MetadataTable<T> {
6059
if (this._autoMapperCalled) {
61-
throw new DatabaseBuilderError("Column key must be informed before the call to 'autoMapper()'");
60+
throw new DatabaseBuilderError(`Mapper '${this.newable.name}', column key must be informed before the call to 'autoMapper()'`);
6261
}
6362
return this.mapper(expression, true, isAutoIncrement);
6463
}
@@ -77,10 +76,6 @@ export class MetadataTable<T> {
7776

7877
private isKeyColumn(key: string) {
7978
return (this.keyColumns().filter(x => x.column === key).length > 0);
80-
// if (Array.isArray(this.keyColumn)) {
81-
// return this.keyColumn.filter(x => x === key).length > 0;
82-
// }
83-
// return key === this.keyColumn;
8479
}
8580

8681
private autoMapperColumns(
@@ -90,14 +85,10 @@ export class MetadataTable<T> {
9085
): void {
9186
for (const key in this.instance) {
9287
if (key !== "constructor" && typeof this.instance[key] !== "function") {
93-
// const type = this._databaseHelper.getType(this.instance[key]);
94-
// if (((type !== FieldType.OBJECT)
9588
if (
9689
this._databaseHelper.isTypeSimple(this.instance[key])
9790
|| references
9891
) {
99-
// && !this.isKeyColumn(key)) {
100-
// && key !== this.keyColumn) {
10192
if (!this.isKeyColumn(key)) {
10293
this.setColumn(key, this.getTypeByValue(this.instance[key]));
10394
}
@@ -120,14 +111,13 @@ export class MetadataTable<T> {
120111

121112
if (key !== "constructor" && typeof keyInstanceMapper !== "function") {
122113
if (!this._databaseHelper.isTypeSimple(keyInstanceMapper)) {
123-
// if (keyInstanceMapper instanceof Object) {
124114
const mapperKey = this.getMapper(keyInstanceMapper.constructor.name);
125115
if (mapperKey !== void 0) {
126116
if (mapperKey.keyColumns() === void 0 || mapperKey.keyColumns().length < 1) {
127-
throw new DatabaseBuilderError(`Not key column for Key '${key}' the type '${keyInstanceMapper.constructor.name}'`);
117+
throw new DatabaseBuilderError(`Mapper '${this.newable.name}', not key column for Key '${key}' the type '${keyInstanceMapper.constructor.name}'`);
128118
}
129119
if (mapperKey.keyColumns().length > 1) {
130-
throw new DatabaseBuilderError(`Composite Id not supported (Key '${key}' the type '${keyInstanceMapper.constructor.name}')`);
120+
throw new DatabaseBuilderError(`Mapper '${this.newable.name}', composite Id not supported (Key '${key}' the type '${keyInstanceMapper.constructor.name}')`);
131121
}
132122
const keyMapped = mapperKey.keyColumns()[0];
133123
this.setColumn(
@@ -136,28 +126,15 @@ export class MetadataTable<T> {
136126
);
137127
} else {
138128
if (!this._databaseHelper.isTypeIgnoredInMapper(keyInstanceMapper)) {
139-
throw new DatabaseBuilderError(`Key '${key}' the type '${keyInstanceMapper.constructor.name}' not before mapped`);
129+
throw new DatabaseBuilderError(`Mapper '${this.newable.name}', key '${key}' the type '${keyInstanceMapper.constructor.name}' not before mapped`);
140130
}
141131
}
142132
}
143133
if (recursive && !this._databaseHelper.isTypeSimple(keyInstanceMapper)) {
144-
// if (recursive && keyInstanceMapper instanceof Object) {
145134
this.autoColumnsModelReferencesRecursive(
146135
keyInstanceMapper, `${ascendingRefName}${key}_`,
147136
recursive);
148137
}
149-
// if (
150-
// (instanceMapper[key] instanceof Object && instanceMapper[key].hasOwnProperty(referencesIdColumn))) {
151-
// this.setColumn(
152-
// `${ascendingRefName}${key}_${referencesIdColumn}`,
153-
// this.getTypeByValue(instanceMapper[key][referencesIdColumn]),
154-
// );
155-
// }
156-
// if (recursive && instanceMapper[key] instanceof Object) {
157-
// this.autoColumnsModelReferencesRecursive(
158-
// instanceMapper[key], `${ascendingRefName}${key}_`,
159-
// recursive);
160-
// }
161138
}
162139
}
163140
}

src/test/create.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LoginOffline } from "./models/login-offline";
12
import { Create } from "./../ddl/create/create";
23
import { CondicaoPagamento } from "./models/condicao-pagamento";
34
import { Marca } from "./models/marca";
@@ -94,4 +95,11 @@ describe("Create", () => {
9495
expect(result).to.equal(`CREATE TABLE IF NOT EXISTS TestClazz( internalKey INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, id INTEGER, description TEXT, disabled BOOLEAN, date INTEGER, dateMoment INTEGER, dateDate INTEGER, numero INTEGER, referenceTest_id INTEGER, referenceTestCode_code TEXT );`);
9596
});
9697

98+
it("LoginOffline", () => {
99+
const create = new Create(LoginOffline, mapper.get(LoginOffline));
100+
const result = create.compile();
101+
expect(result.length > 0).to.equal(true);
102+
expect(result).to.equal(`CREATE TABLE IF NOT EXISTS LoginOffline( internalKey INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, id INTEGER, hash TEXT, permissions TEXT );`);
103+
});
104+
97105
});

src/test/mappers-table-new.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LoginOffline } from "./models/login-offline";
12
import { TestClazzList } from "./models/test-clazz-list";
23
import { TestClazzRef } from "./models/test-clazz-ref";
34
import { TestClazz } from "./models/test-clazz";
@@ -13,6 +14,7 @@ import { MapperBase } from "./../mapper/mapper-base";
1314
import { DatabaseHelper } from "../database-helper";
1415
import { Regiao } from "./models/regiao";
1516
import { TestClazzRefCode } from "./models/test-clazz-ref-code";
17+
import { MapperSettingsModel } from "..";
1618

1719
export class MappersTableNew extends MapperBase {
1820

@@ -42,5 +44,12 @@ export class MappersTableNew extends MapperBase {
4244

4345
this.add(TestClazzList, x => x.internalKey, true);
4446

47+
const settingsReference: MapperSettingsModel = {
48+
references: true,
49+
referencesId: false,
50+
referencesIdRecursive: false
51+
};
52+
this.add(LoginOffline, x => x.internalKey, true, false, settingsReference);
53+
4554
}
4655
}

0 commit comments

Comments
 (0)