@lunora/sql-store 1.0.0-alpha.2 → 1.0.0-alpha.20

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/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export { createSqlCtxDb, decodeGlobalRow, readSqlCdcChanges, runSqlAggregateMigrations, runSqlCdcMigration, runSqlGlobalTableMigrations, runSqlRankMigrations, runSqlSearchMigrations, trimSqlCdcChanges } from './packem_shared/createSqlCtxDb-CQFYyQLP.mjs';
2
- export { decodeBigint, effectiveColumnKind, sqliteDecode, sqliteEncode, tryJsonParse } from './packem_shared/sqliteEncode-Dedu92k4.mjs';
1
+ export { createSqlCtxDb, decodeGlobalRow, readSqlCdcChanges, runSqlAggregateMigrations, runSqlCdcMigration, runSqlGlobalTableMigrations, runSqlRankMigrations, runSqlSearchMigrations, trimSqlCdcChanges } from './packem_shared/createSqlCtxDb-BDqIfMnS.mjs';
2
+ export { decodeBigint, effectiveColumnKind, sqliteDecode, sqliteEncode, tryJsonParse } from './packem_shared/decodeBigint-Dedu92k4.mjs';
@@ -1,6 +1,6 @@
1
- import { aggregateTableName, rankTableName, ftsTableName, hasTrigger, runRowValidators, assertFlatPredicate, mergeWhere, sortColumnName, resolveRankPartition, encodePartitionKey, decodeCursor, RANK_TIEBREAK, CountRlsUnsupportedError, normalizeIdStructurally, assertValidClientId, aggregateSqlFunction, compileWhereSql, normalizeOrderKeys, buildSeekWhere, resolveRelationPredicates, resolveWith, encodeCursor, NotFoundError, applyOnDelete, ConflictError, normalizeCountArgument, selectIndexForCount, encodeAggregateKey, selectIndexForAggregate, readAggregateValue, renderSql, runTriggers, matchesRankStaticWhere, stringifySearchText, selectIndexForGroupBy, matchesStaticWhere, coerceAggregateNumber, foldAggregateTally, NotUniqueError, throwingScheduler, tokenizeSearch, buildFtsMatch, scoreDocument } from '@lunora/do';
1
+ import { aggregateTableName, rankTableName, ftsTableName, hasTrigger, runRowValidators, assertFlatPredicate, mergeWhere, sortColumnName, resolveRankPartition, encodePartitionKey, decodeCursor, RANK_TIEBREAK, CountRlsUnsupportedError, normalizeIdStructurally, assertValidClientId, aggregateSqlFunction, softDeleteScope, compileWhereSql, normalizeOrderKeys, buildSeekWhere, resolveRelationPredicates, resolveWith, applySelect, encodeCursor, NotFoundError, applyOnDelete, ConflictError, normalizeCountArgument, selectIndexForCount, encodeAggregateKey, selectIndexForAggregate, readAggregateValue, renderSql, runTriggers, matchesRankStaticWhere, stringifySearchText, selectIndexForGroupBy, fanOutScalarCounts, matchesStaticWhere, coerceAggregateNumber, foldAggregateTally, NotUniqueError, throwingScheduler, tokenizeSearch, buildFtsMatch, scoreDocument } from '@lunora/do';
2
2
  import { sql } from 'drizzle-orm';
3
- import { sqliteDecode, effectiveColumnKind, sqliteEncode } from './sqliteEncode-Dedu92k4.mjs';
3
+ import { sqliteDecode, sqliteEncode, effectiveColumnKind } from './decodeBigint-Dedu92k4.mjs';
4
4
 
5
5
  const physicalColumn = (field) => field === "_id" || field === "id" ? "id" : field;
6
6
  const columnRefSql = (field) => sql`${sql.identifier(physicalColumn(field))}`;
@@ -79,14 +79,23 @@ const tableColumns = (definition) => {
79
79
  }
80
80
  return columns;
81
81
  };
82
+ const columnKindCache = /* @__PURE__ */ new WeakMap();
83
+ const columnKinds = (definition) => {
84
+ let kinds = columnKindCache.get(definition);
85
+ if (kinds === void 0) {
86
+ kinds = Object.entries(definition.shape).map(([field, validator]) => [field, effectiveColumnKind(validator)]);
87
+ columnKindCache.set(definition, kinds);
88
+ }
89
+ return kinds;
90
+ };
82
91
  const decodeGlobalRow = (definition, row) => {
83
92
  const decoded = {};
84
- for (const [field, validator] of Object.entries(definition.shape)) {
93
+ for (const [field, kind] of columnKinds(definition)) {
85
94
  const raw = row[field];
86
95
  if (raw === void 0) {
87
96
  continue;
88
97
  }
89
- decoded[field] = sqliteDecode(raw, effectiveColumnKind(validator));
98
+ decoded[field] = sqliteDecode(raw, kind);
90
99
  }
91
100
  decoded["_id"] = row["id"];
92
101
  decoded["_creationTime"] = row["_creationTime"];
@@ -212,6 +221,9 @@ const searchViaFts = async (exec, dialect, definition, tableName, search, limit)
212
221
  for (const filter of search.filters) {
213
222
  conditions.push(sql`m.${columnRefSql(filter.field)} = ${serializeColumnValue(filter.value)}`);
214
223
  }
224
+ if (definition.softDeleteMode) {
225
+ conditions.push(sql`m.${columnRefSql(definition.softDeleteMode.field)} IS NULL`);
226
+ }
215
227
  let query = sql`SELECT m.* FROM ${sql.identifier(ftName)} f JOIN ${sql.identifier(tableName)} m ON m.${sql.identifier("id")} = f.${sql.identifier("__id__")} WHERE ${sql.join(conditions, sql` AND `)} ORDER BY f.rank, m.${sql.identifier("_creationTime")} DESC`;
216
228
  if (typeof limit === "number") {
217
229
  query = sql`${query} LIMIT ${sql.raw(String(Math.max(0, Math.floor(limit))))}`;
@@ -225,6 +237,9 @@ const searchViaScan = async (exec, dialect, definition, tableName, search, limit
225
237
  return [];
226
238
  }
227
239
  const conditions = search.filters.map((filter) => sql`${columnRefSql(filter.field)} = ${serializeColumnValue(filter.value)}`);
240
+ if (definition.softDeleteMode) {
241
+ conditions.push(sql`${columnRefSql(definition.softDeleteMode.field)} IS NULL`);
242
+ }
228
243
  let query = sql`SELECT * FROM ${sql.identifier(tableName)}`;
229
244
  if (conditions.length > 0) {
230
245
  query = sql`${query} WHERE ${sql.join(conditions, sql` AND `)}`;
@@ -562,7 +577,12 @@ const createSqlCtxDb = (options) => {
562
577
  fieldRef: columnRefSql,
563
578
  serialize: serializeColumnValue,
564
579
  // MySQL has no `||` string concat; the rest use the portable form compileWhereSql defaults to.
565
- ...dialect.name === "mysql" ? { likeContains: (reference, term) => sql`${reference} LIKE CONCAT('%', ${term}, '%')` } : {}
580
+ // The term is wildcard-escaped by compileContains, so pair it with a backslash `ESCAPE` for literal
581
+ // matching. MySQL treats backslash as a string-literal escape, so the SQL text must contain a DOUBLED
582
+ // backslash (`ESCAPE '\\'`) to denote one literal backslash; drizzle's `sql` tag uses the COOKED
583
+ // template string, so `'\\\\'` here renders `'\\'` in the SQL (a single `'\'` would escape the closing
584
+ // quote and raise a syntax error). SQLite/Postgres take backslash literally and use the portable default.
585
+ ...dialect.name === "mysql" ? { likeContains: (reference, term) => sql`${reference} LIKE CONCAT('%', ${term}, '%') ESCAPE '\\\\'` } : {}
566
586
  };
567
587
  const nullSafeEquals = (reference, value) => nullSafeEqualsSql(dialect.name, reference, value);
568
588
  const upsertSql = (config) => {
@@ -1122,10 +1142,11 @@ const createSqlCtxDb = (options) => {
1122
1142
  if (!aggOptions.field) {
1123
1143
  throw new Error(`aggregate(${tableName}, { op: "${aggOptions.op}" }): "field" is required for non-count reducers`);
1124
1144
  }
1125
- const effective = mergeWhere(aggOptions.baseWhere, aggOptions.where);
1145
+ const aggScope = softDeleteScope(definition.softDeleteMode, void 0);
1146
+ const effective = mergeWhere(mergeWhere(aggOptions.baseWhere, aggOptions.where), aggScope);
1126
1147
  const resolved = await resolveAggregateRelations(effective, tableName, aggOptions.relationBaseWhere);
1127
1148
  const hasRelation = resolved !== effective;
1128
- if (definition.aggregateIndexes && !aggOptions.baseWhere && !hasRelation) {
1149
+ if (definition.aggregateIndexes && !aggOptions.baseWhere && !hasRelation && !aggScope) {
1129
1150
  const planned = selectIndexForAggregate(definition.aggregateIndexes, aggOptions.op, aggOptions.field, aggOptions.where);
1130
1151
  if (planned) {
1131
1152
  const counterReady = await ensureBackfilled(tableName, planned.index);
@@ -1159,10 +1180,11 @@ const createSqlCtxDb = (options) => {
1159
1180
  if (countOptions.restrictsCounts) {
1160
1181
  throw new CountRlsUnsupportedError(tableName);
1161
1182
  }
1162
- const effective = mergeWhere(countOptions.baseWhere, countOptions.where);
1183
+ const countScope = softDeleteScope(definition.softDeleteMode, void 0);
1184
+ const effective = mergeWhere(mergeWhere(countOptions.baseWhere, countOptions.where), countScope);
1163
1185
  const resolved = await resolveAggregateRelations(effective, tableName, countOptions.relationBaseWhere);
1164
1186
  const hasRelation = resolved !== effective;
1165
- if (definition.aggregateIndexes && !countOptions.baseWhere && !hasRelation) {
1187
+ if (definition.aggregateIndexes && !countOptions.baseWhere && !hasRelation && !countScope) {
1166
1188
  const planned = selectIndexForCount(definition.aggregateIndexes, countOptions.where);
1167
1189
  if (planned) {
1168
1190
  const counterReady = await ensureBackfilled(tableName, planned.index);
@@ -1183,7 +1205,7 @@ const createSqlCtxDb = (options) => {
1183
1205
  const rows = await queryAll(exec, dialect, whereCondition ? sql`${query} WHERE ${whereCondition}` : query);
1184
1206
  return Number(rows[0]?.["count"] ?? 0);
1185
1207
  },
1186
- async delete(id, expectedTable) {
1208
+ async delete(id, expectedTable, deleteOptions) {
1187
1209
  const tableName = await resolveTableName(id, expectedTable);
1188
1210
  if (!tableName) {
1189
1211
  return;
@@ -1194,6 +1216,11 @@ const createSqlCtxDb = (options) => {
1194
1216
  }
1195
1217
  const snapshot = await rawRow(tableName, id);
1196
1218
  const existing = decodeRow(definition, snapshot);
1219
+ const hard = deleteOptions?.hard === true;
1220
+ const softField = !hard && definition.softDeleteMode ? definition.softDeleteMode.field : void 0;
1221
+ if (softField && (!existing || existing[softField] !== null && existing[softField] !== void 0)) {
1222
+ return;
1223
+ }
1197
1224
  if (hasMatchingTrigger(tableName, "before", "delete")) {
1198
1225
  await fireTriggers("before", "delete", { id, op: "delete", previous: existing ?? void 0, table: tableName });
1199
1226
  }
@@ -1206,10 +1233,10 @@ const createSqlCtxDb = (options) => {
1206
1233
  `cross-backend cascade from global '${tableName}' into shardBy '${holderTable}' is not supported — would require Query Coordinator fan-out across shards`
1207
1234
  );
1208
1235
  }
1209
- const holders = await writer.findMany(holderTable, { where: { [field]: value } });
1236
+ const holders = await writer.findMany(holderTable, { includeDeleted: hard, where: { [field]: value } });
1210
1237
  return holders.page;
1211
1238
  },
1212
- onCascade: (_holderTable, holderId) => writer.delete(holderId),
1239
+ onCascade: (_holderTable, holderId) => writer.delete(holderId, void 0, deleteOptions),
1213
1240
  onRestrict: (message) => {
1214
1241
  throw new ConflictError(message, "restrict");
1215
1242
  },
@@ -1220,6 +1247,23 @@ const createSqlCtxDb = (options) => {
1220
1247
  });
1221
1248
  await ensureBackfilledForTable(tableName);
1222
1249
  await ensureRankBackfilledForTable(tableName);
1250
+ if (softField && existing) {
1251
+ const merged = { ...existing, [softField]: clock() };
1252
+ const assignments = sql.join(
1253
+ // eslint-disable-next-line unicorn/no-null -- SQL bind value: an absent column binds `null`, matching the patch path.
1254
+ Object.keys(definition.shape).map((field) => sql`${sql.identifier(field)} = ${serializeColumnValue(merged[field] ?? null)}`),
1255
+ sql`, `
1256
+ );
1257
+ await runGuardedWrite(tableName, "UPDATE", assignments, snapshot);
1258
+ await syncAggregates(tableName, existing, merged);
1259
+ await syncRanks(tableName, id, existing, void 0);
1260
+ await syncSearch(tableName, id, merged);
1261
+ await recordCdc(tableName, id, "update", merged);
1262
+ if (hasMatchingTrigger(tableName, "after", "delete")) {
1263
+ await fireTriggers("after", "delete", { id, op: "delete", previous: existing, table: tableName });
1264
+ }
1265
+ return;
1266
+ }
1223
1267
  await runGuardedWrite(tableName, "DELETE", void 0, snapshot);
1224
1268
  tableNameCache.delete(id);
1225
1269
  await syncAggregates(tableName, existing ?? void 0, void 0);
@@ -1250,13 +1294,37 @@ const createSqlCtxDb = (options) => {
1250
1294
  const orderKeys = normalizeOrderKeys(args.orderBy);
1251
1295
  const seek = args.cursor ? buildSeekWhere(orderKeys, decodeCursor(args.cursor)) : void 0;
1252
1296
  const relationFetcher = relationPredicateFetcher;
1253
- const relationCounter = (childTable, where) => {
1254
- if (!isShardLocalTarget(childTable)) {
1255
- return writer.count(childTable, where);
1297
+ const relationGroupedCounter = async (childTable, whereField, values, policyWhere) => {
1298
+ if (isShardLocalTarget(childTable)) {
1299
+ if (!crossShardCounter) {
1300
+ return crossBackendUnsupported(childTable);
1301
+ }
1302
+ return fanOutScalarCounts(crossShardCounter, childTable, whereField, values, policyWhere);
1256
1303
  }
1257
- return crossShardCounter ? crossShardCounter(childTable, where) : crossBackendUnsupported(childTable);
1304
+ const childDefinition = schema.tables[childTable];
1305
+ if (!childDefinition) {
1306
+ throw new Error(`unknown table: ${childTable}`);
1307
+ }
1308
+ const softScope = softDeleteScope(childDefinition.softDeleteMode, void 0);
1309
+ const inFilter = { [whereField]: { in: values } };
1310
+ const combined = mergeWhere(mergeWhere(inFilter, policyWhere), softScope);
1311
+ const resolvedCombined = await resolveAggregateRelations(combined, childTable, void 0);
1312
+ const whereCondition2 = compileWhereSql(resolvedCombined, whereSqlStrategy);
1313
+ const fieldRef = columnRefSql(whereField);
1314
+ let groupQuery = sql`SELECT ${fieldRef} AS __fk__, COUNT(*) AS count FROM ${sql.identifier(childTable)}`;
1315
+ if (whereCondition2) {
1316
+ groupQuery = sql`${groupQuery} WHERE ${whereCondition2}`;
1317
+ }
1318
+ groupQuery = sql`${groupQuery} GROUP BY ${fieldRef}`;
1319
+ const groupRows = await queryAll(exec, dialect, groupQuery);
1320
+ const result = /* @__PURE__ */ new Map();
1321
+ for (const row of groupRows) {
1322
+ result.set(row["__fk__"], Number(row["count"] ?? 0));
1323
+ }
1324
+ return result;
1258
1325
  };
1259
1326
  let predicate = mergeWhere(args.baseWhere, args.where);
1327
+ predicate = mergeWhere(predicate, softDeleteScope(definition.softDeleteMode, args.includeDeleted));
1260
1328
  predicate = await resolveRelationPredicates(predicate, {
1261
1329
  fetcher: relationFetcher,
1262
1330
  maxRelationKeys,
@@ -1282,21 +1350,30 @@ const createSqlCtxDb = (options) => {
1282
1350
  const documents = decodeRows(definition, rows);
1283
1351
  if (limit === void 0) {
1284
1352
  if (args.with) {
1285
- await resolveWith({ counter: relationCounter, fetcher: relationFetcher, parents: documents, schema, tableName, with: args.with });
1353
+ await resolveWith({
1354
+ fetcher: relationFetcher,
1355
+ groupedCounter: relationGroupedCounter,
1356
+ parents: documents,
1357
+ schema,
1358
+ tableName,
1359
+ with: args.with
1360
+ });
1286
1361
  }
1287
- return { continueCursor: null, isDone: true, page: documents };
1362
+ return { continueCursor: null, isDone: true, page: applySelect(documents, args.select, args.with) };
1288
1363
  }
1289
1364
  const hasMore = documents.length > limit;
1290
1365
  const page = hasMore ? documents.slice(0, limit) : documents;
1291
1366
  const last = page.at(-1);
1292
1367
  if (args.with) {
1293
- await resolveWith({ counter: relationCounter, fetcher: relationFetcher, parents: page, schema, tableName, with: args.with });
1368
+ await resolveWith({ fetcher: relationFetcher, groupedCounter: relationGroupedCounter, parents: page, schema, tableName, with: args.with });
1294
1369
  }
1295
1370
  return {
1371
+ // The cursor is encoded from `last` (the full, unprojected row), so
1372
+ // `applySelect` only trims the returned payload — paging is intact.
1296
1373
  // eslint-disable-next-line unicorn/no-null -- public return shape: `continueCursor` is `string | null`; `null` marks the final page.
1297
1374
  continueCursor: hasMore && last ? encodeCursor(last, orderKeys) : null,
1298
1375
  isDone: !hasMore,
1299
- page
1376
+ page: applySelect(page, args.select, args.with)
1300
1377
  };
1301
1378
  },
1302
1379
  async get(id, expectedTable) {
@@ -1322,10 +1399,11 @@ const createSqlCtxDb = (options) => {
1322
1399
  if (agg.op !== "count" && !agg.field) {
1323
1400
  throw new Error(`groupBy(${tableName}, { agg: { op: "${agg.op}" } }): "field" is required for non-count reducers`);
1324
1401
  }
1325
- const effective = mergeWhere(groupOptions.baseWhere, groupOptions.where);
1402
+ const groupScope = softDeleteScope(definition.softDeleteMode, void 0);
1403
+ const effective = mergeWhere(mergeWhere(groupOptions.baseWhere, groupOptions.where), groupScope);
1326
1404
  const resolved = await resolveAggregateRelations(effective, tableName, groupOptions.relationBaseWhere);
1327
1405
  const hasRelation = resolved !== effective;
1328
- if (definition.aggregateIndexes && !groupOptions.baseWhere && !hasRelation) {
1406
+ if (definition.aggregateIndexes && !groupOptions.baseWhere && !hasRelation && !groupScope) {
1329
1407
  const indexed = await tryIndexedGroupBy(tableName, definition.aggregateIndexes, agg, groupOptions);
1330
1408
  if (indexed !== void 0) {
1331
1409
  return indexed;
@@ -1452,6 +1530,26 @@ const createSqlCtxDb = (options) => {
1452
1530
  await fireTriggers("after", "update", { doc: merged, id, op: "update", previous: existing, table: tableName });
1453
1531
  }
1454
1532
  },
1533
+ async restore(id, expectedTable) {
1534
+ const tableName = await resolveTableName(id, expectedTable);
1535
+ if (!tableName) {
1536
+ throw new Error(`document not found: ${id}`);
1537
+ }
1538
+ const definition = schema.tables[tableName];
1539
+ const field = definition?.softDeleteMode?.field;
1540
+ if (!definition || !field) {
1541
+ throw new Error(`ctx.db.restore: table "${tableName}" is not a .softDelete() table`);
1542
+ }
1543
+ const snapshot = await rawRow(tableName, id);
1544
+ const wasDeleted = snapshot?.[field] !== null && snapshot?.[field] !== void 0;
1545
+ await writer.patch(id, { [field]: null }, expectedTable);
1546
+ if (wasDeleted) {
1547
+ const row = decodeRow(definition, snapshot);
1548
+ if (row !== null) {
1549
+ await syncRanks(tableName, id, void 0, row);
1550
+ }
1551
+ }
1552
+ },
1455
1553
  query(tableName) {
1456
1554
  const definition = schema.tables[tableName];
1457
1555
  if (!definition) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/sql-store",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.20",
4
4
  "description": "Internal dialect-parameterized SQL store core for Lunora .global() backends (D1, PlanetScale)",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -25,7 +25,7 @@
25
25
  "directory": "packages/sql-store"
26
26
  },
27
27
  "files": [
28
- "dist",
28
+ "./dist",
29
29
  "README.md",
30
30
  "LICENSE.md",
31
31
  "__assets__"
@@ -50,7 +50,7 @@
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
- "@lunora/do": "1.0.0-alpha.2",
53
+ "@lunora/do": "1.0.0-alpha.20",
54
54
  "drizzle-orm": "^0.45.2"
55
55
  },
56
56
  "engines": {