@entity-access/entity-access 1.0.348 → 1.0.350

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.
@@ -262,16 +262,27 @@ export abstract class BaseDriver {
262
262
  if(where) {
263
263
  where += "\r\n\t\tAND ";
264
264
  }
265
+ const compare = keys[iterator.name];
266
+ if (compare === null) {
267
+ where += `${iterator.quotedColumnName} is null`;
268
+ continue;
269
+ }
265
270
  where += `${iterator.quotedColumnName} = $${i++}`;
266
- values.push(keys[iterator.name]);
271
+ values.push(compare);
267
272
  }
268
273
  } else {
269
274
  for (const iterator of type.keys) {
270
275
  if(where) {
271
276
  where += "\r\n\t\tAND ";
272
277
  }
278
+ const compare = entity[iterator.name];
279
+ if (compare === null) {
280
+ where += `${iterator.quotedColumnName} is null`;
281
+ continue;
282
+ }
283
+
273
284
  where += `${iterator.quotedColumnName} = $${i++}`;
274
- values.push(entity[iterator.name]);
285
+ values.push(compare);
275
286
  continue;
276
287
  }
277
288
  }
@@ -293,16 +304,26 @@ export abstract class BaseDriver {
293
304
  if(where) {
294
305
  where += "\r\n\t\tAND ";
295
306
  }
307
+ const compare = keys[iterator.name];
308
+ if (compare === null) {
309
+ where += `${iterator.quotedColumnName} is null`;
310
+ continue;
311
+ }
296
312
  where += `${iterator.quotedColumnName} = $${i++}`;
297
- values.push(keys[iterator.name]);
313
+ values.push(compare);
298
314
  }
299
315
  } else {
300
316
  for (const iterator of type.keys) {
301
317
  if(where) {
302
318
  where += "\r\n\t\tAND ";
303
319
  }
320
+ const compare = entity[iterator.name];
321
+ if (compare === null) {
322
+ where += `${iterator.quotedColumnName} is null`;
323
+ continue;
324
+ }
304
325
  where += `${iterator.quotedColumnName} = $${i++}`;
305
- values.push(entity[iterator.name]);
326
+ values.push(compare);
306
327
  }
307
328
  }
308
329
  }
@@ -343,16 +364,27 @@ export abstract class BaseDriver {
343
364
  if(where) {
344
365
  where += "\r\n\t\tAND ";
345
366
  }
367
+ const compare = keys[iterator.name];
368
+ if (compare === null) {
369
+ where += `${iterator.quotedColumnName} is null`;
370
+ continue;
371
+ }
372
+
346
373
  where += `${iterator.quotedColumnName} = $${i++}`;
347
- values.push(keys[iterator.name]);
374
+ values.push(compare);
348
375
  }
349
376
  } else {
350
377
  for (const iterator of type.keys) {
351
378
  if(where) {
352
379
  where += "\r\n\t\tAND ";
353
380
  }
381
+ const compare = entity[iterator.name];
382
+ if (compare === null) {
383
+ where += `${iterator.quotedColumnName} is null`;
384
+ continue;
385
+ }
354
386
  where += `${iterator.quotedColumnName} = $${i++}`;
355
- values.push(entity[iterator.name]);
387
+ values.push(compare);
356
388
  }
357
389
  }
358
390
  const text = `SELECT ${columns}\r\n\tFROM ${type.fullyQualifiedTableName}\r\n\tWHERE ${where}`;
@@ -1,9 +1,10 @@
1
1
  import EntityAccessError from "../common/EntityAccessError.js";
2
2
  import Logger from "../common/Logger.js";
3
+ import { modelSymbol } from "../common/symbols/symbols.js";
3
4
  import { AsyncDisposableScope } from "../common/usingAsync.js";
4
5
  import { IDbReader } from "../drivers/base/BaseDriver.js";
5
6
  import EntityType from "../entity-query/EntityType.js";
6
- import { BinaryExpression, CallExpression, DeleteStatement, ExistsExpression, Expression, ExpressionAs, Identifier, InsertStatement, JoinExpression, MemberExpression, NewObjectExpression, NumberLiteral, OrderByExpression, SelectStatement, TableLiteral, UpdateStatement } from "../query/ast/Expressions.js";
7
+ import { BinaryExpression, CallExpression, DeleteStatement, Expression, ExpressionAs, Identifier, InsertStatement, JoinExpression, MemberExpression, NewObjectExpression, NumberLiteral, OrderByExpression, SelectStatement, TableLiteral, UpdateStatement } from "../query/ast/Expressions.js";
7
8
  import { QueryExpander } from "../query/expander/QueryExpander.js";
8
9
  import EntityContext from "./EntityContext.js";
9
10
  import type { EntitySource } from "./EntitySource.js";
@@ -31,29 +32,30 @@ export default class EntityQuery<T = any>
31
32
  }
32
33
 
33
34
  insertInTo(es: EntitySource) {
34
- const model = (es as any).mode as EntityType;
35
- const table = (es as any).model.fullyQualifiedName as TableLiteral;
36
- const fields = [];
35
+ const model = es[modelSymbol] as EntityType;
36
+ const table = model.fullyQualifiedName as TableLiteral;
37
+ const fields = [] as string[];
37
38
  for (const iterator of this.selectStatement.fields) {
38
39
  if (iterator.type !== "ExpressionAs") {
39
- fields.push(iterator);
40
- continue;
40
+ throw new EntityAccessError(`as Expression expected instead of ${iterator.type}`);
41
41
  }
42
42
  const expAs = iterator as ExpressionAs;
43
43
  const field = model.getField(expAs.alias.value);
44
44
  if (!field) {
45
45
  throw new EntityAccessError(`Field ${expAs.alias.value} not found in ${model.name}`);
46
46
  }
47
- fields.push(Expression.as(expAs.expression, field.columnName));
47
+ fields.push(field.quotedColumnName);
48
48
  }
49
- const values = { ... this.selectStatement, fields };
50
- const query = InsertStatement.create({
51
- table,
52
- values
53
- });
49
+ const query = { ... this.selectStatement };
54
50
  const { driver } = this.context;
55
- const insert = driver.compiler.compileExpression(null, query);
56
- return this.context.connection.executeQuery(insert, this.signal);
51
+ const selectQuery = driver.compiler.compileExpression(null, query);
52
+ const insertQuery = `INSERT INTO ${model.fullyQualifiedTableName}(${fields.join(",")})
53
+ ${selectQuery.text}`;
54
+ this.traceQuery?.(insertQuery);
55
+ return this.context.connection.executeQuery({
56
+ text: insertQuery,
57
+ values: selectQuery.values
58
+ }, this.signal);
57
59
  }
58
60
 
59
61
  selectView(parameters: any, fx: any): any {
@@ -217,6 +217,8 @@ export class CallExpression extends Expression {
217
217
  readonly type = "CallExpression";
218
218
  callee: Expression;
219
219
  arguments: Expression[];
220
+ collectionMethod: string;
221
+ castMethod: string;
220
222
  }
221
223
 
222
224
  export class BracketExpression extends Expression {
@@ -268,9 +268,12 @@ export default class ArrowToExpression extends BabelVisitor<Expression> {
268
268
 
269
269
  const method = name[2];
270
270
 
271
+ const castMethod = name[1] === "cast" ? name[2] : void 0;
272
+
271
273
  const reWrittenCe = CallExpression.create({
272
274
  callee: Expression.identifier(name.join(".")),
273
- arguments: ce.arguments
275
+ arguments: ce.arguments,
276
+ castMethod
274
277
  });
275
278
 
276
279
 
@@ -290,6 +293,7 @@ export default class ArrowToExpression extends BabelVisitor<Expression> {
290
293
  }
291
294
 
292
295
  const left = CallExpression.create({
296
+ collectionMethod: method,
293
297
  callee: MemberExpression.create({
294
298
  target: mapCallee.target,
295
299
  property: Expression.identifier(method),
@@ -0,0 +1,24 @@
1
+ import assert from "assert";
2
+ import { TestConfig } from "../../TestConfig.js";
3
+ import { createContext } from "../../model/createContext.js";
4
+ import Sql from "../../../sql/Sql.js";
5
+
6
+ export default async function(this: TestConfig) {
7
+
8
+ if (!this.db) {
9
+ return;
10
+ }
11
+
12
+ const context = await createContext(this.driver);
13
+
14
+ await context.messages.asQuery()
15
+ .where(void 0, (p) => (x) => x.messageID > 100)
16
+ .select(void 0, (p) => (x) => ({
17
+ fromID: x.fromID,
18
+ toID: x.toID,
19
+ message: x.message
20
+ }))
21
+ .trace(console.log)
22
+ .insertInTo(context.archivedMessages);
23
+
24
+ }
@@ -38,6 +38,10 @@ export class ShoppingContext extends EntityContext {
38
38
  public emailAddresses = this.model.register(EmailAddress);
39
39
 
40
40
  public userCategoryTags = this.model.register(UserCategoryTag);
41
+
42
+ public messages = this.model.register(UserMessage);
43
+
44
+ public archivedMessages = this.model.register(ArchivedUserMessage);
41
45
  }
42
46
 
43
47
  @Table("Users")
@@ -392,4 +396,36 @@ export class OrderItem {
392
396
  public product: Product;
393
397
  public productPrice: ProductPrice;
394
398
 
399
+ }
400
+
401
+ @Table("UserMessages")
402
+ export class UserMessage {
403
+
404
+ @Column({ dataType: "BigInt", key: true, generated: "identity"})
405
+ messageID: number;
406
+
407
+ @Column({ dataType: "BigInt"})
408
+ fromID: number;
409
+
410
+ @Column({ dataType: "BigInt"})
411
+ toID: number;
412
+
413
+ @Column({ dataType: "Char"})
414
+ message: string;
415
+ }
416
+
417
+ @Table("ArchivedUserMessages")
418
+ export class ArchivedUserMessage {
419
+
420
+ @Column({ dataType: "BigInt", key: true, generated: "identity"})
421
+ messageID: number;
422
+
423
+ @Column({ dataType: "BigInt"})
424
+ fromID: number;
425
+
426
+ @Column({ dataType: "BigInt"})
427
+ toID: number;
428
+
429
+ @Column({ dataType: "Char"})
430
+ message: string;
395
431
  }