@lunora/do 0.0.0 → 1.0.0-alpha.10
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/LICENSE.md +105 -0
- package/README.md +115 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/index.d.mts +6347 -0
- package/dist/index.d.ts +6347 -0
- package/dist/index.mjs +37 -0
- package/dist/packem_shared/ADMIN_FUNCTIONS-D_UiYJFk.mjs +316 -0
- package/dist/packem_shared/AGGREGATE_SQL_FUNCTION-CFk6adSu.mjs +54 -0
- package/dist/packem_shared/AUTH_METRICS_BUCKETS_TABLE-CiHHYeJi.mjs +84 -0
- package/dist/packem_shared/CDC_LOG_TABLE-DSycmnDf.mjs +107 -0
- package/dist/packem_shared/ConflictError-C0STs6bU.mjs +13 -0
- package/dist/packem_shared/CountRlsUnsupportedError-28ZvvwKS.mjs +133 -0
- package/dist/packem_shared/DATA_MIGRATION_STATE_TABLE-PTtTiQ7U.mjs +237 -0
- package/dist/packem_shared/DEFAULT_MAX_RELATION_KEYS-DU-Y4-LJ.mjs +209 -0
- package/dist/packem_shared/FUNCTION_METRICS_BUCKETS_TABLE-UDNVD7FS.mjs +248 -0
- package/dist/packem_shared/LogBuffer-B_Ezju_N.mjs +37 -0
- package/dist/packem_shared/MAIL_RETENTION-CPpgl-dX.mjs +104 -0
- package/dist/packem_shared/MAX_SQL_ROWS-dDcFE1YZ.mjs +29 -0
- package/dist/packem_shared/MIN_ADMIN_TOKEN_LENGTH-CCAvoFlr.mjs +1 -0
- package/dist/packem_shared/NotFoundError-CMuMZt81.mjs +10 -0
- package/dist/packem_shared/NotUniqueError-DZQtH02h.mjs +1835 -0
- package/dist/packem_shared/RANK_TIEBREAK-CXhdcA1o.mjs +91 -0
- package/dist/packem_shared/RLS_UNWRAP_SYMBOL-EtGQdC9d.mjs +132 -0
- package/dist/packem_shared/ROOT_DO_SIZE_WARN_BYTES-DKwBF3Jp.mjs +4996 -0
- package/dist/packem_shared/ReactiveCache-1hDydFyv.mjs +232 -0
- package/dist/packem_shared/SCAN_DEP-DLJF8dsj.mjs +19 -0
- package/dist/packem_shared/SESSION_DO_TTL_DEFAULT-ilPZsVwu.mjs +180 -0
- package/dist/packem_shared/SHARD_REGISTRY_DO_NAME-BsAbi5Mn.mjs +146 -0
- package/dist/packem_shared/aggregateTableName-CxNqY1Sl.mjs +64 -0
- package/dist/packem_shared/applyOnDelete-BQ-8ZlZ1.mjs +175 -0
- package/dist/packem_shared/applySelect-BvZdFUBT.mjs +101 -0
- package/dist/packem_shared/armRestore-BJk53Ro8.mjs +55 -0
- package/dist/packem_shared/backfillAggregateIndexes-BZsOqDXP.mjs +81 -0
- package/dist/packem_shared/buildFtsMatch-BLEMawrp.mjs +38 -0
- package/dist/packem_shared/compileWhereSql-CXrhFA3G.mjs +127 -0
- package/dist/packem_shared/createSystemReader-8CzSZP9V.mjs +80 -0
- package/dist/packem_shared/ctx-db-idempotency-BdcNpvY4.mjs +108 -0
- package/dist/packem_shared/ctx-db-shapes-DVoeZpo-.mjs +53 -0
- package/dist/packem_shared/do-exec-5eQy5cEi.mjs +12 -0
- package/dist/packem_shared/do-sql-BCHCWtrD.mjs +87 -0
- package/dist/packem_shared/exportShardRows-DZEhUeyI.mjs +156 -0
- package/dist/packem_shared/hasTrigger-5N6_Fx0A.mjs +20 -0
- package/dist/packem_shared/renderSql-D6eUcn2N.mjs +16 -0
- package/dist/packem_shared/runShardMigrations-nIwoQeOK.mjs +105 -0
- package/dist/packem_shared/security-audit-CucgBice.mjs +158 -0
- package/dist/packem_shared/serialize-sql-BlRUoiQe.mjs +14 -0
- package/dist/packem_shared/serveRelationFanout-C6lDaesn.mjs +24 -0
- package/dist/packem_shared/stableStringify-CyHKJXre.mjs +30 -0
- package/dist/packem_shared/subscriptionListDeltas-ce84gpwL.mjs +111 -0
- package/package.json +41 -17
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { s as serializeSqlValue } from './serialize-sql-BlRUoiQe.mjs';
|
|
2
|
+
|
|
3
|
+
const compareStrings = (a, b) => {
|
|
4
|
+
if (a < b) {
|
|
5
|
+
return -1;
|
|
6
|
+
}
|
|
7
|
+
return a > b ? 1 : 0;
|
|
8
|
+
};
|
|
9
|
+
const NOT_RESOLVABLE = /* @__PURE__ */ Symbol("not-resolvable");
|
|
10
|
+
const resolvePartitionValue = (value) => {
|
|
11
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
12
|
+
const operatorKeys = Object.keys(value);
|
|
13
|
+
if (operatorKeys.length === 1 && operatorKeys[0] === "eq") {
|
|
14
|
+
return value.eq;
|
|
15
|
+
}
|
|
16
|
+
return NOT_RESOLVABLE;
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
};
|
|
20
|
+
const encodePartitionKey = (partitionBy, source) => {
|
|
21
|
+
if (partitionBy.length === 0) {
|
|
22
|
+
return "";
|
|
23
|
+
}
|
|
24
|
+
const ordered = {};
|
|
25
|
+
for (const field of [...partitionBy].toSorted(compareStrings)) {
|
|
26
|
+
ordered[field] = source[field] ?? null;
|
|
27
|
+
}
|
|
28
|
+
return JSON.stringify(ordered);
|
|
29
|
+
};
|
|
30
|
+
const RANK_TIEBREAK = "__id__";
|
|
31
|
+
const sortColumnName = (i) => `__sort_k${String(i)}__`;
|
|
32
|
+
const matchesRankStaticWhere = (document, predicate) => {
|
|
33
|
+
for (const [field, expected] of Object.entries(predicate)) {
|
|
34
|
+
const actual = document[field];
|
|
35
|
+
if (expected !== null && typeof expected === "object" && !Array.isArray(expected)) {
|
|
36
|
+
const operatorKeys = Object.keys(expected);
|
|
37
|
+
if (operatorKeys.length === 1 && operatorKeys[0] === "eq") {
|
|
38
|
+
if (actual !== expected.eq) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
if (actual !== expected) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
};
|
|
51
|
+
const resolveRankPartition = (index, where) => {
|
|
52
|
+
const partitionBy = index.partitionBy ?? [];
|
|
53
|
+
const requested = where ?? {};
|
|
54
|
+
for (const key of Object.keys(requested)) {
|
|
55
|
+
if (key === "AND" || key === "OR" || key === "NOT") {
|
|
56
|
+
return void 0;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const resolved = {};
|
|
60
|
+
for (const field of partitionBy) {
|
|
61
|
+
if (field in requested) {
|
|
62
|
+
const value = resolvePartitionValue(requested[field]);
|
|
63
|
+
if (value === NOT_RESOLVABLE) {
|
|
64
|
+
return void 0;
|
|
65
|
+
}
|
|
66
|
+
resolved[field] = value;
|
|
67
|
+
} else if (index.where && field in index.where) {
|
|
68
|
+
resolved[field] = index.where[field];
|
|
69
|
+
} else {
|
|
70
|
+
return void 0;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return resolved;
|
|
74
|
+
};
|
|
75
|
+
const rankTableName = (table, indexName) => `${table}__rank_${indexName}`;
|
|
76
|
+
const rankKeyFromDocument = (index, document_) => {
|
|
77
|
+
return {
|
|
78
|
+
partitionKey: encodePartitionKey(index.partitionBy ?? [], document_),
|
|
79
|
+
rowId: document_["_id"],
|
|
80
|
+
// Serialize each sort value the same way `syncRankIndexEntry` writes the
|
|
81
|
+
// stored `__sort_k<i>__` column, so a peer shard's comparison matches
|
|
82
|
+
// byte-for-byte. This also makes the values JSON-safe for the
|
|
83
|
+
// cross-shard RPC wire — `serializeSqlValue` turns bigint/Date/object
|
|
84
|
+
// into a string|number|null, where raw bigint would crash JSON.stringify
|
|
85
|
+
// and a raw Date would serialize to a different shape than the store.
|
|
86
|
+
// eslint-disable-next-line unicorn/no-null -- mirrors syncRankIndexEntry: a missing field serializes via `?? null` so the stored and wire bytes agree
|
|
87
|
+
sortValues: index.sortBy.map((key) => serializeSqlValue(document_[key.field] ?? null))
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export { RANK_TIEBREAK, encodePartitionKey, matchesRankStaticWhere, rankKeyFromDocument as rankKeyFromDoc, rankTableName, resolveRankPartition, sortColumnName };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
const RLS_UNWRAP_SYMBOL = /* @__PURE__ */ Symbol.for("lunora.ctxdb.rls-unwrap");
|
|
2
|
+
class RlsRequiredError extends Error {
|
|
3
|
+
code = "RLS_REQUIRED";
|
|
4
|
+
status = 403;
|
|
5
|
+
table;
|
|
6
|
+
constructor(table) {
|
|
7
|
+
super(
|
|
8
|
+
`ctx.db access to "${table}" is denied: the schema is marked .rls("required"), so this table is protected. Apply RLS with .use(rls(policies)) in the procedure, or mark the table .public() to opt it out.`
|
|
9
|
+
);
|
|
10
|
+
this.name = "RlsRequiredError";
|
|
11
|
+
this.table = table;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
const guardWriter = (raw, schema, tableOfId) => {
|
|
15
|
+
if (schema.rlsMode !== "required") {
|
|
16
|
+
return raw;
|
|
17
|
+
}
|
|
18
|
+
const base = raw;
|
|
19
|
+
const isProtected = (tableName) => {
|
|
20
|
+
const definition = schema.tables[tableName];
|
|
21
|
+
return definition !== void 0 && definition.isPublic !== true;
|
|
22
|
+
};
|
|
23
|
+
const guardTable = (tableName) => {
|
|
24
|
+
if (isProtected(tableName)) {
|
|
25
|
+
throw new RlsRequiredError(tableName);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const guardById = async (id, expectedTable) => {
|
|
29
|
+
if (expectedTable !== void 0) {
|
|
30
|
+
guardTable(expectedTable);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const tableName = await tableOfId(id);
|
|
34
|
+
if (tableName !== void 0) {
|
|
35
|
+
guardTable(tableName);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const baseRankBefore = base.rankBefore;
|
|
39
|
+
const guarded = {
|
|
40
|
+
...raw,
|
|
41
|
+
[RLS_UNWRAP_SYMBOL]: raw,
|
|
42
|
+
aggregate: (tableName, options) => {
|
|
43
|
+
guardTable(tableName);
|
|
44
|
+
return base.aggregate(tableName, options);
|
|
45
|
+
},
|
|
46
|
+
count: (tableName, whereOrArgs) => {
|
|
47
|
+
guardTable(tableName);
|
|
48
|
+
return base.count(tableName, whereOrArgs);
|
|
49
|
+
},
|
|
50
|
+
delete: async (id, expectedTable, options) => {
|
|
51
|
+
await guardById(id, expectedTable);
|
|
52
|
+
return base.delete(id, expectedTable, options);
|
|
53
|
+
},
|
|
54
|
+
deleteMany: async (ids, options, expectedTable) => {
|
|
55
|
+
for (const id of ids) {
|
|
56
|
+
await guardById(id, expectedTable);
|
|
57
|
+
}
|
|
58
|
+
return base.deleteMany(ids, options, expectedTable);
|
|
59
|
+
},
|
|
60
|
+
findFirst: (tableName, args) => {
|
|
61
|
+
guardTable(tableName);
|
|
62
|
+
return base.findFirst(tableName, args);
|
|
63
|
+
},
|
|
64
|
+
findFirstOrThrow: (tableName, args) => {
|
|
65
|
+
guardTable(tableName);
|
|
66
|
+
return base.findFirstOrThrow(tableName, args);
|
|
67
|
+
},
|
|
68
|
+
findMany: (tableName, args) => {
|
|
69
|
+
guardTable(tableName);
|
|
70
|
+
return base.findMany(tableName, args);
|
|
71
|
+
},
|
|
72
|
+
get: async (id, expectedTable) => {
|
|
73
|
+
await guardById(id, expectedTable);
|
|
74
|
+
return base.get(id, expectedTable);
|
|
75
|
+
},
|
|
76
|
+
groupBy: (tableName, options) => {
|
|
77
|
+
guardTable(tableName);
|
|
78
|
+
return base.groupBy(tableName, options);
|
|
79
|
+
},
|
|
80
|
+
insert: (tableName, document, options) => {
|
|
81
|
+
guardTable(tableName);
|
|
82
|
+
return base.insert(tableName, document, options);
|
|
83
|
+
},
|
|
84
|
+
insertMany: (tableName, documents, options) => {
|
|
85
|
+
guardTable(tableName);
|
|
86
|
+
return base.insertMany(tableName, documents, options);
|
|
87
|
+
},
|
|
88
|
+
insertManyUnsafe: (tableName, documents, options) => {
|
|
89
|
+
guardTable(tableName);
|
|
90
|
+
return base.insertManyUnsafe(tableName, documents, options);
|
|
91
|
+
},
|
|
92
|
+
patch: async (id, patch, expectedTable) => {
|
|
93
|
+
await guardById(id, expectedTable);
|
|
94
|
+
return base.patch(id, patch, expectedTable);
|
|
95
|
+
},
|
|
96
|
+
patchMany: async (patches, options, expectedTable) => {
|
|
97
|
+
for (const entry of patches) {
|
|
98
|
+
await guardById(entry.id, expectedTable);
|
|
99
|
+
}
|
|
100
|
+
return base.patchMany(patches, options, expectedTable);
|
|
101
|
+
},
|
|
102
|
+
query: (tableName) => {
|
|
103
|
+
guardTable(tableName);
|
|
104
|
+
return base.query(tableName);
|
|
105
|
+
},
|
|
106
|
+
rank: (tableName, indexName, options) => {
|
|
107
|
+
guardTable(tableName);
|
|
108
|
+
return base.rank(tableName, indexName, options);
|
|
109
|
+
},
|
|
110
|
+
rankPage: (tableName, indexName, options) => {
|
|
111
|
+
guardTable(tableName);
|
|
112
|
+
return base.rankPage(tableName, indexName, options);
|
|
113
|
+
},
|
|
114
|
+
replace: async (id, document, expectedTable) => {
|
|
115
|
+
await guardById(id, expectedTable);
|
|
116
|
+
return base.replace(id, document, expectedTable);
|
|
117
|
+
},
|
|
118
|
+
restore: async (id, expectedTable) => {
|
|
119
|
+
await guardById(id, expectedTable);
|
|
120
|
+
return base.restore?.(id, expectedTable);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
if (baseRankBefore) {
|
|
124
|
+
guarded["rankBefore"] = (tableName, indexName, options) => {
|
|
125
|
+
guardTable(tableName);
|
|
126
|
+
return baseRankBefore(tableName, indexName, options);
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
return guarded;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export { RLS_UNWRAP_SYMBOL, RlsRequiredError, guardWriter };
|