defuss-db 1.1.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -90
- package/dist/client.cjs +241 -222
- package/dist/client.d.ts +16 -77
- package/dist/client.mjs +231 -223
- package/dist/index.cjs +866 -30
- package/dist/index.d.cts +243 -56
- package/dist/index.d.mts +243 -56
- package/dist/index.d.ts +243 -56
- package/dist/index.mjs +856 -31
- package/dist/runtime-C-5MF9ur.mjs +242 -0
- package/dist/runtime-CJK5Yy0J.d.ts +17 -0
- package/dist/runtime-E8GdpV6o.cjs +251 -0
- package/dist/server.cjs +1064 -642
- package/dist/server.d.ts +77 -210
- package/dist/server.mjs +1055 -645
- package/package.json +12 -5
package/dist/index.mjs
CHANGED
|
@@ -1,70 +1,895 @@
|
|
|
1
|
+
class AggregationBuilder {
|
|
2
|
+
baseSource;
|
|
3
|
+
joins;
|
|
4
|
+
operations;
|
|
5
|
+
constructor(baseSource, joins = [], operations = []) {
|
|
6
|
+
this.baseSource = baseSource;
|
|
7
|
+
this.joins = joins;
|
|
8
|
+
this.operations = operations;
|
|
9
|
+
}
|
|
10
|
+
join(source, spec) {
|
|
11
|
+
assertUniqueAlias(this.getSourceAliases(), source.as);
|
|
12
|
+
return new AggregationBuilder(
|
|
13
|
+
this.baseSource,
|
|
14
|
+
[
|
|
15
|
+
...this.joins,
|
|
16
|
+
{
|
|
17
|
+
source,
|
|
18
|
+
spec: {
|
|
19
|
+
type: spec.type ?? "left",
|
|
20
|
+
left: spec.left,
|
|
21
|
+
right: spec.right
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
this.operations
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
project(mapping) {
|
|
29
|
+
return new AggregationBuilder(
|
|
30
|
+
this.baseSource,
|
|
31
|
+
this.joins,
|
|
32
|
+
[
|
|
33
|
+
...this.operations,
|
|
34
|
+
{
|
|
35
|
+
kind: "project",
|
|
36
|
+
mapping
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
alias(mapping) {
|
|
42
|
+
return new AggregationBuilder(
|
|
43
|
+
this.baseSource,
|
|
44
|
+
this.joins,
|
|
45
|
+
[
|
|
46
|
+
...this.operations,
|
|
47
|
+
{
|
|
48
|
+
kind: "alias",
|
|
49
|
+
mapping
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
compute(field, resolver) {
|
|
55
|
+
return new AggregationBuilder(
|
|
56
|
+
this.baseSource,
|
|
57
|
+
this.joins,
|
|
58
|
+
[
|
|
59
|
+
...this.operations,
|
|
60
|
+
{
|
|
61
|
+
kind: "compute",
|
|
62
|
+
field,
|
|
63
|
+
resolver
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
computeMany(mapping) {
|
|
69
|
+
return new AggregationBuilder(
|
|
70
|
+
this.baseSource,
|
|
71
|
+
this.joins,
|
|
72
|
+
[
|
|
73
|
+
...this.operations,
|
|
74
|
+
{
|
|
75
|
+
kind: "computeMany",
|
|
76
|
+
mapping
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
mapRows(mapper) {
|
|
82
|
+
return new AggregationBuilder(this.baseSource, this.joins, [
|
|
83
|
+
...this.operations,
|
|
84
|
+
{
|
|
85
|
+
kind: "mapRows",
|
|
86
|
+
mapper
|
|
87
|
+
}
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
removeFields(paths) {
|
|
91
|
+
return new AggregationBuilder(this.baseSource, this.joins, [
|
|
92
|
+
...this.operations,
|
|
93
|
+
{
|
|
94
|
+
kind: "removeFields",
|
|
95
|
+
paths
|
|
96
|
+
}
|
|
97
|
+
]);
|
|
98
|
+
}
|
|
99
|
+
mergeConsecutive(merger) {
|
|
100
|
+
return new AggregationBuilder(this.baseSource, this.joins, [
|
|
101
|
+
...this.operations,
|
|
102
|
+
{
|
|
103
|
+
kind: "mergeConsecutive",
|
|
104
|
+
merger
|
|
105
|
+
}
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
distinctBy(selector, options = {}) {
|
|
109
|
+
return new AggregationBuilder(this.baseSource, this.joins, [
|
|
110
|
+
...this.operations,
|
|
111
|
+
{
|
|
112
|
+
kind: "distinctBy",
|
|
113
|
+
selector,
|
|
114
|
+
options
|
|
115
|
+
}
|
|
116
|
+
]);
|
|
117
|
+
}
|
|
118
|
+
groupBy(keys, reducers) {
|
|
119
|
+
return new AggregationBuilder(this.baseSource, this.joins, [
|
|
120
|
+
...this.operations,
|
|
121
|
+
{
|
|
122
|
+
kind: "groupBy",
|
|
123
|
+
keys,
|
|
124
|
+
reducers
|
|
125
|
+
}
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
128
|
+
sortBy(input) {
|
|
129
|
+
return new AggregationBuilder(this.baseSource, this.joins, [
|
|
130
|
+
...this.operations,
|
|
131
|
+
isSortComparator(input) ? {
|
|
132
|
+
kind: "sortBy",
|
|
133
|
+
comparator: input
|
|
134
|
+
} : {
|
|
135
|
+
kind: "sortBy",
|
|
136
|
+
specs: normalizeSortSpecs(
|
|
137
|
+
input
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
]);
|
|
141
|
+
}
|
|
142
|
+
async execute() {
|
|
143
|
+
const sources = await this.loadSourceRows();
|
|
144
|
+
let rows = sources[this.baseSource.as].map((row) => ({
|
|
145
|
+
[this.baseSource.as]: row
|
|
146
|
+
}));
|
|
147
|
+
for (const join of this.joins) {
|
|
148
|
+
rows = applyJoin(rows, sources[join.source.as], join);
|
|
149
|
+
}
|
|
150
|
+
for (const operation of this.operations) {
|
|
151
|
+
rows = await this.applyOperation(rows, sources, operation);
|
|
152
|
+
}
|
|
153
|
+
return rows;
|
|
154
|
+
}
|
|
155
|
+
getSourceAliases() {
|
|
156
|
+
return [this.baseSource.as, ...this.joins.map((join) => join.source.as)];
|
|
157
|
+
}
|
|
158
|
+
async loadSourceRows() {
|
|
159
|
+
const loadedEntries = await Promise.all(
|
|
160
|
+
[this.baseSource, ...this.joins.map((join) => join.source)].map(async (source) => [
|
|
161
|
+
source.as,
|
|
162
|
+
await loadSourceRows(source)
|
|
163
|
+
])
|
|
164
|
+
);
|
|
165
|
+
return Object.fromEntries(loadedEntries);
|
|
166
|
+
}
|
|
167
|
+
async applyOperation(rows, sources, operation) {
|
|
168
|
+
switch (operation.kind) {
|
|
169
|
+
case "project":
|
|
170
|
+
return mapRowsSequentially(
|
|
171
|
+
rows,
|
|
172
|
+
sources,
|
|
173
|
+
this,
|
|
174
|
+
async (row, context) => resolveProjection(operation.mapping, row, context)
|
|
175
|
+
);
|
|
176
|
+
case "alias":
|
|
177
|
+
return mapRowsSequentially(rows, sources, this, async (row, context) => ({
|
|
178
|
+
...row,
|
|
179
|
+
...await resolveProjection(operation.mapping, row, context)
|
|
180
|
+
}));
|
|
181
|
+
case "compute":
|
|
182
|
+
return mapRowsSequentially(rows, sources, this, async (row, context) => ({
|
|
183
|
+
...row,
|
|
184
|
+
[operation.field]: await resolveValue(operation.resolver, row, context)
|
|
185
|
+
}));
|
|
186
|
+
case "computeMany":
|
|
187
|
+
return mapRowsSequentially(rows, sources, this, async (row, context) => ({
|
|
188
|
+
...row,
|
|
189
|
+
...await resolveProjection(operation.mapping, row, context)
|
|
190
|
+
}));
|
|
191
|
+
case "mapRows":
|
|
192
|
+
return mapRowsSequentially(rows, sources, this, operation.mapper);
|
|
193
|
+
case "removeFields":
|
|
194
|
+
return mapRowsSequentially(rows, sources, this, async (row, context) => {
|
|
195
|
+
const paths = await resolveRemovePaths(operation.paths, row, context);
|
|
196
|
+
return removePathsFromRow(row, paths);
|
|
197
|
+
});
|
|
198
|
+
case "mergeConsecutive":
|
|
199
|
+
return mergeRowsSequentially(rows, sources, this, operation.merger);
|
|
200
|
+
case "distinctBy":
|
|
201
|
+
return distinctRows(rows, sources, this, operation.selector, operation.options);
|
|
202
|
+
case "groupBy":
|
|
203
|
+
return groupRows(rows, sources, this, operation.keys, operation.reducers);
|
|
204
|
+
case "sortBy":
|
|
205
|
+
return sortRows(rows, sources, this, operation);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function createAggregation(input, options) {
|
|
210
|
+
const baseSource = normalizeBaseSource(input, options);
|
|
211
|
+
assertUniqueAlias([], baseSource.as);
|
|
212
|
+
return new AggregationBuilder(
|
|
213
|
+
baseSource
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
function countRows() {
|
|
217
|
+
return (rows) => rows.length;
|
|
218
|
+
}
|
|
219
|
+
function sumBy(resolver) {
|
|
220
|
+
return async (rows, context) => {
|
|
221
|
+
let total = 0;
|
|
222
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
223
|
+
const value = await resolveValue(
|
|
224
|
+
resolver,
|
|
225
|
+
rows[index],
|
|
226
|
+
createRowContext(index, rows, context.sources, context.aggregation)
|
|
227
|
+
);
|
|
228
|
+
if (typeof value === "number") {
|
|
229
|
+
total += value;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return total;
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function avgBy(resolver) {
|
|
236
|
+
return async (rows, context) => {
|
|
237
|
+
let total = 0;
|
|
238
|
+
let count = 0;
|
|
239
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
240
|
+
const value = await resolveValue(
|
|
241
|
+
resolver,
|
|
242
|
+
rows[index],
|
|
243
|
+
createRowContext(index, rows, context.sources, context.aggregation)
|
|
244
|
+
);
|
|
245
|
+
if (typeof value === "number") {
|
|
246
|
+
total += value;
|
|
247
|
+
count += 1;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return count === 0 ? 0 : total / count;
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
function minBy(resolver) {
|
|
254
|
+
return async (rows, context) => {
|
|
255
|
+
let minimum = void 0;
|
|
256
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
257
|
+
const value = await resolveValue(
|
|
258
|
+
resolver,
|
|
259
|
+
rows[index],
|
|
260
|
+
createRowContext(index, rows, context.sources, context.aggregation)
|
|
261
|
+
);
|
|
262
|
+
if (minimum === void 0 || compareSortValues(value, minimum, "asc") < 0) {
|
|
263
|
+
minimum = value;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return minimum;
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
function maxBy(resolver) {
|
|
270
|
+
return async (rows, context) => {
|
|
271
|
+
let maximum = void 0;
|
|
272
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
273
|
+
const value = await resolveValue(
|
|
274
|
+
resolver,
|
|
275
|
+
rows[index],
|
|
276
|
+
createRowContext(index, rows, context.sources, context.aggregation)
|
|
277
|
+
);
|
|
278
|
+
if (maximum === void 0 || compareSortValues(value, maximum, "desc") < 0) {
|
|
279
|
+
maximum = value;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return maximum;
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function firstBy(resolver) {
|
|
286
|
+
return async (rows, context) => {
|
|
287
|
+
const row = rows[0];
|
|
288
|
+
if (!row) {
|
|
289
|
+
return void 0;
|
|
290
|
+
}
|
|
291
|
+
if (!resolver) {
|
|
292
|
+
return row;
|
|
293
|
+
}
|
|
294
|
+
return resolveValue(
|
|
295
|
+
resolver,
|
|
296
|
+
row,
|
|
297
|
+
createRowContext(0, rows, context.sources, context.aggregation)
|
|
298
|
+
);
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function lastBy(resolver) {
|
|
302
|
+
return async (rows, context) => {
|
|
303
|
+
const index = rows.length - 1;
|
|
304
|
+
const row = rows[index];
|
|
305
|
+
if (!row) {
|
|
306
|
+
return void 0;
|
|
307
|
+
}
|
|
308
|
+
if (!resolver) {
|
|
309
|
+
return row;
|
|
310
|
+
}
|
|
311
|
+
return resolveValue(
|
|
312
|
+
resolver,
|
|
313
|
+
row,
|
|
314
|
+
createRowContext(index, rows, context.sources, context.aggregation)
|
|
315
|
+
);
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
const aggregationReducers = {
|
|
319
|
+
count: countRows,
|
|
320
|
+
sum: sumBy,
|
|
321
|
+
avg: avgBy,
|
|
322
|
+
min: minBy,
|
|
323
|
+
max: maxBy,
|
|
324
|
+
first: firstBy,
|
|
325
|
+
last: lastBy
|
|
326
|
+
};
|
|
327
|
+
function normalizeBaseSource(input, options) {
|
|
328
|
+
if (isAggregationSource(input)) {
|
|
329
|
+
return input;
|
|
330
|
+
}
|
|
331
|
+
if (isAggregationTable(input)) {
|
|
332
|
+
const table = input;
|
|
333
|
+
return {
|
|
334
|
+
table,
|
|
335
|
+
as: options?.as ?? "base",
|
|
336
|
+
where: options?.where
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
return {
|
|
340
|
+
rows: input,
|
|
341
|
+
as: options?.as ?? "base"
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
function isAggregationTable(value) {
|
|
345
|
+
return typeof value === "object" && value !== null && "find" in value && "definition" in value && typeof value.find === "function";
|
|
346
|
+
}
|
|
347
|
+
function isAggregationSource(value) {
|
|
348
|
+
return typeof value === "object" && value !== null && "as" in value && ("table" in value && isAggregationTable(value.table) || "rows" in value);
|
|
349
|
+
}
|
|
350
|
+
async function loadSourceRows(source) {
|
|
351
|
+
if ("table" in source) {
|
|
352
|
+
return source.table.find(source.where ?? {});
|
|
353
|
+
}
|
|
354
|
+
const loadedRows = typeof source.rows === "function" ? await source.rows() : await source.rows;
|
|
355
|
+
return [...loadedRows];
|
|
356
|
+
}
|
|
357
|
+
async function mapRowsSequentially(rows, sources, aggregation, mapper) {
|
|
358
|
+
const nextRows = [];
|
|
359
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
360
|
+
nextRows.push(
|
|
361
|
+
await mapper(rows[index], createRowContext(index, rows, sources, aggregation))
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
return nextRows;
|
|
365
|
+
}
|
|
366
|
+
async function resolveProjection(mapping, row, context) {
|
|
367
|
+
const projected = {};
|
|
368
|
+
for (const [field, resolver] of Object.entries(mapping)) {
|
|
369
|
+
projected[field] = await resolveValue(resolver, row, context);
|
|
370
|
+
}
|
|
371
|
+
return projected;
|
|
372
|
+
}
|
|
373
|
+
async function resolveValue(resolver, row, context) {
|
|
374
|
+
if (typeof resolver === "string") {
|
|
375
|
+
return getValueAtPath(row, resolver);
|
|
376
|
+
}
|
|
377
|
+
return resolver(row, context);
|
|
378
|
+
}
|
|
379
|
+
async function resolveRemovePaths(paths, row, context) {
|
|
380
|
+
if (Array.isArray(paths)) {
|
|
381
|
+
return paths;
|
|
382
|
+
}
|
|
383
|
+
return paths(row, context);
|
|
384
|
+
}
|
|
385
|
+
async function mergeRowsSequentially(rows, sources, aggregation, merger) {
|
|
386
|
+
const nextRows = [];
|
|
387
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
388
|
+
const currentRow = rows[index];
|
|
389
|
+
if (nextRows.length === 0) {
|
|
390
|
+
nextRows.push(currentRow);
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
const merged = await merger(nextRows[nextRows.length - 1], currentRow, {
|
|
394
|
+
index,
|
|
395
|
+
rows,
|
|
396
|
+
sources,
|
|
397
|
+
aggregation
|
|
398
|
+
});
|
|
399
|
+
if (merged === null || merged === void 0) {
|
|
400
|
+
nextRows.push(currentRow);
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
nextRows[nextRows.length - 1] = merged;
|
|
404
|
+
}
|
|
405
|
+
return nextRows;
|
|
406
|
+
}
|
|
407
|
+
async function distinctRows(rows, sources, aggregation, selector, options) {
|
|
408
|
+
const keep = options.keep ?? "first";
|
|
409
|
+
if (keep === "last") {
|
|
410
|
+
const lastSeen = /* @__PURE__ */ new Map();
|
|
411
|
+
const keys = [];
|
|
412
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
413
|
+
const key = await resolveDistinctKey(
|
|
414
|
+
selector,
|
|
415
|
+
rows[index],
|
|
416
|
+
createRowContext(index, rows, sources, aggregation)
|
|
417
|
+
);
|
|
418
|
+
keys.push(key);
|
|
419
|
+
lastSeen.set(key, index);
|
|
420
|
+
}
|
|
421
|
+
return rows.filter((_, index) => lastSeen.get(keys[index]) === index);
|
|
422
|
+
}
|
|
423
|
+
const nextRows = [];
|
|
424
|
+
const seen = /* @__PURE__ */ new Set();
|
|
425
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
426
|
+
const key = await resolveDistinctKey(
|
|
427
|
+
selector,
|
|
428
|
+
rows[index],
|
|
429
|
+
createRowContext(index, rows, sources, aggregation)
|
|
430
|
+
);
|
|
431
|
+
if (seen.has(key)) {
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
seen.add(key);
|
|
435
|
+
nextRows.push(rows[index]);
|
|
436
|
+
}
|
|
437
|
+
return nextRows;
|
|
438
|
+
}
|
|
439
|
+
async function groupRows(rows, sources, aggregation, keys, reducers) {
|
|
440
|
+
const groups = /* @__PURE__ */ new Map();
|
|
441
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
442
|
+
const row = rows[index];
|
|
443
|
+
const keyRow = await resolveGroupKeyRow(
|
|
444
|
+
keys,
|
|
445
|
+
row,
|
|
446
|
+
createRowContext(index, rows, sources, aggregation)
|
|
447
|
+
);
|
|
448
|
+
const serializedKey = await serializeKeyParts([keyRow]);
|
|
449
|
+
const existing = groups.get(serializedKey);
|
|
450
|
+
if (existing) {
|
|
451
|
+
existing.rows.push(row);
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
groups.set(serializedKey, {
|
|
455
|
+
key: keyRow,
|
|
456
|
+
rows: [row]
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
const nextRows = [];
|
|
460
|
+
for (const group of groups.values()) {
|
|
461
|
+
const groupedRow = { ...group.key };
|
|
462
|
+
const groupContext = {
|
|
463
|
+
key: group.key,
|
|
464
|
+
rows: group.rows,
|
|
465
|
+
sources,
|
|
466
|
+
aggregation
|
|
467
|
+
};
|
|
468
|
+
for (const [field, reducer] of Object.entries(reducers)) {
|
|
469
|
+
groupedRow[field] = await reducer(group.rows, groupContext);
|
|
470
|
+
}
|
|
471
|
+
nextRows.push(groupedRow);
|
|
472
|
+
}
|
|
473
|
+
return nextRows;
|
|
474
|
+
}
|
|
475
|
+
async function sortRows(rows, sources, aggregation, operation) {
|
|
476
|
+
if (operation.comparator) {
|
|
477
|
+
return stableSortAsync(
|
|
478
|
+
rows,
|
|
479
|
+
async (left, right) => operation.comparator(left, right, {
|
|
480
|
+
rows,
|
|
481
|
+
sources,
|
|
482
|
+
aggregation
|
|
483
|
+
})
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
const specs = operation.specs ?? [];
|
|
487
|
+
if (specs.length === 0) {
|
|
488
|
+
return [...rows];
|
|
489
|
+
}
|
|
490
|
+
const decorated = await Promise.all(
|
|
491
|
+
rows.map(async (row, index) => ({
|
|
492
|
+
row,
|
|
493
|
+
index,
|
|
494
|
+
keys: await Promise.all(
|
|
495
|
+
specs.map(
|
|
496
|
+
(spec) => resolveSortValue(spec, row, createRowContext(index, rows, sources, aggregation))
|
|
497
|
+
)
|
|
498
|
+
)
|
|
499
|
+
}))
|
|
500
|
+
);
|
|
501
|
+
decorated.sort((left, right) => {
|
|
502
|
+
for (let index = 0; index < specs.length; index += 1) {
|
|
503
|
+
const compared = compareSortValues(
|
|
504
|
+
left.keys[index],
|
|
505
|
+
right.keys[index],
|
|
506
|
+
specs[index].direction ?? "asc"
|
|
507
|
+
);
|
|
508
|
+
if (compared !== 0) {
|
|
509
|
+
return compared;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return left.index - right.index;
|
|
513
|
+
});
|
|
514
|
+
return decorated.map((entry) => entry.row);
|
|
515
|
+
}
|
|
516
|
+
function applyJoin(currentRows, joinedRows, join) {
|
|
517
|
+
if (join.spec.type === "right") {
|
|
518
|
+
return applyRightJoin(currentRows, joinedRows, join);
|
|
519
|
+
}
|
|
520
|
+
const nextRows = [];
|
|
521
|
+
for (const currentRow of currentRows) {
|
|
522
|
+
const matches = joinedRows.filter(
|
|
523
|
+
(joinedRow) => valuesEqual(
|
|
524
|
+
getValueAtPath(currentRow, join.spec.left),
|
|
525
|
+
getValueAtPath(joinedRow, join.spec.right)
|
|
526
|
+
)
|
|
527
|
+
);
|
|
528
|
+
if (matches.length === 0) {
|
|
529
|
+
if (join.spec.type === "left") {
|
|
530
|
+
nextRows.push({
|
|
531
|
+
...currentRow,
|
|
532
|
+
[join.source.as]: null
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
for (const match of matches) {
|
|
538
|
+
nextRows.push({
|
|
539
|
+
...currentRow,
|
|
540
|
+
[join.source.as]: match
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
return nextRows;
|
|
545
|
+
}
|
|
546
|
+
function applyRightJoin(currentRows, joinedRows, join) {
|
|
547
|
+
const nextRows = [];
|
|
548
|
+
const currentRowTemplate = currentRows[0];
|
|
549
|
+
for (const joinedRow of joinedRows) {
|
|
550
|
+
const matches = currentRows.filter(
|
|
551
|
+
(currentRow) => valuesEqual(
|
|
552
|
+
getValueAtPath(currentRow, join.spec.left),
|
|
553
|
+
getValueAtPath(joinedRow, join.spec.right)
|
|
554
|
+
)
|
|
555
|
+
);
|
|
556
|
+
if (matches.length === 0) {
|
|
557
|
+
nextRows.push({
|
|
558
|
+
...createNullRow(currentRowTemplate),
|
|
559
|
+
[join.source.as]: joinedRow
|
|
560
|
+
});
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
for (const match of matches) {
|
|
564
|
+
nextRows.push({
|
|
565
|
+
...match,
|
|
566
|
+
[join.source.as]: joinedRow
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return nextRows;
|
|
571
|
+
}
|
|
572
|
+
function createNullRow(row) {
|
|
573
|
+
if (!row) {
|
|
574
|
+
return {};
|
|
575
|
+
}
|
|
576
|
+
const nextRow = {};
|
|
577
|
+
for (const key of Object.keys(row)) {
|
|
578
|
+
nextRow[key] = null;
|
|
579
|
+
}
|
|
580
|
+
return nextRow;
|
|
581
|
+
}
|
|
582
|
+
function removePathsFromRow(row, paths) {
|
|
583
|
+
let nextRow = row;
|
|
584
|
+
for (const path of paths) {
|
|
585
|
+
nextRow = removePath(nextRow, path);
|
|
586
|
+
}
|
|
587
|
+
return nextRow;
|
|
588
|
+
}
|
|
589
|
+
function removePath(row, path) {
|
|
590
|
+
const segments = path.split(".").filter(Boolean);
|
|
591
|
+
if (segments.length === 0) {
|
|
592
|
+
return row;
|
|
593
|
+
}
|
|
594
|
+
const nextRow = cloneContainer(row);
|
|
595
|
+
let sourceCursor = row;
|
|
596
|
+
let targetCursor = nextRow;
|
|
597
|
+
for (let index = 0; index < segments.length - 1; index += 1) {
|
|
598
|
+
if (sourceCursor === null || sourceCursor === void 0 || typeof sourceCursor !== "object") {
|
|
599
|
+
return nextRow;
|
|
600
|
+
}
|
|
601
|
+
const segment = segments[index];
|
|
602
|
+
const sourceValue = sourceCursor[segment];
|
|
603
|
+
if (sourceValue === null || sourceValue === void 0 || typeof sourceValue !== "object") {
|
|
604
|
+
return nextRow;
|
|
605
|
+
}
|
|
606
|
+
targetCursor[segment] = cloneContainer(sourceValue);
|
|
607
|
+
targetCursor = targetCursor[segment];
|
|
608
|
+
sourceCursor = sourceValue;
|
|
609
|
+
}
|
|
610
|
+
delete targetCursor[segments[segments.length - 1]];
|
|
611
|
+
return nextRow;
|
|
612
|
+
}
|
|
613
|
+
function getValueAtPath(target, path) {
|
|
614
|
+
if (!path) {
|
|
615
|
+
return void 0;
|
|
616
|
+
}
|
|
617
|
+
let current = target;
|
|
618
|
+
for (const segment of path.split(".").filter(Boolean)) {
|
|
619
|
+
if (current === null) {
|
|
620
|
+
return null;
|
|
621
|
+
}
|
|
622
|
+
if (current === void 0 || typeof current !== "object") {
|
|
623
|
+
return void 0;
|
|
624
|
+
}
|
|
625
|
+
current = current[segment];
|
|
626
|
+
}
|
|
627
|
+
return current;
|
|
628
|
+
}
|
|
629
|
+
async function resolveDistinctKey(selector, row, context) {
|
|
630
|
+
if (typeof selector === "string") {
|
|
631
|
+
return serializeKeyParts([getValueAtPath(row, selector)]);
|
|
632
|
+
}
|
|
633
|
+
if (Array.isArray(selector)) {
|
|
634
|
+
return serializeKeyParts(selector.map((path) => getValueAtPath(row, path)));
|
|
635
|
+
}
|
|
636
|
+
const resolved = await selector(row, context);
|
|
637
|
+
return serializeKeyParts(Array.isArray(resolved) ? resolved : [resolved]);
|
|
638
|
+
}
|
|
639
|
+
async function resolveGroupKeyRow(keys, row, context) {
|
|
640
|
+
if (typeof keys === "string") {
|
|
641
|
+
return {
|
|
642
|
+
[keys]: getValueAtPath(row, keys)
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
if (Array.isArray(keys)) {
|
|
646
|
+
const keyRow = {};
|
|
647
|
+
for (const key of keys) {
|
|
648
|
+
keyRow[key] = getValueAtPath(row, key);
|
|
649
|
+
}
|
|
650
|
+
return keyRow;
|
|
651
|
+
}
|
|
652
|
+
return resolveProjection(keys, row, context);
|
|
653
|
+
}
|
|
654
|
+
async function resolveSortValue(spec, row, context) {
|
|
655
|
+
if (isFieldSortSpec(spec)) {
|
|
656
|
+
return getValueAtPath(row, spec.field);
|
|
657
|
+
}
|
|
658
|
+
return resolveValue(spec.by, row, context);
|
|
659
|
+
}
|
|
660
|
+
function isFieldSortSpec(spec) {
|
|
661
|
+
return "field" in spec;
|
|
662
|
+
}
|
|
663
|
+
function isSortComparator(value) {
|
|
664
|
+
return typeof value === "function";
|
|
665
|
+
}
|
|
666
|
+
function normalizeSortSpecs(input) {
|
|
667
|
+
if (Array.isArray(input)) {
|
|
668
|
+
return [...input];
|
|
669
|
+
}
|
|
670
|
+
return [input];
|
|
671
|
+
}
|
|
672
|
+
async function stableSortAsync(rows, comparator) {
|
|
673
|
+
if (rows.length <= 1) {
|
|
674
|
+
return [...rows];
|
|
675
|
+
}
|
|
676
|
+
const midpoint = Math.floor(rows.length / 2);
|
|
677
|
+
const leftRows = await stableSortAsync(rows.slice(0, midpoint), comparator);
|
|
678
|
+
const rightRows = await stableSortAsync(rows.slice(midpoint), comparator);
|
|
679
|
+
return mergeSortedRows(leftRows, rightRows, comparator);
|
|
680
|
+
}
|
|
681
|
+
async function mergeSortedRows(leftRows, rightRows, comparator) {
|
|
682
|
+
const merged = [];
|
|
683
|
+
let leftIndex = 0;
|
|
684
|
+
let rightIndex = 0;
|
|
685
|
+
while (leftIndex < leftRows.length && rightIndex < rightRows.length) {
|
|
686
|
+
if (await comparator(leftRows[leftIndex], rightRows[rightIndex]) <= 0) {
|
|
687
|
+
merged.push(leftRows[leftIndex]);
|
|
688
|
+
leftIndex += 1;
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
merged.push(rightRows[rightIndex]);
|
|
692
|
+
rightIndex += 1;
|
|
693
|
+
}
|
|
694
|
+
return [...merged, ...leftRows.slice(leftIndex), ...rightRows.slice(rightIndex)];
|
|
695
|
+
}
|
|
696
|
+
function compareSortValues(left, right, direction) {
|
|
697
|
+
const factor = direction === "desc" ? -1 : 1;
|
|
698
|
+
if (left === right) {
|
|
699
|
+
return 0;
|
|
700
|
+
}
|
|
701
|
+
if (left === null || left === void 0) {
|
|
702
|
+
return 1 * factor;
|
|
703
|
+
}
|
|
704
|
+
if (right === null || right === void 0) {
|
|
705
|
+
return -1 * factor;
|
|
706
|
+
}
|
|
707
|
+
if (left instanceof Date && right instanceof Date) {
|
|
708
|
+
return comparePrimitiveValues(left.getTime(), right.getTime()) * factor;
|
|
709
|
+
}
|
|
710
|
+
if (typeof left === "number" && typeof right === "number") {
|
|
711
|
+
return comparePrimitiveValues(left, right) * factor;
|
|
712
|
+
}
|
|
713
|
+
if (typeof left === "bigint" && typeof right === "bigint") {
|
|
714
|
+
return comparePrimitiveValues(left, right) * factor;
|
|
715
|
+
}
|
|
716
|
+
if (typeof left === "boolean" && typeof right === "boolean") {
|
|
717
|
+
return comparePrimitiveValues(Number(left), Number(right)) * factor;
|
|
718
|
+
}
|
|
719
|
+
return String(left).localeCompare(String(right)) * factor;
|
|
720
|
+
}
|
|
721
|
+
function comparePrimitiveValues(left, right) {
|
|
722
|
+
if (left < right) {
|
|
723
|
+
return -1;
|
|
724
|
+
}
|
|
725
|
+
if (left > right) {
|
|
726
|
+
return 1;
|
|
727
|
+
}
|
|
728
|
+
return 0;
|
|
729
|
+
}
|
|
730
|
+
async function serializeKeyParts(parts) {
|
|
731
|
+
const normalized = await Promise.all(parts.map((part) => normalizeKeyPart(part)));
|
|
732
|
+
return JSON.stringify(normalized);
|
|
733
|
+
}
|
|
734
|
+
async function normalizeKeyPart(value) {
|
|
735
|
+
if (value instanceof Date) {
|
|
736
|
+
return {
|
|
737
|
+
__defussType: "date",
|
|
738
|
+
value: value.toISOString()
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
if (typeof value === "bigint") {
|
|
742
|
+
return {
|
|
743
|
+
__defussType: "bigint",
|
|
744
|
+
value: String(value)
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
if (value instanceof ArrayBuffer) {
|
|
748
|
+
return {
|
|
749
|
+
__defussType: "arraybuffer",
|
|
750
|
+
value: Array.from(new Uint8Array(value))
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
if (isBlobLike(value)) {
|
|
754
|
+
return {
|
|
755
|
+
__defussType: "blob",
|
|
756
|
+
type: value.type,
|
|
757
|
+
value: Array.from(new Uint8Array(await value.arrayBuffer()))
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
if (Array.isArray(value)) {
|
|
761
|
+
return Promise.all(value.map((item) => normalizeKeyPart(item)));
|
|
762
|
+
}
|
|
763
|
+
if (typeof value === "object" && value !== null) {
|
|
764
|
+
const entries = await Promise.all(
|
|
765
|
+
Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(async ([key, nestedValue]) => [key, await normalizeKeyPart(nestedValue)])
|
|
766
|
+
);
|
|
767
|
+
return Object.fromEntries(entries);
|
|
768
|
+
}
|
|
769
|
+
return value;
|
|
770
|
+
}
|
|
771
|
+
function valuesEqual(left, right) {
|
|
772
|
+
if (left instanceof Date && right instanceof Date) {
|
|
773
|
+
return left.getTime() === right.getTime();
|
|
774
|
+
}
|
|
775
|
+
if (left instanceof ArrayBuffer && right instanceof ArrayBuffer) {
|
|
776
|
+
if (left.byteLength !== right.byteLength) {
|
|
777
|
+
return false;
|
|
778
|
+
}
|
|
779
|
+
const leftView = new Uint8Array(left);
|
|
780
|
+
const rightView = new Uint8Array(right);
|
|
781
|
+
for (let index = 0; index < leftView.length; index += 1) {
|
|
782
|
+
if (leftView[index] !== rightView[index]) {
|
|
783
|
+
return false;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
return true;
|
|
787
|
+
}
|
|
788
|
+
return left === right;
|
|
789
|
+
}
|
|
790
|
+
function createRowContext(index, rows, sources, aggregation) {
|
|
791
|
+
return {
|
|
792
|
+
index,
|
|
793
|
+
rows,
|
|
794
|
+
sources,
|
|
795
|
+
aggregation
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
function cloneContainer(value) {
|
|
799
|
+
if (Array.isArray(value)) {
|
|
800
|
+
return [...value];
|
|
801
|
+
}
|
|
802
|
+
return { ...value };
|
|
803
|
+
}
|
|
804
|
+
function isBlobLike(value) {
|
|
805
|
+
return typeof value === "object" && value !== null && typeof value.arrayBuffer === "function" && typeof value.slice === "function" && typeof value.type === "string";
|
|
806
|
+
}
|
|
807
|
+
function assertUniqueAlias(existingAliases, alias) {
|
|
808
|
+
if (!alias.trim()) {
|
|
809
|
+
throw new Error("defuss-db aggregation: source aliases must be non-empty.");
|
|
810
|
+
}
|
|
811
|
+
if (existingAliases.includes(alias)) {
|
|
812
|
+
throw new Error(`defuss-db aggregation: source alias '${alias}' is already in use.`);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
1
816
|
class DefussTable {
|
|
2
817
|
provider;
|
|
3
|
-
|
|
818
|
+
definition;
|
|
4
819
|
/**
|
|
5
|
-
* Initializes the DefussTable with the specified table
|
|
820
|
+
* Initializes the DefussTable with the specified table definition and provider.
|
|
6
821
|
* @param provider - The DefussProvider instance.
|
|
7
|
-
* @param
|
|
822
|
+
* @param definition - The table definition.
|
|
8
823
|
*/
|
|
9
|
-
constructor(provider,
|
|
824
|
+
constructor(provider, definition) {
|
|
10
825
|
this.provider = provider;
|
|
11
|
-
this.
|
|
826
|
+
this.definition = definition;
|
|
12
827
|
}
|
|
13
828
|
/**
|
|
14
829
|
* Creates the table if it doesn't exist.
|
|
15
830
|
*/
|
|
16
831
|
async init() {
|
|
17
|
-
await this.provider.
|
|
832
|
+
await this.provider.ensureTable(this.definition);
|
|
18
833
|
}
|
|
19
834
|
/**
|
|
20
|
-
* Inserts an item into the table
|
|
835
|
+
* Inserts an item into the table.
|
|
21
836
|
* @param value - The item to insert.
|
|
22
|
-
* @param indexData - A JSON object representing dynamic indices.
|
|
23
837
|
* @returns The primary key of the inserted row.
|
|
24
838
|
*/
|
|
25
|
-
async insert(value
|
|
26
|
-
return this.provider.insert(this.
|
|
839
|
+
async insert(value) {
|
|
840
|
+
return this.provider.insert(this.definition, value);
|
|
27
841
|
}
|
|
28
842
|
/**
|
|
29
|
-
* Finds items based on provided
|
|
30
|
-
* @param
|
|
843
|
+
* Finds items based on the provided selector.
|
|
844
|
+
* @param selector - A selector over real value fields. All fields are optional.
|
|
31
845
|
* @returns An array of matching items.
|
|
32
846
|
*/
|
|
33
|
-
async find(
|
|
34
|
-
return this.provider.find(this.
|
|
847
|
+
async find(selector = {}) {
|
|
848
|
+
return this.provider.find(this.definition, selector);
|
|
35
849
|
}
|
|
36
850
|
/**
|
|
37
|
-
* Finds a single item based on provided
|
|
38
|
-
* @param
|
|
851
|
+
* Finds a single item based on the provided selector.
|
|
852
|
+
* @param selector - A selector over real value fields. All fields are optional.
|
|
39
853
|
* @returns The first matching item or null if not found.
|
|
40
854
|
*/
|
|
41
|
-
async findOne(
|
|
42
|
-
return this.provider.findOne(this.
|
|
855
|
+
async findOne(selector = {}) {
|
|
856
|
+
return this.provider.findOne(this.definition, selector);
|
|
43
857
|
}
|
|
44
858
|
/**
|
|
45
|
-
* Updates items based on provided
|
|
46
|
-
* @param
|
|
859
|
+
* Updates items based on the provided selector.
|
|
860
|
+
* @param selector - A selector over real value fields.
|
|
47
861
|
* @param dataToUpdate - A JSON object representing fields to update.
|
|
48
862
|
*/
|
|
49
|
-
async update(
|
|
50
|
-
await this.provider.update(this.
|
|
863
|
+
async update(selector, dataToUpdate) {
|
|
864
|
+
await this.provider.update(this.definition, selector, dataToUpdate);
|
|
51
865
|
}
|
|
52
866
|
/**
|
|
53
|
-
* Deletes items based on provided
|
|
54
|
-
* @param
|
|
867
|
+
* Deletes items based on the provided selector.
|
|
868
|
+
* @param selector - A selector over real value fields.
|
|
55
869
|
*/
|
|
56
|
-
async delete(
|
|
57
|
-
await this.provider.delete(this.
|
|
870
|
+
async delete(selector) {
|
|
871
|
+
await this.provider.delete(this.definition, selector);
|
|
58
872
|
}
|
|
59
873
|
/**
|
|
60
|
-
* Upserts an item into the table based on
|
|
874
|
+
* Upserts an item into the table based on the provided selector.
|
|
875
|
+
* @param selector - A non-empty selector resolving to `id` or a declared unique index.
|
|
61
876
|
* @param value - The item to insert or update.
|
|
62
|
-
* @param indexData - A JSON object representing dynamic indices used to identify the record.
|
|
63
877
|
* @returns The primary key of the upserted row.
|
|
64
878
|
*/
|
|
65
|
-
async upsert(
|
|
66
|
-
return this.provider.upsert(this.
|
|
879
|
+
async upsert(selector, value) {
|
|
880
|
+
return this.provider.upsert(this.definition, selector, value);
|
|
67
881
|
}
|
|
882
|
+
/**
|
|
883
|
+
* Starts a provider-agnostic aggregation pipeline rooted in this table.
|
|
884
|
+
* Any `where` selector is applied before rows enter the aggregation layer.
|
|
885
|
+
*/
|
|
886
|
+
aggregate(options) {
|
|
887
|
+
return createAggregation(this, options);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
function defineTable(definition) {
|
|
892
|
+
return definition;
|
|
68
893
|
}
|
|
69
894
|
|
|
70
|
-
export { DefussTable };
|
|
895
|
+
export { AggregationBuilder, DefussTable, aggregationReducers, avgBy, countRows, createAggregation, defineTable, firstBy, lastBy, maxBy, minBy, sumBy };
|