typeorm 0.3.29 → 0.3.31

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.
Files changed (69) hide show
  1. package/README.md +2 -2
  2. package/browser/data-source/BaseDataSourceOptions.d.ts +7 -20
  3. package/browser/data-source/BaseDataSourceOptions.js.map +1 -1
  4. package/browser/driver/cockroachdb/CockroachQueryRunner.js +4 -4
  5. package/browser/driver/cockroachdb/CockroachQueryRunner.js.map +1 -1
  6. package/browser/driver/sqlserver/SqlServerQueryRunner.js +4 -2
  7. package/browser/driver/sqlserver/SqlServerQueryRunner.js.map +1 -1
  8. package/browser/driver/types/InvalidFindOptionsWhereBehavior.d.ts +15 -0
  9. package/browser/driver/types/InvalidFindOptionsWhereBehavior.js +3 -0
  10. package/browser/driver/types/InvalidFindOptionsWhereBehavior.js.map +1 -0
  11. package/browser/entity-manager/EntityManager.d.ts +23 -0
  12. package/browser/entity-manager/EntityManager.js +79 -102
  13. package/browser/entity-manager/EntityManager.js.map +1 -1
  14. package/browser/find-options/operator/JsonContains.d.ts +1 -1
  15. package/browser/find-options/operator/JsonContains.js.map +1 -1
  16. package/browser/metadata/types/ClosureTreeOptions.d.ts +1 -0
  17. package/browser/metadata/types/ClosureTreeOptions.js.map +1 -1
  18. package/browser/metadata-builder/ClosureJunctionEntityMetadataBuilder.js +6 -2
  19. package/browser/metadata-builder/ClosureJunctionEntityMetadataBuilder.js.map +1 -1
  20. package/browser/persistence/SubjectChangedColumnsComputer.js +6 -2
  21. package/browser/persistence/SubjectChangedColumnsComputer.js.map +1 -1
  22. package/browser/persistence/SubjectExecutor.js +0 -8
  23. package/browser/persistence/SubjectExecutor.js.map +1 -1
  24. package/browser/query-builder/QueryBuilder.js +24 -24
  25. package/browser/query-builder/QueryBuilder.js.map +1 -1
  26. package/browser/query-builder/SelectQueryBuilder.d.ts +6 -6
  27. package/browser/query-builder/SelectQueryBuilder.js +27 -22
  28. package/browser/query-builder/SelectQueryBuilder.js.map +1 -1
  29. package/browser/repository/TreeRepository.js +2 -2
  30. package/browser/repository/TreeRepository.js.map +1 -1
  31. package/browser/util/OrmUtils.d.ts +21 -1
  32. package/browser/util/OrmUtils.js +74 -1
  33. package/browser/util/OrmUtils.js.map +1 -1
  34. package/commands/MigrationGenerateCommand.d.ts +6 -0
  35. package/commands/MigrationGenerateCommand.js +13 -2
  36. package/commands/MigrationGenerateCommand.js.map +1 -1
  37. package/data-source/BaseDataSourceOptions.d.ts +7 -20
  38. package/data-source/BaseDataSourceOptions.js.map +1 -1
  39. package/driver/cockroachdb/CockroachQueryRunner.js +4 -4
  40. package/driver/cockroachdb/CockroachQueryRunner.js.map +1 -1
  41. package/driver/sqlserver/SqlServerQueryRunner.js +4 -2
  42. package/driver/sqlserver/SqlServerQueryRunner.js.map +1 -1
  43. package/driver/types/InvalidFindOptionsWhereBehavior.d.ts +15 -0
  44. package/driver/types/InvalidFindOptionsWhereBehavior.js +4 -0
  45. package/driver/types/InvalidFindOptionsWhereBehavior.js.map +1 -0
  46. package/entity-manager/EntityManager.d.ts +23 -0
  47. package/entity-manager/EntityManager.js +79 -102
  48. package/entity-manager/EntityManager.js.map +1 -1
  49. package/find-options/operator/JsonContains.d.ts +1 -1
  50. package/find-options/operator/JsonContains.js.map +1 -1
  51. package/metadata/types/ClosureTreeOptions.d.ts +1 -0
  52. package/metadata/types/ClosureTreeOptions.js.map +1 -1
  53. package/metadata-builder/ClosureJunctionEntityMetadataBuilder.js +6 -2
  54. package/metadata-builder/ClosureJunctionEntityMetadataBuilder.js.map +1 -1
  55. package/package.json +23 -24
  56. package/persistence/SubjectChangedColumnsComputer.js +6 -2
  57. package/persistence/SubjectChangedColumnsComputer.js.map +1 -1
  58. package/persistence/SubjectExecutor.js +0 -8
  59. package/persistence/SubjectExecutor.js.map +1 -1
  60. package/query-builder/QueryBuilder.js +24 -24
  61. package/query-builder/QueryBuilder.js.map +1 -1
  62. package/query-builder/SelectQueryBuilder.d.ts +6 -6
  63. package/query-builder/SelectQueryBuilder.js +27 -22
  64. package/query-builder/SelectQueryBuilder.js.map +1 -1
  65. package/repository/TreeRepository.js +2 -2
  66. package/repository/TreeRepository.js.map +1 -1
  67. package/util/OrmUtils.d.ts +21 -1
  68. package/util/OrmUtils.js +74 -1
  69. package/util/OrmUtils.js.map +1 -1
@@ -0,0 +1,15 @@
1
+ export type InvalidFindOptionsWhereBehavior = {
2
+ /**
3
+ * How to handle null values in where conditions.
4
+ * - 'ignore': Skip null properties (default)
5
+ * - 'sql-null': Transform null to SQL NULL
6
+ * - 'throw': Throw an error when null is encountered
7
+ */
8
+ readonly null?: "ignore" | "sql-null" | "throw";
9
+ /**
10
+ * How to handle undefined values in where conditions.
11
+ * - 'ignore': Skip undefined properties (default)
12
+ * - 'throw': Throw an error when undefined is encountered
13
+ */
14
+ readonly undefined?: "ignore" | "throw";
15
+ };
@@ -0,0 +1,3 @@
1
+ export {};
2
+
3
+ //# sourceMappingURL=InvalidFindOptionsWhereBehavior.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../browser/src/driver/types/InvalidFindOptionsWhereBehavior.ts"],"names":[],"mappings":"","file":"InvalidFindOptionsWhereBehavior.js","sourcesContent":["export type InvalidFindOptionsWhereBehavior = {\n /**\n * How to handle null values in where conditions.\n * - 'ignore': Skip null properties (default)\n * - 'sql-null': Transform null to SQL NULL\n * - 'throw': Throw an error when null is encountered\n */\n readonly null?: \"ignore\" | \"sql-null\" | \"throw\"\n\n /**\n * How to handle undefined values in where conditions.\n * - 'ignore': Skip undefined properties (default)\n * - 'throw': Throw an error when undefined is encountered\n */\n readonly undefined?: \"ignore\" | \"throw\"\n}\n"],"sourceRoot":"../.."}
@@ -212,6 +212,23 @@ export declare class EntityManager {
212
212
  */
213
213
  insert<Entity extends ObjectLiteral>(target: EntityTarget<Entity>, entity: QueryDeepPartialEntity<Entity> | QueryDeepPartialEntity<Entity>[]): Promise<InsertResult>;
214
214
  upsert<Entity extends ObjectLiteral>(target: EntityTarget<Entity>, entityOrEntities: QueryDeepPartialEntity<Entity> | QueryDeepPartialEntity<Entity>[], conflictPathsOrOptions: string[] | UpsertOptions<Entity>): Promise<InsertResult>;
215
+ /**
216
+ * Shared by update/delete/softDelete/restore. Object criteria is normalized
217
+ * via {@link OrmUtils.normalizeWhereCriteria}, then rejected if it would
218
+ * produce no predicate and therefore render as an always-true `WHERE 1=1` —
219
+ * an empty object or array, an empty OR-branch, a bare primitive inside an
220
+ * OR-array, or a keyless object. Primitive id criteria is returned untouched
221
+ * for `whereInIds` (rejected only when wholly empty).
222
+ *
223
+ * @param criteria the raw criteria passed to the operation
224
+ * @param methodName the calling method, used in the error message
225
+ * @returns the criteria to build with, and whether to execute it via
226
+ * `whereInIds` (primitive) or `where` (object)
227
+ */
228
+ protected normalizeAndValidateWhereCriteria(criteria: any, methodName: string): {
229
+ criteria: any;
230
+ isPrimitive: boolean;
231
+ };
215
232
  /**
216
233
  * Updates entity partially. Entity can be found by a given condition(s).
217
234
  * Unlike save method executes a primitive operation without cascades, relations and other operations included.
@@ -372,6 +389,12 @@ export declare class EntityManager {
372
389
  * Decrements some column by provided value of the entities matched given conditions.
373
390
  */
374
391
  decrement<Entity extends ObjectLiteral>(entityClass: EntityTarget<Entity>, conditions: any, propertyPath: string, value: number | string): Promise<UpdateResult>;
392
+ /**
393
+ * Shared implementation of {@link increment} and {@link decrement}: builds a
394
+ * `column = column +/- value` UPDATE and delegates execution to {@link update},
395
+ * so the criteria handling stays aligned with the other write methods.
396
+ */
397
+ protected incrementOrDecrementBy<Entity extends ObjectLiteral>(operation: "increment" | "decrement", entityClass: EntityTarget<Entity>, conditions: any, propertyPath: string, value: number | string): Promise<UpdateResult>;
375
398
  /**
376
399
  * Gets repository for the given entity class or name.
377
400
  * If single database connection mode is used, then repository is obtained from the
@@ -336,6 +336,51 @@ export class EntityManager {
336
336
  }
337
337
  return qb.execute();
338
338
  }
339
+ /**
340
+ * Shared by update/delete/softDelete/restore. Object criteria is normalized
341
+ * via {@link OrmUtils.normalizeWhereCriteria}, then rejected if it would
342
+ * produce no predicate and therefore render as an always-true `WHERE 1=1` —
343
+ * an empty object or array, an empty OR-branch, a bare primitive inside an
344
+ * OR-array, or a keyless object. Primitive id criteria is returned untouched
345
+ * for `whereInIds` (rejected only when wholly empty).
346
+ *
347
+ * @param criteria the raw criteria passed to the operation
348
+ * @param methodName the calling method, used in the error message
349
+ * @returns the criteria to build with, and whether to execute it via
350
+ * `whereInIds` (primitive) or `where` (object)
351
+ */
352
+ normalizeAndValidateWhereCriteria(criteria, methodName) {
353
+ const rejectEmpty = () => {
354
+ throw new TypeORMError(`Empty criteria(s) are not allowed for the ${methodName} method.`);
355
+ };
356
+ if (OrmUtils.isPrimitiveCriteria(criteria)) {
357
+ if (OrmUtils.isCriteriaNullOrEmpty(criteria))
358
+ rejectEmpty();
359
+ return { criteria, isPrimitive: true };
360
+ }
361
+ const normalizedCriteria = OrmUtils.normalizeWhereCriteria(criteria, this.connection.options.invalidWhereValuesBehavior);
362
+ // On the object-criteria path, `.where()` builds a predicate only from
363
+ // an object's own keys. Anything else yields an empty predicate list
364
+ // that renders as an always-true `1=1`, so a criterion is unsafe unless
365
+ // it is a non-empty object. This must reject:
366
+ // - primitives (a bare number/string in a mixed OR-array like
367
+ // `[1, { id: 2 }]` — `.where(1)` produces no predicate),
368
+ // - empty plain objects (`{}`), empty arrays (`[]`), and
369
+ // - empty non-plain objects, e.g. an empty entity instance
370
+ // (`new Post()`), which isCriteriaNullOrEmpty does not catch.
371
+ // Value-type criteria (Date, Buffer) execute via the primitive branch
372
+ // and never reach here.
373
+ const rendersNoPredicate = (value) => value === null ||
374
+ typeof value !== "object" ||
375
+ Object.keys(value).length === 0;
376
+ const isEmpty = Array.isArray(normalizedCriteria)
377
+ ? normalizedCriteria.length === 0 ||
378
+ normalizedCriteria.some(rendersNoPredicate)
379
+ : rendersNoPredicate(normalizedCriteria);
380
+ if (isEmpty)
381
+ rejectEmpty();
382
+ return { criteria: normalizedCriteria, isPrimitive: false };
383
+ }
339
384
  /**
340
385
  * Updates entity partially. Entity can be found by a given condition(s).
341
386
  * Unlike save method executes a primitive operation without cascades, relations and other operations included.
@@ -343,31 +388,19 @@ export class EntityManager {
343
388
  * Does not check if entity exist in the database.
344
389
  * Condition(s) cannot be empty.
345
390
  */
346
- update(target, criteria, partialEntity, options) {
347
- // if user passed empty criteria or empty list of criterias, then throw an error
348
- if (OrmUtils.isCriteriaNullOrEmpty(criteria)) {
349
- return Promise.reject(new TypeORMError(`Empty criteria(s) are not allowed for the update method.`));
350
- }
351
- if (OrmUtils.isPrimitiveCriteria(criteria)) {
352
- const qb = this.createQueryBuilder()
353
- .update(target)
354
- .set(partialEntity)
355
- .whereInIds(criteria);
356
- if (options?.returning !== undefined) {
357
- qb.returning(options.returning);
358
- }
359
- return qb.execute();
391
+ async update(target, criteria, partialEntity, options) {
392
+ const { criteria: whereCriteria, isPrimitive } = this.normalizeAndValidateWhereCriteria(criteria, "update");
393
+ const qb = this.createQueryBuilder().update(target).set(partialEntity);
394
+ if (isPrimitive) {
395
+ qb.whereInIds(whereCriteria);
360
396
  }
361
397
  else {
362
- const qb = this.createQueryBuilder()
363
- .update(target)
364
- .set(partialEntity)
365
- .where(criteria);
366
- if (options?.returning !== undefined) {
367
- qb.returning(options.returning);
368
- }
369
- return qb.execute();
398
+ qb.where(whereCriteria);
399
+ }
400
+ if (options?.returning !== undefined) {
401
+ qb.returning(options.returning);
370
402
  }
403
+ return qb.execute();
371
404
  }
372
405
  /**
373
406
  * Updates all entities of target type, setting fields from supplied partial entity.
@@ -390,25 +423,10 @@ export class EntityManager {
390
423
  * Does not check if entity exist in the database.
391
424
  * Condition(s) cannot be empty.
392
425
  */
393
- delete(targetOrEntity, criteria) {
394
- // if user passed empty criteria or empty list of criterias, then throw an error
395
- if (OrmUtils.isCriteriaNullOrEmpty(criteria)) {
396
- return Promise.reject(new TypeORMError(`Empty criteria(s) are not allowed for the delete method.`));
397
- }
398
- if (OrmUtils.isPrimitiveCriteria(criteria)) {
399
- return this.createQueryBuilder()
400
- .delete()
401
- .from(targetOrEntity)
402
- .whereInIds(criteria)
403
- .execute();
404
- }
405
- else {
406
- return this.createQueryBuilder()
407
- .delete()
408
- .from(targetOrEntity)
409
- .where(criteria)
410
- .execute();
411
- }
426
+ async delete(targetOrEntity, criteria) {
427
+ const { criteria: whereCriteria, isPrimitive } = this.normalizeAndValidateWhereCriteria(criteria, "delete");
428
+ const qb = this.createQueryBuilder().delete().from(targetOrEntity);
429
+ return (isPrimitive ? qb.whereInIds(whereCriteria) : qb.where(whereCriteria)).execute();
412
430
  }
413
431
  /**
414
432
  * Deletes all entities of target type.
@@ -427,25 +445,10 @@ export class EntityManager {
427
445
  * Does not check if entity exist in the database.
428
446
  * Condition(s) cannot be empty.
429
447
  */
430
- softDelete(targetOrEntity, criteria) {
431
- // if user passed empty criteria or empty list of criterias, then throw an error
432
- if (OrmUtils.isCriteriaNullOrEmpty(criteria)) {
433
- return Promise.reject(new TypeORMError(`Empty criteria(s) are not allowed for the softDelete method.`));
434
- }
435
- if (OrmUtils.isPrimitiveCriteria(criteria)) {
436
- return this.createQueryBuilder()
437
- .softDelete()
438
- .from(targetOrEntity)
439
- .whereInIds(criteria)
440
- .execute();
441
- }
442
- else {
443
- return this.createQueryBuilder()
444
- .softDelete()
445
- .from(targetOrEntity)
446
- .where(criteria)
447
- .execute();
448
- }
448
+ async softDelete(targetOrEntity, criteria) {
449
+ const { criteria: whereCriteria, isPrimitive } = this.normalizeAndValidateWhereCriteria(criteria, "softDelete");
450
+ const qb = this.createQueryBuilder().softDelete().from(targetOrEntity);
451
+ return (isPrimitive ? qb.whereInIds(whereCriteria) : qb.where(whereCriteria)).execute();
449
452
  }
450
453
  /**
451
454
  * Restores entities by a given condition(s).
@@ -454,25 +457,10 @@ export class EntityManager {
454
457
  * Does not check if entity exist in the database.
455
458
  * Condition(s) cannot be empty.
456
459
  */
457
- restore(targetOrEntity, criteria) {
458
- // if user passed empty criteria or empty list of criterias, then throw an error
459
- if (OrmUtils.isCriteriaNullOrEmpty(criteria)) {
460
- return Promise.reject(new TypeORMError(`Empty criteria(s) are not allowed for the restore method.`));
461
- }
462
- if (OrmUtils.isPrimitiveCriteria(criteria)) {
463
- return this.createQueryBuilder()
464
- .restore()
465
- .from(targetOrEntity)
466
- .whereInIds(criteria)
467
- .execute();
468
- }
469
- else {
470
- return this.createQueryBuilder()
471
- .restore()
472
- .from(targetOrEntity)
473
- .where(criteria)
474
- .execute();
475
- }
460
+ async restore(targetOrEntity, criteria) {
461
+ const { criteria: whereCriteria, isPrimitive } = this.normalizeAndValidateWhereCriteria(criteria, "restore");
462
+ const qb = this.createQueryBuilder().restore().from(targetOrEntity);
463
+ return (isPrimitive ? qb.whereInIds(whereCriteria) : qb.where(whereCriteria)).execute();
476
464
  }
477
465
  /**
478
466
  * Checks whether any entity exists with the given options.
@@ -714,45 +702,34 @@ export class EntityManager {
714
702
  * Increments some column by provided value of the entities matched given conditions.
715
703
  */
716
704
  async increment(entityClass, conditions, propertyPath, value) {
717
- const metadata = this.connection.getMetadata(entityClass);
718
- const column = metadata.findColumnWithPropertyPath(propertyPath);
719
- if (!column)
720
- throw new TypeORMError(`Column ${propertyPath} was not found in ${metadata.targetName} entity.`);
721
- if (isNaN(Number(value)))
722
- throw new TypeORMError(`Value "${value}" is not a number.`);
723
- // convert possible embedded path "social.likes" into object { social: { like: () => value } }
724
- const values = propertyPath
725
- .split(".")
726
- .reduceRight((value, key) => ({ [key]: value }), () => this.connection.driver.escape(column.databaseName) +
727
- " + " +
728
- value);
729
- return this.createQueryBuilder(entityClass, "entity")
730
- .update(entityClass)
731
- .set(values)
732
- .where(conditions)
733
- .execute();
705
+ return this.incrementOrDecrementBy("increment", entityClass, conditions, propertyPath, value);
734
706
  }
735
707
  /**
736
708
  * Decrements some column by provided value of the entities matched given conditions.
737
709
  */
738
710
  async decrement(entityClass, conditions, propertyPath, value) {
711
+ return this.incrementOrDecrementBy("decrement", entityClass, conditions, propertyPath, value);
712
+ }
713
+ /**
714
+ * Shared implementation of {@link increment} and {@link decrement}: builds a
715
+ * `column = column +/- value` UPDATE and delegates execution to {@link update},
716
+ * so the criteria handling stays aligned with the other write methods.
717
+ */
718
+ incrementOrDecrementBy(operation, entityClass, conditions, propertyPath, value) {
739
719
  const metadata = this.connection.getMetadata(entityClass);
740
720
  const column = metadata.findColumnWithPropertyPath(propertyPath);
741
721
  if (!column)
742
722
  throw new TypeORMError(`Column ${propertyPath} was not found in ${metadata.targetName} entity.`);
743
723
  if (isNaN(Number(value)))
744
724
  throw new TypeORMError(`Value "${value}" is not a number.`);
725
+ const operator = operation === "increment" ? "+" : "-";
745
726
  // convert possible embedded path "social.likes" into object { social: { like: () => value } }
746
727
  const values = propertyPath
747
728
  .split(".")
748
729
  .reduceRight((value, key) => ({ [key]: value }), () => this.connection.driver.escape(column.databaseName) +
749
- " - " +
730
+ ` ${operator} ` +
750
731
  value);
751
- return this.createQueryBuilder(entityClass, "entity")
752
- .update(entityClass)
753
- .set(values)
754
- .where(conditions)
755
- .execute();
732
+ return this.update(entityClass, conditions, values);
756
733
  }
757
734
  /**
758
735
  * Gets repository for the given entity class or name.