@lunora/do 1.0.0-alpha.7 → 1.0.0-alpha.9

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.
Files changed (20) hide show
  1. package/dist/index.d.mts +627 -21
  2. package/dist/index.d.ts +627 -21
  3. package/dist/index.mjs +11 -10
  4. package/dist/packem_shared/{ADMIN_FUNCTIONS-CHcC8fKV.mjs → ADMIN_FUNCTIONS-D_UiYJFk.mjs} +3 -1
  5. package/dist/packem_shared/{CDC_LOG_TABLE-Ctdmxmrv.mjs → CDC_LOG_TABLE-DSycmnDf.mjs} +5 -1
  6. package/dist/packem_shared/{DEFAULT_MAX_RELATION_KEYS-Dou2PWdO.mjs → DEFAULT_MAX_RELATION_KEYS-CHEvKjZt.mjs} +50 -1
  7. package/dist/packem_shared/{NotUniqueError-h_thNFSZ.mjs → NotUniqueError-Cwv7Pe7J.mjs} +9 -7
  8. package/dist/packem_shared/{rank-CrkEIpF4.mjs → RANK_TIEBREAK-CXhdcA1o.mjs} +2 -13
  9. package/dist/packem_shared/{ROOT_DO_SIZE_WARN_BYTES-2DxWrdla.mjs → ROOT_DO_SIZE_WARN_BYTES-DKwBF3Jp.mjs} +1067 -29
  10. package/dist/packem_shared/{ReactiveCache-ByVzgH3d.mjs → ReactiveCache-1hDydFyv.mjs} +1 -28
  11. package/dist/packem_shared/{backfillAggregateIndexes-BbVPvciS.mjs → backfillAggregateIndexes-BZsOqDXP.mjs} +2 -1
  12. package/dist/packem_shared/ctx-db-idempotency-BdcNpvY4.mjs +108 -0
  13. package/dist/packem_shared/ctx-db-shapes-DVoeZpo-.mjs +53 -0
  14. package/dist/packem_shared/{runShardMigrations-PabobOjF.mjs → runShardMigrations-nIwoQeOK.mjs} +5 -3
  15. package/dist/packem_shared/serialize-sql-BlRUoiQe.mjs +14 -0
  16. package/dist/packem_shared/{serveRelationFanout-oxaM6_WL.mjs → serveRelationFanout-C6lDaesn.mjs} +1 -1
  17. package/dist/packem_shared/stableStringify-CyHKJXre.mjs +30 -0
  18. package/package.json +1 -1
  19. package/dist/packem_shared/RANK_TIEBREAK-C6blLR5K.mjs +0 -1
  20. package/dist/packem_shared/ctx-db-idempotency-DkC9rP91.mjs +0 -35
@@ -1,11 +1,6 @@
1
+ import { stableStringify } from './stableStringify-CyHKJXre.mjs';
1
2
  import { depKey, SCAN_DEP } from './SCAN_DEP-DLJF8dsj.mjs';
2
3
 
3
- const compareKeys = (a, b) => {
4
- if (a < b) {
5
- return -1;
6
- }
7
- return a > b ? 1 : 0;
8
- };
9
4
  const DEFAULT_MAX_ENTRIES = 1e3;
10
5
  const DEFAULT_MAX_BYTES = 4 * 1024 * 1024;
11
6
  const estimateBytes = (value) => {
@@ -232,28 +227,6 @@ class ReactiveCache {
232
227
  }
233
228
  }
234
229
  }
235
- const stableStringify = (value) => {
236
- if (value === void 0) {
237
- return "null";
238
- }
239
- if (value === null || typeof value !== "object") {
240
- return JSON.stringify(value);
241
- }
242
- if (Array.isArray(value)) {
243
- return `[${value.map((item) => stableStringify(item)).join(",")}]`;
244
- }
245
- const record = value;
246
- const keys = Object.keys(record).toSorted(compareKeys);
247
- const parts = [];
248
- for (const key of keys) {
249
- const raw = record[key];
250
- if (raw === void 0) {
251
- continue;
252
- }
253
- parts.push(`${JSON.stringify(key)}:${stableStringify(raw)}`);
254
- }
255
- return `{${parts.join(",")}}`;
256
- };
257
230
  const reactiveCacheKey = (functionPath, args, identity) => `${identity ?? "\0anon"}\0${functionPath}:${stableStringify(args)}`;
258
231
 
259
232
  export { ReactiveCache, reactiveCacheKey, stableStringify };
@@ -4,7 +4,8 @@ import { encodeAggregateKey, foldAggregateTally, aggregateTableName } from './ag
4
4
  import { r as runDrizzle } from './do-exec-5eQy5cEi.mjs';
5
5
  import { D as DOC_COLUMN, r as rowToDocument, A as AGG_KEY, a as AGG_VALUE, b as AGG_COUNT } from './do-sql-BCHCWtrD.mjs';
6
6
  import { param } from './renderSql-D6eUcn2N.mjs';
7
- import { s as sortColumnName, m as matchesRankStaticWhere, e as encodePartitionKey, b as serializeSqlValue, r as rankTableName } from './rank-CrkEIpF4.mjs';
7
+ import { sortColumnName, matchesRankStaticWhere, encodePartitionKey, rankTableName } from './RANK_TIEBREAK-CXhdcA1o.mjs';
8
+ import { s as serializeSqlValue } from './serialize-sql-BlRUoiQe.mjs';
8
9
 
9
10
  const backfillAggregateIndex = (sql$1, tableName, index) => {
10
11
  const aggTable = aggregateTableName(tableName, index.name);
@@ -0,0 +1,108 @@
1
+ import { sql } from 'drizzle-orm';
2
+ import { r as runDrizzle } from './do-exec-5eQy5cEi.mjs';
3
+
4
+ const CLIENT_WATERMARK_TABLE = "__client_watermark";
5
+ const migrateClientWatermark = (sql$1) => {
6
+ runDrizzle(
7
+ sql$1,
8
+ sql`CREATE TABLE IF NOT EXISTS ${sql.identifier(CLIENT_WATERMARK_TABLE)} (
9
+ identity TEXT NOT NULL,
10
+ client_id TEXT NOT NULL,
11
+ last_mutation_id INTEGER NOT NULL,
12
+ PRIMARY KEY (identity, client_id)
13
+ )`
14
+ );
15
+ };
16
+ const readClientWatermark = (sql$1, identity, clientId) => {
17
+ const rows = runDrizzle(
18
+ sql$1,
19
+ sql`SELECT last_mutation_id FROM ${sql.identifier(CLIENT_WATERMARK_TABLE)} WHERE identity = ${identity} AND client_id = ${clientId} LIMIT 1`
20
+ ).toArray();
21
+ return rows[0]?.last_mutation_id ?? 0;
22
+ };
23
+ const advanceClientWatermark = (sql$1, identity, clientId, mutationId) => {
24
+ runDrizzle(
25
+ sql$1,
26
+ sql`INSERT INTO ${sql.identifier(CLIENT_WATERMARK_TABLE)} (identity, client_id, last_mutation_id) VALUES (${identity}, ${clientId}, ${mutationId})
27
+ ON CONFLICT(identity, client_id) DO UPDATE SET last_mutation_id = MAX(last_mutation_id, excluded.last_mutation_id)`
28
+ );
29
+ };
30
+
31
+ const GLOBAL_SHAPE_SNAPSHOT_TABLE = "__global_shape_snapshot";
32
+ const migrateGlobalShapeSnapshot = (sql$1) => {
33
+ runDrizzle(
34
+ sql$1,
35
+ sql`CREATE TABLE IF NOT EXISTS ${sql.identifier(GLOBAL_SHAPE_SNAPSHOT_TABLE)} (
36
+ connection_id TEXT NOT NULL,
37
+ sub_id TEXT NOT NULL,
38
+ members TEXT NOT NULL,
39
+ PRIMARY KEY (connection_id, sub_id)
40
+ )`
41
+ );
42
+ };
43
+ const readGlobalShapeSnapshot = (sql$1, connectionId, subId) => {
44
+ const rows = runDrizzle(
45
+ sql$1,
46
+ sql`SELECT members FROM ${sql.identifier(GLOBAL_SHAPE_SNAPSHOT_TABLE)} WHERE connection_id = ${connectionId} AND sub_id = ${subId} LIMIT 1`
47
+ ).toArray();
48
+ const raw = rows[0]?.members;
49
+ if (raw === void 0) {
50
+ return /* @__PURE__ */ new Map();
51
+ }
52
+ try {
53
+ const parsed = JSON.parse(raw);
54
+ if (parsed === null || typeof parsed !== "object") {
55
+ return /* @__PURE__ */ new Map();
56
+ }
57
+ return new Map(Object.entries(parsed));
58
+ } catch {
59
+ return /* @__PURE__ */ new Map();
60
+ }
61
+ };
62
+ const writeGlobalShapeSnapshot = (sql$1, connectionId, subId, snapshot) => {
63
+ const members = JSON.stringify(Object.fromEntries(snapshot));
64
+ runDrizzle(
65
+ sql$1,
66
+ sql`INSERT INTO ${sql.identifier(GLOBAL_SHAPE_SNAPSHOT_TABLE)} (connection_id, sub_id, members) VALUES (${connectionId}, ${subId}, ${members})
67
+ ON CONFLICT(connection_id, sub_id) DO UPDATE SET members = excluded.members`
68
+ );
69
+ };
70
+ const deleteGlobalShapeSnapshot = (sql$1, connectionId, subId) => {
71
+ runDrizzle(sql$1, sql`DELETE FROM ${sql.identifier(GLOBAL_SHAPE_SNAPSHOT_TABLE)} WHERE connection_id = ${connectionId} AND sub_id = ${subId}`);
72
+ };
73
+ const deleteGlobalShapeSnapshotsForConnection = (sql$1, connectionId) => {
74
+ runDrizzle(sql$1, sql`DELETE FROM ${sql.identifier(GLOBAL_SHAPE_SNAPSHOT_TABLE)} WHERE connection_id = ${connectionId}`);
75
+ };
76
+
77
+ const IDEMPOTENCY_TABLE = "__idempotency";
78
+ const migrateIdempotency = (sql$1) => {
79
+ runDrizzle(
80
+ sql$1,
81
+ sql`CREATE TABLE IF NOT EXISTS ${sql.identifier(IDEMPOTENCY_TABLE)} (
82
+ identity TEXT NOT NULL,
83
+ mutation_id TEXT NOT NULL,
84
+ result_json TEXT NOT NULL,
85
+ ts REAL NOT NULL,
86
+ PRIMARY KEY (identity, mutation_id)
87
+ )`
88
+ );
89
+ };
90
+ const readIdempotent = (sql$1, identity, mutationId) => {
91
+ const rows = runDrizzle(
92
+ sql$1,
93
+ sql`SELECT result_json, ts FROM ${sql.identifier(IDEMPOTENCY_TABLE)} WHERE identity = ${identity} AND mutation_id = ${mutationId} LIMIT 1`
94
+ ).toArray();
95
+ const row = rows[0];
96
+ return row === void 0 ? void 0 : { resultJson: row.result_json, ts: row.ts };
97
+ };
98
+ const writeIdempotent = (sql$1, identity, mutationId, resultJson, ts) => {
99
+ runDrizzle(
100
+ sql$1,
101
+ sql`INSERT OR IGNORE INTO ${sql.identifier(IDEMPOTENCY_TABLE)} (identity, mutation_id, result_json, ts) VALUES (${identity}, ${mutationId}, ${resultJson}, ${ts})`
102
+ );
103
+ };
104
+ const trimIdempotent = (sql$1, olderThanTs) => {
105
+ runDrizzle(sql$1, sql`DELETE FROM ${sql.identifier(IDEMPOTENCY_TABLE)} WHERE ts < ${olderThanTs}`);
106
+ };
107
+
108
+ export { CLIENT_WATERMARK_TABLE as C, GLOBAL_SHAPE_SNAPSHOT_TABLE as G, IDEMPOTENCY_TABLE as I, migrateIdempotency as a, migrateGlobalShapeSnapshot as b, advanceClientWatermark as c, deleteGlobalShapeSnapshot as d, deleteGlobalShapeSnapshotsForConnection as e, readGlobalShapeSnapshot as f, readIdempotent as g, writeIdempotent as h, migrateClientWatermark as m, readClientWatermark as r, trimIdempotent as t, writeGlobalShapeSnapshot as w };
@@ -0,0 +1,53 @@
1
+ import { sql } from 'drizzle-orm';
2
+ import { r as runDrizzle } from './do-exec-5eQy5cEi.mjs';
3
+ import { D as DOC_COLUMN, r as rowToDocument, j as jsonPathSql } from './do-sql-BCHCWtrD.mjs';
4
+ import { compileWhereSql } from './compileWhereSql-CXrhFA3G.mjs';
5
+ import { s as serializeSqlValue } from './serialize-sql-BlRUoiQe.mjs';
6
+
7
+ const shapeWhereStrategy = { fieldRef: jsonPathSql, serialize: serializeSqlValue };
8
+ const idInClause = (ids) => {
9
+ if (ids.length === 0) {
10
+ return void 0;
11
+ }
12
+ return sql`id IN (${sql.join(
13
+ ids.map((id) => sql`${id}`),
14
+ sql`, `
15
+ )})`;
16
+ };
17
+ const composeWhere = (effectiveWhere, idRestriction) => {
18
+ const conditions = [];
19
+ if (idRestriction) {
20
+ conditions.push(idRestriction);
21
+ }
22
+ const compiled = compileWhereSql(effectiveWhere, shapeWhereStrategy);
23
+ if (compiled) {
24
+ conditions.push(compiled);
25
+ }
26
+ if (conditions.length === 0) {
27
+ return sql``;
28
+ }
29
+ return sql` WHERE ${sql.join(conditions, sql` AND `)}`;
30
+ };
31
+ const selectShapeRows = (sql$1, table, effectiveWhere) => {
32
+ const whereClause = composeWhere(effectiveWhere, void 0);
33
+ const rows = runDrizzle(sql$1, sql`SELECT id, _creationTime, ${sql.identifier(DOC_COLUMN)} FROM ${sql.identifier(table)}${whereClause}`).toArray();
34
+ const result = [];
35
+ for (const row of rows) {
36
+ const doc = rowToDocument(row);
37
+ const { id } = row;
38
+ if (doc !== void 0 && typeof id === "string") {
39
+ result.push({ doc, id });
40
+ }
41
+ }
42
+ return result;
43
+ };
44
+ const selectShapeMemberIds = (sql$1, table, effectiveWhere, ids) => {
45
+ if (ids.length === 0) {
46
+ return /* @__PURE__ */ new Set();
47
+ }
48
+ const whereClause = composeWhere(effectiveWhere, idInClause(ids));
49
+ const rows = runDrizzle(sql$1, sql`SELECT id FROM ${sql.identifier(table)}${whereClause}`).toArray();
50
+ return new Set(rows.map((row) => row.id));
51
+ };
52
+
53
+ export { selectShapeRows as a, selectShapeMemberIds as s };
@@ -1,10 +1,10 @@
1
1
  import { sql } from 'drizzle-orm';
2
2
  import { aggregateTableName } from './aggregateTableName-CxNqY1Sl.mjs';
3
- import { migrateCdcLog, migrateCdcMeta } from './CDC_LOG_TABLE-Ctdmxmrv.mjs';
4
- import { m as migrateIdempotency } from './ctx-db-idempotency-DkC9rP91.mjs';
3
+ import { migrateCdcLog, migrateCdcMeta } from './CDC_LOG_TABLE-DSycmnDf.mjs';
4
+ import { m as migrateClientWatermark, a as migrateIdempotency, b as migrateGlobalShapeSnapshot } from './ctx-db-idempotency-BdcNpvY4.mjs';
5
5
  import { r as runDrizzle } from './do-exec-5eQy5cEi.mjs';
6
6
  import { D as DOC_COLUMN, j as jsonPathSql, c as createIndexSql, t as tableColumns, i as isFtsAvailable, A as AGG_KEY, a as AGG_VALUE, b as AGG_COUNT } from './do-sql-BCHCWtrD.mjs';
7
- import { s as sortColumnName, r as rankTableName } from './rank-CrkEIpF4.mjs';
7
+ import { sortColumnName, rankTableName } from './RANK_TIEBREAK-CXhdcA1o.mjs';
8
8
  import { ftsTableName } from './buildFtsMatch-BLEMawrp.mjs';
9
9
 
10
10
  const migrateSecondaryIndexes = (sql$1, tableName, definition) => {
@@ -96,8 +96,10 @@ const runShardMigrations = (sql$1, schema, options = {}) => {
96
96
  if (options.cdc) {
97
97
  migrateCdcLog(sql$1);
98
98
  migrateCdcMeta(sql$1);
99
+ migrateClientWatermark(sql$1);
99
100
  }
100
101
  migrateIdempotency(sql$1);
102
+ migrateGlobalShapeSnapshot(sql$1);
101
103
  };
102
104
 
103
105
  export { runShardMigrations };
@@ -0,0 +1,14 @@
1
+ const serializeSqlValue = (value) => {
2
+ if (typeof value === "boolean") {
3
+ return value ? 1 : 0;
4
+ }
5
+ if (value === null || typeof value === "string" || typeof value === "number") {
6
+ return value;
7
+ }
8
+ if (typeof value === "bigint") {
9
+ return value.toString();
10
+ }
11
+ return JSON.stringify(value);
12
+ };
13
+
14
+ export { serializeSqlValue as s };
@@ -1,4 +1,4 @@
1
- import { RELATION_FUNCTION_PREFIX } from './ADMIN_FUNCTIONS-CHcC8fKV.mjs';
1
+ import { RELATION_FUNCTION_PREFIX } from './ADMIN_FUNCTIONS-D_UiYJFk.mjs';
2
2
 
3
3
  const serveRelationFanout = async (schema, database, functionPath, args) => {
4
4
  const table = typeof args["table"] === "string" ? args["table"] : "";
@@ -0,0 +1,30 @@
1
+ const compareKeys = (a, b) => {
2
+ if (a < b) {
3
+ return -1;
4
+ }
5
+ return a > b ? 1 : 0;
6
+ };
7
+ const stableStringify = (value) => {
8
+ if (value === void 0) {
9
+ return "null";
10
+ }
11
+ if (value === null || typeof value !== "object") {
12
+ return JSON.stringify(value);
13
+ }
14
+ if (Array.isArray(value)) {
15
+ return `[${value.map((item) => stableStringify(item)).join(",")}]`;
16
+ }
17
+ const record = value;
18
+ const keys = Object.keys(record).toSorted(compareKeys);
19
+ const parts = [];
20
+ for (const key of keys) {
21
+ const raw = record[key];
22
+ if (raw === void 0) {
23
+ continue;
24
+ }
25
+ parts.push(`${JSON.stringify(key)}:${stableStringify(raw)}`);
26
+ }
27
+ return `{${parts.join(",")}}`;
28
+ };
29
+
30
+ export { stableStringify };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/do",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-alpha.9",
4
4
  "description": "Lunora Durable Objects: ShardDO (SQLite, OCC, hibernated WebSocket subscriptions) and SessionDO",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -1 +0,0 @@
1
- export { R as RANK_TIEBREAK, e as encodePartitionKey, m as matchesRankStaticWhere, c as rankKeyFromDoc, r as rankTableName, a as resolveRankPartition, s as sortColumnName } from './rank-CrkEIpF4.mjs';
@@ -1,35 +0,0 @@
1
- import { sql } from 'drizzle-orm';
2
- import { r as runDrizzle } from './do-exec-5eQy5cEi.mjs';
3
-
4
- const IDEMPOTENCY_TABLE = "__idempotency";
5
- const migrateIdempotency = (sql$1) => {
6
- runDrizzle(
7
- sql$1,
8
- sql`CREATE TABLE IF NOT EXISTS ${sql.identifier(IDEMPOTENCY_TABLE)} (
9
- identity TEXT NOT NULL,
10
- mutation_id TEXT NOT NULL,
11
- result_json TEXT NOT NULL,
12
- ts REAL NOT NULL,
13
- PRIMARY KEY (identity, mutation_id)
14
- )`
15
- );
16
- };
17
- const readIdempotent = (sql$1, identity, mutationId) => {
18
- const rows = runDrizzle(
19
- sql$1,
20
- sql`SELECT result_json, ts FROM ${sql.identifier(IDEMPOTENCY_TABLE)} WHERE identity = ${identity} AND mutation_id = ${mutationId} LIMIT 1`
21
- ).toArray();
22
- const row = rows[0];
23
- return row === void 0 ? void 0 : { resultJson: row.result_json, ts: row.ts };
24
- };
25
- const writeIdempotent = (sql$1, identity, mutationId, resultJson, ts) => {
26
- runDrizzle(
27
- sql$1,
28
- sql`INSERT OR IGNORE INTO ${sql.identifier(IDEMPOTENCY_TABLE)} (identity, mutation_id, result_json, ts) VALUES (${identity}, ${mutationId}, ${resultJson}, ${ts})`
29
- );
30
- };
31
- const trimIdempotent = (sql$1, olderThanTs) => {
32
- runDrizzle(sql$1, sql`DELETE FROM ${sql.identifier(IDEMPOTENCY_TABLE)} WHERE ts < ${olderThanTs}`);
33
- };
34
-
35
- export { IDEMPOTENCY_TABLE as I, migrateIdempotency as m, readIdempotent as r, trimIdempotent as t, writeIdempotent as w };