velocious 1.0.112 → 1.0.114

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 (28) hide show
  1. package/dist/src/database/query/index.d.ts +46 -120
  2. package/dist/src/database/query/index.d.ts.map +1 -1
  3. package/dist/src/database/query/index.js +31 -219
  4. package/dist/src/database/query/join-object.d.ts.map +1 -1
  5. package/dist/src/database/query/join-object.js +7 -2
  6. package/dist/src/database/query/model-class-query.d.ts +77 -0
  7. package/dist/src/database/query/model-class-query.d.ts.map +1 -0
  8. package/dist/src/database/query/model-class-query.js +212 -0
  9. package/dist/src/database/query-parser/select-parser.d.ts.map +1 -1
  10. package/dist/src/database/query-parser/select-parser.js +3 -1
  11. package/dist/src/database/record/index.d.ts +42 -67
  12. package/dist/src/database/record/index.d.ts.map +1 -1
  13. package/dist/src/database/record/index.js +33 -57
  14. package/dist/src/database/record/instance-relationships/base.d.ts +35 -24
  15. package/dist/src/database/record/instance-relationships/base.d.ts.map +1 -1
  16. package/dist/src/database/record/instance-relationships/base.js +24 -11
  17. package/dist/src/database/record/instance-relationships/belongs-to.d.ts +11 -1
  18. package/dist/src/database/record/instance-relationships/belongs-to.d.ts.map +1 -1
  19. package/dist/src/database/record/instance-relationships/belongs-to.js +14 -3
  20. package/dist/src/database/record/instance-relationships/has-many.d.ts +23 -3
  21. package/dist/src/database/record/instance-relationships/has-many.d.ts.map +1 -1
  22. package/dist/src/database/record/instance-relationships/has-many.js +16 -4
  23. package/dist/src/database/record/instance-relationships/has-one.d.ts +21 -4
  24. package/dist/src/database/record/instance-relationships/has-one.d.ts.map +1 -1
  25. package/dist/src/database/record/instance-relationships/has-one.js +19 -8
  26. package/dist/src/environment-handlers/node/cli/commands/generate/base-models.d.ts.map +1 -1
  27. package/dist/src/environment-handlers/node/cli/commands/generate/base-models.js +26 -10
  28. package/package.json +1 -1
@@ -12,7 +12,7 @@ import HasManyRelationship from "./relationships/has-many.js";
12
12
  import HasOneInstanceRelationship from "./instance-relationships/has-one.js";
13
13
  import HasOneRelationship from "./relationships/has-one.js";
14
14
  import * as inflection from "inflection";
15
- import Query from "../query/index.js";
15
+ import ModelClassQuery from "../query/model-class-query.js";
16
16
  import restArgsError from "../../utils/rest-args-error.js";
17
17
  import ValidatorsPresence from "./validators/presence.js";
18
18
  import ValidatorsUniqueness from "./validators/uniqueness.js";
@@ -27,21 +27,18 @@ class ValidationError extends Error {
27
27
  }
28
28
  /**
29
29
  * @param {VelociousDatabaseRecord} model
30
+ * @returns {void}
30
31
  */
31
32
  setModel(model) {
32
33
  this._model = model;
33
34
  }
34
- /**
35
- * @returns {Record<string, ValidationErrorObjectType[]>}
36
- */
35
+ /** @returns {Record<string, ValidationErrorObjectType[]>} */
37
36
  getValidationErrors() {
38
37
  if (!this._validationErrors)
39
38
  throw new Error("Validation errors hasn't been set");
40
39
  return this._validationErrors;
41
40
  }
42
- /**
43
- * @param {Record<string, ValidationErrorObjectType[]>} validationErrors
44
- */
41
+ /** @param {Record<string, ValidationErrorObjectType[]>} validationErrors */
45
42
  setValidationErrors(validationErrors) {
46
43
  this._validationErrors = validationErrors;
47
44
  }
@@ -902,34 +899,26 @@ class VelociousDatabaseRecord {
902
899
  assignments[name] = newValue;
903
900
  translation.assign(assignments);
904
901
  }
905
- /**
906
- * @returns {Query}
907
- */
902
+ /** @returns {ModelClassQuery<typeof this>} */
908
903
  static _newQuery() {
909
904
  const handler = new Handler();
910
- const query = new Query({
905
+ const query = new ModelClassQuery({
911
906
  driver: this.connection(),
912
907
  handler,
913
908
  modelClass: this
914
909
  });
915
910
  return query.from(new FromTable(this.tableName()));
916
911
  }
917
- /**
918
- * @returns {string}
919
- */
912
+ /** @returns {string} */
920
913
  static orderableColumn() {
921
914
  // FIXME: Allow to change to 'created_at' if using UUID?
922
915
  return this.primaryKey();
923
916
  }
924
- /**
925
- * @returns {Query}
926
- */
917
+ /** @returns {ModelClassQuery<typeof this>} */
927
918
  static all() {
928
919
  return this._newQuery();
929
920
  }
930
- /**
931
- * @returns {Promise<number>}
932
- */
921
+ /** @returns {Promise<number>} */
933
922
  static async count() {
934
923
  return await this._newQuery().count();
935
924
  }
@@ -959,7 +948,7 @@ class VelociousDatabaseRecord {
959
948
  }
960
949
  /**
961
950
  * @param {{[key: string]: any}} conditions
962
- * @param {function() : void} callback
951
+ * @param {function() : void} [callback]
963
952
  * @returns {Promise<InstanceType<typeof this>>}
964
953
  */
965
954
  static async findOrCreateBy(conditions, callback) {
@@ -967,68 +956,65 @@ class VelociousDatabaseRecord {
967
956
  }
968
957
  /**
969
958
  * @param {object} conditions
970
- * @param {function(import("../record/index.js").default) : void} callback
959
+ * @param {function(InstanceType<typeof this>) : void} [callback]
971
960
  * @returns {Promise<InstanceType<typeof this>>}
972
961
  */
973
962
  static async findOrInitializeBy(conditions, callback) {
974
963
  return await this._newQuery().findOrInitializeBy(conditions, callback);
975
964
  }
976
- /**
977
- * @returns {Promise<InstanceType<typeof this>>}
978
- */
965
+ /** @returns {Promise<InstanceType<typeof this>>} */
979
966
  static async first() {
980
967
  return await this._newQuery().first();
981
968
  }
982
969
  /**
983
970
  * @param {string|{[key: string]: any}} join
984
- * @returns {Query}
971
+ * @returns {ModelClassQuery<typeof this>}
985
972
  */
986
973
  static joins(join) {
987
974
  return this._newQuery().joins(join);
988
975
  }
989
- /**
990
- * @returns {Promise<InstanceType<typeof this>>}
991
- */
976
+ /** @returns {Promise<InstanceType<typeof this>>} */
992
977
  static async last() {
993
978
  return await this._newQuery().last();
994
979
  }
995
980
  /**
996
981
  * @param {number} value
997
- * @returns {Query}
982
+ * @returns {ModelClassQuery<typeof this>}
998
983
  */
999
984
  static limit(value) {
1000
985
  return this._newQuery().limit(value);
1001
986
  }
1002
987
  /**
1003
988
  * @param {string | number} order
1004
- * @returns {Query}
989
+ * @returns {ModelClassQuery<typeof this>}
1005
990
  */
1006
991
  static order(order) {
1007
992
  return this._newQuery().order(order);
1008
993
  }
1009
994
  /**
1010
995
  * @param {import("../query/index.js").NestedPreloadRecord} preload
1011
- * @returns {Query}
996
+ * @returns {ModelClassQuery<typeof this>}
1012
997
  */
1013
998
  static preload(preload) {
1014
999
  return this._newQuery().preload(preload);
1015
1000
  }
1016
1001
  /**
1017
1002
  * @param {import("../query/index.js").SelectArgumentType} select
1018
- * @returns {Query}
1003
+ * @returns {ModelClassQuery<typeof this>}
1019
1004
  */
1020
1005
  static select(select) {
1021
1006
  return this._newQuery().select(select);
1022
1007
  }
1023
1008
  /**
1024
- * @returns {Promise<VelociousDatabaseRecord[]>}
1009
+ * @returns {Promise<InstanceType<typeof this>[]>}
1025
1010
  */
1026
- static toArray() {
1027
- return this._newQuery().toArray();
1011
+ static async toArray() {
1012
+ const modelClassQuery = /** @type {ModelClassQuery<typeof this>} */ (this._newQuery());
1013
+ return await modelClassQuery.toArray();
1028
1014
  }
1029
1015
  /**
1030
1016
  * @param {import("../query/index.js").WhereArgumentType} where
1031
- * @returns {Query}
1017
+ * @returns {ModelClassQuery<typeof this>}
1032
1018
  */
1033
1019
  static where(where) {
1034
1020
  return this._newQuery().where(where);
@@ -1235,9 +1221,7 @@ class VelociousDatabaseRecord {
1235
1221
  }
1236
1222
  return belongsToChanges;
1237
1223
  }
1238
- /**
1239
- * @returns {Promise<void>}
1240
- */
1224
+ /** @returns {Promise<void>} */
1241
1225
  async _createNewRecord() {
1242
1226
  if (!this.getModelClass().connection()["insertSql"]) {
1243
1227
  throw new Error(`No insertSql on ${this.getModelClass().connection().constructor.name}`);
@@ -1276,9 +1260,7 @@ class VelociousDatabaseRecord {
1276
1260
  instanceRelationship.setPreloaded(true);
1277
1261
  }
1278
1262
  }
1279
- /**
1280
- * @returns {Promise<void>}
1281
- */
1263
+ /** @returns {Promise<void>} */
1282
1264
  async _updateRecordWithChanges() {
1283
1265
  /** @type {Record<string, any>} */
1284
1266
  const conditions = {};
@@ -1298,9 +1280,7 @@ class VelociousDatabaseRecord {
1298
1280
  await this._reloadWithId(this.id());
1299
1281
  }
1300
1282
  }
1301
- /**
1302
- * @returns {number|string}
1303
- */
1283
+ /** @returns {number|string} */
1304
1284
  id() {
1305
1285
  if (!this.getModelClass()._columnNameToAttributeName) {
1306
1286
  throw new Error(`Column names mapping hasn't been set on ${this.constructor.name}. Has the model been initialized?`);
@@ -1312,13 +1292,9 @@ class VelociousDatabaseRecord {
1312
1292
  }
1313
1293
  return this.readAttribute(attributeName);
1314
1294
  }
1315
- /**
1316
- * @returns {boolean}
1317
- */
1295
+ /** @returns {boolean} */
1318
1296
  isPersisted() { return !this._isNewRecord; }
1319
- /**
1320
- * @returns {boolean}
1321
- */
1297
+ /** @returns {boolean} */
1322
1298
  isNewRecord() { return this._isNewRecord; }
1323
1299
  /**
1324
1300
  * @param {boolean} newIsNewRecord
@@ -1328,14 +1304,16 @@ class VelociousDatabaseRecord {
1328
1304
  this._isNewRecord = newIsNewRecord;
1329
1305
  }
1330
1306
  /**
1307
+ * @template {typeof VelociousDatabaseRecord} MC
1331
1308
  * @param {string | number} id
1309
+ * @returns {Promise<void>}
1332
1310
  */
1333
1311
  async _reloadWithId(id) {
1334
1312
  const primaryKey = this.getModelClass().primaryKey();
1335
1313
  /** @type {Record<string, any>} */
1336
1314
  const whereObject = {};
1337
1315
  whereObject[primaryKey] = id;
1338
- const query = this.getModelClass().where(whereObject);
1316
+ const query = /** @type {import("../query/model-class-query.js").default<MC>} */ (this.getModelClass().where(whereObject));
1339
1317
  const reloadedModel = await query.first();
1340
1318
  if (!reloadedModel)
1341
1319
  throw new Error(`${this.constructor.name}#${id} couldn't be reloaded - record didn't exist`);
@@ -1346,7 +1324,7 @@ class VelociousDatabaseRecord {
1346
1324
  * @returns {Promise<void>}
1347
1325
  */
1348
1326
  async reload() {
1349
- this._reloadWithId(this.readAttribute("id"));
1327
+ await this._reloadWithId(this.readAttribute("id"));
1350
1328
  }
1351
1329
  async _runValidations() {
1352
1330
  /** @type {Record<string, {type: string, message: string}>} */
@@ -1367,9 +1345,7 @@ class VelociousDatabaseRecord {
1367
1345
  throw validationError;
1368
1346
  }
1369
1347
  }
1370
- /**
1371
- * @returns {string[]}
1372
- */
1348
+ /** @returns {string[]} */
1373
1349
  fullErrorMessages() {
1374
1350
  /** @type {string[]} */
1375
1351
  const validationErrorMessages = [];
@@ -1,30 +1,37 @@
1
- export default class VelociousDatabaseRecordBaseInstanceRelationship {
1
+ /**
2
+ * @template {typeof import("../index.js").default} MC
3
+ * @template {typeof import("../index.js").default} TMC
4
+ * @typedef {object} InstanceRelationshipsBaseArgs
5
+ * @property {InstanceType<MC>} model
6
+ @property {import("../relationships/base.js").default} relationship
7
+ */
8
+ /**
9
+ * A generic query over some model type.
10
+ * @template {typeof import("../index.js").default} MC
11
+ * @template {typeof import("../index.js").default} TMC
12
+ */
13
+ export default class VelociousDatabaseRecordBaseInstanceRelationship<MC extends typeof import("../index.js").default, TMC extends typeof import("../index.js").default> {
2
14
  /**
3
- * @param {object} args
4
- * @param {import("../index.js").default} args.model
5
- * @param {import("../relationships/base.js").default} args.relationship
15
+ * @param {InstanceRelationshipsBaseArgs<MC, TMC>} args
6
16
  */
7
- constructor({ model, relationship }: {
8
- model: import("../index.js").default;
9
- relationship: import("../relationships/base.js").default;
10
- });
17
+ constructor({ model, relationship }: InstanceRelationshipsBaseArgs<MC, TMC>);
11
18
  /** @type {boolean | undefined} */
12
19
  _autoSave: boolean | undefined;
13
20
  _dirty: boolean;
14
- model: import("../index.js").default;
21
+ model: InstanceType<MC>;
15
22
  relationship: import("../relationships/base.js").default;
16
23
  /**
17
24
  * @abstract
18
- * @param {import("../index.js").default[] | import("../index.js").default} models
25
+ * @param {InstanceType<TMC>[] | InstanceType<TMC>} models
19
26
  * @returns {void}
20
27
  */
21
- addToLoaded(models: import("../index.js").default[] | import("../index.js").default): void;
28
+ addToLoaded(models: InstanceType<TMC>[] | InstanceType<TMC>): void;
22
29
  /**
23
30
  * @abstract
24
31
  * @param {Record<string, any>} attributes
25
- * @returns {import("../index.js").default}
32
+ * @returns {InstanceType<TMC>}
26
33
  */
27
- build(attributes: Record<string, any>): import("../index.js").default;
34
+ build(attributes: Record<string, any>): InstanceType<TMC>;
28
35
  /** @returns {boolean | undefined} Whether the relationship should be auto-saved before saving the parent model */
29
36
  getAutoSave(): boolean | undefined;
30
37
  /**
@@ -46,13 +53,13 @@ export default class VelociousDatabaseRecordBaseInstanceRelationship {
46
53
  load(): Promise<void>;
47
54
  /** @returns {boolean} Whether the relationship has been preloaded */
48
55
  isLoaded(): boolean;
49
- /** @returns {import("../index.js").default | Array<import("../index.js").default> | undefined} The loaded model or models (depending on relationship type) */
50
- loaded(): import("../index.js").default | Array<import("../index.js").default> | undefined;
51
- /** @param {import("../index.js").default|Array<import("../index.js").default>|undefined} model */
52
- setLoaded(model: import("../index.js").default | Array<import("../index.js").default> | undefined): void;
53
- _loaded: import("../index.js").default | import("../index.js").default[];
54
- /** @returns {import("../index.js").default | import("../index.js").default[] | undefined} */
55
- getLoadedOrUndefined(): import("../index.js").default | import("../index.js").default[] | undefined;
56
+ /** @returns {InstanceType<TMC> | Array<InstanceType<TMC>> | undefined} The loaded model or models (depending on relationship type) */
57
+ loaded(): InstanceType<TMC> | Array<InstanceType<TMC>> | undefined;
58
+ /** @param {InstanceType<TMC> | Array<InstanceType<TMC>> | undefined} model */
59
+ setLoaded(model: InstanceType<TMC> | Array<InstanceType<TMC>> | undefined): void;
60
+ _loaded: InstanceType<TMC> | InstanceType<TMC>[];
61
+ /** @returns {InstanceType<TMC> | InstanceType<TMC>[] | undefined} */
62
+ getLoadedOrUndefined(): InstanceType<TMC> | InstanceType<TMC>[] | undefined;
56
63
  /** @returns {boolean} The loaded model or models (depending on relationship type) */
57
64
  getPreloaded(): boolean;
58
65
  /** @param {boolean} isPreloaded */
@@ -60,15 +67,19 @@ export default class VelociousDatabaseRecordBaseInstanceRelationship {
60
67
  _preloaded: boolean;
61
68
  /** @returns {string} The foreign key for this relationship */
62
69
  getForeignKey(): string;
63
- /** @returns {import("../index.js").default} model */
64
- getModel(): import("../index.js").default;
70
+ /** @returns {InstanceType<MC>} */
71
+ getModel(): InstanceType<MC>;
65
72
  /** @returns {string} The primary key for this relationship's model */
66
73
  getPrimaryKey(): string;
67
74
  /** @returns {import("../relationships/base.js").default} The relationship object that this instance relationship is based on */
68
75
  getRelationship(): import("../relationships/base.js").default;
69
- /** @returns {typeof import("../index.js").default | undefined} The model class that this instance relationship */
70
- getTargetModelClass(): typeof import("../index.js").default | undefined;
76
+ /** @returns {TMC | undefined} The model class that this instance relationship */
77
+ getTargetModelClass(): TMC | undefined;
71
78
  /** @returns {string} The type of relationship (e.g. "has_many", "belongs_to", etc.) */
72
79
  getType(): string;
73
80
  }
81
+ export type InstanceRelationshipsBaseArgs<MC extends typeof import("../index.js").default, TMC extends typeof import("../index.js").default> = {
82
+ model: InstanceType<MC>;
83
+ relationship: import("../relationships/base.js").default;
84
+ };
74
85
  //# sourceMappingURL=base.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/database/record/instance-relationships/base.js"],"names":[],"mappings":"AAEA;IAIE;;;;OAIG;IACH,qCAHG;QAA4C,KAAK,EAAzC,OAAO,aAAa,EAAE,OAAO;QACoB,YAAY,EAA7D,OAAO,0BAA0B,EAAE,OAAO;KACpD,EAKA;IAZD,kCAAkC;IAClC,WADW,OAAO,GAAG,SAAS,CACT;IAQnB,gBAAmB;IACnB,qCAAkB;IAClB,yDAAgC;IAGlC;;;;OAIG;IACH,oBAHW,OAAO,aAAa,EAAE,OAAO,EAAE,GAAG,OAAO,aAAa,EAAE,OAAO,GAC7D,IAAI,CAIhB;IAED;;;;OAIG;IACH,kBAHW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjB,OAAO,aAAa,EAAE,OAAO,CAIzC;IAED,kHAAkH;IAClH,eADc,OAAO,GAAG,SAAS,CACM;IAEvC;;;OAGG;IACH,8BAHW,OAAO,GACL,IAAI,CAEkD;IAEnE;;;OAGG;IACH,mBAHW,OAAO,GACL,IAAI,CAE4B;IAE7C,+EAA+E;IAC/E,YADc,OAAO,CACY;IAEjC;;;OAGG;IACH,QAFa,OAAO,CAAC,IAAI,CAAC,CAIzB;IAED,sEAAsE;IACtE,YADe,OAAO,CACqB;IAE3C,8JAA8J;IAC9J,UADc,OAAO,aAAa,EAAE,OAAO,GAAG,KAAK,CAAC,OAAO,aAAa,EAAE,OAAO,CAAC,GAAG,SAAS,CAO7F;IAED,kGAAkG;IAClG,iBADY,OAAO,aAAa,EAAE,OAAO,GAAC,KAAK,CAAC,OAAO,aAAa,EAAE,OAAO,CAAC,GAAC,SAAS,QAC/C;IAAtB,yEAAoB;IAEvC,6FAA6F;IAC7F,wBADc,OAAO,aAAa,EAAE,OAAO,GAAG,OAAO,aAAa,EAAE,OAAO,EAAE,GAAG,SAAS,CAC3C;IAE9C,qFAAqF;IACrF,gBADc,OAAO,CAC6B;IAElD,mCAAmC;IACnC,0BADY,OAAO,QACwC;IAA/B,oBAA6B;IAEzD,8DAA8D;IAC9D,iBADc,MAAM,CAC6C;IAEjE,qDAAqD;IACrD,YADc,OAAO,aAAa,EAAE,OAAO,CACX;IAEhC,sEAAsE;IACtE,iBADc,MAAM,CAC6C;IAEjE,gIAAgI;IAChI,mBADc,OAAO,0BAA0B,EAAE,OAAO,CACV;IAE9C,kHAAkH;IAClH,uBADc,cAAc,aAAa,EAAE,OAAO,GAAG,SAAS,CACe;IAE7E,uFAAuF;IACvF,WADc,MAAM,CACiC;CACtD"}
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/database/record/instance-relationships/base.js"],"names":[],"mappings":"AAEA;;;;;;GAMG;AAEH;;;;GAIG;AACH,qEAHoD,EAAE,SAAzC,cAAe,aAAa,EAAE,OAAQ,EACC,GAAG,SAA1C,cAAe,aAAa,EAAE,OAAQ;IAMjD;;OAEG;IACH,qCAFW,6BAA6B,CAAC,EAAE,EAAE,GAAG,CAAC,EAMhD;IAVD,kCAAkC;IAClC,WADW,OAAO,GAAG,SAAS,CACT;IAMnB,gBAAmB;IACnB,wBAAkB;IAClB,yDAAgC;IAGlC;;;;OAIG;IACH,oBAHW,YAAY,CAAC,GAAG,CAAC,EAAE,GAAG,YAAY,CAAC,GAAG,CAAC,GACrC,IAAI,CAIhB;IAED;;;;OAIG;IACH,kBAHW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjB,YAAY,CAAC,GAAG,CAAC,CAI7B;IAED,kHAAkH;IAClH,eADc,OAAO,GAAG,SAAS,CACM;IAEvC;;;OAGG;IACH,8BAHW,OAAO,GACL,IAAI,CAEkD;IAEnE;;;OAGG;IACH,mBAHW,OAAO,GACL,IAAI,CAE4B;IAE7C,+EAA+E;IAC/E,YADc,OAAO,CACY;IAEjC;;;OAGG;IACH,QAFa,OAAO,CAAC,IAAI,CAAC,CAIzB;IAED,sEAAsE;IACtE,YADe,OAAO,CACqB;IAE3C,sIAAsI;IACtI,UADc,YAAY,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,GAAG,SAAS,CAOrE;IAED,8EAA8E;IAC9E,iBADY,YAAY,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,GAAG,SAAS,QAC3B;IAAtB,iDAAoB;IAEvC,qEAAqE;IACrE,wBADc,YAAY,CAAC,GAAG,CAAC,GAAG,YAAY,CAAC,GAAG,CAAC,EAAE,GAAG,SAAS,CACnB;IAE9C,qFAAqF;IACrF,gBADc,OAAO,CAC6B;IAElD,mCAAmC;IACnC,0BADY,OAAO,QACwC;IAA/B,oBAA6B;IAEzD,8DAA8D;IAC9D,iBADc,MAAM,CAC6C;IAEjE,kCAAkC;IAClC,YADc,YAAY,CAAC,EAAE,CAAC,CACE;IAEhC,sEAAsE;IACtE,iBADc,MAAM,CAC6C;IAEjE,gIAAgI;IAChI,mBADc,OAAO,0BAA0B,EAAE,OAAO,CACV;IAE9C,iFAAiF;IACjF,uBADc,GAAG,GAAG,SAAS,CAK5B;IAED,uFAAuF;IACvF,WADc,MAAM,CACiC;CACtD;0CAlHmD,EAAE,SAAzC,cAAe,aAAa,EAAE,OAAQ,EACC,GAAG,SAA1C,cAAe,aAAa,EAAE,OAAQ;WAErC,YAAY,CAAC,EAAE,CAAC;kBAChB,OAAO,0BAA0B,EAAE,OAAO"}
@@ -1,11 +1,21 @@
1
1
  // @ts-check
2
+ /**
3
+ * @template {typeof import("../index.js").default} MC
4
+ * @template {typeof import("../index.js").default} TMC
5
+ * @typedef {object} InstanceRelationshipsBaseArgs
6
+ * @property {InstanceType<MC>} model
7
+ @property {import("../relationships/base.js").default} relationship
8
+ */
9
+ /**
10
+ * A generic query over some model type.
11
+ * @template {typeof import("../index.js").default} MC
12
+ * @template {typeof import("../index.js").default} TMC
13
+ */
2
14
  export default class VelociousDatabaseRecordBaseInstanceRelationship {
3
15
  /** @type {boolean | undefined} */
4
16
  _autoSave = undefined;
5
17
  /**
6
- * @param {object} args
7
- * @param {import("../index.js").default} args.model
8
- * @param {import("../relationships/base.js").default} args.relationship
18
+ * @param {InstanceRelationshipsBaseArgs<MC, TMC>} args
9
19
  */
10
20
  constructor({ model, relationship }) {
11
21
  this._dirty = false;
@@ -14,7 +24,7 @@ export default class VelociousDatabaseRecordBaseInstanceRelationship {
14
24
  }
15
25
  /**
16
26
  * @abstract
17
- * @param {import("../index.js").default[] | import("../index.js").default} models
27
+ * @param {InstanceType<TMC>[] | InstanceType<TMC>} models
18
28
  * @returns {void}
19
29
  */
20
30
  addToLoaded(models) {
@@ -23,7 +33,7 @@ export default class VelociousDatabaseRecordBaseInstanceRelationship {
23
33
  /**
24
34
  * @abstract
25
35
  * @param {Record<string, any>} attributes
26
- * @returns {import("../index.js").default}
36
+ * @returns {InstanceType<TMC>}
27
37
  */
28
38
  build(attributes) {
29
39
  throw new Error("'build' not implemented");
@@ -51,16 +61,16 @@ export default class VelociousDatabaseRecordBaseInstanceRelationship {
51
61
  }
52
62
  /** @returns {boolean} Whether the relationship has been preloaded */
53
63
  isLoaded() { return Boolean(this._loaded); }
54
- /** @returns {import("../index.js").default | Array<import("../index.js").default> | undefined} The loaded model or models (depending on relationship type) */
64
+ /** @returns {InstanceType<TMC> | Array<InstanceType<TMC>> | undefined} The loaded model or models (depending on relationship type) */
55
65
  loaded() {
56
66
  if (!this._preloaded && this.model.isPersisted()) {
57
67
  throw new Error(`${this.model.constructor.name}#${this.relationship.getRelationshipName()} hasn't been preloaded`);
58
68
  }
59
69
  return this._loaded;
60
70
  }
61
- /** @param {import("../index.js").default|Array<import("../index.js").default>|undefined} model */
71
+ /** @param {InstanceType<TMC> | Array<InstanceType<TMC>> | undefined} model */
62
72
  setLoaded(model) { this._loaded = model; }
63
- /** @returns {import("../index.js").default | import("../index.js").default[] | undefined} */
73
+ /** @returns {InstanceType<TMC> | InstanceType<TMC>[] | undefined} */
64
74
  getLoadedOrUndefined() { return this._loaded; }
65
75
  /** @returns {boolean} The loaded model or models (depending on relationship type) */
66
76
  getPreloaded() { return this._preloaded || false; }
@@ -68,14 +78,17 @@ export default class VelociousDatabaseRecordBaseInstanceRelationship {
68
78
  setPreloaded(isPreloaded) { this._preloaded = isPreloaded; }
69
79
  /** @returns {string} The foreign key for this relationship */
70
80
  getForeignKey() { return this.getRelationship().getForeignKey(); }
71
- /** @returns {import("../index.js").default} model */
81
+ /** @returns {InstanceType<MC>} */
72
82
  getModel() { return this.model; }
73
83
  /** @returns {string} The primary key for this relationship's model */
74
84
  getPrimaryKey() { return this.getRelationship().getPrimaryKey(); }
75
85
  /** @returns {import("../relationships/base.js").default} The relationship object that this instance relationship is based on */
76
86
  getRelationship() { return this.relationship; }
77
- /** @returns {typeof import("../index.js").default | undefined} The model class that this instance relationship */
78
- getTargetModelClass() { return this.getRelationship().getTargetModelClass(); }
87
+ /** @returns {TMC | undefined} The model class that this instance relationship */
88
+ getTargetModelClass() {
89
+ const TargetModelClass = /** @type {TMC} */ (this.getRelationship().getTargetModelClass());
90
+ return TargetModelClass;
91
+ }
79
92
  /** @returns {string} The type of relationship (e.g. "has_many", "belongs_to", etc.) */
80
93
  getType() { return this.getRelationship().getType(); }
81
94
  }
@@ -1,4 +1,14 @@
1
- export default class VelociousDatabaseRecordBelongsToInstanceRelationship extends BaseInstanceRelationship {
1
+ /**
2
+ * A generic query over some model type.
3
+ * @template {typeof import("../index.js").default} MC
4
+ * @template {typeof import("../index.js").default} TMC
5
+ */
6
+ export default class VelociousDatabaseRecordBelongsToInstanceRelationship<MC extends typeof import("../index.js").default, TMC extends typeof import("../index.js").default> extends BaseInstanceRelationship<any, any> {
7
+ /**
8
+ * @param {Record<string, any>} data
9
+ * @returns {InstanceType<TMC>}
10
+ */
11
+ build(data: Record<string, any>): InstanceType<TMC>;
2
12
  }
3
13
  import BaseInstanceRelationship from "./base.js";
4
14
  //# sourceMappingURL=belongs-to.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"belongs-to.d.ts","sourceRoot":"","sources":["../../../../../src/database/record/instance-relationships/belongs-to.js"],"names":[],"mappings":"AAIA;CAgCC;qCAlCoC,WAAW"}
1
+ {"version":3,"file":"belongs-to.d.ts","sourceRoot":"","sources":["../../../../../src/database/record/instance-relationships/belongs-to.js"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,0EAHoD,EAAE,SAAzC,cAAe,aAAa,EAAE,OAAQ,EACC,GAAG,SAA1C,cAAe,aAAa,EAAE,OAAQ;IAUjD;;;OAGG;IACH,YAHW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjB,YAAY,CAAC,GAAG,CAAC,CAY7B;CAiBF;qCA9CoC,WAAW"}
@@ -1,15 +1,26 @@
1
1
  // @ts-check
2
2
  import BaseInstanceRelationship from "./base.js";
3
+ /**
4
+ * A generic query over some model type.
5
+ * @template {typeof import("../index.js").default} MC
6
+ * @template {typeof import("../index.js").default} TMC
7
+ */
3
8
  export default class VelociousDatabaseRecordBelongsToInstanceRelationship extends BaseInstanceRelationship {
9
+ /**
10
+ * @param {import("./base.js").InstanceRelationshipsBaseArgs<MC, TMC>} args
11
+ */
12
+ constructor(args) {
13
+ super(args);
14
+ }
4
15
  /**
5
16
  * @param {Record<string, any>} data
6
- * @returns {import("../index.js").default}
17
+ * @returns {InstanceType<TMC>}
7
18
  */
8
19
  build(data) {
9
- const TargetModelClass = this.getTargetModelClass();
20
+ const TargetModelClass = /** @type {TMC} */ (this.getTargetModelClass());
10
21
  if (!TargetModelClass)
11
22
  throw new Error("Can't build a new record without a target model");
12
- const newInstance = new TargetModelClass(data);
23
+ const newInstance = /** @type {InstanceType<TMC>} */ (new TargetModelClass(data));
13
24
  this._loaded = newInstance;
14
25
  return newInstance;
15
26
  }
@@ -1,8 +1,28 @@
1
- export default class VelociousDatabaseRecordHasManyInstanceRelationship extends BaseInstanceRelationship {
1
+ /**
2
+ * A generic query over some model type.
3
+ * @template {typeof import("../index.js").default} MC
4
+ * @template {typeof import("../index.js").default} TMC
5
+ */
6
+ export default class VelociousDatabaseRecordHasManyInstanceRelationship<MC extends typeof import("../index.js").default, TMC extends typeof import("../index.js").default> extends BaseInstanceRelationship<any, any> {
2
7
  /**
3
- * @param {import("../index.js").default[]} models
8
+ * @param {Record<string, any>} data
9
+ * @returns {InstanceType<TMC>}
4
10
  */
5
- setLoaded(models: import("../index.js").default[]): void;
11
+ build(data: Record<string, any>): InstanceType<TMC>;
12
+ /**
13
+ * @returns {Array<InstanceType<TMC>>} The loaded model or models (depending on relationship type)
14
+ */
15
+ loaded(): Array<InstanceType<TMC>>;
16
+ /**
17
+ * @param {InstanceType<MC>[] | InstanceType<MC>} models
18
+ * @returns {void}
19
+ */
20
+ addToLoaded(models: InstanceType<MC>[] | InstanceType<MC>): void;
21
+ /**
22
+ * @param {InstanceType<TMC>[]} models
23
+ * @returns {void}
24
+ */
25
+ setLoaded(models: InstanceType<TMC>[]): void;
6
26
  }
7
27
  import BaseInstanceRelationship from "./base.js";
8
28
  //# sourceMappingURL=has-many.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"has-many.d.ts","sourceRoot":"","sources":["../../../../../src/database/record/instance-relationships/has-many.js"],"names":[],"mappings":"AAIA;IAuGE;;OAEG;IACH,kBAFW,OAAO,aAAa,EAAE,OAAO,EAAE,QAMzC;CACF;qCAjHoC,WAAW"}
1
+ {"version":3,"file":"has-many.d.ts","sourceRoot":"","sources":["../../../../../src/database/record/instance-relationships/has-many.js"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,wEAHoD,EAAE,SAAzC,cAAe,aAAa,EAAE,OAAQ,EACC,GAAG,SAA1C,cAAe,aAAa,EAAE,OAAQ;IAUjD;;;OAGG;IACH,YAHW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjB,YAAY,CAAC,GAAG,CAAC,CAmC7B;IAsBD;;OAEG;IACH,UAFa,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAYpC;IAED;;;OAGG;IACH,oBAHW,YAAY,CAAC,EAAE,CAAC,EAAE,GAAG,YAAY,CAAC,EAAE,CAAC,GACnC,IAAI,CAwBhB;IAED;;;OAGG;IACH,kBAHW,YAAY,CAAC,GAAG,CAAC,EAAE,GACjB,IAAI,CAMhB;CACF;qCA9HoC,WAAW"}
@@ -1,9 +1,20 @@
1
1
  // @ts-check
2
2
  import BaseInstanceRelationship from "./base.js";
3
+ /**
4
+ * A generic query over some model type.
5
+ * @template {typeof import("../index.js").default} MC
6
+ * @template {typeof import("../index.js").default} TMC
7
+ */
3
8
  export default class VelociousDatabaseRecordHasManyInstanceRelationship extends BaseInstanceRelationship {
9
+ /**
10
+ * @param {import("./base.js").InstanceRelationshipsBaseArgs<MC, TMC>} args
11
+ */
12
+ constructor(args) {
13
+ super(args);
14
+ }
4
15
  /**
5
16
  * @param {Record<string, any>} data
6
- * @returns {import("../index.js").default}
17
+ * @returns {InstanceType<TMC>}
7
18
  */
8
19
  build(data) {
9
20
  // Spawn new model of the targeted class
@@ -48,7 +59,7 @@ export default class VelociousDatabaseRecordHasManyInstanceRelationship extends
48
59
  this.setPreloaded(true);
49
60
  }
50
61
  /**
51
- * @returns {import("../index.js").default | Array<import("../index.js").default> | undefined} The loaded model or models (depending on relationship type)
62
+ * @returns {Array<InstanceType<TMC>>} The loaded model or models (depending on relationship type)
52
63
  */
53
64
  loaded() {
54
65
  if (!this._preloaded && this.model.isPersisted()) {
@@ -60,7 +71,7 @@ export default class VelociousDatabaseRecordHasManyInstanceRelationship extends
60
71
  return this._loaded;
61
72
  }
62
73
  /**
63
- * @param {import("../index.js").default[] | import("../index.js").default} models
74
+ * @param {InstanceType<MC>[] | InstanceType<MC>} models
64
75
  * @returns {void}
65
76
  */
66
77
  addToLoaded(models) {
@@ -93,7 +104,8 @@ export default class VelociousDatabaseRecordHasManyInstanceRelationship extends
93
104
  }
94
105
  }
95
106
  /**
96
- * @param {import("../index.js").default[]} models
107
+ * @param {InstanceType<TMC>[]} models
108
+ * @returns {void}
97
109
  */
98
110
  setLoaded(models) {
99
111
  if (!Array.isArray(models))
@@ -1,7 +1,24 @@
1
- export default class VelociousDatabaseRecordHasOneInstanceRelationship extends BaseInstanceRelationship {
2
- /** @type {import("../index.js").default | undefined} */
3
- _loaded: import("../index.js").default | undefined;
4
- getLoadedOrUndefined(): import("../index.js").default;
1
+ /**
2
+ * A generic query over some model type.
3
+ * @template {typeof import("../index.js").default} MC
4
+ * @template {typeof import("../index.js").default} TMC
5
+ */
6
+ export default class VelociousDatabaseRecordHasOneInstanceRelationship<MC extends typeof import("../index.js").default, TMC extends typeof import("../index.js").default> extends BaseInstanceRelationship<any, any> {
7
+ /** @type {InstanceType<TMC> | undefined} */
8
+ _loaded: InstanceType<TMC> | undefined;
9
+ /**
10
+ * @param {Record<string, any>} data
11
+ * @returns {InstanceType<TMC>}
12
+ */
13
+ build(data: Record<string, any>): InstanceType<TMC>;
14
+ /**
15
+ * @returns {InstanceType<TMC> | Array<InstanceType<TMC>> | undefined} The loaded model or models (depending on relationship type)
16
+ */
17
+ loaded(): InstanceType<TMC> | Array<InstanceType<TMC>> | undefined;
18
+ getLoadedOrUndefined(): InstanceType<TMC>;
19
+ /** @param {InstanceType<TMC> | Array<InstanceType<TMC>>} model */
20
+ setLoaded(model: InstanceType<TMC> | Array<InstanceType<TMC>>): void;
21
+ getTargetModelClass(): typeof import("../index.js").default;
5
22
  }
6
23
  import BaseInstanceRelationship from "./base.js";
7
24
  //# sourceMappingURL=has-one.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"has-one.d.ts","sourceRoot":"","sources":["../../../../../src/database/record/instance-relationships/has-one.js"],"names":[],"mappings":"AAIA;IACE,wDAAwD;IACxD,SADW,OAAO,aAAa,EAAE,OAAO,GAAG,SAAS,CACjC;IAiDnB,sDAA8C;CAU/C;qCA/DoC,WAAW"}
1
+ {"version":3,"file":"has-one.d.ts","sourceRoot":"","sources":["../../../../../src/database/record/instance-relationships/has-one.js"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,uEAHoD,EAAE,SAAzC,cAAe,aAAa,EAAE,OAAQ,EACC,GAAG,SAA1C,cAAe,aAAa,EAAE,OAAQ;IAUjD,4CAA4C;IAC5C,SADW,YAAY,CAAC,GAAG,CAAC,GAAG,SAAS,CACrB;IAEnB;;;OAGG;IACH,YAHW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACjB,YAAY,CAAC,GAAG,CAAC,CAY7B;IAsBD;;OAEG;IACH,UAFa,YAAY,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,GAAG,SAAS,CAQpE;IAED,0CAA8C;IAE9C,kEAAkE;IAClE,iBADY,YAAY,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,QAKvD;IAED,4DAAwE;CACzE;qCA3EoC,WAAW"}