@ja-platform/db 0.0.0-dev-20261028223022 → 0.0.0-stage
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.
- package/README.md +3 -0
- package/package.json +3 -52
- package/LICENSE +0 -521
- package/dist/@types/Schema.d.ts +0 -1
- package/dist/@types/app.d.ts +0 -12
- package/dist/@types/browser.d.ts +0 -31
- package/dist/@types/credentials/ApiCredentials.d.ts +0 -18
- package/dist/@types/credentials/MobileCredentials.d.ts +0 -22
- package/dist/@types/credentials/index.d.ts +0 -2
- package/dist/@types/database/Batch.d.ts +0 -61
- package/dist/@types/database/Collection.d.ts +0 -85
- package/dist/@types/database/Database.d.ts +0 -22
- package/dist/@types/database/DatabaseObject.d.ts +0 -51
- package/dist/@types/database/GenericDatabase.d.ts +0 -4
- package/dist/@types/database/JourneyAPIAdapter.d.ts +0 -45
- package/dist/@types/database/ObjectDisplayScope.d.ts +0 -22
- package/dist/@types/database/OnlineDB.d.ts +0 -8
- package/dist/@types/database/adapters/BaseAdapter.d.ts +0 -26
- package/dist/@types/database/adapters/DatabaseAdapter.d.ts +0 -22
- package/dist/@types/database/adapters/WebSQLAdapter.d.ts +0 -50
- package/dist/@types/index.d.ts +0 -2
- package/dist/@types/query/InternalQuery.d.ts +0 -17
- package/dist/@types/query/Query.d.ts +0 -133
- package/dist/@types/query/getPreloadRelationships.d.ts +0 -9
- package/dist/@types/query/queryOperations.d.ts +0 -87
- package/dist/@types/types/Attachment.d.ts +0 -104
- package/dist/@types/types/Location.d.ts +0 -18
- package/dist/@types/types/ObjectData.d.ts +0 -12
- package/dist/@types/types/TypeOperations.d.ts +0 -42
- package/dist/@types/utils/FunctionQueue.d.ts +0 -32
- package/dist/@types/utils/JourneyPromise.d.ts +0 -2
- package/dist/@types/utils/base64.d.ts +0 -4
- package/dist/@types/utils/retryableFetch.d.ts +0 -3
- package/dist/Schema.js +0 -6
- package/dist/Schema.js.map +0 -1
- package/dist/app.js +0 -30
- package/dist/app.js.map +0 -1
- package/dist/browser.js +0 -77
- package/dist/browser.js.map +0 -1
- package/dist/credentials/ApiCredentials.js +0 -61
- package/dist/credentials/ApiCredentials.js.map +0 -1
- package/dist/credentials/MobileCredentials.js +0 -73
- package/dist/credentials/MobileCredentials.js.map +0 -1
- package/dist/credentials/index.js +0 -19
- package/dist/credentials/index.js.map +0 -1
- package/dist/database/Batch.js +0 -189
- package/dist/database/Batch.js.map +0 -1
- package/dist/database/Collection.js +0 -69
- package/dist/database/Collection.js.map +0 -1
- package/dist/database/Database.js +0 -47
- package/dist/database/Database.js.map +0 -1
- package/dist/database/DatabaseObject.js +0 -646
- package/dist/database/DatabaseObject.js.map +0 -1
- package/dist/database/GenericDatabase.js +0 -3
- package/dist/database/GenericDatabase.js.map +0 -1
- package/dist/database/JourneyAPIAdapter.js +0 -357
- package/dist/database/JourneyAPIAdapter.js.map +0 -1
- package/dist/database/ObjectDisplayScope.js +0 -117
- package/dist/database/ObjectDisplayScope.js.map +0 -1
- package/dist/database/OnlineDB.js +0 -16
- package/dist/database/OnlineDB.js.map +0 -1
- package/dist/database/adapters/BaseAdapter.js +0 -66
- package/dist/database/adapters/BaseAdapter.js.map +0 -1
- package/dist/database/adapters/DatabaseAdapter.js +0 -3
- package/dist/database/adapters/DatabaseAdapter.js.map +0 -1
- package/dist/database/adapters/WebSQLAdapter.js +0 -343
- package/dist/database/adapters/WebSQLAdapter.js.map +0 -1
- package/dist/index.js +0 -20
- package/dist/index.js.map +0 -1
- package/dist/query/InternalQuery.js +0 -3
- package/dist/query/InternalQuery.js.map +0 -1
- package/dist/query/Query.js +0 -414
- package/dist/query/Query.js.map +0 -1
- package/dist/query/getPreloadRelationships.js +0 -45
- package/dist/query/getPreloadRelationships.js.map +0 -1
- package/dist/query/queryOperations.js +0 -678
- package/dist/query/queryOperations.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/types/Attachment.js +0 -198
- package/dist/types/Attachment.js.map +0 -1
- package/dist/types/Location.js +0 -38
- package/dist/types/Location.js.map +0 -1
- package/dist/types/ObjectData.js +0 -3
- package/dist/types/ObjectData.js.map +0 -1
- package/dist/types/TypeOperations.js +0 -267
- package/dist/types/TypeOperations.js.map +0 -1
- package/dist/utils/FunctionQueue.js +0 -91
- package/dist/utils/FunctionQueue.js.map +0 -1
- package/dist/utils/JourneyPromise.js +0 -3
- package/dist/utils/JourneyPromise.js.map +0 -1
- package/dist/utils/base64.js +0 -40
- package/dist/utils/base64.js.map +0 -1
- package/dist/utils/retryableFetch.js +0 -57
- package/dist/utils/retryableFetch.js.map +0 -1
- package/readme.md +0 -37
package/dist/query/Query.js
DELETED
|
@@ -1,414 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Query = void 0;
|
|
4
|
-
const getPreloadRelationships_1 = require("./getPreloadRelationships");
|
|
5
|
-
const queryOperations_1 = require("./queryOperations");
|
|
6
|
-
const DatabaseObject_1 = require("../database/DatabaseObject");
|
|
7
|
-
const Batch_1 = require("../database/Batch");
|
|
8
|
-
const parser_schema_1 = require("@ja-platform/parser-schema");
|
|
9
|
-
const cloneDeep = require('lodash/cloneDeep');
|
|
10
|
-
class Query {
|
|
11
|
-
static isQuery(value) {
|
|
12
|
-
if (value == null) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
if (value instanceof Query) {
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
if (typeof value == 'object') {
|
|
19
|
-
// This covers other implementations of the Query class
|
|
20
|
-
return typeof value.where == 'function' && typeof value.toArray == 'function';
|
|
21
|
-
}
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
constructor(adapter, type, expression, ordering) {
|
|
25
|
-
// type is the object type, not the query type
|
|
26
|
-
if (adapter == null || type == null) {
|
|
27
|
-
throw new Error('not enough arguments');
|
|
28
|
-
}
|
|
29
|
-
if (ordering == null) {
|
|
30
|
-
ordering = [];
|
|
31
|
-
}
|
|
32
|
-
if (expression == null) {
|
|
33
|
-
expression = new queryOperations_1.TrueExpression();
|
|
34
|
-
}
|
|
35
|
-
this.ordering = ordering;
|
|
36
|
-
this.expression = expression;
|
|
37
|
-
this.adapter = adapter;
|
|
38
|
-
this.type = type;
|
|
39
|
-
this.limitNumber = null;
|
|
40
|
-
this.skipNumber = 0;
|
|
41
|
-
// Relationship structure to preload.
|
|
42
|
-
this._include = {};
|
|
43
|
-
// Watch this variable for changes to determine when the query should be reloaded.
|
|
44
|
-
this._freshness = nextFreshness();
|
|
45
|
-
// We throw an exception when old API methods are being used, instead of just failing silently.
|
|
46
|
-
Object.defineProperty(this, 'length', {
|
|
47
|
-
get: function () {
|
|
48
|
-
throw new Error('Use .count() instead of .length on a query.');
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
// We cannot check access to all indices, but we can check for [0], which is the most commonly used case.
|
|
52
|
-
Object.defineProperty(this, '0', {
|
|
53
|
-
get: function () {
|
|
54
|
-
throw new Error('Queries are not arrays. Use .fetch() on a query to get an array.');
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
/** @internal */
|
|
59
|
-
_clone(newType) {
|
|
60
|
-
if (newType == null) {
|
|
61
|
-
newType = this.type;
|
|
62
|
-
}
|
|
63
|
-
let clonedQuery = new Query(this.adapter, newType, this.expression, this.ordering);
|
|
64
|
-
clonedQuery.limitNumber = this.limitNumber;
|
|
65
|
-
clonedQuery.skipNumber = this.skipNumber;
|
|
66
|
-
clonedQuery._include = cloneDeep(this._include);
|
|
67
|
-
return clonedQuery;
|
|
68
|
-
}
|
|
69
|
-
/** @internal */
|
|
70
|
-
_filter(objects) {
|
|
71
|
-
var result = [];
|
|
72
|
-
for (var i = 0; i < objects.length; i++) {
|
|
73
|
-
var object = objects[i];
|
|
74
|
-
if (this.expression == null || this.expression.evaluate(object)) {
|
|
75
|
-
result.push(object);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return result;
|
|
79
|
-
}
|
|
80
|
-
// Since we don't store any query results, this doesn't actually reload anything.
|
|
81
|
-
// However, it changes the value of _freshness, so that users of this query know to reload it.
|
|
82
|
-
_reload() {
|
|
83
|
-
this._freshness = nextFreshness();
|
|
84
|
-
}
|
|
85
|
-
/** @internal */
|
|
86
|
-
_sort(objects) {
|
|
87
|
-
const ordering = this.ordering;
|
|
88
|
-
if (ordering.length > 0) {
|
|
89
|
-
objects.sort(function (a, b) {
|
|
90
|
-
for (let i = 0; i < ordering.length; i++) {
|
|
91
|
-
let attribute = ordering[i];
|
|
92
|
-
let ascending = 1;
|
|
93
|
-
if (attribute[0] == '-') {
|
|
94
|
-
ascending = -1;
|
|
95
|
-
attribute = attribute.substring(1);
|
|
96
|
-
}
|
|
97
|
-
const valueA = a.attributes[attribute];
|
|
98
|
-
const valueB = b.attributes[attribute];
|
|
99
|
-
const diff = compare(valueA, valueB);
|
|
100
|
-
if (diff !== 0) {
|
|
101
|
-
return diff * ascending;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
const idDiff = compare(a.id, b.id);
|
|
105
|
-
// Do final sorting by ID to make it deterministic.
|
|
106
|
-
return idDiff;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
return objects;
|
|
110
|
-
}
|
|
111
|
-
/** @internal */
|
|
112
|
-
async _fetch() {
|
|
113
|
-
const data = await this.adapter.executeQuery(this);
|
|
114
|
-
var result = [];
|
|
115
|
-
for (let i = 0; i < data.length; i++) {
|
|
116
|
-
const objdata = data[i];
|
|
117
|
-
const object = DatabaseObject_1.DatabaseObject.build(this.adapter, this.type, objdata.id, objdata);
|
|
118
|
-
result.push(object);
|
|
119
|
-
}
|
|
120
|
-
if (Object.keys(this._include).length > 0) {
|
|
121
|
-
return preload(this.adapter, this.type, result, this._include);
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
return result;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
_explain() {
|
|
128
|
-
return this.adapter.explain(this);
|
|
129
|
-
}
|
|
130
|
-
// This version pre-loads relationships in the display format. It is purely an optimization.
|
|
131
|
-
// If columns (Array of FormatStringExpression) is specified, it is used to determine
|
|
132
|
-
// the relationship structure. Otherwise, the displayFormat is used.
|
|
133
|
-
_fetchWithDisplay(columns) {
|
|
134
|
-
const relationships = (0, getPreloadRelationships_1.getPreloadRelationships)(this.type, columns);
|
|
135
|
-
return this._includeInternal(relationships)._fetch();
|
|
136
|
-
}
|
|
137
|
-
/** @internal */
|
|
138
|
-
async _get() {
|
|
139
|
-
if (arguments.length > 0) {
|
|
140
|
-
// e.g. .get({make: 'Nokia'})
|
|
141
|
-
return this.where.apply(this, arguments)._get();
|
|
142
|
-
}
|
|
143
|
-
const results = await this.adapter.executeQuery(this.limit(1));
|
|
144
|
-
let data;
|
|
145
|
-
if (results.length > 0) {
|
|
146
|
-
data = results[0]; // skip is already applied by the executeQuery
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
data = null;
|
|
150
|
-
}
|
|
151
|
-
if (data) {
|
|
152
|
-
return DatabaseObject_1.DatabaseObject.build(this.adapter, this.type, data.id, data);
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
where(filters, ...args) {
|
|
159
|
-
const cloned = this._clone();
|
|
160
|
-
if (arguments.length > 0) {
|
|
161
|
-
let e;
|
|
162
|
-
if (typeof filters == 'object') {
|
|
163
|
-
// example: where({make: 'Nokia', model: '5800})
|
|
164
|
-
e = (0, queryOperations_1.expressionFromHash)(this.type, filters);
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
// example: where('make = ? and model = ?', 'Nokia', '5800')
|
|
168
|
-
e = (0, queryOperations_1.parse)(this.type, filters, args);
|
|
169
|
-
}
|
|
170
|
-
const combined = new queryOperations_1.AndExpression([this.expression, e]);
|
|
171
|
-
cloned.expression = combined;
|
|
172
|
-
}
|
|
173
|
-
return cloned;
|
|
174
|
-
}
|
|
175
|
-
order_by(...fields) {
|
|
176
|
-
const attributes = Array.prototype.slice.call(fields);
|
|
177
|
-
let q = this._clone();
|
|
178
|
-
q.ordering = attributes;
|
|
179
|
-
return q;
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Limit the maximum number of results returned by the query.
|
|
183
|
-
*
|
|
184
|
-
* A new query is returned - the original one is not modified.
|
|
185
|
-
*
|
|
186
|
-
* @param n maximum number of results
|
|
187
|
-
*/
|
|
188
|
-
limit(n) {
|
|
189
|
-
let q = this._clone();
|
|
190
|
-
q.limitNumber = n;
|
|
191
|
-
return q;
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Skip the first n results in the query. Typically used together with limit() for pagination.
|
|
195
|
-
*
|
|
196
|
-
* A new query is returned - the original one is not modified.
|
|
197
|
-
*
|
|
198
|
-
* @param n number of results to skip.
|
|
199
|
-
*/
|
|
200
|
-
skip(n) {
|
|
201
|
-
let q = this._clone();
|
|
202
|
-
q.skipNumber = n;
|
|
203
|
-
return q;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Pre-load belongs-to relationships when executing the query.
|
|
207
|
-
*
|
|
208
|
-
* This allows the implementation to batch the lookups, which is faster than loading each related
|
|
209
|
-
* object individually.
|
|
210
|
-
*
|
|
211
|
-
* Nested belongs-to relationships are supported by using dots, e.g. `.include('owner.region')`.
|
|
212
|
-
*
|
|
213
|
-
* A new query is returned - the original one is not modified.
|
|
214
|
-
*
|
|
215
|
-
* @param relationships belongs-to names
|
|
216
|
-
*/
|
|
217
|
-
include(...relationships) {
|
|
218
|
-
let q = this._clone();
|
|
219
|
-
for (let rel of relationships) {
|
|
220
|
-
normalizeInclude(q._include, rel);
|
|
221
|
-
}
|
|
222
|
-
return q;
|
|
223
|
-
}
|
|
224
|
-
_destroy_all() {
|
|
225
|
-
return this._fetch().then((objects) => {
|
|
226
|
-
let batch = new Batch_1.Batch(this.adapter);
|
|
227
|
-
objects.forEach(function (object) {
|
|
228
|
-
batch.destroy(object);
|
|
229
|
-
});
|
|
230
|
-
return batch._execute();
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
_count() {
|
|
234
|
-
return this.adapter.count(this);
|
|
235
|
-
}
|
|
236
|
-
// Aliases
|
|
237
|
-
/**
|
|
238
|
-
* Execute the query and return the results.
|
|
239
|
-
*/
|
|
240
|
-
toArray() {
|
|
241
|
-
return this._toArray();
|
|
242
|
-
}
|
|
243
|
-
first() {
|
|
244
|
-
return this._first.apply(this, arguments);
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Return the number of results matched by the query.
|
|
248
|
-
*
|
|
249
|
-
* If .limit() was used, it would be respected for the count.
|
|
250
|
-
*/
|
|
251
|
-
count() {
|
|
252
|
-
return this._count.apply(this, arguments);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Return executions stats for the query.
|
|
256
|
-
*
|
|
257
|
-
* The format of the results is not fixed, and may change across versions.
|
|
258
|
-
* For debugging use only.
|
|
259
|
-
*/
|
|
260
|
-
explain() {
|
|
261
|
-
return this._explain.apply(this, arguments);
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Destroy all objects matched by this query.
|
|
265
|
-
*
|
|
266
|
-
* skip() and limit() is respected for this.
|
|
267
|
-
*/
|
|
268
|
-
destroyAll() {
|
|
269
|
-
return this._destroyAll.apply(this, arguments);
|
|
270
|
-
}
|
|
271
|
-
_toArray() {
|
|
272
|
-
return this._fetch.apply(this, arguments);
|
|
273
|
-
}
|
|
274
|
-
_first() {
|
|
275
|
-
return this._get.apply(this, arguments);
|
|
276
|
-
}
|
|
277
|
-
_destroyAll() {
|
|
278
|
-
return this._destroy_all.apply(this, arguments);
|
|
279
|
-
}
|
|
280
|
-
/**
|
|
281
|
-
* Order by one or more attributes.
|
|
282
|
-
*
|
|
283
|
-
* A new query is returned - the original one is not modified.
|
|
284
|
-
*
|
|
285
|
-
* @param fields an array of field names to order by. Prefix with - for descending, e.g. `-updated_at`.
|
|
286
|
-
*/
|
|
287
|
-
orderBy(...fields) {
|
|
288
|
-
return this.order_by.apply(this, fields);
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* For debugging purposes only.
|
|
292
|
-
*
|
|
293
|
-
* The format of the result is not defined.
|
|
294
|
-
*/
|
|
295
|
-
toJSON() {
|
|
296
|
-
return {
|
|
297
|
-
type: this.type.name,
|
|
298
|
-
expression: this.expression,
|
|
299
|
-
ordering: this.ordering,
|
|
300
|
-
limit: this.limitNumber,
|
|
301
|
-
skip: this.skipNumber,
|
|
302
|
-
include: this._include
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
_includeInternal(relationshipHash) {
|
|
306
|
-
let q = this._clone();
|
|
307
|
-
deepMerge(q._include, relationshipHash);
|
|
308
|
-
return q;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
exports.Query = Query;
|
|
312
|
-
function compare(a, b) {
|
|
313
|
-
if (typeof a == 'string' && typeof b == 'string') {
|
|
314
|
-
// Case insensitive comparison
|
|
315
|
-
a = a.toLowerCase();
|
|
316
|
-
b = b.toLowerCase();
|
|
317
|
-
}
|
|
318
|
-
// We can't > or < with `null` or `undefined` - do this manually.
|
|
319
|
-
// null, undefined < anything else
|
|
320
|
-
// Note that `== null` also matches `undefined`.
|
|
321
|
-
if (a == null && b == null) {
|
|
322
|
-
return 0;
|
|
323
|
-
}
|
|
324
|
-
else if (a == null) {
|
|
325
|
-
return -1;
|
|
326
|
-
}
|
|
327
|
-
else if (b == null) {
|
|
328
|
-
return 1;
|
|
329
|
-
}
|
|
330
|
-
else if (a > b) {
|
|
331
|
-
return 1;
|
|
332
|
-
}
|
|
333
|
-
else if (a < b) {
|
|
334
|
-
return -1;
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
return 0;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
var globalFreshness = 0;
|
|
341
|
-
function nextFreshness() {
|
|
342
|
-
globalFreshness += 1;
|
|
343
|
-
return globalFreshness;
|
|
344
|
-
}
|
|
345
|
-
// Merge hashes of hashes (no non-hash values)
|
|
346
|
-
function deepMerge(a, b) {
|
|
347
|
-
Object.keys(b).forEach(function (key) {
|
|
348
|
-
if (!(key in a)) {
|
|
349
|
-
a[key] = {};
|
|
350
|
-
}
|
|
351
|
-
deepMerge(a[key], b[key]);
|
|
352
|
-
});
|
|
353
|
-
return a;
|
|
354
|
-
}
|
|
355
|
-
function normalizeInclude(into, expression) {
|
|
356
|
-
const dot = expression.indexOf('.');
|
|
357
|
-
if (dot < 0) {
|
|
358
|
-
if (!(expression in into)) {
|
|
359
|
-
into[expression] = {};
|
|
360
|
-
}
|
|
361
|
-
return into;
|
|
362
|
-
}
|
|
363
|
-
const head = expression.substring(0, dot); // The first part of the expression
|
|
364
|
-
const tail = expression.substring(dot + 1); // The rest of the expression
|
|
365
|
-
if (!(head in into)) {
|
|
366
|
-
into[head] = {};
|
|
367
|
-
}
|
|
368
|
-
normalizeInclude(into[head], tail);
|
|
369
|
-
return into;
|
|
370
|
-
}
|
|
371
|
-
// Recursively preload a relationship structure
|
|
372
|
-
// objects: Array of DatabaseObject
|
|
373
|
-
// relationships example: {'room' => {'building' => {}}, 'otherrel' => {}}
|
|
374
|
-
async function preload(adapter, type, objects, relationships) {
|
|
375
|
-
let outerPromises = [];
|
|
376
|
-
for (let key in relationships) {
|
|
377
|
-
const relatedType = type.getAttribute(key).type;
|
|
378
|
-
if (!(relatedType instanceof parser_schema_1.SchemaModelType)) {
|
|
379
|
-
throw new Error(`${type.name}.${key} is not a belongs-to relationship`);
|
|
380
|
-
}
|
|
381
|
-
const nestedRelationships = relationships[key];
|
|
382
|
-
// Get ids of all the belongsTo objects in the relationship
|
|
383
|
-
let ids = [];
|
|
384
|
-
for (let object of objects) {
|
|
385
|
-
const id = object[key + '_id'];
|
|
386
|
-
ids.push(id);
|
|
387
|
-
}
|
|
388
|
-
let nestedObjects = [];
|
|
389
|
-
const promise = adapter.getAll(relatedType.name, ids).then(async (data) => {
|
|
390
|
-
for (let i = 0; i < objects.length; i++) {
|
|
391
|
-
const object = objects[i];
|
|
392
|
-
const relatedData = data[i];
|
|
393
|
-
if (relatedData != null) {
|
|
394
|
-
const related = DatabaseObject_1.DatabaseObject.build(adapter, relatedType, relatedData.id, relatedData);
|
|
395
|
-
object._cache(key, related);
|
|
396
|
-
nestedObjects.push(related);
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
// We need to record the fact that we couldn't find the object, so that other
|
|
400
|
-
// code doesn't need to try again.
|
|
401
|
-
object._cache(key, null);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
if (Object.keys(nestedRelationships).length > 0) {
|
|
405
|
-
// Recursively load nested relationships
|
|
406
|
-
await preload(adapter, relatedType, nestedObjects, nestedRelationships);
|
|
407
|
-
}
|
|
408
|
-
});
|
|
409
|
-
outerPromises.push(promise);
|
|
410
|
-
}
|
|
411
|
-
await Promise.all(outerPromises);
|
|
412
|
-
return objects;
|
|
413
|
-
}
|
|
414
|
-
//# sourceMappingURL=Query.js.map
|
package/dist/query/Query.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Query.js","sourceRoot":"","sources":["../../src/query/Query.ts"],"names":[],"mappings":";;;AAAA,uEAAsF;AAEtF,uDAAuH;AACvH,+DAA4D;AAC5D,6CAA0C;AAI1C,8DAA6D;AAE7D,MAAM,SAAS,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAE9C;IAqBE,MAAM,CAAC,OAAO,CAAC,KAAU;QACvB,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;YAClB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,OAAO,IAAI,CAAC;QACd,CAAC;QACD,IAAI,OAAO,KAAK,IAAI,QAAQ,EAAE,CAAC;YAC7B,uDAAuD;YACvD,OAA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AAwC;gBACxC,MAAM,OAAO,CAAC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,mBAAmB,CAAC,CAAC;YAC1E,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;IAED,MAAM,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IACjC,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { getPreloadRelationships, RelationshipHash } from './getPreloadRelationships';\nimport { DatabaseAdapter } from '../database/adapters/DatabaseAdapter';\nimport { Expression, TrueExpression, expressionFromHash, parse as parseQuery, AndExpression } from './queryOperations';\nimport { DatabaseObject } from '../database/DatabaseObject';\nimport { Batch } from '../database/Batch';\nimport { ObjectData } from '../types/ObjectData';\nimport { FormatStringExpression } from '@ja-platform/parser-expressions';\nimport * as j from '../utils/JourneyPromise';\nimport { SchemaModelType } from '@ja-platform/parser-schema';\n\nconst cloneDeep = require('lodash/cloneDeep');\n\nexport class Query<T extends DatabaseObject = DatabaseObject> {\n /** @internal */\n readonly adapter: DatabaseAdapter;\n readonly type: SchemaModelType;\n\n /** @internal */\n expression: Expression;\n\n /** @internal */\n ordering: any[];\n /** @internal */\n limitNumber: number;\n /** @internal */\n skipNumber: number;\n\n /** @internal */\n private _freshness: number;\n\n /** @internal */\n private _include: RelationshipHash;\n\n static isQuery(value: any): boolean {\n if (value == null) {\n return false;\n }\n if (value instanceof Query) {\n return true;\n }\n if (typeof value == 'object') {\n // This covers other implementations of the Query class\n return typeof value.where == 'function' && typeof value.toArray == 'function';\n }\n return false;\n }\n\n constructor(adapter: DatabaseAdapter, type: SchemaModelType, expression?: Expression, ordering?: any[]) {\n // type is the object type, not the query type\n if (adapter == null || type == null) {\n throw new Error('not enough arguments');\n }\n\n if (ordering == null) {\n ordering = [];\n }\n\n if (expression == null) {\n expression = new TrueExpression();\n }\n\n this.ordering = ordering;\n this.expression = expression;\n this.adapter = adapter;\n this.type = type;\n this.limitNumber = null;\n this.skipNumber = 0;\n // Relationship structure to preload.\n this._include = {};\n\n // Watch this variable for changes to determine when the query should be reloaded.\n this._freshness = nextFreshness();\n\n // We throw an exception when old API methods are being used, instead of just failing silently.\n Object.defineProperty(this, 'length', {\n get: function () {\n throw new Error('Use .count() instead of .length on a query.');\n }\n });\n\n // We cannot check access to all indices, but we can check for [0], which is the most commonly used case.\n Object.defineProperty(this, '0', {\n get: function () {\n throw new Error('Queries are not arrays. Use .fetch() on a query to get an array.');\n }\n });\n }\n\n /** @internal */\n _clone(newType?: SchemaModelType): Query<T> {\n if (newType == null) {\n newType = this.type;\n }\n let clonedQuery = new Query<T>(this.adapter, newType, this.expression, this.ordering);\n clonedQuery.limitNumber = this.limitNumber;\n clonedQuery.skipNumber = this.skipNumber;\n clonedQuery._include = cloneDeep(this._include);\n return clonedQuery;\n }\n\n /** @internal */\n _filter(objects: ObjectData[]): ObjectData[] {\n var result = [];\n for (var i = 0; i < objects.length; i++) {\n var object = objects[i];\n if (this.expression == null || this.expression.evaluate(object)) {\n result.push(object);\n }\n }\n return result;\n }\n\n // Since we don't store any query results, this doesn't actually reload anything.\n // However, it changes the value of _freshness, so that users of this query know to reload it.\n private _reload() {\n this._freshness = nextFreshness();\n }\n\n /** @internal */\n _sort(objects: ObjectData[]): ObjectData[] {\n const ordering = this.ordering;\n if (ordering.length > 0) {\n objects.sort(function (a, b) {\n for (let i = 0; i < ordering.length; i++) {\n let attribute = ordering[i];\n let ascending = 1;\n if (attribute[0] == '-') {\n ascending = -1;\n attribute = attribute.substring(1);\n }\n const valueA = a.attributes[attribute];\n const valueB = b.attributes[attribute];\n\n const diff = compare(valueA, valueB);\n if (diff !== 0) {\n return diff * ascending;\n }\n }\n const idDiff = compare(a.id, b.id);\n\n // Do final sorting by ID to make it deterministic.\n return idDiff;\n });\n }\n return objects;\n }\n\n /** @internal */\n async _fetch(): Promise<T[]> {\n const data = await this.adapter.executeQuery(this);\n var result = [];\n\n for (let i = 0; i < data.length; i++) {\n const objdata = data[i];\n const object = DatabaseObject.build(this.adapter, this.type, objdata.id, objdata) as T;\n result.push(object);\n }\n\n if (Object.keys(this._include).length > 0) {\n return preload(this.adapter, this.type, result, this._include);\n } else {\n return result;\n }\n }\n\n private _explain() {\n return this.adapter.explain(this);\n }\n\n // This version pre-loads relationships in the display format. It is purely an optimization.\n // If columns (Array of FormatStringExpression) is specified, it is used to determine\n // the relationship structure. Otherwise, the displayFormat is used.\n _fetchWithDisplay(columns?: FormatStringExpression[] | Record<string, FormatStringExpression>) {\n const relationships = getPreloadRelationships(this.type, columns);\n return this._includeInternal(relationships)._fetch();\n }\n\n /** @internal */\n async _get(): Promise<T> {\n if (arguments.length > 0) {\n // e.g. .get({make: 'Nokia'})\n return this.where.apply(this, arguments)._get();\n }\n\n const results = await this.adapter.executeQuery(this.limit(1));\n let data: any;\n if (results.length > 0) {\n data = results[0]; // skip is already applied by the executeQuery\n } else {\n data = null;\n }\n\n if (data) {\n return DatabaseObject.build(this.adapter, this.type, data.id, data) as T;\n } else {\n return null;\n }\n }\n /**\n * Apply additional query filters, as a new query.\n *\n * @param query the query string\n * @param args query arguments\n */\n where(query: string, ...args: any[]): Query<T>;\n\n /**\n * Apply additional filters, as a new query.\n *\n * @param filters an object of {field: value}\n */\n where(filters?: any): Query<T>;\n\n /**\n * Return a copy of this query.\n */\n where(): Query<T>;\n\n where(filters?: any, ...args: any[]): Query<T> {\n const cloned = this._clone();\n if (arguments.length > 0) {\n let e: Expression;\n if (typeof filters == 'object') {\n // example: where({make: 'Nokia', model: '5800})\n e = expressionFromHash(this.type, filters);\n } else {\n // example: where('make = ? and model = ?', 'Nokia', '5800')\n e = parseQuery(this.type, filters, args);\n }\n const combined = new AndExpression([this.expression, e]);\n cloned.expression = combined;\n }\n return cloned;\n }\n\n private order_by(...fields: string[]) {\n const attributes = Array.prototype.slice.call(fields);\n let q = this._clone();\n q.ordering = attributes;\n return q;\n }\n\n /**\n * Limit the maximum number of results returned by the query.\n *\n * A new query is returned - the original one is not modified.\n *\n * @param n maximum number of results\n */\n limit(n: number) {\n let q = this._clone();\n q.limitNumber = n;\n return q;\n }\n\n /**\n * Skip the first n results in the query. Typically used together with limit() for pagination.\n *\n * A new query is returned - the original one is not modified.\n *\n * @param n number of results to skip.\n */\n skip(n: number) {\n let q = this._clone();\n q.skipNumber = n;\n return q;\n }\n\n /**\n * Pre-load belongs-to relationships when executing the query.\n *\n * This allows the implementation to batch the lookups, which is faster than loading each related\n * object individually.\n *\n * Nested belongs-to relationships are supported by using dots, e.g. `.include('owner.region')`.\n *\n * A new query is returned - the original one is not modified.\n *\n * @param relationships belongs-to names\n */\n include(...relationships: string[]) {\n let q = this._clone();\n for (let rel of relationships) {\n normalizeInclude(q._include, rel);\n }\n return q;\n }\n\n private _destroy_all() {\n return this._fetch().then((objects: DatabaseObject[]) => {\n let batch = new Batch(this.adapter);\n\n objects.forEach(function (object) {\n batch.destroy(object);\n });\n\n return batch._execute();\n });\n }\n\n private _count() {\n return this.adapter.count(this);\n }\n\n // Aliases\n\n /**\n * Execute the query and return the results.\n */\n toArray(): j.Promise<T[]> {\n return this._toArray();\n }\n\n /**\n * Apply additional filters, then return the first result.\n *\n * @param filters an object of {attribute: value}\n */\n first(filters?: { [key: string]: any }): j.Promise<T | null>;\n\n /**\n * Apply additional query filters, then return the first result.\n *\n * Same as .where(...).first().\n *\n * @param query the query string\n * @param args query arguments\n */\n first(query: string, ...args: any[]): j.Promise<T | null>;\n\n /**\n * Return the first result matched by this query.\n */\n first(): j.Promise<T | null>;\n\n first(): j.Promise<T | null> {\n return this._first.apply(this, arguments);\n }\n\n /**\n * Return the number of results matched by the query.\n *\n * If .limit() was used, it would be respected for the count.\n */\n count(): j.Promise<number> {\n return this._count.apply(this, arguments);\n }\n\n /**\n * Return executions stats for the query.\n *\n * The format of the results is not fixed, and may change across versions.\n * For debugging use only.\n */\n explain(): j.Promise<any> {\n return this._explain.apply(this, arguments);\n }\n\n /**\n * Destroy all objects matched by this query.\n *\n * skip() and limit() is respected for this.\n */\n destroyAll(): j.Promise<void> {\n return this._destroyAll.apply(this, arguments);\n }\n\n private _toArray(): Promise<T[]> {\n return this._fetch.apply(this, arguments);\n }\n\n private _first(): Promise<T | null> {\n return this._get.apply(this, arguments);\n }\n\n private _destroyAll(): Promise<void> {\n return this._destroy_all.apply(this, arguments);\n }\n\n /**\n * Order by one or more attributes.\n *\n * A new query is returned - the original one is not modified.\n *\n * @param fields an array of field names to order by. Prefix with - for descending, e.g. `-updated_at`.\n */\n orderBy(...fields: string[]): Query<T> {\n return this.order_by.apply(this, fields);\n }\n\n /**\n * For debugging purposes only.\n *\n * The format of the result is not defined.\n */\n toJSON() {\n return {\n type: this.type.name,\n expression: this.expression,\n ordering: this.ordering,\n limit: this.limitNumber,\n skip: this.skipNumber,\n include: this._include\n };\n }\n\n private _includeInternal(relationshipHash: RelationshipHash) {\n let q = this._clone();\n deepMerge(q._include, relationshipHash);\n return q;\n }\n}\n\nfunction compare(a: any, b: any) {\n if (typeof a == 'string' && typeof b == 'string') {\n // Case insensitive comparison\n a = a.toLowerCase();\n b = b.toLowerCase();\n }\n\n // We can't > or < with `null` or `undefined` - do this manually.\n // null, undefined < anything else\n // Note that `== null` also matches `undefined`.\n if (a == null && b == null) {\n return 0;\n } else if (a == null) {\n return -1;\n } else if (b == null) {\n return 1;\n } else if (a > b) {\n return 1;\n } else if (a < b) {\n return -1;\n } else {\n return 0;\n }\n}\n\nvar globalFreshness = 0;\n\nfunction nextFreshness() {\n globalFreshness += 1;\n return globalFreshness;\n}\n\n// Merge hashes of hashes (no non-hash values)\nfunction deepMerge(a: RelationshipHash, b: RelationshipHash): RelationshipHash {\n Object.keys(b).forEach(function (key) {\n if (!(key in a)) {\n a[key] = {};\n }\n deepMerge(a[key], b[key]);\n });\n return a;\n}\n\nfunction normalizeInclude(into: RelationshipHash, expression: string) {\n const dot = expression.indexOf('.');\n if (dot < 0) {\n if (!(expression in into)) {\n into[expression] = {};\n }\n return into;\n }\n\n const head = expression.substring(0, dot); // The first part of the expression\n const tail = expression.substring(dot + 1); // The rest of the expression\n\n if (!(head in into)) {\n into[head] = {};\n }\n normalizeInclude(into[head], tail);\n return into;\n}\n\n// Recursively preload a relationship structure\n// objects: Array of DatabaseObject\n// relationships example: {'room' => {'building' => {}}, 'otherrel' => {}}\nasync function preload<T extends DatabaseObject>(\n adapter: DatabaseAdapter,\n type: SchemaModelType,\n objects: T[],\n relationships: RelationshipHash\n): Promise<T[]> {\n let outerPromises: Promise<void>[] = [];\n\n for (let key in relationships) {\n const relatedType = type.getAttribute(key).type;\n if (!(relatedType instanceof SchemaModelType)) {\n throw new Error(`${type.name}.${key} is not a belongs-to relationship`);\n }\n const nestedRelationships = relationships[key];\n\n // Get ids of all the belongsTo objects in the relationship\n let ids = [];\n for (let object of objects) {\n const id = (object as Record<string, any>)[key + '_id'];\n ids.push(id);\n }\n\n let nestedObjects: DatabaseObject[] = [];\n\n const promise = adapter.getAll(relatedType.name, ids).then(async (data) => {\n for (let i = 0; i < objects.length; i++) {\n const object = objects[i];\n const relatedData = data[i];\n if (relatedData != null) {\n const related = DatabaseObject.build(adapter, relatedType, relatedData.id, relatedData);\n object._cache(key, related);\n nestedObjects.push(related);\n } else {\n // We need to record the fact that we couldn't find the object, so that other\n // code doesn't need to try again.\n object._cache(key, null);\n }\n }\n\n if (Object.keys(nestedRelationships).length > 0) {\n // Recursively load nested relationships\n await preload(adapter, relatedType, nestedObjects, nestedRelationships);\n }\n });\n\n outerPromises.push(promise);\n }\n\n await Promise.all(outerPromises);\n return objects;\n}\n"]}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getPreloadRelationships = getPreloadRelationships;
|
|
4
|
-
const parser_expressions_1 = require("@ja-platform/parser-expressions");
|
|
5
|
-
const parser_schema_1 = require("@ja-platform/parser-schema");
|
|
6
|
-
/**
|
|
7
|
-
* Collect relationships needed by query displays, including related models' displays.
|
|
8
|
-
*/
|
|
9
|
-
function getPreloadRelationships(type, columns) {
|
|
10
|
-
var _a;
|
|
11
|
-
const relationships = {};
|
|
12
|
-
const displays = columns == null ? [type.displayFormat] : Array.isArray(columns) ? columns : Object.values(columns);
|
|
13
|
-
for (const display of displays) {
|
|
14
|
-
for (const parameter of (_a = display === null || display === void 0 ? void 0 : display.parameters) !== null && _a !== void 0 ? _a : []) {
|
|
15
|
-
if (parameter instanceof parser_expressions_1.ShorthandExpression) {
|
|
16
|
-
extractRelationshipStructure(type, parameter, relationships, 0);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return relationships;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Follow a shorthand path and expand its terminal model's display within the depth limit.
|
|
24
|
-
*/
|
|
25
|
-
function extractRelationshipStructure(type, expression, into, depth) {
|
|
26
|
-
var _a;
|
|
27
|
-
var _b, _c;
|
|
28
|
-
var _d;
|
|
29
|
-
for (const field of expression.path) {
|
|
30
|
-
const child = type.getField(String(field));
|
|
31
|
-
if (!(child instanceof parser_schema_1.SchemaModelType))
|
|
32
|
-
return;
|
|
33
|
-
into = (_b = into[_d = String(field)]) !== null && _b !== void 0 ? _b : (into[_d] = {});
|
|
34
|
-
type = child;
|
|
35
|
-
depth++;
|
|
36
|
-
}
|
|
37
|
-
if (depth > 5)
|
|
38
|
-
return;
|
|
39
|
-
for (const parameter of (_c = (_a = type.displayFormat) === null || _a === void 0 ? void 0 : _a.parameters) !== null && _c !== void 0 ? _c : []) {
|
|
40
|
-
if (parameter instanceof parser_expressions_1.ShorthandExpression) {
|
|
41
|
-
extractRelationshipStructure(type, parameter, into, depth);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
//# sourceMappingURL=getPreloadRelationships.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getPreloadRelationships.js","sourceRoot":"","sources":["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AAM,SAAS,UAAI,MAAA,IAAI,CAAC,aAAa,0CAAE,UAAU,mCAAI,EAAE,EAAE,CAAC;QAC7D,IAAI,SAAS,YAAY,wCAAmB,EAAE,CAAC;YAC7C,4BAA4B,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;QAC7D,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { AbstractFormatExpression, ShorthandExpression } from '@ja-platform/parser-expressions';\nimport { SchemaModelType } from '@ja-platform/parser-schema';\n\nexport interface RelationshipHash {\n [key: string]: RelationshipHash;\n}\n\n/**\n * Collect relationships needed by query displays, including related models' displays.\n */\nexport function getPreloadRelationships(\n type: SchemaModelType,\n columns?: AbstractFormatExpression<any>[] | Record<string, AbstractFormatExpression<any>>\n): RelationshipHash {\n const relationships: RelationshipHash = {};\n const displays = columns == null ? [type.displayFormat] : Array.isArray(columns) ? columns : Object.values(columns);\n for (const display of displays) {\n for (const parameter of display?.parameters ?? []) {\n if (parameter instanceof ShorthandExpression) {\n extractRelationshipStructure(type, parameter, relationships, 0);\n }\n }\n }\n return relationships;\n}\n\n/**\n * Follow a shorthand path and expand its terminal model's display within the depth limit.\n */\nfunction extractRelationshipStructure(\n type: SchemaModelType,\n expression: ShorthandExpression,\n into: RelationshipHash,\n depth: number\n): void {\n for (const field of expression.path) {\n const child = type.getField(String(field));\n if (!(child instanceof SchemaModelType)) return;\n into = into[String(field)] ??= {};\n type = child;\n depth++;\n }\n if (depth > 5) return;\n for (const parameter of type.displayFormat?.parameters ?? []) {\n if (parameter instanceof ShorthandExpression) {\n extractRelationshipStructure(type, parameter, into, depth);\n }\n }\n}\n"]}
|