local-lite 2.0.7 → 3.0.1
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 +8 -30
- package/dist/adapters/local/index.d.ts +1 -0
- package/dist/adapters/local/index.js +1 -0
- package/dist/adapters/local/sqlite-wasm.d.ts +25 -0
- package/dist/adapters/local/sqlite-wasm.js +55 -0
- package/dist/adapters/remote/convex.d.ts +1 -1
- package/dist/adapters/remote/convex.js +25 -3
- package/dist/adapters/remote/demo.d.ts +3 -3
- package/dist/adapters/remote/demo.js +11 -3
- package/dist/adapters/remote/firestore.d.ts +1 -1
- package/dist/adapters/remote/firestore.js +57 -91
- package/dist/adapters/remote/index.d.ts +2 -1
- package/dist/adapters/remote/index.js +1 -0
- package/dist/adapters/remote/postgres.d.ts +37 -0
- package/dist/adapters/remote/postgres.js +119 -0
- package/dist/adapters/remote/self-hosted.d.ts +12 -0
- package/dist/adapters/remote/self-hosted.js +55 -4
- package/dist/adapters/remote/supabase.d.ts +1 -1
- package/dist/adapters/remote/supabase.js +22 -5
- package/dist/auth/apple.js +0 -6
- package/dist/auth/github.js +0 -3
- package/dist/auth/google.js +0 -3
- package/dist/auth/identity.d.ts +0 -5
- package/dist/auth/identity.js +0 -3
- package/dist/auth/microsoft.js +0 -3
- package/dist/auth/unified.d.ts +5 -5
- package/dist/auth/unified.js +1 -1
- package/dist/config/index.d.ts +0 -12
- package/dist/config/index.js +3 -26
- package/dist/config/types.d.ts +3 -2
- package/dist/core/audit.d.ts +52 -0
- package/dist/core/audit.js +153 -0
- package/dist/core/client.d.ts +4 -0
- package/dist/core/client.js +7 -0
- package/dist/core/conflict.d.ts +24 -0
- package/dist/core/conflict.js +77 -0
- package/dist/core/field-encryption.d.ts +25 -0
- package/dist/core/field-encryption.js +128 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +2 -0
- package/dist/core/query-builder.d.ts +51 -0
- package/dist/core/query-builder.js +278 -35
- package/dist/core/query.js +6 -1
- package/dist/core/types.d.ts +2 -1
- package/dist/devtools/BenchmarkModal.js +285 -1
- package/dist/email/index.d.ts +1 -1
- package/dist/email/index.js +2 -2
- package/dist/index.d.ts +2 -27
- package/dist/index.js +2 -27
- package/dist/presence/index.d.ts +45 -0
- package/dist/presence/index.js +142 -0
- package/dist/reactive/event-emitter.d.ts +10 -1
- package/dist/reactive/event-emitter.js +44 -1
- package/dist/reactive/live-query.d.ts +18 -0
- package/dist/reactive/live-query.js +71 -0
- package/dist/schema/index.d.ts +1 -0
- package/dist/schema/index.js +1 -0
- package/dist/schema/typed.d.ts +38 -0
- package/dist/schema/typed.js +33 -0
- package/dist/server/dashboard.d.ts +2 -0
- package/dist/server/dashboard.js +182 -2
- package/dist/solid/index.d.ts +24 -0
- package/dist/solid/index.js +35 -0
- package/dist/storage/firebase.d.ts +3 -3
- package/dist/storage/firebase.js +40 -94
- package/dist/svelte/index.d.ts +15 -0
- package/dist/svelte/index.js +27 -0
- package/dist/testing/index.d.ts +22 -0
- package/dist/testing/index.js +74 -0
- package/dist/vue/index.d.ts +27 -0
- package/dist/vue/index.js +37 -0
- package/package.json +46 -9
- package/skills/local-lite-auth/SKILL.md +1 -1
- package/skills/local-lite-custom-adapter/SKILL.md +7 -4
- package/skills/local-lite-react/SKILL.md +1 -1
- package/skills/local-lite-setup/SKILL.md +3 -2
- package/skills/local-lite-upgrade/SKILL.md +16 -0
- package/dist/kv/index.d.ts +0 -3
- package/dist/kv/index.js +0 -19
- package/dist/kv/manager.d.ts +0 -52
- package/dist/kv/manager.js +0 -167
- package/dist/kv/store.d.ts +0 -13
- package/dist/kv/store.js +0 -85
- package/dist/kv/types.d.ts +0 -27
- package/dist/kv/types.js +0 -2
- package/dist/react/kv.d.ts +0 -9
- package/dist/react/kv.js +0 -53
|
@@ -41,6 +41,43 @@ function formatColumnIdentifier(col) {
|
|
|
41
41
|
function escapeLikePattern(value) {
|
|
42
42
|
return String(value).replace(/[\\%_]/g, '\\$&');
|
|
43
43
|
}
|
|
44
|
+
const OP_MAP = {
|
|
45
|
+
'>=': '$gte',
|
|
46
|
+
'<=': '$lte',
|
|
47
|
+
'>': '$gt',
|
|
48
|
+
'<': '$lt',
|
|
49
|
+
'!=': '$ne',
|
|
50
|
+
'<>': '$ne',
|
|
51
|
+
'=': '$eq',
|
|
52
|
+
'==': '$eq',
|
|
53
|
+
like: '$contains',
|
|
54
|
+
in: '$in',
|
|
55
|
+
'not in': '$nin',
|
|
56
|
+
$eq: '$eq',
|
|
57
|
+
$ne: '$ne',
|
|
58
|
+
$gt: '$gt',
|
|
59
|
+
$gte: '$gte',
|
|
60
|
+
$lt: '$lt',
|
|
61
|
+
$lte: '$lte',
|
|
62
|
+
$in: '$in',
|
|
63
|
+
$nin: '$nin',
|
|
64
|
+
$contains: '$contains',
|
|
65
|
+
$startswith: '$startsWith',
|
|
66
|
+
$endswith: '$endsWith',
|
|
67
|
+
};
|
|
68
|
+
function mergeWhereOperator(target, column, operatorOrValue, value) {
|
|
69
|
+
if (value !== undefined) {
|
|
70
|
+
const rawOp = String(operatorOrValue).toLowerCase().trim();
|
|
71
|
+
const opKey = OP_MAP[rawOp] ?? '$eq';
|
|
72
|
+
const existing = typeof target[column] === 'object' && target[column] !== null
|
|
73
|
+
? target[column]
|
|
74
|
+
: {};
|
|
75
|
+
target[column] = { ...existing, [opKey]: value };
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
target[column] = operatorOrValue;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
44
81
|
/**
|
|
45
82
|
* Compiles a single column comparison value into one or more SQL fragments.
|
|
46
83
|
* Supports null, boolean, scalar, and operator-object forms ($eq, $gt, $in, …).
|
|
@@ -150,9 +187,16 @@ function compileWhereConditions(where, params) {
|
|
|
150
187
|
clauses.push(`(${andClauses.join(' AND ')})`);
|
|
151
188
|
}
|
|
152
189
|
}
|
|
190
|
+
// Handle $not — negate the compiled sub-conditions
|
|
191
|
+
if (where.$not && typeof where.$not === 'object') {
|
|
192
|
+
const notList = compileWhereConditions(where.$not, params);
|
|
193
|
+
if (notList.length > 0) {
|
|
194
|
+
clauses.push(`NOT (${notList.join(' AND ')})`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
153
197
|
// Handle regular column conditions
|
|
154
198
|
for (const [col, val] of Object.entries(where)) {
|
|
155
|
-
if (col === '$or' || col === '$and')
|
|
199
|
+
if (col === '$or' || col === '$and' || col === '$not')
|
|
156
200
|
continue;
|
|
157
201
|
const safeCol = formatColumnIdentifier(col);
|
|
158
202
|
const clause = compileComparison(safeCol, val, params);
|
|
@@ -162,6 +206,112 @@ function compileWhereConditions(where, params) {
|
|
|
162
206
|
}
|
|
163
207
|
return clauses;
|
|
164
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Batch-loads declared relationships or explicit WithRelationSpec entries for hydrated rows.
|
|
211
|
+
*/
|
|
212
|
+
async function preloadRelations(client, parentTable, rows, relations, includeDeleted) {
|
|
213
|
+
if (!rows || rows.length === 0 || relations.length === 0)
|
|
214
|
+
return;
|
|
215
|
+
const rootClient = 'parent' in client ? client.parent : client;
|
|
216
|
+
const declaredRels = rootClient?.relationships ?? [];
|
|
217
|
+
const defaultPk = rootClient?.primaryKey ?? 'id';
|
|
218
|
+
for (const relInput of relations) {
|
|
219
|
+
const spec = typeof relInput === 'string' ? { table: relInput } : relInput;
|
|
220
|
+
let relType = spec.type;
|
|
221
|
+
let targetTable = spec.table;
|
|
222
|
+
let foreignKey = spec.foreignKey;
|
|
223
|
+
let localKey = spec.localKey ?? defaultPk;
|
|
224
|
+
const propName = spec.as ?? spec.table;
|
|
225
|
+
if (!relType || !foreignKey) {
|
|
226
|
+
// 1) Check hasMany: child.references === parentTable && child.table === targetTable
|
|
227
|
+
const hasManyMatch = declaredRels.find((r) => r.references === parentTable && r.table === targetTable);
|
|
228
|
+
if (hasManyMatch) {
|
|
229
|
+
relType = relType ?? 'hasMany';
|
|
230
|
+
foreignKey = foreignKey ?? hasManyMatch.foreignKey;
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
// 2) Check belongsTo: parentTable.references === targetTable (or singular/plural match)
|
|
234
|
+
const belongsToMatch = declaredRels.find((r) => r.table === parentTable &&
|
|
235
|
+
(r.references === targetTable ||
|
|
236
|
+
r.references === `${targetTable}s` ||
|
|
237
|
+
r.foreignKey === `${targetTable}_id`));
|
|
238
|
+
if (belongsToMatch) {
|
|
239
|
+
relType = relType ?? 'belongsTo';
|
|
240
|
+
targetTable = belongsToMatch.references;
|
|
241
|
+
foreignKey = foreignKey ?? belongsToMatch.foreignKey;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (!relType || !foreignKey) {
|
|
246
|
+
throw new Error(`Cannot resolve relation "${propName}" from "${parentTable}". Declare it in client relationships or pass an explicit WithRelationSpec.`);
|
|
247
|
+
}
|
|
248
|
+
if (relType === 'hasMany') {
|
|
249
|
+
const parentKeys = Array.from(new Set(rows
|
|
250
|
+
.map((r) => r?.[localKey])
|
|
251
|
+
.filter((v) => v !== null && v !== undefined)));
|
|
252
|
+
if (parentKeys.length === 0) {
|
|
253
|
+
for (const r of rows)
|
|
254
|
+
r[propName] = [];
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
const qb = new QueryBuilder(client, targetTable)
|
|
258
|
+
.where({ [foreignKey]: { $in: parentKeys } })
|
|
259
|
+
.withDeleted(includeDeleted);
|
|
260
|
+
if (spec.where)
|
|
261
|
+
qb.where(spec.where);
|
|
262
|
+
const children = await qb.execute();
|
|
263
|
+
const grouped = new Map();
|
|
264
|
+
for (const child of children) {
|
|
265
|
+
const fkVal = child?.[foreignKey];
|
|
266
|
+
let bucket = grouped.get(fkVal);
|
|
267
|
+
if (!bucket) {
|
|
268
|
+
bucket = [];
|
|
269
|
+
grouped.set(fkVal, bucket);
|
|
270
|
+
}
|
|
271
|
+
bucket.push(child);
|
|
272
|
+
}
|
|
273
|
+
for (const r of rows) {
|
|
274
|
+
r[propName] = grouped.get(r?.[localKey]) ?? [];
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
// belongsTo
|
|
279
|
+
const fkValues = Array.from(new Set(rows
|
|
280
|
+
.map((r) => r?.[foreignKey])
|
|
281
|
+
.filter((v) => v !== null && v !== undefined)));
|
|
282
|
+
if (fkValues.length === 0) {
|
|
283
|
+
for (const r of rows)
|
|
284
|
+
r[propName] = null;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
const qb = new QueryBuilder(client, targetTable)
|
|
288
|
+
.where({ [localKey]: { $in: fkValues } })
|
|
289
|
+
.withDeleted(includeDeleted);
|
|
290
|
+
if (spec.where)
|
|
291
|
+
qb.where(spec.where);
|
|
292
|
+
const parents = await qb.execute();
|
|
293
|
+
const byKey = new Map();
|
|
294
|
+
for (const p of parents) {
|
|
295
|
+
byKey.set(p?.[localKey], p);
|
|
296
|
+
}
|
|
297
|
+
for (const r of rows) {
|
|
298
|
+
r[propName] = byKey.get(r?.[foreignKey]) ?? null;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async function hydrateAndPreload(client, tableName, rawRows, withRelations, includeDeleted) {
|
|
304
|
+
const hydrator = 'hydrateRow' in client
|
|
305
|
+
? client.hydrateRow.bind(client)
|
|
306
|
+
: client.parent?.hydrateRow?.bind(client.parent);
|
|
307
|
+
const hydrated = hydrator && rawRows && rawRows.length > 0
|
|
308
|
+
? rawRows.map((r) => hydrator(tableName, r))
|
|
309
|
+
: rawRows;
|
|
310
|
+
if (withRelations.length > 0 && hydrated && hydrated.length > 0) {
|
|
311
|
+
await preloadRelations(client, tableName, hydrated, withRelations, includeDeleted);
|
|
312
|
+
}
|
|
313
|
+
return hydrated;
|
|
314
|
+
}
|
|
165
315
|
// ---------------------------------------------------------------------------
|
|
166
316
|
/**
|
|
167
317
|
* Fluent, type-safe SQL Query Builder for local-first SQLite operations.
|
|
@@ -171,8 +321,11 @@ class QueryBuilder {
|
|
|
171
321
|
client;
|
|
172
322
|
tableName;
|
|
173
323
|
selectCols = ['*'];
|
|
324
|
+
isDistinct = false;
|
|
174
325
|
joins = [];
|
|
175
326
|
whereConditions = {};
|
|
327
|
+
havingConditions = {};
|
|
328
|
+
withRelations = [];
|
|
176
329
|
orderColumn;
|
|
177
330
|
orderDirection = 'ASC';
|
|
178
331
|
limitCount;
|
|
@@ -194,6 +347,13 @@ class QueryBuilder {
|
|
|
194
347
|
}
|
|
195
348
|
return this;
|
|
196
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* Adds DISTINCT to the SELECT projection.
|
|
352
|
+
*/
|
|
353
|
+
distinct(enabled = true) {
|
|
354
|
+
this.isDistinct = enabled;
|
|
355
|
+
return this;
|
|
356
|
+
}
|
|
197
357
|
/**
|
|
198
358
|
* Inner joins another SQLite table on a matching column expression.
|
|
199
359
|
*/
|
|
@@ -217,38 +377,48 @@ class QueryBuilder {
|
|
|
217
377
|
*/
|
|
218
378
|
where(columnOrFilter, operatorOrValue, value) {
|
|
219
379
|
if (typeof columnOrFilter === 'string') {
|
|
220
|
-
|
|
221
|
-
const rawOp = String(operatorOrValue).toLowerCase().trim();
|
|
222
|
-
const opKey = rawOp === '>='
|
|
223
|
-
? '$gte'
|
|
224
|
-
: rawOp === '<='
|
|
225
|
-
? '$lte'
|
|
226
|
-
: rawOp === '>'
|
|
227
|
-
? '$gt'
|
|
228
|
-
: rawOp === '<'
|
|
229
|
-
? '$lt'
|
|
230
|
-
: rawOp === '!=' || rawOp === '<>'
|
|
231
|
-
? '$ne'
|
|
232
|
-
: rawOp === '=' || rawOp === '=='
|
|
233
|
-
? '$eq'
|
|
234
|
-
: rawOp === 'like'
|
|
235
|
-
? '$contains'
|
|
236
|
-
: '$eq';
|
|
237
|
-
const existing = typeof this.whereConditions[columnOrFilter] === 'object' &&
|
|
238
|
-
this.whereConditions[columnOrFilter] !== null
|
|
239
|
-
? this.whereConditions[columnOrFilter]
|
|
240
|
-
: {};
|
|
241
|
-
this.whereConditions[columnOrFilter] = { ...existing, [opKey]: value };
|
|
242
|
-
}
|
|
243
|
-
else {
|
|
244
|
-
this.whereConditions[columnOrFilter] = operatorOrValue;
|
|
245
|
-
}
|
|
380
|
+
mergeWhereOperator(this.whereConditions, columnOrFilter, operatorOrValue, value);
|
|
246
381
|
}
|
|
247
382
|
else if (columnOrFilter && typeof columnOrFilter === 'object') {
|
|
248
383
|
this.whereConditions = { ...this.whereConditions, ...columnOrFilter };
|
|
249
384
|
}
|
|
250
385
|
return this;
|
|
251
386
|
}
|
|
387
|
+
/**
|
|
388
|
+
* Adds a negated WHERE condition ($not). Multiple calls are combined with $and.
|
|
389
|
+
*/
|
|
390
|
+
whereNot(filter) {
|
|
391
|
+
if (!filter || typeof filter !== 'object')
|
|
392
|
+
return this;
|
|
393
|
+
if (this.whereConditions.$not === undefined) {
|
|
394
|
+
this.whereConditions.$not = filter;
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
const existing = Array.isArray(this.whereConditions.$and)
|
|
398
|
+
? this.whereConditions.$and
|
|
399
|
+
: [];
|
|
400
|
+
this.whereConditions.$and = [...existing, { $not: filter }];
|
|
401
|
+
}
|
|
402
|
+
return this;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Adds an OR group of filter objects to the WHERE clause.
|
|
406
|
+
*/
|
|
407
|
+
orWhere(...filters) {
|
|
408
|
+
const valid = filters.filter((f) => f && typeof f === 'object');
|
|
409
|
+
if (valid.length === 0)
|
|
410
|
+
return this;
|
|
411
|
+
if (Array.isArray(this.whereConditions.$or)) {
|
|
412
|
+
const existing = Array.isArray(this.whereConditions.$and)
|
|
413
|
+
? this.whereConditions.$and
|
|
414
|
+
: [];
|
|
415
|
+
this.whereConditions.$and = [...existing, { $or: valid }];
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
this.whereConditions.$or = valid;
|
|
419
|
+
}
|
|
420
|
+
return this;
|
|
421
|
+
}
|
|
252
422
|
/**
|
|
253
423
|
* Orders query results by column and direction.
|
|
254
424
|
*/
|
|
@@ -264,6 +434,68 @@ class QueryBuilder {
|
|
|
264
434
|
this.groupByCols = columns.flat();
|
|
265
435
|
return this;
|
|
266
436
|
}
|
|
437
|
+
/**
|
|
438
|
+
* Adds HAVING criteria to filter grouped aggregation results.
|
|
439
|
+
*/
|
|
440
|
+
having(columnOrFilter, operatorOrValue, value) {
|
|
441
|
+
if (typeof columnOrFilter === 'string') {
|
|
442
|
+
mergeWhereOperator(this.havingConditions, columnOrFilter, operatorOrValue, value);
|
|
443
|
+
}
|
|
444
|
+
else if (columnOrFilter && typeof columnOrFilter === 'object') {
|
|
445
|
+
this.havingConditions = { ...this.havingConditions, ...columnOrFilter };
|
|
446
|
+
}
|
|
447
|
+
return this;
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Eagerly preloads related records from declared client relationships or explicit relation specs.
|
|
451
|
+
*/
|
|
452
|
+
with(...relations) {
|
|
453
|
+
this.withRelations.push(...relations);
|
|
454
|
+
return this;
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Returns a shallow-cloned QueryBuilder with independent mutable state.
|
|
458
|
+
*/
|
|
459
|
+
clone() {
|
|
460
|
+
const copy = new QueryBuilder(this.client, this.tableName);
|
|
461
|
+
copy.selectCols = [...this.selectCols];
|
|
462
|
+
copy.isDistinct = this.isDistinct;
|
|
463
|
+
copy.joins = this.joins.map((j) => ({ ...j }));
|
|
464
|
+
copy.whereConditions = { ...this.whereConditions };
|
|
465
|
+
copy.havingConditions = { ...this.havingConditions };
|
|
466
|
+
copy.withRelations = [...this.withRelations];
|
|
467
|
+
copy.orderColumn = this.orderColumn;
|
|
468
|
+
copy.orderDirection = this.orderDirection;
|
|
469
|
+
copy.limitCount = this.limitCount;
|
|
470
|
+
copy.offsetCount = this.offsetCount;
|
|
471
|
+
copy.groupByCols = [...this.groupByCols];
|
|
472
|
+
copy.includeDeleted = this.includeDeleted;
|
|
473
|
+
return copy;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Compiles the query once into a reusable PreparedQuery handle.
|
|
477
|
+
*/
|
|
478
|
+
prepare() {
|
|
479
|
+
const { sql, params } = this.toSQL();
|
|
480
|
+
const client = this.client;
|
|
481
|
+
const tableName = this.tableName;
|
|
482
|
+
const withRelations = [...this.withRelations];
|
|
483
|
+
const includeDeleted = this.includeDeleted;
|
|
484
|
+
const run = async (overrideParams) => {
|
|
485
|
+
const effectiveParams = overrideParams ?? params;
|
|
486
|
+
const rawRows = await client.query(sql, effectiveParams);
|
|
487
|
+
return hydrateAndPreload(client, tableName, rawRows, withRelations, includeDeleted);
|
|
488
|
+
};
|
|
489
|
+
return {
|
|
490
|
+
sql,
|
|
491
|
+
params,
|
|
492
|
+
execute: run,
|
|
493
|
+
async first(overrideParams) {
|
|
494
|
+
const rows = await run(overrideParams);
|
|
495
|
+
return rows.length > 0 ? rows[0] : null;
|
|
496
|
+
},
|
|
497
|
+
};
|
|
498
|
+
}
|
|
267
499
|
/**
|
|
268
500
|
* Limits the number of returned records.
|
|
269
501
|
*/
|
|
@@ -313,7 +545,7 @@ class QueryBuilder {
|
|
|
313
545
|
const params = [];
|
|
314
546
|
const projections = this.selectCols.map(formatColumnIdentifier).join(', ');
|
|
315
547
|
const fromTable = `"${this.tableName.replace(/"/g, '""')}"`;
|
|
316
|
-
let sql = `SELECT ${projections} FROM ${fromTable}`;
|
|
548
|
+
let sql = `SELECT ${this.isDistinct ? 'DISTINCT ' : ''}${projections} FROM ${fromTable}`;
|
|
317
549
|
// Append JOINs
|
|
318
550
|
for (const j of this.joins) {
|
|
319
551
|
const joinTable = `"${j.table.replace(/"/g, '""')}"`;
|
|
@@ -344,6 +576,14 @@ class QueryBuilder {
|
|
|
344
576
|
if (this.groupByCols.length > 0) {
|
|
345
577
|
sql += ` GROUP BY ${this.groupByCols.map(formatColumnIdentifier).join(', ')}`;
|
|
346
578
|
}
|
|
579
|
+
// HAVING
|
|
580
|
+
if (this.havingConditions &&
|
|
581
|
+
Object.keys(this.havingConditions).length > 0) {
|
|
582
|
+
const havingClauses = compileWhereConditions(this.havingConditions, params);
|
|
583
|
+
if (havingClauses.length > 0) {
|
|
584
|
+
sql += ` HAVING ${havingClauses.join(' AND ')}`;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
347
587
|
// ORDER BY
|
|
348
588
|
if (this.orderColumn) {
|
|
349
589
|
sql += ` ORDER BY ${formatColumnIdentifier(this.orderColumn)} ${this.orderDirection}`;
|
|
@@ -382,13 +622,7 @@ class QueryBuilder {
|
|
|
382
622
|
const { sql, params } = this.toSQL();
|
|
383
623
|
try {
|
|
384
624
|
const rawRows = await this.client.query(sql, params);
|
|
385
|
-
|
|
386
|
-
? this.client.hydrateRow.bind(this.client)
|
|
387
|
-
: this.client.parent?.hydrateRow?.bind(this.client.parent);
|
|
388
|
-
if (hydrator && rawRows && rawRows.length > 0) {
|
|
389
|
-
return rawRows.map((r) => hydrator(this.tableName, r));
|
|
390
|
-
}
|
|
391
|
-
return rawRows;
|
|
625
|
+
return await hydrateAndPreload(this.client, this.tableName, rawRows, this.withRelations, this.includeDeleted);
|
|
392
626
|
}
|
|
393
627
|
catch (err) {
|
|
394
628
|
const msg = String(err?.message || err || '');
|
|
@@ -417,7 +651,9 @@ class QueryBuilder {
|
|
|
417
651
|
*/
|
|
418
652
|
async count() {
|
|
419
653
|
const originalSelect = this.selectCols;
|
|
654
|
+
const originalWith = this.withRelations;
|
|
420
655
|
this.selectCols = ['COUNT(*) AS _count'];
|
|
656
|
+
this.withRelations = [];
|
|
421
657
|
try {
|
|
422
658
|
const rows = await this.execute();
|
|
423
659
|
const countVal = rows[0]?._count;
|
|
@@ -425,6 +661,7 @@ class QueryBuilder {
|
|
|
425
661
|
}
|
|
426
662
|
finally {
|
|
427
663
|
this.selectCols = originalSelect;
|
|
664
|
+
this.withRelations = originalWith;
|
|
428
665
|
}
|
|
429
666
|
}
|
|
430
667
|
/**
|
|
@@ -432,8 +669,10 @@ class QueryBuilder {
|
|
|
432
669
|
*/
|
|
433
670
|
async sum(column) {
|
|
434
671
|
const originalSelect = this.selectCols;
|
|
672
|
+
const originalWith = this.withRelations;
|
|
435
673
|
const safeCol = formatColumnIdentifier(column);
|
|
436
674
|
this.selectCols = [`COALESCE(SUM(${safeCol}), 0) AS _sum`];
|
|
675
|
+
this.withRelations = [];
|
|
437
676
|
try {
|
|
438
677
|
const rows = await this.execute();
|
|
439
678
|
const sumVal = rows[0]?._sum;
|
|
@@ -441,6 +680,7 @@ class QueryBuilder {
|
|
|
441
680
|
}
|
|
442
681
|
finally {
|
|
443
682
|
this.selectCols = originalSelect;
|
|
683
|
+
this.withRelations = originalWith;
|
|
444
684
|
}
|
|
445
685
|
}
|
|
446
686
|
/**
|
|
@@ -448,8 +688,10 @@ class QueryBuilder {
|
|
|
448
688
|
*/
|
|
449
689
|
async avg(column) {
|
|
450
690
|
const originalSelect = this.selectCols;
|
|
691
|
+
const originalWith = this.withRelations;
|
|
451
692
|
const safeCol = formatColumnIdentifier(column);
|
|
452
693
|
this.selectCols = [`AVG(${safeCol}) AS _avg`];
|
|
694
|
+
this.withRelations = [];
|
|
453
695
|
try {
|
|
454
696
|
const rows = await this.execute();
|
|
455
697
|
const avgVal = rows[0]?._avg;
|
|
@@ -459,6 +701,7 @@ class QueryBuilder {
|
|
|
459
701
|
}
|
|
460
702
|
finally {
|
|
461
703
|
this.selectCols = originalSelect;
|
|
704
|
+
this.withRelations = originalWith;
|
|
462
705
|
}
|
|
463
706
|
}
|
|
464
707
|
}
|
package/dist/core/query.js
CHANGED
|
@@ -91,9 +91,14 @@ function matchesWhere(row, where) {
|
|
|
91
91
|
if (!andMatches)
|
|
92
92
|
return false;
|
|
93
93
|
}
|
|
94
|
+
// Handle $not combinator
|
|
95
|
+
if ('$not' in where && where.$not !== undefined) {
|
|
96
|
+
if (matchesWhere(row, where.$not))
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
94
99
|
// Field-level conditions
|
|
95
100
|
for (const [key, filter] of Object.entries(where)) {
|
|
96
|
-
if (key === '$or' || key === '$and')
|
|
101
|
+
if (key === '$or' || key === '$and' || key === '$not')
|
|
97
102
|
continue;
|
|
98
103
|
if (!matchesField(row[key], filter)) {
|
|
99
104
|
return false;
|
package/dist/core/types.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export interface ConflictContext {
|
|
|
47
47
|
}
|
|
48
48
|
export type ConflictResolver = (context: ConflictContext) => ConflictResolution | Promise<ConflictResolution>;
|
|
49
49
|
export type ConflictStrategy = 'last-write-wins' | 'server-wins' | 'client-wins' | 'field-merge';
|
|
50
|
-
export type FieldMergeWinner = 'remote' | 'local' | 'newer' | 'max' | 'min';
|
|
50
|
+
export type FieldMergeWinner = 'remote' | 'local' | 'newer' | 'max' | 'min' | 'set-union' | 'pn-counter' | 'lww-element-set';
|
|
51
51
|
export type FieldMergeCustomResolver = (localVal: any, remoteVal: any, context: ConflictContext) => any;
|
|
52
52
|
export type FieldMergeRule = FieldMergeWinner | FieldMergeCustomResolver;
|
|
53
53
|
export interface FieldMergeResolverOptions {
|
|
@@ -128,6 +128,7 @@ export type FieldFilter<T = any> = T | ComparisonOperator<T> | ((value: T) => bo
|
|
|
128
128
|
export type WhereFilter = Record<string, FieldFilter> | ((row: Record<string, any>) => boolean) | {
|
|
129
129
|
$or?: (Record<string, FieldFilter> | ((row: Record<string, any>) => boolean))[];
|
|
130
130
|
$and?: (Record<string, FieldFilter> | ((row: Record<string, any>) => boolean))[];
|
|
131
|
+
$not?: Record<string, FieldFilter> | ((row: Record<string, any>) => boolean);
|
|
131
132
|
[key: string]: any;
|
|
132
133
|
};
|
|
133
134
|
export interface QueryOptions {
|