velocious 1.0.111 → 1.0.113

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.
File without changes
@@ -1,44 +1,27 @@
1
1
  /**
2
- * A generic query over some model type.
2
+ * @typedef {object} QueryArgsType
3
+ * @property {import("../drivers/base.js").default} driver
4
+ * @property {Array<import("./from-base.js").default>} [froms]
5
+ * @property {string[]} [groups]
6
+ * @property {Array<import("./join-base.js").default>} [joins]
7
+ * @property {import("../handler.js").default} handler
8
+ * @property {number | null} [limit]
9
+ * @property {number | null} [offset]
10
+ * @property {Array<import("./order-base.js").default>} [orders]
11
+ * @property {number | null} [page]
12
+ * @property {number} [perPage]
13
+ * @property {NestedPreloadRecord} [preload]
14
+ * @property {Array<import("./select-base.js").default>} [selects]
15
+ * @property {Array<import("./where-base.js").default>} [wheres]
3
16
  */
4
17
  export default class VelociousDatabaseQuery {
5
18
  /**
6
- * @param {object} args
7
- * @param {import("../drivers/base.js").default} args.driver
8
- * @param {Array<import("./from-base.js").default>} [args.froms]
9
- * @param {string[]} [args.groups]
10
- * @param {Array<import("./join-base.js").default>} [args.joins]
11
- * @param {import("../handler.js").default} args.handler
12
- * @param {number | null} [args.limit]
13
- * @param {typeof import("../record/index.js").default} [args.modelClass]
14
- * @param {number | null} [args.offset]
15
- * @param {Array<import("./order-base.js").default>} [args.orders]
16
- * @param {number | null} [args.page]
17
- * @param {number} [args.perPage]
18
- * @param {NestedPreloadRecord} [args.preload]
19
- * @param {Array<import("./select-base.js").default>} [args.selects]
20
- * @param {Array<import("./where-base.js").default>} [args.wheres]
19
+ * @param {QueryArgsType} args
21
20
  */
22
- constructor({ driver, froms, groups, joins, handler, limit, modelClass, offset, orders, page, perPage, preload, selects, wheres, ...restArgs }: {
23
- driver: import("../drivers/base.js").default;
24
- froms?: Array<import("./from-base.js").default>;
25
- groups?: string[];
26
- joins?: Array<import("./join-base.js").default>;
27
- handler: import("../handler.js").default;
28
- limit?: number | null;
29
- modelClass?: typeof import("../record/index.js").default;
30
- offset?: number | null;
31
- orders?: Array<import("./order-base.js").default>;
32
- page?: number | null;
33
- perPage?: number;
34
- preload?: NestedPreloadRecord;
35
- selects?: Array<import("./select-base.js").default>;
36
- wheres?: Array<import("./where-base.js").default>;
37
- });
21
+ constructor({ driver, froms, groups, joins, handler, limit, offset, orders, page, perPage, preload, selects, wheres }: QueryArgsType);
38
22
  driver: import("../drivers/base.js").default;
39
23
  handler: import("../handler.js").default;
40
24
  logger: Logger;
41
- modelClass: typeof import("../record/index.js").default;
42
25
  _froms: import("./from-base.js").default[];
43
26
  _groups: string[];
44
27
  _joins: import("./join-base.js").default[];
@@ -51,75 +34,16 @@ export default class VelociousDatabaseQuery {
51
34
  _selects: SelectBase[];
52
35
  /** @type {import("./where-base.js").default[]} */
53
36
  _wheres: import("./where-base.js").default[];
54
- /**
55
- * @returns {VelociousDatabaseQuery}
56
- */
57
- clone(): VelociousDatabaseQuery;
58
- /**
59
- * @returns {Promise<number>}
60
- */
61
- count(): Promise<number>;
62
- /**
63
- * @returns {import("./from-base.js").default[]}
64
- */
37
+ /** @returns {this} */
38
+ clone(): this;
39
+ /** @returns {import("./from-base.js").default[]} */
65
40
  getFroms(): import("./from-base.js").default[];
66
- /**
67
- * @returns {string[]}
68
- */
41
+ /** @returns {string[]} */
69
42
  getGroups(): string[];
70
- /**
71
- * @returns {typeof import("../record/index.js").default}
72
- */
73
- getModelClass(): typeof import("../record/index.js").default;
74
- /**
75
- * @returns {import("../query-parser/options.js").default}
76
- */
43
+ /** @returns {import("../query-parser/options.js").default} */
77
44
  getOptions(): import("../query-parser/options.js").default;
78
- /**
79
- * @returns {Array<import("./select-base.js").default>}
80
- */
45
+ /** @returns {Array<import("./select-base.js").default>} */
81
46
  getSelects(): Array<import("./select-base.js").default>;
82
- /**
83
- * @returns {Promise<void>}
84
- */
85
- destroyAll(): Promise<void>;
86
- /**
87
- * @param {number|string} recordId
88
- * @returns {Promise<import("../record/index.js").default>}
89
- */
90
- find(recordId: number | string): Promise<import("../record/index.js").default>;
91
- /**
92
- * @param {{[key: string]: any}} conditions
93
- * @returns {Promise<import("../record/index.js").default|null>}
94
- */
95
- findBy(conditions: {
96
- [key: string]: any;
97
- }): Promise<import("../record/index.js").default | null>;
98
- /**
99
- * @param {{[key: string]: any}} conditions
100
- * @param {function() : void} callback
101
- * @returns {Promise<import("../record/index.js").default>}
102
- */
103
- findOrCreateBy(conditions: {
104
- [key: string]: any;
105
- }, callback: () => void): Promise<import("../record/index.js").default>;
106
- /**
107
- * @param {{[key: string]: any}} conditions
108
- * @returns {Promise<import("../record/index.js").default>}
109
- */
110
- findByOrFail(conditions: {
111
- [key: string]: any;
112
- }): Promise<import("../record/index.js").default>;
113
- /**
114
- * @param {object} conditions
115
- * @param {function(import("../record/index.js").default) : void} callback
116
- * @returns {Promise<import("../record/index.js").default>}
117
- */
118
- findOrInitializeBy(conditions: object, callback: (arg0: import("../record/index.js").default) => void): Promise<import("../record/index.js").default>;
119
- /**
120
- * @returns {Promise<import("../record/index.js").default>}
121
- */
122
- first(): Promise<import("../record/index.js").default>;
123
47
  /**
124
48
  * @param {string|import("./from-base.js").default} from
125
49
  * @returns {this}
@@ -137,10 +61,6 @@ export default class VelociousDatabaseQuery {
137
61
  joins(join: string | {
138
62
  [key: string]: any;
139
63
  }): this;
140
- /**
141
- * @returns {Promise<import("../record/index.js").default>}
142
- */
143
- last(): Promise<import("../record/index.js").default>;
144
64
  /**
145
65
  * @param {number} value
146
66
  * @returns {this}
@@ -176,9 +96,7 @@ export default class VelociousDatabaseQuery {
176
96
  * @returns {this}
177
97
  */
178
98
  reorder(order: string | number): this;
179
- /**
180
- * @returns {this}
181
- */
99
+ /** @returns {this} */
182
100
  reverseOrder(): this;
183
101
  /**
184
102
  * @param {SelectArgumentType} select
@@ -189,15 +107,8 @@ export default class VelociousDatabaseQuery {
189
107
  * @returns {Promise<Array<object>>} Array of results from the database
190
108
  */
191
109
  _executeQuery(): Promise<Array<object>>;
192
- /**
193
- * @returns {Promise<Array<object>>} Array of results from the database
194
- */
110
+ /** @returns {Promise<Array<object>>} Array of results from the database */
195
111
  results(): Promise<Array<object>>;
196
- /**
197
- * Converts query results to array of model instances
198
- * @returns {Promise<Array<import("../record/index.js").default>>}
199
- */
200
- toArray(): Promise<Array<import("../record/index.js").default>>;
201
112
  /**
202
113
  * Generates SQL string representing this query
203
114
  * @returns {string} SQL string representing this query
@@ -205,15 +116,30 @@ export default class VelociousDatabaseQuery {
205
116
  toSql(): string;
206
117
  /**
207
118
  * @param {WhereArgumentType} where
208
- * @returns {VelociousDatabaseQuery} This query instance
119
+ * @returns {this} This query instance
209
120
  */
210
- where(where: WhereArgumentType): VelociousDatabaseQuery;
121
+ where(where: WhereArgumentType): this;
211
122
  }
212
123
  export type NestedPreloadRecord = {
213
124
  [key: string]: boolean | NestedPreloadRecord;
214
125
  };
215
126
  export type SelectArgumentType = string | string[] | import("./select-base.js").default | import("./select-base.js").default[];
216
127
  export type WhereArgumentType = object | string;
128
+ export type QueryArgsType = {
129
+ driver: import("../drivers/base.js").default;
130
+ froms?: Array<import("./from-base.js").default>;
131
+ groups?: string[];
132
+ joins?: Array<import("./join-base.js").default>;
133
+ handler: import("../handler.js").default;
134
+ limit?: number | null;
135
+ offset?: number | null;
136
+ orders?: Array<import("./order-base.js").default>;
137
+ page?: number | null;
138
+ perPage?: number;
139
+ preload?: NestedPreloadRecord;
140
+ selects?: Array<import("./select-base.js").default>;
141
+ wheres?: Array<import("./where-base.js").default>;
142
+ };
217
143
  import { Logger } from "../../logger.js";
218
144
  import SelectBase from "./select-base.js";
219
145
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/database/query/index.js"],"names":[],"mappings":"AAwBA;;GAEG;AACH;IACE;;;;;;;;;;;;;;;;OAgBG;IACH,gJAfG;QAAmD,MAAM,EAAjD,OAAO,oBAAoB,EAAE,OAAO;QACW,KAAK,G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H,cAHW,MAAM,GACJ,IAAI,CAKhB;IAED;;;OAGG;IACH,aAHW,MAAM,GAAG,MAAM,GACb,IAAI,CAYhB;IAED;;;OAGG;IACH,iBAHW,MAAM,GACJ,IAAI,CAWhB;IAED;;;OAGG;IACH,iBAHW,MAAM,GACJ,IAAI,CAKhB;IAED;;;OAGG;IACH,cAHW,mBAAmB,GACjB,IAAI,CAKhB;IAED;;;OAGG;IACH,eAHW,MAAM,GAAG,MAAM,GACb,IAAI,CAMhB;IAED;;OAEG;IACH,gBAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,eAHW,kBAAkB,GAChB,IAAI,CAoBhB;IAED;;OAEG;IACH,iBAFa,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CASlC;IAED;;OAEG;IACH,WAFa,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAIlC;IAED;;;OAGG;IACH,WAFa,OAAO,CAAC,KAAK,CAAC,OAAO,oBAAoB,EAAE,OAAO,CAAC,CAAC,CAyBhE;IAED;;;OAGG;IACH,SAFa,MAAM,CAE4B;IAE/C;;;OAGG;IACH,aAHW,iBAAiB,GACf,sBAAsB,CAYlC;CACF;kCA9gBY;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,GAAG,mBAAmB,CAAA;CAAE;iCAC/C,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,kBAAkB,EAAE,OAAO,GAAG,OAAO,kBAAkB,EAAE,OAAO,EAAE;gCAC7F,MAAM,GAAG,MAAM;uBASP,iBAAiB;uBAIf,kBAAkB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/database/query/index.js"],"names":[],"mappings":"AAoBA;;;;;;;;;;;;;;;GAeG;AAEH;IACE;;OAEG;IACH,uHAFW,aAAa,EAoCvB;IAhBC,6CAAoB;IACpB,yCAAsB;IACtB,eAA8B;IAC9B,2CAAmB;IACnB,kBAAqB;IACrB,2CAAmB;IACnB,eAAmB;IACnB,gBA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,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAGnC;IAED;;;OAGG;IACH,SAFa,MAAM,CAE4B;IAE/C;;;OAGG;IACH,aAHW,iBAAiB,GACf,IAAI,CAYhB;CACF;kCAxSY;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,GAAG,mBAAmB,CAAA;CAAE;iCAC/C,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,kBAAkB,EAAE,OAAO,GAAG,OAAO,kBAAkB,EAAE,OAAO,EAAE;gCAC7F,MAAM,GAAG,MAAM;;YAiBd,OAAO,oBAAoB,EAAE,OAAO;YACpC,KAAK,CAAC,OAAO,gBAAgB,EAAE,OAAO,CAAC;aACvC,MAAM,EAAE;YACR,KAAK,CAAC,OAAO,gBAAgB,EAAE,OAAO,CAAC;aACvC,OAAO,eAAe,EAAE,OAAO;YAC/B,MAAM,GAAG,IAAI;aACb,MAAM,GAAG,IAAI;aACb,KAAK,CAAC,OAAO,iBAAiB,EAAE,OAAO,CAAC;WACxC,MAAM,GAAG,IAAI;cACb,MAAM;cACN,mBAAmB;cACnB,KAAK,CAAC,OAAO,kBAAkB,EAAE,OAAO,CAAC;aACzC,KAAK,CAAC,OAAO,iBAAiB,EAAE,OAAO,CAAC;;uBArBjC,iBAAiB;uBAEf,kBAAkB"}
@@ -6,50 +6,43 @@
6
6
  */
7
7
  import FromPlain from "./from-plain.js";
8
8
  import { incorporate } from "incorporator";
9
- import * as inflection from "inflection";
10
9
  import { isPlainObject } from "is-plain-object";
11
10
  import JoinObject from "./join-object.js";
12
11
  import JoinPlain from "./join-plain.js";
13
12
  import { Logger } from "../../logger.js";
14
13
  import OrderPlain from "./order-plain.js";
15
- import Preloader from "./preloader.js";
16
- import RecordNotFoundError from "../record/record-not-found-error.js";
17
14
  import SelectBase from "./select-base.js";
18
15
  import SelectPlain from "./select-plain.js";
19
16
  import WhereHash from "./where-hash.js";
20
17
  import WherePlain from "./where-plain.js";
21
- import restArgsError from "../../utils/rest-args-error.js";
22
18
  /**
23
- * A generic query over some model type.
19
+ * @typedef {object} QueryArgsType
20
+ * @property {import("../drivers/base.js").default} driver
21
+ * @property {Array<import("./from-base.js").default>} [froms]
22
+ * @property {string[]} [groups]
23
+ * @property {Array<import("./join-base.js").default>} [joins]
24
+ * @property {import("../handler.js").default} handler
25
+ * @property {number | null} [limit]
26
+ * @property {number | null} [offset]
27
+ * @property {Array<import("./order-base.js").default>} [orders]
28
+ * @property {number | null} [page]
29
+ * @property {number} [perPage]
30
+ * @property {NestedPreloadRecord} [preload]
31
+ * @property {Array<import("./select-base.js").default>} [selects]
32
+ * @property {Array<import("./where-base.js").default>} [wheres]
24
33
  */
25
34
  export default class VelociousDatabaseQuery {
26
35
  /**
27
- * @param {object} args
28
- * @param {import("../drivers/base.js").default} args.driver
29
- * @param {Array<import("./from-base.js").default>} [args.froms]
30
- * @param {string[]} [args.groups]
31
- * @param {Array<import("./join-base.js").default>} [args.joins]
32
- * @param {import("../handler.js").default} args.handler
33
- * @param {number | null} [args.limit]
34
- * @param {typeof import("../record/index.js").default} [args.modelClass]
35
- * @param {number | null} [args.offset]
36
- * @param {Array<import("./order-base.js").default>} [args.orders]
37
- * @param {number | null} [args.page]
38
- * @param {number} [args.perPage]
39
- * @param {NestedPreloadRecord} [args.preload]
40
- * @param {Array<import("./select-base.js").default>} [args.selects]
41
- * @param {Array<import("./where-base.js").default>} [args.wheres]
36
+ * @param {QueryArgsType} args
42
37
  */
43
- constructor({ driver, froms = [], groups = [], joins = [], handler, limit = null, modelClass, offset = null, orders = [], page = null, perPage, preload = {}, selects = [], wheres = [], ...restArgs }) {
38
+ constructor({ driver, froms = [], groups = [], joins = [], handler, limit = null, offset = null, orders = [], page = null, perPage, preload = {}, selects = [], wheres = [] }) {
44
39
  if (!driver)
45
40
  throw new Error("No driver given to query");
46
41
  if (!handler)
47
42
  throw new Error("No handler given to query");
48
- restArgsError(restArgs);
49
43
  this.driver = driver;
50
44
  this.handler = handler;
51
45
  this.logger = new Logger(this);
52
- this.modelClass = modelClass;
53
46
  this._froms = froms;
54
47
  this._groups = groups;
55
48
  this._joins = joins;
@@ -63,18 +56,16 @@ export default class VelociousDatabaseQuery {
63
56
  /** @type {import("./where-base.js").default[]} */
64
57
  this._wheres = wheres;
65
58
  }
66
- /**
67
- * @returns {VelociousDatabaseQuery}
68
- */
59
+ /** @returns {this} */
69
60
  clone() {
70
- const newQuery = new VelociousDatabaseQuery({
61
+ const QueryClass = /** @type {new (args: QueryArgsType) => this} */ (this.constructor);
62
+ const newQuery = new QueryClass({
71
63
  driver: this.driver,
72
64
  froms: [...this._froms],
73
65
  handler: this.handler.clone(),
74
66
  groups: [...this._groups],
75
67
  joins: [...this._joins],
76
68
  limit: this._limit,
77
- modelClass: this.modelClass,
78
69
  offset: this._offset,
79
70
  orders: [...this._orders],
80
71
  page: this._page,
@@ -85,152 +76,18 @@ export default class VelociousDatabaseQuery {
85
76
  });
86
77
  return newQuery;
87
78
  }
88
- /**
89
- * @returns {Promise<number>}
90
- */
91
- async count() {
92
- // Generate count SQL
93
- let sql = `COUNT(${this.driver.quoteTable(this.getModelClass().tableName())}.${this.driver.quoteColumn(this.getModelClass().primaryKey())})`;
94
- if (this.driver.getType() == "pgsql")
95
- sql += "::int";
96
- sql += " AS count";
97
- // Clone query and execute count
98
- const countQuery = this.clone();
99
- countQuery._selects = [];
100
- countQuery.select(sql);
101
- const results = /** @type {{ count: number }[]} */ (await countQuery._executeQuery());
102
- // The query isn't grouped and a single result has been given
103
- if (results.length == 1) {
104
- return results[0].count;
105
- }
106
- // The query may be grouped and a lot of different counts a given
107
- let countResult = 0;
108
- for (const result of results) {
109
- if (!("count" in result)) {
110
- throw new Error("Invalid count result");
111
- }
112
- countResult += result.count;
113
- }
114
- return countResult;
115
- }
116
- /**
117
- * @returns {import("./from-base.js").default[]}
118
- */
79
+ /** @returns {import("./from-base.js").default[]} */
119
80
  getFroms() {
120
81
  return this._froms;
121
82
  }
122
- /**
123
- * @returns {string[]}
124
- */
83
+ /** @returns {string[]} */
125
84
  getGroups() {
126
85
  return this._groups;
127
86
  }
128
- /**
129
- * @returns {typeof import("../record/index.js").default}
130
- */
131
- getModelClass() {
132
- if (!this.modelClass)
133
- throw new Error("modelClass not set");
134
- return this.modelClass;
135
- }
136
- /**
137
- * @returns {import("../query-parser/options.js").default}
138
- */
87
+ /** @returns {import("../query-parser/options.js").default} */
139
88
  getOptions() { return this.driver.options(); }
140
- /**
141
- * @returns {Array<import("./select-base.js").default>}
142
- */
89
+ /** @returns {Array<import("./select-base.js").default>} */
143
90
  getSelects() { return this._selects; }
144
- /**
145
- * @returns {Promise<void>}
146
- */
147
- async destroyAll() {
148
- const records = await this.toArray();
149
- for (const record of records) {
150
- await record.destroy();
151
- }
152
- }
153
- /**
154
- * @param {number|string} recordId
155
- * @returns {Promise<import("../record/index.js").default>}
156
- */
157
- async find(recordId) {
158
- /** @type {{[key: string]: number | string}} */
159
- const conditions = {};
160
- conditions[this.getModelClass().primaryKey()] = recordId;
161
- const query = this.clone().where(conditions);
162
- const record = await query.first();
163
- if (!record) {
164
- throw new RecordNotFoundError(`Couldn't find ${this.getModelClass().name} with '${this.getModelClass().primaryKey()}'=${recordId}`);
165
- }
166
- return record;
167
- }
168
- /**
169
- * @param {{[key: string]: any}} conditions
170
- * @returns {Promise<import("../record/index.js").default|null>}
171
- */
172
- async findBy(conditions) {
173
- /** @type {{[key: string]: number | string}} */
174
- const newConditions = {};
175
- for (const key in conditions) {
176
- const keyUnderscore = inflection.underscore(key);
177
- newConditions[keyUnderscore] = conditions[key];
178
- }
179
- return await this.clone().where(newConditions).first();
180
- }
181
- /**
182
- * @param {{[key: string]: any}} conditions
183
- * @param {function() : void} callback
184
- * @returns {Promise<import("../record/index.js").default>}
185
- */
186
- async findOrCreateBy(conditions, callback) {
187
- const record = await this.findOrInitializeBy(conditions, callback);
188
- if (record.isNewRecord()) {
189
- await record.save();
190
- }
191
- return record;
192
- }
193
- /**
194
- * @param {{[key: string]: any}} conditions
195
- * @returns {Promise<import("../record/index.js").default>}
196
- */
197
- async findByOrFail(conditions) {
198
- /** @type {{[key: string]: number | string}} */
199
- const newConditions = {};
200
- for (const key in conditions) {
201
- const keyUnderscore = inflection.underscore(key);
202
- newConditions[keyUnderscore] = conditions[key];
203
- }
204
- const model = await this.clone().where(newConditions).first();
205
- if (!model) {
206
- throw new Error("Record not found");
207
- }
208
- return model;
209
- }
210
- /**
211
- * @param {object} conditions
212
- * @param {function(import("../record/index.js").default) : void} callback
213
- * @returns {Promise<import("../record/index.js").default>}
214
- */
215
- async findOrInitializeBy(conditions, callback) {
216
- const record = await this.findBy(conditions);
217
- if (record)
218
- return record;
219
- const ModelClass = this.getModelClass();
220
- const newRecord = new ModelClass(conditions);
221
- if (callback) {
222
- callback(newRecord);
223
- }
224
- return newRecord;
225
- }
226
- /**
227
- * @returns {Promise<import("../record/index.js").default>}
228
- */
229
- async first() {
230
- const newQuery = this.clone().limit(1).reorder(`${this.driver.quoteTable(this.getModelClass().tableName())}.${this.driver.quoteColumn(this.getModelClass().orderableColumn())}`);
231
- const results = await newQuery.toArray();
232
- return results[0];
233
- }
234
91
  /**
235
92
  * @param {string|import("./from-base.js").default} from
236
93
  * @returns {this}
@@ -265,15 +122,6 @@ export default class VelociousDatabaseQuery {
265
122
  }
266
123
  return this;
267
124
  }
268
- /**
269
- * @returns {Promise<import("../record/index.js").default>}
270
- */
271
- async last() {
272
- const primaryKey = this.getModelClass().primaryKey();
273
- const tableName = this.getModelClass().tableName();
274
- const results = await this.clone().reorder(`${this.driver.quoteTable(tableName)}.${this.driver.quoteColumn(primaryKey)} DESC`).limit(1).toArray();
275
- return results[0];
276
- }
277
125
  /**
278
126
  * @param {number} value
279
127
  * @returns {this}
@@ -344,9 +192,7 @@ export default class VelociousDatabaseQuery {
344
192
  this.order(order);
345
193
  return this;
346
194
  }
347
- /**
348
- * @returns {this}
349
- */
195
+ /** @returns {this} */
350
196
  reverseOrder() {
351
197
  for (const order of this._orders) {
352
198
  order.setReverseOrder(true);
@@ -384,35 +230,10 @@ export default class VelociousDatabaseQuery {
384
230
  this.logger.debug(() => ["SQL:", sql]);
385
231
  return results;
386
232
  }
387
- /**
388
- * @returns {Promise<Array<object>>} Array of results from the database
389
- */
233
+ /** @returns {Promise<Array<object>>} Array of results from the database */
390
234
  async results() {
391
235
  return await this._executeQuery();
392
236
  }
393
- /**
394
- * Converts query results to array of model instances
395
- * @returns {Promise<Array<import("../record/index.js").default>>}
396
- */
397
- async toArray() {
398
- const models = [];
399
- const results = await this.results();
400
- for (const result of results) {
401
- const ModelClass = this.getModelClass();
402
- const model = new ModelClass();
403
- model.loadExistingRecord(result);
404
- models.push(model);
405
- }
406
- if (Object.keys(this._preload).length > 0 && models.length > 0) {
407
- const preloader = new Preloader({
408
- modelClass: this.modelClass,
409
- models,
410
- preload: this._preload
411
- });
412
- await preloader.run();
413
- }
414
- return models;
415
- }
416
237
  /**
417
238
  * Generates SQL string representing this query
418
239
  * @returns {string} SQL string representing this query
@@ -420,7 +241,7 @@ export default class VelociousDatabaseQuery {
420
241
  toSql() { return this.driver.queryToSql(this); }
421
242
  /**
422
243
  * @param {WhereArgumentType} where
423
- * @returns {VelociousDatabaseQuery} This query instance
244
+ * @returns {this} This query instance
424
245
  */
425
246
  where(where) {
426
247
  if (typeof where == "string") {
@@ -1 +1 @@
1
- {"version":3,"file":"join-object.d.ts","sourceRoot":"","sources":["../../../../src/database/query/join-object.js"],"names":[],"mappings":"AAIA;;GAEG;AAEH;IACE;;OAEG;IACH,oBAFW,UAAU,EAKpB;IADC,mBAAoB;IAGtB,gBAIC;IAED;;;;;;OAMG;IACH,iBANW,UAAU,cACV,cAAc,oBAAoB,EAAE,OAAO,OAC3C,MAAM,cACN,MAAM,GACJ,MAAM,CAqClB;CACF;yBA7DY;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,GAAG,UAAU,CAAA;CAAC;qBAH7B,gBAAgB"}
1
+ {"version":3,"file":"join-object.d.ts","sourceRoot":"","sources":["../../../../src/database/query/join-object.js"],"names":[],"mappings":"AAIA;;GAEG;AAEH;IACE;;OAEG;IACH,oBAFW,UAAU,EAKpB;IADC,mBAAoB;IAGtB,gBAWC;IAED;;;;;;OAMG;IACH,iBANW,UAAU,cACV,cAAc,oBAAoB,EAAE,OAAO,OAC3C,MAAM,cACN,MAAM,GACJ,MAAM,CAqClB;CACF;yBApEY;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,GAAG,UAAU,CAAA;CAAC;qBAH7B,gBAAgB"}
@@ -12,8 +12,13 @@ export default class VelociousDatabaseQueryJoinObject extends JoinBase {
12
12
  this.object = object;
13
13
  }
14
14
  toSql() {
15
- const modelClass = this.getQuery().modelClass;
16
- return this.joinObject(this.object, modelClass, "", 0);
15
+ const query = this.getQuery();
16
+ if (query.constructor.name != "VelociousDatabaseQueryModelClassQuery") {
17
+ throw new Error(`Query has to be a ModelClassQuery but was a ${query.constructor.name}`);
18
+ }
19
+ // @ts-expect-error
20
+ const ModelClass = /** @type {typeof import("../record/index.js").default} */ (query.modelClass);
21
+ return this.joinObject(this.object, ModelClass, "", 0);
17
22
  }
18
23
  /**
19
24
  * @param {JoinObject} join
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @typedef {{[key: string]: boolean | NestedPreloadRecord }} NestedPreloadRecord
3
+ * @typedef {string | string[] | import("./select-base.js").default | import("./select-base.js").default[]} SelectArgumentType
4
+ * @typedef {object | string} WhereArgumentType
5
+ */
6
+ /**
7
+ * @template {typeof import("../record/index.js").default} MC
8
+ * @typedef {InstanceType<MC>} ModelOf
9
+ */
10
+ /**
11
+ * @template {typeof import("../record/index.js").default} MC
12
+ * @typedef {import("./index.js").QueryArgsType & object} ModelClassQueryArgsType
13
+ * @property {MC} modelClass
14
+ */
15
+ /**
16
+ * A generic query over some model type.
17
+ * @template {typeof import("../record/index.js").default} MC
18
+ */
19
+ export default class VelociousDatabaseQueryModelClassQuery<MC extends typeof import("../record/index.js").default> extends DatabaseQuery {
20
+ /**
21
+ * @param {ModelClassQueryArgsType<MC>} args
22
+ */
23
+ constructor(args: ModelClassQueryArgsType<MC>);
24
+ modelClass: any;
25
+ /** @returns {this} */
26
+ clone(): this;
27
+ /** @returns {Promise<number>} */
28
+ count(): Promise<number>;
29
+ /** @returns {MC} */
30
+ getModelClass(): MC;
31
+ /** @returns {Promise<void>} */
32
+ destroyAll(): Promise<void>;
33
+ /**
34
+ * @param {number|string} recordId
35
+ * @returns {Promise<InstanceType<MC>>}
36
+ */
37
+ find(recordId: number | string): Promise<InstanceType<MC>>;
38
+ /**
39
+ * @param {{[key: string]: any}} conditions
40
+ * @returns {Promise<InstanceType<MC> | null>}
41
+ */
42
+ findBy(conditions: {
43
+ [key: string]: any;
44
+ }): Promise<InstanceType<MC> | null>;
45
+ /**
46
+ * @param {{[key: string]: any}} conditions
47
+ * @param {function() : void} callback
48
+ * @returns {Promise<InstanceType<MC>>}
49
+ */
50
+ findOrCreateBy(conditions: {
51
+ [key: string]: any;
52
+ }, callback: () => void): Promise<InstanceType<MC>>;
53
+ /**
54
+ * @param {{[key: string]: any}} conditions
55
+ * @returns {Promise<InstanceType<MC>>}
56
+ */
57
+ findByOrFail(conditions: {
58
+ [key: string]: any;
59
+ }): Promise<InstanceType<MC>>;
60
+ /**
61
+ * @param {object} conditions
62
+ * @param {function(import("../record/index.js").default) : void} callback
63
+ * @returns {Promise<InstanceType<MC>>}
64
+ */
65
+ findOrInitializeBy(conditions: object, callback: (arg0: import("../record/index.js").default) => void): Promise<InstanceType<MC>>;
66
+ /**
67
+ * @returns {Promise<InstanceType<MC>>}
68
+ */
69
+ first(): Promise<InstanceType<MC>>;
70
+ /**
71
+ * @returns {Promise<InstanceType<MC>>}
72
+ */
73
+ last(): Promise<InstanceType<MC>>;
74
+ /**
75
+ * @param {NestedPreloadRecord} data
76
+ * @returns {this}
77
+ */
78
+ preload(data: NestedPreloadRecord): this;
79
+ /**
80
+ * @param {SelectArgumentType} select
81
+ * @returns {this}
82
+ */
83
+ select(select: SelectArgumentType): this;
84
+ /**
85
+ * Converts query results to array of model instances
86
+ * @returns {Promise<Array<InstanceType<MC>>>}
87
+ */
88
+ toArray(): Promise<Array<InstanceType<MC>>>;
89
+ }
90
+ export type NestedPreloadRecord = {
91
+ [key: string]: boolean | NestedPreloadRecord;
92
+ };
93
+ export type SelectArgumentType = string | string[] | import("./select-base.js").default | import("./select-base.js").default[];
94
+ export type WhereArgumentType = object | string;
95
+ export type ModelOf<MC extends typeof import("../record/index.js").default> = InstanceType<MC>;
96
+ export type ModelClassQueryArgsType<MC extends typeof import("../record/index.js").default> = import("./index.js").QueryArgsType & object;
97
+ import DatabaseQuery from "./index.js";
98
+ //# sourceMappingURL=model-class-query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-class-query.d.ts","sourceRoot":"","sources":["../../../../src/database/query/model-class-query.js"],"names":[],"mappings":"AAWA;;;;GAIG;AACH;;;GAGG;AACH;;;;GAIG;AAEH;;;GAGG;AACH,2DAF2D,EAAE,SAAhD,cAAe,oBAAoB,EAAE,OAAQ;IAGxD;;OAEG;IACH,kBAFW,uBAAuB,CAAC,EAAE,CAAC,EAUrC;IADC,gBAA4B;IAG9B,sBAAsB;IACtB,SADc,IAAI,CAsBjB;IAED,iCAAiC;IACjC,SADc,OAAO,CAAC,MAAM,CAAC,CAqC5B;IAED,oBAAoB;IACpB,iBADc,EAAE,CAKf;IAED,+BAA+B;IAC/B,cADc,OAAO,CAAC,IAAI,CAAC,CAO1B;IAED;;;OAGG;IACH,eAHW,MAAM,GAAC,MAAM,GACX,OAAO,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,CAmBrC;IAED;;;OAGG;IACH,mBAHW;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAC,GAClB,OAAO,CAAC,YAAY,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAiB5C;IAED;;;;OAIG;IACH,2BAJW;QAAC,CAAC,GAAG,EAAE,MAAM,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