@entity-access/entity-access 1.0.461 → 1.0.463
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/decorators/IColumn.d.ts +3 -0
- package/dist/decorators/IColumn.d.ts.map +1 -1
- package/dist/drivers/base/BaseDriver.d.ts.map +1 -1
- package/dist/drivers/base/BaseDriver.js +13 -14
- package/dist/drivers/base/BaseDriver.js.map +1 -1
- package/dist/entity-query/EntityType.js +2 -2
- package/dist/entity-query/EntityType.js.map +1 -1
- package/dist/migrations/sql-server/SqlServerAutomaticMigrations.js +8 -8
- package/dist/migrations/sql-server/SqlServerAutomaticMigrations.js.map +1 -1
- package/dist/model/EntityModel.d.ts.map +1 -1
- package/dist/model/EntityModel.js +3 -0
- package/dist/model/EntityModel.js.map +1 -1
- package/dist/model/EntityQuery.js +8 -8
- package/dist/model/EntityQuery.js.map +1 -1
- package/dist/model/EntitySource.js +1 -2
- package/dist/model/EntitySource.js.map +1 -1
- package/dist/query/ast/ExpressionToSql.d.ts.map +1 -1
- package/dist/query/ast/ExpressionToSql.js +9 -36
- package/dist/query/ast/ExpressionToSql.js.map +1 -1
- package/dist/query/expander/QueryExpander.js +2 -2
- package/dist/query/expander/QueryExpander.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/decorators/IColumn.ts +4 -0
- package/src/drivers/base/BaseDriver.ts +12 -13
- package/src/entity-query/EntityType.ts +2 -2
- package/src/migrations/sql-server/SqlServerAutomaticMigrations.ts +8 -8
- package/src/model/EntityModel.ts +3 -0
- package/src/model/EntityQuery.ts +11 -11
- package/src/model/EntitySource.ts +1 -1
- package/src/query/ast/ExpressionToSql.ts +10 -37
- package/src/query/expander/QueryExpander.ts +4 -4
- package/test.js +10 -4
|
@@ -45,11 +45,11 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
45
45
|
async createIndexes(context: EntityContext, type: EntityType, fkColumns: IColumn[]) {
|
|
46
46
|
for (const iterator of fkColumns) {
|
|
47
47
|
const filter = iterator.nullable
|
|
48
|
-
? `${ iterator.
|
|
48
|
+
? `${ iterator.quotedColumnName} IS NOT NULL`
|
|
49
49
|
: "";
|
|
50
50
|
const indexDef: IIndex = {
|
|
51
51
|
name: `IX_${type.name}_${iterator.name}`,
|
|
52
|
-
columns: [{ name: iterator.
|
|
52
|
+
columns: [{ name: iterator.quotedColumnName, descending: iterator.indexOrder !== "ascending"}],
|
|
53
53
|
filter
|
|
54
54
|
};
|
|
55
55
|
await this.migrateIndex(context, indexDef, type);
|
|
@@ -67,8 +67,8 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
for (const iterator of nonKeyColumns) {
|
|
70
|
-
const columnName = iterator
|
|
71
|
-
let def = `IF COL_LENGTH(${ SqlServerLiteral.escapeLiteral(name)}, ${ SqlServerLiteral.escapeLiteral(columnName)}) IS NULL ALTER TABLE ${name} ADD ${
|
|
70
|
+
const { quotedColumnName, columnName } = iterator;
|
|
71
|
+
let def = `IF COL_LENGTH(${ SqlServerLiteral.escapeLiteral(name)}, ${ SqlServerLiteral.escapeLiteral(columnName)}) IS NULL ALTER TABLE ${name} ADD ${quotedColumnName} `;
|
|
72
72
|
|
|
73
73
|
if (iterator.computed) {
|
|
74
74
|
def += ` AS ${iterator.computed} ${iterator.stored ? "PERSISTED" : ""}`;
|
|
@@ -106,7 +106,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
106
106
|
const fields = [];
|
|
107
107
|
|
|
108
108
|
for (const iterator of keys) {
|
|
109
|
-
let def = iterator.
|
|
109
|
+
let def = iterator.quotedColumnName + " ";
|
|
110
110
|
if (iterator.generated) {
|
|
111
111
|
switch(iterator.generated) {
|
|
112
112
|
case "identity":
|
|
@@ -122,7 +122,7 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
122
122
|
|
|
123
123
|
await driver.executeQuery(`IF OBJECT_ID(${ SqlServerLiteral.escapeLiteral(name)}) IS NULL BEGIN
|
|
124
124
|
CREATE TABLE ${name} (${fields.join(",")}
|
|
125
|
-
, CONSTRAINT PK_${name} PRIMARY KEY(${keys.map((x) => x.
|
|
125
|
+
, CONSTRAINT PK_${name} PRIMARY KEY(${keys.map((x) => x.quotedColumnName)})
|
|
126
126
|
);
|
|
127
127
|
END`);
|
|
128
128
|
|
|
@@ -183,9 +183,9 @@ export default class SqlServerAutomaticMigrations extends SqlServerMigrations {
|
|
|
183
183
|
const driver = context.connection;
|
|
184
184
|
|
|
185
185
|
let text = `ALTER TABLE ${name} ADD CONSTRAINT ${constraint.name}
|
|
186
|
-
foreign key (${constraint.fkMap.map((x) => x.fkColumn.
|
|
186
|
+
foreign key (${constraint.fkMap.map((x) => x.fkColumn.quotedColumnName).join(",")})
|
|
187
187
|
references ${constraint.fkMap[0].relatedKeyColumn.entityType.name}(
|
|
188
|
-
${constraint.fkMap.map((x) => x.relatedKeyColumn.
|
|
188
|
+
${constraint.fkMap.map((x) => x.relatedKeyColumn.quotedColumnName).join(",")}
|
|
189
189
|
) `;
|
|
190
190
|
|
|
191
191
|
switch(constraint.onDelete) {
|
package/src/model/EntityModel.ts
CHANGED
|
@@ -7,6 +7,7 @@ import EntityType from "../entity-query/EntityType.js";
|
|
|
7
7
|
import { IStringTransformer } from "../query/ast/IStringTransformer.js";
|
|
8
8
|
import { IEntityRelation } from "../decorators/IColumn.js";
|
|
9
9
|
import type QueryCompiler from "../compiler/QueryCompiler.js";
|
|
10
|
+
import { Expression } from "../query/ast/Expressions.js";
|
|
10
11
|
|
|
11
12
|
const driverModelCache = Symbol("driverModelCache");
|
|
12
13
|
|
|
@@ -28,6 +29,8 @@ const getOrCreateModel = (map: Map<any, EntityType>, type: IClassOf<any>, compil
|
|
|
28
29
|
column.entityType = t;
|
|
29
30
|
column.quotedColumnName = quote(column.columnName);
|
|
30
31
|
column.quotedName = quote(column.name);
|
|
32
|
+
column.quotedColumnNameExp = Expression.quotedIdentifier(column.columnName);
|
|
33
|
+
column.quotedNameExp = Expression.quotedIdentifier(column.name);
|
|
31
34
|
t.addColumn(column);
|
|
32
35
|
}
|
|
33
36
|
t.indexes.push(... original.indexes.map((i) => ({ ... i, columns: [ ... i.columns.map((c) => ( { ... c}))] })));
|
package/src/model/EntityQuery.ts
CHANGED
|
@@ -78,11 +78,11 @@ export default class EntityQuery<T = any>
|
|
|
78
78
|
const propertyName = iterator.alias.value;
|
|
79
79
|
const column = type.getField(propertyName);
|
|
80
80
|
if (column) {
|
|
81
|
-
fields.push(Expression.member(selectStatement.sourceParameter,
|
|
81
|
+
fields.push(Expression.member(selectStatement.sourceParameter, column.quotedColumnNameExp));
|
|
82
82
|
modelFields.push(
|
|
83
83
|
Expression.member(
|
|
84
84
|
sourceParameter,
|
|
85
|
-
|
|
85
|
+
column.quotedColumnNameExp)
|
|
86
86
|
);
|
|
87
87
|
continue;
|
|
88
88
|
}
|
|
@@ -329,8 +329,8 @@ export default class EntityQuery<T = any>
|
|
|
329
329
|
let where: Expression;
|
|
330
330
|
for (const iterator of this.type.keys) {
|
|
331
331
|
const compare = Expression.equal(
|
|
332
|
-
Expression.member(sp,
|
|
333
|
-
Expression.member(as,
|
|
332
|
+
Expression.member(sp, iterator.quotedColumnNameExp),
|
|
333
|
+
Expression.member(as, iterator.quotedColumnNameExp)
|
|
334
334
|
);
|
|
335
335
|
where = where ? Expression.logicalAnd(where, compare) : compare;
|
|
336
336
|
}
|
|
@@ -458,20 +458,20 @@ export default class EntityQuery<T = any>
|
|
|
458
458
|
const eAs = iterator as ExpressionAs;
|
|
459
459
|
const { field } = this.type.getProperty(eAs.alias.value);
|
|
460
460
|
fieldMap.add(field.columnName);
|
|
461
|
-
set.push(Expression.assign(
|
|
461
|
+
set.push(Expression.assign(field.quotedColumnNameExp, Expression.member(as, Expression.quotedIdentifier(eAs.alias.value))));
|
|
462
462
|
}
|
|
463
463
|
|
|
464
464
|
let where = null as Expression;
|
|
465
465
|
for (const iterator of this.type.keys) {
|
|
466
466
|
const compare = Expression.equal(
|
|
467
|
-
Expression.member(as,
|
|
468
|
-
Expression.member(sp,
|
|
467
|
+
Expression.member(as, iterator.quotedColumnNameExp),
|
|
468
|
+
Expression.member(sp, iterator.quotedColumnNameExp)
|
|
469
469
|
);
|
|
470
470
|
where = where ? Expression.logicalAnd(where, compare) : compare;
|
|
471
471
|
if (fieldMap.has(iterator.columnName)) {
|
|
472
472
|
continue;
|
|
473
473
|
}
|
|
474
|
-
this.selectStatement.fields.push(Expression.member(this.selectStatement.sourceParameter,
|
|
474
|
+
this.selectStatement.fields.push(Expression.member(this.selectStatement.sourceParameter, iterator.quotedColumnNameExp));
|
|
475
475
|
}
|
|
476
476
|
|
|
477
477
|
let returnUpdated = null as ExpressionAs[];
|
|
@@ -479,8 +479,8 @@ export default class EntityQuery<T = any>
|
|
|
479
479
|
returnUpdated = [];
|
|
480
480
|
for (const iterator of this.type.columns) {
|
|
481
481
|
returnUpdated.push(Expression.as(
|
|
482
|
-
|
|
483
|
-
|
|
482
|
+
iterator.quotedColumnNameExp,
|
|
483
|
+
iterator.quotedNameExp
|
|
484
484
|
));
|
|
485
485
|
}
|
|
486
486
|
}
|
|
@@ -550,7 +550,7 @@ export default class EntityQuery<T = any>
|
|
|
550
550
|
const column = type.getColumn((me.property as Identifier).value);
|
|
551
551
|
if (column) {
|
|
552
552
|
fields.push(Expression.as(
|
|
553
|
-
Expression.member(select.sourceParameter, column.
|
|
553
|
+
Expression.member(select.sourceParameter, column.quotedColumnNameExp)
|
|
554
554
|
, Expression.quotedIdentifier(column.name)));
|
|
555
555
|
continue;
|
|
556
556
|
}
|
|
@@ -264,7 +264,7 @@ export class EntitySource<T = any> {
|
|
|
264
264
|
Object.setPrototypeOf(returnEntity, this.model.typeClass.prototype);
|
|
265
265
|
|
|
266
266
|
for (const iterator of this.model.columns) {
|
|
267
|
-
returnFields.push(
|
|
267
|
+
returnFields.push(iterator.quotedColumnNameExp);
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
if (mode === "selectOrInsert" || mode === "upsert") {
|
|
@@ -176,27 +176,6 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
|
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
visitCallExpression(e: CallExpression): ITextQuery {
|
|
179
|
-
// let us check if we are using any of array extension methods...
|
|
180
|
-
// .some alias .any
|
|
181
|
-
// .find alias .firstOrDefault
|
|
182
|
-
// if (e.callee.type === "MemberExpression") {
|
|
183
|
-
// const me = e.callee as MemberExpression;
|
|
184
|
-
// if (me.target.type === "CallExpression") {
|
|
185
|
-
// // nested...
|
|
186
|
-
// const ce = me.target as CallExpression;
|
|
187
|
-
// const cme = ce.callee as MemberExpression;
|
|
188
|
-
// if(cme.property.type !== "Identifier") {
|
|
189
|
-
// throw new EntityAccessError("Invalid expression");
|
|
190
|
-
// }
|
|
191
|
-
// const property = cme.property as Identifier;
|
|
192
|
-
// if(property.value !== "filter") {
|
|
193
|
-
// throw new EntityAccessError("Invalid expression");
|
|
194
|
-
// }
|
|
195
|
-
|
|
196
|
-
// filter = e;
|
|
197
|
-
// e = cme.target as CallExpression;
|
|
198
|
-
// }
|
|
199
|
-
// }
|
|
200
179
|
|
|
201
180
|
const targetProperty = this.getPropertyChain(e.callee as ExpressionType);
|
|
202
181
|
if (targetProperty) {
|
|
@@ -308,16 +287,12 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
|
|
|
308
287
|
for (const { fkColumn, relatedKeyColumn } of relation.relation.relatedRelation.fkMap) {
|
|
309
288
|
const targetKey = MemberExpression.create({
|
|
310
289
|
target: parameter,
|
|
311
|
-
property:
|
|
312
|
-
value: relatedKeyColumn.columnName
|
|
313
|
-
})
|
|
290
|
+
property: relatedKeyColumn.quotedColumnNameExp
|
|
314
291
|
});
|
|
315
292
|
|
|
316
293
|
const relatedKey = MemberExpression.create({
|
|
317
294
|
target: select.sourceParameter,
|
|
318
|
-
property:
|
|
319
|
-
value: fkColumn.columnName
|
|
320
|
-
})
|
|
295
|
+
property: fkColumn.quotedColumnNameExp
|
|
321
296
|
});
|
|
322
297
|
const join = Expression.equal(targetKey, relatedKey);
|
|
323
298
|
where = where
|
|
@@ -363,16 +338,12 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
|
|
|
363
338
|
|
|
364
339
|
const targetKey = MemberExpression.create({
|
|
365
340
|
target: parameter,
|
|
366
|
-
property:
|
|
367
|
-
value: relatedKeyColumn.columnName
|
|
368
|
-
})
|
|
341
|
+
property: relatedKeyColumn.quotedColumnNameExp
|
|
369
342
|
});
|
|
370
343
|
|
|
371
344
|
const relatedKey = MemberExpression.create({
|
|
372
345
|
target: param1,
|
|
373
|
-
property:
|
|
374
|
-
value: fkColumn.columnName
|
|
375
|
-
})
|
|
346
|
+
property: fkColumn.quotedColumnNameExp
|
|
376
347
|
});
|
|
377
348
|
|
|
378
349
|
const join = Expression.equal(targetKey, relatedKey);
|
|
@@ -438,7 +409,9 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
|
|
|
438
409
|
// chain[0] = namingConvention(chain[0]);
|
|
439
410
|
// }
|
|
440
411
|
|
|
441
|
-
|
|
412
|
+
const quote = this.compiler.quote;
|
|
413
|
+
|
|
414
|
+
return [ QueryParameter.create(() => name) , "." , chain.map((x) => quote(x.member)).join(".")];
|
|
442
415
|
}
|
|
443
416
|
}
|
|
444
417
|
|
|
@@ -816,7 +789,7 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
|
|
|
816
789
|
const { relation, field } = peModel.getProperty(id.value);
|
|
817
790
|
if (field) {
|
|
818
791
|
// we need to replace field with column name...
|
|
819
|
-
return Expression.member(target, field.
|
|
792
|
+
return Expression.member(target, field.quotedColumnNameExp);
|
|
820
793
|
}
|
|
821
794
|
if (relation) {
|
|
822
795
|
|
|
@@ -854,8 +827,8 @@ export default class ExpressionToSql extends Visitor<ITextQuery> {
|
|
|
854
827
|
const peColumn = relation.isInverseRelation ? relatedKeyColumn : fkColumn;
|
|
855
828
|
const joinColumn = relation.isInverseRelation ? fkColumn : relatedKeyColumn;
|
|
856
829
|
const joinOn = Expression.equal(
|
|
857
|
-
Expression.member(pe, peColumn.
|
|
858
|
-
Expression.member(joinParameter, joinColumn.
|
|
830
|
+
Expression.member(pe, peColumn.quotedColumnNameExp),
|
|
831
|
+
Expression.member(joinParameter, joinColumn.quotedColumnNameExp));
|
|
859
832
|
where = where
|
|
860
833
|
? Expression.logicalAnd(where, joinOn)
|
|
861
834
|
: joinOn;
|
|
@@ -133,8 +133,8 @@ export class QueryExpander {
|
|
|
133
133
|
const joinColumn = relatedKeyColumn;
|
|
134
134
|
const relatedColumn = fkColumn;
|
|
135
135
|
const joinOn = Expression.equal(
|
|
136
|
-
Expression.member(joinParameter,
|
|
137
|
-
Expression.member(select.sourceParameter,
|
|
136
|
+
Expression.member(joinParameter, joinColumn.quotedColumnNameExp),
|
|
137
|
+
Expression.member(select.sourceParameter, relatedColumn.quotedColumnNameExp)
|
|
138
138
|
);
|
|
139
139
|
where = where ? Expression.logicalAnd(where, joinOn) : joinOn;
|
|
140
140
|
}
|
|
@@ -212,9 +212,9 @@ export class QueryExpander {
|
|
|
212
212
|
for (const { fkColumn, relatedKeyColumn } of relation.fkMap) {
|
|
213
213
|
const joinOn = Expression.equal(
|
|
214
214
|
Expression.member(selectJoinParameter,
|
|
215
|
-
|
|
215
|
+
fkColumn.quotedColumnNameExp),
|
|
216
216
|
Expression.member(select.sourceParameter,
|
|
217
|
-
|
|
217
|
+
relatedKeyColumn.quotedColumnNameExp)
|
|
218
218
|
);
|
|
219
219
|
where = where ? Expression.logicalAnd(where, joinOn) : joinOn;
|
|
220
220
|
}
|
package/test.js
CHANGED
|
@@ -145,15 +145,21 @@ let failed = 0;
|
|
|
145
145
|
|
|
146
146
|
for (const { error, name } of results) {
|
|
147
147
|
if (error) {
|
|
148
|
-
exitCode = 1;
|
|
149
|
-
failed++;
|
|
150
|
-
console.error(`${name} failed`);
|
|
151
|
-
console.error(error?.stack ?? error);
|
|
152
148
|
continue;
|
|
153
149
|
}
|
|
154
150
|
console.log(`${name} executed.`);
|
|
155
151
|
}
|
|
156
152
|
|
|
153
|
+
for (const { error, name } of results) {
|
|
154
|
+
if (!error) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
exitCode = 1;
|
|
158
|
+
failed++;
|
|
159
|
+
console.error(`${name} failed`);
|
|
160
|
+
console.error(error?.stack ?? error);
|
|
161
|
+
}
|
|
162
|
+
|
|
157
163
|
if (exitCode === 0) {
|
|
158
164
|
console.log(`${results.length} tests ran successfully.`);
|
|
159
165
|
} else {
|