objection 3.1.5 → 3.2.0

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 (33) hide show
  1. package/README.md +3 -3
  2. package/lib/model/AjvValidator.js +40 -15
  3. package/lib/model/Model.js +3 -1
  4. package/lib/model/graph/ModelGraphNode.js +12 -0
  5. package/lib/model/modelToJson.js +2 -1
  6. package/lib/queryBuilder/QueryBuilder.js +156 -45
  7. package/lib/queryBuilder/RelationExpression.js +8 -8
  8. package/lib/queryBuilder/graph/GraphFetcher.js +3 -1
  9. package/lib/queryBuilder/graph/GraphOptions.js +21 -2
  10. package/lib/queryBuilder/graph/GraphUpsert.js +30 -0
  11. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +20 -14
  12. package/lib/queryBuilder/graph/insert/GraphInsert.js +6 -0
  13. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +2 -0
  14. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +15 -3
  15. package/lib/queryBuilder/join/RelationJoiner.js +17 -0
  16. package/lib/queryBuilder/operations/DelegateOperation.js +4 -0
  17. package/lib/queryBuilder/operations/NoneOperation.js +37 -0
  18. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +5 -0
  19. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +20 -0
  20. package/lib/queryBuilder/operations/eager/EagerOperation.js +3 -5
  21. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +8 -2
  22. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +15 -3
  23. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +8 -2
  24. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +9 -0
  25. package/lib/relations/RelationProperty.js +7 -3
  26. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +1 -0
  27. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +5 -0
  28. package/lib/relations/hasMany/HasManyRelateOperation.js +1 -0
  29. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +6 -0
  30. package/lib/utils/identifierMapping.js +8 -2
  31. package/lib/utils/objectUtils.js +9 -5
  32. package/package.json +14 -10
  33. package/typings/objection/index.d.ts +59 -40
@@ -30,6 +30,7 @@ declare namespace Objection {
30
30
 
31
31
  const snakeCaseMappers: SnakeCaseMappersFactory;
32
32
  const knexSnakeCaseMappers: KnexSnakeCaseMappersFactory;
33
+ const knexIdentifierMapping: KnexIdentifierMappingFactory;
33
34
 
34
35
  const transaction: transaction;
35
36
  const initialize: initialize;
@@ -126,11 +127,13 @@ declare namespace Objection {
126
127
  | string
127
128
  | number
128
129
  | boolean
130
+ | bigint
129
131
  | Date
130
132
  | Buffer
131
133
  | string[]
132
134
  | number[]
133
135
  | boolean[]
136
+ | bigint[]
134
137
  | Date[]
135
138
  | Buffer[]
136
139
  | null;
@@ -158,10 +161,7 @@ declare namespace Objection {
158
161
  type AnyModelConstructor = ModelConstructor<Model>;
159
162
  type ModifierFunction<QB extends AnyQueryBuilder> = (qb: QB, ...args: any[]) => void;
160
163
  type Modifier<QB extends AnyQueryBuilder = AnyQueryBuilder> =
161
- | ModifierFunction<QB>
162
- | string
163
- | string[]
164
- | Record<string, Expression<PrimitiveValue>>;
164
+ ModifierFunction<QB> | string | string[] | Record<string, Expression<PrimitiveValue>>;
165
165
  type OrderByDirection = 'asc' | 'desc' | 'ASC' | 'DESC';
166
166
  type OrderByNulls = 'first' | 'last';
167
167
 
@@ -201,10 +201,10 @@ declare namespace Objection {
201
201
  */
202
202
  type PartialModelObject<T extends Model> = {
203
203
  [K in DataPropertyNames<T>]?: Defined<T[K]> extends Model
204
- ? T[K]
204
+ ? T[K] | PartialModelObject<Defined<T[K]>>
205
205
  : Defined<T[K]> extends Array<infer I>
206
206
  ? I extends Model
207
- ? I[]
207
+ ? I[] | PartialModelObject<I>[]
208
208
  : Expression<T[K]>
209
209
  : Expression<T[K]>;
210
210
  };
@@ -216,6 +216,8 @@ declare namespace Objection {
216
216
  '#dbRef'?: MaybeCompositeId;
217
217
  '#ref'?: string;
218
218
  '#id'?: string;
219
+ '#unrelate'?: boolean;
220
+ '#delete'?: boolean;
219
221
  };
220
222
 
221
223
  /**
@@ -597,6 +599,7 @@ declare namespace Objection {
597
599
 
598
600
  interface WithMethod<QB extends AnyQueryBuilder> {
599
601
  (alias: string, expr: CallbackVoid<QB> | AnyQueryBuilder | Raw): QB;
602
+ (alias: string, columns: string[], expr: CallbackVoid<QB> | AnyQueryBuilder | Raw): QB;
600
603
  }
601
604
 
602
605
  interface JoinRelatedOptions {
@@ -612,6 +615,7 @@ declare namespace Objection {
612
615
  (table: TableRef<QB>, leftCol: ColumnRef, op: Operator, rightCol: ColumnRef): QB;
613
616
  (table: TableRef<QB>, leftCol: ColumnRef, rightCol: ColumnRef): QB;
614
617
  (table: TableRef<QB>, cb: CallbackVoid<Knex.JoinClause>): QB;
618
+ (table: TableRef<QB>, columns: { [leftCol: string]: ColumnRef }): QB;
615
619
  (table: TableRef<QB>, raw: Raw): QB;
616
620
  (raw: Raw): QB;
617
621
  }
@@ -624,6 +628,7 @@ declare namespace Objection {
624
628
 
625
629
  interface AggregateMethod<QB extends AnyQueryBuilder> {
626
630
  (column: ColumnRef): QB;
631
+ (aliasToColumnDict: { [alias: string]: ColumnRef }): QB;
627
632
  }
628
633
 
629
634
  interface CountMethod<QB extends AnyQueryBuilder> {
@@ -655,7 +660,7 @@ declare namespace Objection {
655
660
  interface FirstMethod {
656
661
  <QB extends AnyQueryBuilder>(
657
662
  this: QB,
658
- ): QB extends ArrayQueryBuilder<QB> ? MaybeSingleQueryBuilder<QB> : QB;
663
+ ): [ResultType<QB>] extends [any[]] ? MaybeSingleQueryBuilder<QB> : QB;
659
664
  }
660
665
 
661
666
  type ForIdValue = MaybeCompositeId | AnyQueryBuilder;
@@ -693,10 +698,11 @@ declare namespace Objection {
693
698
  }
694
699
 
695
700
  interface TableRefForMethod {
696
- (modelClass: ModelClass<Model> | typeof Model): string;
701
+ (modelClassOrTableName: string | AnyModelConstructor): string;
697
702
  }
698
703
 
699
704
  interface AliasForMethod<QB extends AnyQueryBuilder> {
705
+ (modelClassOrTableName: string | AnyModelConstructor): string | null;
700
706
  (modelClassOrTableName: string | AnyModelConstructor, alias: string): QB;
701
707
  }
702
708
 
@@ -707,7 +713,7 @@ declare namespace Objection {
707
713
  interface ReturningMethod {
708
714
  <QB extends AnyQueryBuilder>(
709
715
  this: QB,
710
- column: string | Raw | (string | Raw)[],
716
+ column: string | Raw | (string | Raw)[] | readonly (string | Raw)[],
711
717
  ): QB extends NumberQueryBuilder<QB> ? ArrayQueryBuilder<QB> : QB;
712
718
  }
713
719
 
@@ -856,6 +862,8 @@ declare namespace Objection {
856
862
  export class QueryBuilder<M extends Model, R = M[]> implements CatchablePromiseLike<R> {
857
863
  static forClass: ForClassMethod;
858
864
 
865
+ constructor(modelClass: ModelConstructor<M>);
866
+
859
867
  select: SelectMethod<this>;
860
868
  columns: SelectMethod<this>;
861
869
  column: SelectMethod<this>;
@@ -1031,6 +1039,7 @@ declare namespace Objection {
1031
1039
  increment: IncrementDecrementMethod<this>;
1032
1040
  decrement: IncrementDecrementMethod<this>;
1033
1041
  first: FirstMethod;
1042
+ none: IdentityMethod<this>;
1034
1043
 
1035
1044
  orderBy: OrderByMethod<this>;
1036
1045
  orderByRaw: OrderByRawMethod<this>;
@@ -1091,9 +1100,7 @@ declare namespace Objection {
1091
1100
  truncate(): Promise<void>;
1092
1101
  allowGraph: AllowGraphMethod<this>;
1093
1102
 
1094
- throwIfNotFound: (
1095
- arg?: any,
1096
- ) => R extends Model | undefined ? SingleQueryBuilder<QueryBuilder<M, M>> : this;
1103
+ throwIfNotFound: (arg?: any) => R extends Model | undefined ? SingleQueryBuilder<this> : this;
1097
1104
 
1098
1105
  returning: ReturningMethod;
1099
1106
  forUpdate: IdentityMethod<this>;
@@ -1110,6 +1117,8 @@ declare namespace Objection {
1110
1117
  modelClass: ModelClassMethod<M>;
1111
1118
  tableNameFor: TableRefForMethod;
1112
1119
  tableRefFor: TableRefForMethod;
1120
+ tableName(): string;
1121
+ tableRef(): string;
1113
1122
  reject: OneArgMethod<any, this>;
1114
1123
  resolve: OneArgMethod<any, this>;
1115
1124
  transacting: OneArgMethod<TransactionOrKnex, this>;
@@ -1120,12 +1129,13 @@ declare namespace Objection {
1120
1129
  toKnexQuery<T extends {} = ModelObject<M>>(): Knex.QueryBuilder<T, T[]>;
1121
1130
  knex(knex?: Knex): Knex;
1122
1131
  clone(): this;
1132
+ emptyInstance(): this;
1123
1133
 
1124
1134
  page(page: number, pageSize: number): PageQueryBuilder<this>;
1125
1135
  range(): PageQueryBuilder<this>;
1126
1136
  range(start: number, end: number): PageQueryBuilder<this>;
1127
- offset(offset: number): this;
1128
- limit(limit: number): this;
1137
+ offset(offset: number, options?: boolean | { skipBinding?: boolean }): this;
1138
+ limit(limit: number, options?: boolean | { skipBinding?: boolean }): this;
1129
1139
  resultSize(): Promise<number>;
1130
1140
 
1131
1141
  runBefore: RunBeforeMethod<this>;
@@ -1302,6 +1312,7 @@ declare namespace Objection {
1302
1312
 
1303
1313
  export interface ToJsonOptions extends CloneOptions {
1304
1314
  virtuals?: boolean | string[];
1315
+ format?: Pojo;
1305
1316
  }
1306
1317
 
1307
1318
  export interface ValidatorContext {
@@ -1334,6 +1345,7 @@ declare namespace Objection {
1334
1345
  upperCase?: boolean;
1335
1346
  underscoreBeforeDigits?: boolean;
1336
1347
  underscoreBetweenUppercaseLetters?: boolean;
1348
+ noDoubleUnderscores?: boolean;
1337
1349
  }
1338
1350
 
1339
1351
  export interface ColumnNameMappers {
@@ -1354,11 +1366,12 @@ declare namespace Objection {
1354
1366
  (options?: SnakeCaseMappersOptions): KnexMappers;
1355
1367
  }
1356
1368
 
1369
+ export interface KnexIdentifierMappingFactory {
1370
+ (colToProp: Record<string, string>): KnexMappers;
1371
+ }
1372
+
1357
1373
  export type ValidationErrorType =
1358
- | 'ModelValidation'
1359
- | 'RelationExpression'
1360
- | 'UnallowedRelation'
1361
- | 'InvalidGraph';
1374
+ 'ModelValidation' | 'RelationExpression' | 'UnallowedRelation' | 'InvalidGraph';
1362
1375
 
1363
1376
  export class ValidationError extends Error {
1364
1377
  constructor(args: CreateValidationErrorArgs & { modelClass?: ModelClass<Model> });
@@ -1426,15 +1439,17 @@ declare namespace Objection {
1426
1439
  interface PrototypeType<T> extends Function {
1427
1440
  prototype: T;
1428
1441
  }
1429
-
1442
+
1430
1443
  interface ConstructorFunctionType<T = any> extends PrototypeType<T> {
1431
1444
  new (...args: any[]): T;
1432
1445
  }
1433
-
1446
+
1434
1447
  // for internal use on generic static this deduction, copied from https://github.com/microsoft/TypeScript/issues/5863#issuecomment-1483978415
1435
- type ConstructorType<T = unknown, Static extends Record<string, any> = PrototypeType<T>> = (ConstructorFunctionType<T> | PrototypeType<T>) & {
1448
+ type ConstructorType<T = unknown, Static extends Record<string, any> = PrototypeType<T>> = (
1449
+ ConstructorFunctionType<T> | PrototypeType<T>
1450
+ ) & {
1436
1451
  [Key in keyof Static]: Static[Key];
1437
- };
1452
+ };
1438
1453
 
1439
1454
  export interface ModelConstructor<M extends Model> extends Constructor<M> {}
1440
1455
 
@@ -1442,16 +1457,18 @@ declare namespace Objection {
1442
1457
  QueryBuilder: typeof QueryBuilder;
1443
1458
 
1444
1459
  tableName: string;
1445
- idColumn: string | string[];
1460
+ idColumn: null | string | string[] | readonly string[];
1446
1461
  jsonSchema: JSONSchema;
1447
1462
  relationMappings: RelationMappings | RelationMappingsThunk;
1448
1463
  modelPaths: string[];
1449
- jsonAttributes: string[];
1464
+ jsonAttributes: string[] | readonly string[];
1450
1465
  virtualAttributes: string[];
1451
1466
  uidProp: string;
1452
1467
  uidRefProp: string;
1453
1468
  dbRefProp: string;
1454
1469
  propRefRegex: RegExp;
1470
+ graphUnrelateProp: string;
1471
+ graphDeleteProp: string;
1455
1472
  pickJsonSchemaProperties: boolean;
1456
1473
  relatedFindQueryMutates: boolean;
1457
1474
  relatedInsertQueryMutates: boolean;
@@ -1551,16 +1568,18 @@ declare namespace Objection {
1551
1568
  static QueryBuilder: typeof QueryBuilder;
1552
1569
 
1553
1570
  static tableName: string;
1554
- static idColumn: string | string[];
1571
+ static idColumn: null | string | string[] | readonly string[];
1555
1572
  static jsonSchema: JSONSchema;
1556
1573
  static relationMappings: RelationMappings | RelationMappingsThunk;
1557
1574
  static modelPaths: string[];
1558
- static jsonAttributes: string[];
1575
+ static jsonAttributes: string[] | readonly string[];
1559
1576
  static virtualAttributes: string[];
1560
1577
  static uidProp: string;
1561
1578
  static uidRefProp: string;
1562
1579
  static dbRefProp: string;
1563
1580
  static propRefRegex: RegExp;
1581
+ static graphUnrelateProp: string;
1582
+ static graphDeleteProp: string;
1564
1583
  static pickJsonSchemaProperties: boolean;
1565
1584
  static relatedFindQueryMutates: boolean;
1566
1585
  static relatedInsertQueryMutates: boolean;
@@ -1581,7 +1600,7 @@ declare namespace Objection {
1581
1600
  static defaultGraphOptions?: GraphOptions;
1582
1601
 
1583
1602
  static query<M extends Model>(
1584
- this: Constructor<M>,
1603
+ this: ConstructorType<M>,
1585
1604
  trxOrKnex?: TransactionOrKnex,
1586
1605
  ): QueryBuilderType<M>;
1587
1606
 
@@ -1596,8 +1615,8 @@ declare namespace Objection {
1596
1615
  trxOrKnex?: TransactionOrKnex,
1597
1616
  ): QueryBuilderType<RM>;
1598
1617
 
1599
- static fromJson<M extends Model>(this: Constructor<M>, json: object, opt?: ModelOptions): M;
1600
- static fromDatabaseJson<M extends Model>(this: Constructor<M>, json: object): M;
1618
+ static fromJson<M extends Model>(this: ConstructorType<M>, json: object, opt?: ModelOptions): M;
1619
+ static fromDatabaseJson<M extends Model>(this: ConstructorType<M>, json: object): M;
1601
1620
 
1602
1621
  static columnNameToPropertyName(columnName: string): string;
1603
1622
  static propertyNameToColumnName(propertyName: string): string;
@@ -1623,14 +1642,14 @@ declare namespace Objection {
1623
1642
  static bindTransaction<M>(this: M, trxOrKnex: TransactionOrKnex): M;
1624
1643
 
1625
1644
  static fetchGraph<M extends Model>(
1626
- this: Constructor<M>,
1645
+ this: ConstructorType<M>,
1627
1646
  modelOrObject: PartialModelObject<M>,
1628
1647
  expression: RelationExpression<M>,
1629
1648
  options?: FetchGraphOptions,
1630
1649
  ): SingleQueryBuilder<QueryBuilderType<M>>;
1631
1650
 
1632
1651
  static fetchGraph<M extends Model>(
1633
- this: Constructor<M>,
1652
+ this: ConstructorType<M>,
1634
1653
  modelOrObject: PartialModelObject<M>[],
1635
1654
  expression: RelationExpression<M>,
1636
1655
  options?: FetchGraphOptions,
@@ -1686,7 +1705,7 @@ declare namespace Objection {
1686
1705
  $formatDatabaseJson(json: Pojo): Pojo;
1687
1706
  $parseDatabaseJson(json: Pojo): Pojo;
1688
1707
 
1689
- $formatJson(json: Pojo): Pojo;
1708
+ $formatJson(json: Pojo, opt?: Pojo): Pojo;
1690
1709
  $parseJson(json: Pojo, opt?: ModelOptions): Pojo;
1691
1710
 
1692
1711
  $beforeValidate(jsonSchema: JSONSchema, json: Pojo, opt: ModelOptions): JSONSchema;
@@ -1850,14 +1869,7 @@ declare namespace Objection {
1850
1869
  * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
1851
1870
  */
1852
1871
  export type JSONSchemaTypeName =
1853
- | 'string'
1854
- | 'number'
1855
- | 'integer'
1856
- | 'boolean'
1857
- | 'object'
1858
- | 'array'
1859
- | 'null'
1860
- | string;
1872
+ 'string' | 'number' | 'integer' | 'boolean' | 'object' | 'array' | 'null' | string;
1861
1873
 
1862
1874
  export type JSONSchemaType = JSONSchemaArray[] | boolean | number | null | object | string;
1863
1875
 
@@ -1889,6 +1901,13 @@ declare namespace Objection {
1889
1901
  $schema?: JSONSchemaVersion;
1890
1902
  $comment?: string;
1891
1903
 
1904
+ /**
1905
+ * @see https://json-schema.org/draft/2019-09/release-notes
1906
+ */
1907
+ $defs?: {
1908
+ [key: string]: JSONSchemaDefinition;
1909
+ };
1910
+
1892
1911
  /**
1893
1912
  * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
1894
1913
  */