Skip to content

Commit 1efe381

Browse files
committed
v.0.1.0-beta.1
Expressive improvements in relational mapping. With this, this version has breaks changes, related to relational mapping.
1 parent 9b8ebff commit 1efe381

37 files changed

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

src/core/columns-values-builder.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export abstract class ColumnsValuesBuilder<
2020
column: string,
2121
value: ValueTypeToParse,
2222
fieldType: FieldType,
23-
isKeyColumn: boolean,
24-
isAutoIncrement: boolean): TThis {
23+
isKeyColumn?: boolean,
24+
isAutoIncrement?: boolean): TThis {
2525
this.columns.push({
2626
name: column,
2727
type: fieldType,
@@ -35,8 +35,8 @@ export abstract class ColumnsValuesBuilder<
3535
public setValue<TReturn extends ValueTypeToParse>(
3636
expression: ExpressionOrColumn<TReturn, T>,
3737
value: TReturn,
38-
isKeyColumn: boolean,
39-
isAutoIncrement: boolean
38+
isKeyColumn?: boolean,
39+
isAutoIncrement?: boolean
4040
): TThis {
4141
return this.setColumnValue(
4242
Utils.getColumn(expression),
@@ -49,8 +49,8 @@ export abstract class ColumnsValuesBuilder<
4949

5050
public set<TReturn extends ValueTypeToParse>(
5151
expression: ExpressionOrColumn<TReturn, T>,
52-
isKeyColumn: boolean,
53-
isAutoIncrement: boolean
52+
isKeyColumn?: boolean,
53+
isAutoIncrement?: boolean
5454
): TThis {
5555
return this.setValue(
5656
expression,
@@ -66,6 +66,7 @@ export abstract class ColumnsValuesBuilder<
6666
keyColumns: [],
6767
params: [],
6868
};
69+
result.keyColumns = this.columns.filter(x => x.isKeyColumn).map(x => x.name);
6970
this.columns.forEach((column) => {
7071
const columnName = this.columnFormat(column);
7172
if (columnName !== void 0) {

src/core/enums/expression-or-value-enum.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export enum ExpressionOrValueEnum {
22
Expression,
33
Value,
44
Ref,
5-
Projection
5+
Projection,
6+
Null
67
}

src/core/enums/field-type.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ export enum FieldType {
55
DATE,
66
OBJECT,
77
FUNCTION,
8+
ARRAY,
9+
NULL
810
}

src/core/utils.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ export class Utils {
134134
public static expressionOrValue<T>(
135135
value: TypeWhere<T>
136136
): ExpressionOrValueEnum {
137-
return this.isProjectionsHelper(value)
138-
? ExpressionOrValueEnum.Projection
139-
: this.isColumnRef(value)
140-
? ExpressionOrValueEnum.Ref
141-
: this.isValue(value)
142-
? ExpressionOrValueEnum.Value
143-
: ExpressionOrValueEnum.Expression;
137+
return value === void 0
138+
? ExpressionOrValueEnum.Null
139+
: this.isProjectionsHelper(value)
140+
? ExpressionOrValueEnum.Projection
141+
: this.isColumnRef(value)
142+
? ExpressionOrValueEnum.Ref
143+
: this.isValue(value)
144+
? ExpressionOrValueEnum.Value
145+
: ExpressionOrValueEnum.Expression;
144146
}
145147

146148
public static getValueByTypeOrString<T>(param: TypeOrString<T>): string {
@@ -181,6 +183,11 @@ export class Utils {
181183
column: compiled.projection,
182184
params: compiled.params
183185
};
186+
case (ExpressionOrValueEnum.Null):
187+
return {
188+
column: Condition.IsNull,
189+
params: []
190+
};
184191
}
185192
}
186193

src/crud/crud.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ export class Crud {
2222
}
2323

2424
public update<T>(typeT: new () => T, modelToSave: T = void 0, alias: string = void 0,
25-
metadata: MetadataTable<T> = this._getMapper.getMapper(typeT),
25+
metadata: MetadataTable<T> = this._getMapper.get(typeT),
2626
database: DatabaseBase = this.getDatabase(),
2727
): Update<T> {
2828
return new Update(typeT, modelToSave, metadata, alias, database, this.enableLog);
2929
}
3030

3131
public insert<T>(typeT: new () => T, modelToSave: T = void 0, alias: string = void 0,
32-
metadata: MetadataTable<T> = this._getMapper.getMapper(typeT),
32+
metadata: MetadataTable<T> = this._getMapper.get(typeT),
3333
database: DatabaseBase = this.getDatabase(),
3434
): Insert<T> {
3535
return new Insert(typeT, modelToSave, metadata, alias, database, this.enableLog);
3636
}
3737

3838
public query<T>(typeT: new () => T, alias: string = void 0,
39-
metadata: MetadataTable<T> = this._getMapper.getMapper(typeT),
39+
metadata: MetadataTable<T> = this._getMapper.get(typeT),
4040
database: DatabaseBase = this.getDatabase(),
4141
): Query<T> {
4242
return new Query(typeT, alias, metadata, database, this.enableLog);

src/crud/projection-builder.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Projection } from "./enums/projection";
1010
import { ProjectionCase } from "./projection-case";
1111
import { MetadataTable } from "../metadata-table";
1212
import { ProjectionsHelper } from "../core/projections-helper";
13+
import { DatabaseBuilderError } from "..";
1314

1415
export class ProjectionBuilder<T> {
1516
private _projection: ProjectionCompiled = new ProjectionCompiled();
@@ -32,8 +33,11 @@ export class ProjectionBuilder<T> {
3233
this.apply(ProjectionsUtils.WILDCARD);
3334
}
3435

35-
public allByMap(metadade: MetadataTable<T>) {
36-
this.selectAllColumns(metadade.mapperTable);
36+
public allByMap(metadata: MetadataTable<T>) {
37+
if (metadata === void 0) {
38+
throw new DatabaseBuilderError(`Mapper not found for '${this._typeT.name}'`);
39+
}
40+
this.selectAllColumns(metadata.mapperTable);
3741
}
3842

3943
public proj(): ProjectionsHelper<T> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export class UpdateColumnsBuilder<T> extends ColumnsValuesBuilder<T, UpdateColum
88
}
99

1010
protected columnFormat(column: Column): string {
11-
return `${column.name} = ?`;
11+
return column.isKeyColumn ? void 0 : `${column.name} = ?`;
1212
}
1313
}

src/crud/where-base-builder.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,46 @@ export abstract class WhereBaseBuilder<
477477
column1: string,
478478
column2: string | string[],
479479
) {
480-
const conditionsArray = this._pendingConditions.concat(conditions);
480+
// TODO: verificar se colunas não são condition, para remover a condition
481+
let conditionsArray = this._pendingConditions.concat(conditions);
481482
this._pendingConditions = [];
482-
return this.buildConditions(conditionsArray, column1, column2);
483+
const isConditionIsNullInColumn2 = column2 === Condition.IsNull;
484+
if (isConditionIsNullInColumn2) {
485+
conditionsArray = this.conditionIsNull(conditionsArray);
486+
}
487+
// const isConditionInColumn2 = this.isEqualAnyCondition(column2);
488+
return this.buildConditions(
489+
conditionsArray,
490+
column1,
491+
isConditionIsNullInColumn2 ? void 0 : column2);
492+
}
493+
494+
private conditionIsNull(currentConditions: Condition[]): Condition[] {
495+
// new scope
496+
if (!currentConditions || (currentConditions.length === 1 && currentConditions[0] === void 0)) {
497+
return [Condition.IsNull];
498+
}
499+
switch (currentConditions.toString()) {
500+
case [Condition.Equal].toString():
501+
return [Condition.IsNull];
502+
case [Condition.Not, Condition.Equal].toString():
503+
return [Condition.Not, Condition.IsNull];
504+
default:
505+
return currentConditions;
506+
}
507+
}
508+
509+
private isEqualAnyCondition(value: string | string[]): boolean {
510+
if (Array.isArray(value)) {
511+
for (const v of value) {
512+
const r = this.isEqualAnyCondition(v);
513+
if (r) {
514+
return true;
515+
}
516+
}
517+
return false;
518+
}
519+
return (Object as any).values(Condition).filter((x: string) => x === value).length > 0;
483520
}
484521

485522
private buildConditions(

src/database-helper.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import { DatabaseBuilderError } from "./core/errors";
77

88
export class DatabaseHelper {
99

10+
public isTypeSimple(value: ValueTypeToParse): boolean {
11+
const type = this.getType(value);
12+
return type !== FieldType.OBJECT && type !== FieldType.FUNCTION && type !== FieldType.ARRAY;
13+
}
14+
15+
public isTypeIgnoredInMapper(value: ValueTypeToParse): boolean {
16+
const type = this.getType(value);
17+
return type === FieldType.ARRAY;
18+
}
19+
1020
public getType(value: ValueTypeToParse): FieldType {
1121
const valueFormatted = this.preFormatValue(value);
1222
const tipo = typeof valueFormatted;
@@ -19,14 +29,21 @@ export class DatabaseHelper {
1929
return FieldType.BOOLEAN;
2030
case "object":
2131
// tratar date como inteiro
22-
if (valueFormatted.constructor.name === "Date"
23-
|| valueFormatted.constructor.name === "Moment") {
32+
if (
33+
valueFormatted.constructor.name === "Date"
34+
|| valueFormatted.constructor.name === "Moment"
35+
) {
2436
return FieldType.DATE;
2537
}
38+
if (Array.isArray(valueFormatted)) {
39+
return FieldType.ARRAY;
40+
}
2641
// serializar todos os objetos
2742
return FieldType.OBJECT;
2843
case "function":
2944
return FieldType.FUNCTION;
45+
case "undefined":
46+
return FieldType.NULL;
3047
default:
3148
throw new DatabaseBuilderError(`type: '${tipo}', value: '${valueFormatted}' não configurado!`);
3249
}

0 commit comments

Comments
 (0)