clivly 0.24.0 → 0.25.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 +34 -7
- package/{app-id-DcQ4u1c8.cjs → app-id-C2F25WhK.cjs} +1 -1
- package/{app-id-5cB6yioz.mjs → app-id-CMMadyIC.mjs} +1 -1
- package/auth-better-auth.cjs +51 -0
- package/auth-better-auth.d.cts +51 -1
- package/auth-better-auth.d.mts +51 -1
- package/auth-better-auth.mjs +51 -1
- package/auth-clerk.cjs +53 -0
- package/auth-clerk.d.cts +12 -1
- package/auth-clerk.d.mts +12 -1
- package/auth-clerk.mjs +53 -1
- package/auth-supabase.cjs +10 -0
- package/auth-supabase.d.cts +17 -1
- package/auth-supabase.d.mts +17 -1
- package/auth-supabase.mjs +10 -1
- package/auth-webhook-3if7gY6b.mjs +162 -0
- package/auth-webhook-BihEUc9C.cjs +197 -0
- package/auth-webhook-DKdaCZrW.d.cts +30 -0
- package/auth-webhook-DKdaCZrW.d.mts +30 -0
- package/auth-workos.cjs +43 -0
- package/auth-workos.d.cts +12 -1
- package/auth-workos.d.mts +12 -1
- package/auth-workos.mjs +43 -1
- package/drizzle.cjs +269 -17
- package/drizzle.d.cts +23 -3
- package/drizzle.d.mts +23 -3
- package/drizzle.mjs +270 -19
- package/{from-sql-CFgXkCe2.cjs → from-sql-Dw2ipynp.cjs} +7 -1
- package/{from-sql-S8dSvU-e.d.cts → from-sql-MyPE5elx.d.cts} +1 -1
- package/{from-sql-CpGP7K3J.d.mts → from-sql-YW6LfdVK.d.mts} +1 -1
- package/{from-sql-BD7gR4bC.mjs → from-sql-kqs9CbQ-.mjs} +7 -1
- package/{index-CIupXz_w.d.cts → index-B4kWWHYm.d.cts} +2 -2
- package/{index-BoemTWH4.d.mts → index-CKElWhvq.d.mts} +2 -2
- package/index.cjs +3354 -2829
- package/index.d.cts +3 -3
- package/index.d.mts +3 -3
- package/index.mjs +3354 -2829
- package/{introspector-5w2x89RD.mjs → introspector-B1S7qmvy.mjs} +4 -4
- package/{introspector-DT_Ma59k.cjs → introspector-DRmEXDai.cjs} +4 -4
- package/kysely.cjs +163 -2
- package/kysely.d.cts +16 -3
- package/kysely.d.mts +16 -3
- package/kysely.mjs +163 -3
- package/{namespace-probe-CUR2zG2l.d.mts → namespace-probe-LKC4OU5X.d.mts} +54 -19
- package/package.json +1 -1
- package/{projection-guard-DOt_nbGn.mjs → projection-guard-CADdAdzg.mjs} +1 -1
- package/{projection-guard-C70l5U24.cjs → projection-guard-yhVTvx37.cjs} +1 -1
- package/sdk.cjs +1 -1
- package/sdk.d.cts +1 -1
- package/sdk.d.mts +1 -1
- package/sdk.mjs +1 -1
- package/sql.cjs +5 -5
- package/sql.d.cts +2 -2
- package/sql.d.mts +2 -2
- package/sql.mjs +5 -5
- package/{src-CZIdwwI2.cjs → src-BQasv4EL.cjs} +368 -38
- package/{src-tFhC1Ru2.mjs → src-CAn-zrEl.mjs} +368 -38
- package/{supabase-contact-BXQ0nnLq.cjs → supabase-contact-0GmBVpiQ.cjs} +24 -15
- package/{supabase-contact-3ke1KKUs.mjs → supabase-contact-D-oDC4Qk.mjs} +24 -15
- package/supabase.cjs +39 -9
- package/supabase.d.cts +3 -2
- package/supabase.d.mts +3 -2
- package/supabase.mjs +39 -9
- package/{sync-Q0O-2kFI.mjs → sync-CnWT5juF.mjs} +65 -5
- package/{sync-CdA9dgZz.cjs → sync-R0iVlooB.cjs} +94 -4
- package/track-writes-4yyZbTZX.d.cts +17 -0
- package/track-writes-BJSWg8wg.d.mts +17 -0
- package/track-writes-BnZAig2Y.mjs +77 -0
- package/track-writes-C22_e_W-.cjs +112 -0
- package/{types-CzL7lUH9.d.cts → types-BPG4l-oM.d.cts} +53 -18
- package/vite.d.cts +1 -1
- package/vite.d.mts +1 -1
package/drizzle.cjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_drizzle = require("./drizzle-C0bfX-6a.cjs");
|
|
3
|
-
const require_sync = require("./sync-
|
|
3
|
+
const require_sync = require("./sync-R0iVlooB.cjs");
|
|
4
4
|
const require_projection = require("./projection-w4Z6vSL2.cjs");
|
|
5
5
|
const require_projection_sql = require("./projection-sql-DS5sVTY-.cjs");
|
|
6
|
-
const require_from_sql = require("./from-sql-
|
|
7
|
-
const
|
|
6
|
+
const require_from_sql = require("./from-sql-Dw2ipynp.cjs");
|
|
7
|
+
const require_track_writes = require("./track-writes-C22_e_W-.cjs");
|
|
8
|
+
const require_app_id = require("./app-id-C2F25WhK.cjs");
|
|
8
9
|
let drizzle_orm = require("drizzle-orm");
|
|
9
10
|
let drizzle_orm_pg_core = require("drizzle-orm/pg-core");
|
|
10
11
|
// ../sdk/src/drizzle-keyset.ts
|
|
@@ -68,7 +69,7 @@ function normalizeRows(result) {
|
|
|
68
69
|
throw new Error("fromDrizzleProjection: db.execute() returned neither an array of rows nor { rows }.");
|
|
69
70
|
}
|
|
70
71
|
function fromDrizzleProjection(db, projection, opts) {
|
|
71
|
-
const client = db;
|
|
72
|
+
const client = require_track_writes.untracked(db);
|
|
72
73
|
const known = /* @__PURE__ */ new Set([require_projection.BASE_REF, ...projection.joins.map((j) => j.key)]);
|
|
73
74
|
if (projection.identity.kind === "base_pk" && projection.identity.column === void 0 && opts.basePkColumn === void 0) throw new Error(`fromDrizzleProjection: projection "${projection.entityKey}" has a base_pk identity with no column — pass { basePkColumn }.`);
|
|
74
75
|
for (const [field, binding] of Object.entries(projection.fieldBindings)) assertKnownRef(binding.from, known, `field "${field}"`);
|
|
@@ -92,7 +93,7 @@ function fromDrizzleProjection(db, projection, opts) {
|
|
|
92
93
|
const head = [`select ${selectItems.join(", ")}`, fromClause].join(" ");
|
|
93
94
|
const idSql = drizzle_orm.sql.raw(identityExpr);
|
|
94
95
|
const page = async (where, order, limit) => normalizeRows(await client.execute(drizzle_orm.sql`${drizzle_orm.sql.raw(head)} where ${where} order by ${order} limit ${limit}`));
|
|
95
|
-
return {
|
|
96
|
+
return require_sync.withFetchByIds({
|
|
96
97
|
entity: opts.entity,
|
|
97
98
|
pushSource: projection.baseTable,
|
|
98
99
|
cursorField: cursorToken,
|
|
@@ -105,7 +106,7 @@ function fromDrizzleProjection(db, projection, opts) {
|
|
|
105
106
|
}, page));
|
|
106
107
|
return require_from_sql.hideCursorAlias(rows);
|
|
107
108
|
}
|
|
108
|
-
};
|
|
109
|
+
}, async (ids) => require_from_sql.hideCursorAlias(await page(drizzle_orm.sql`${idSql} in (${drizzle_orm.sql.join(ids.map((id) => drizzle_orm.sql`${id}`), drizzle_orm.sql`, `)})`, drizzle_orm.sql`${idSql}`, ids.length)));
|
|
109
110
|
}
|
|
110
111
|
/**
|
|
111
112
|
* The identity-only paging path `fromDrizzleProjection` takes when
|
|
@@ -124,7 +125,7 @@ function fromDrizzleProjectionByIdentity(client, projection, identityExpr, fromC
|
|
|
124
125
|
selectItems.push(`${refSql(binding.from)} AS ${require_projection_sql.quoteIdent(token)}`);
|
|
125
126
|
}
|
|
126
127
|
const head = [`select ${selectItems.join(", ")}`, fromClause].join(" ");
|
|
127
|
-
return {
|
|
128
|
+
return require_sync.withFetchByIds({
|
|
128
129
|
entity: opts.entity,
|
|
129
130
|
pushSource: projection.baseTable,
|
|
130
131
|
cursorField: "id",
|
|
@@ -136,7 +137,7 @@ function fromDrizzleProjectionByIdentity(client, projection, identityExpr, fromC
|
|
|
136
137
|
const query = drizzle_orm.sql`${drizzle_orm.sql.raw(head)}${where} order by ${drizzle_orm.sql.raw(identityExpr)} asc limit ${limit}`;
|
|
137
138
|
return normalizeRows(await client.execute(query));
|
|
138
139
|
}
|
|
139
|
-
};
|
|
140
|
+
}, async (ids) => normalizeRows(await client.execute(drizzle_orm.sql`${drizzle_orm.sql.raw(head)} where ${drizzle_orm.sql.raw(identityExpr)} in (${drizzle_orm.sql.join(ids.map((id) => drizzle_orm.sql`${id}`), drizzle_orm.sql`, `)}) limit ${ids.length}`)));
|
|
140
141
|
}
|
|
141
142
|
// ../sdk/src/projection-preview.ts
|
|
142
143
|
/** `db.execute` returns either an array or `{ rows }`, depending on driver. */
|
|
@@ -147,10 +148,253 @@ function rowsOf(result) {
|
|
|
147
148
|
throw new Error("previewProjection: db.execute() returned neither an array of rows nor { rows }.");
|
|
148
149
|
}
|
|
149
150
|
async function previewProjection(db, projection, opts) {
|
|
150
|
-
const client = db;
|
|
151
|
+
const client = require_track_writes.untracked(db);
|
|
151
152
|
const { sql } = await import("drizzle-orm");
|
|
152
153
|
return rowsOf(await client.execute(sql.raw(require_projection_sql.compileProjectionPreview(projection, opts.limit))));
|
|
153
154
|
}
|
|
155
|
+
// ../sdk/src/track-drizzle.ts
|
|
156
|
+
const noop = () => void 0;
|
|
157
|
+
function safely(look) {
|
|
158
|
+
try {
|
|
159
|
+
return look();
|
|
160
|
+
} catch {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const WRITES = /* @__PURE__ */ new Set([
|
|
165
|
+
"insert",
|
|
166
|
+
"update",
|
|
167
|
+
"delete"
|
|
168
|
+
]);
|
|
169
|
+
const USES_DATABASE = /* @__PURE__ */ new Set([
|
|
170
|
+
"select",
|
|
171
|
+
"selectDistinct",
|
|
172
|
+
"selectDistinctOn",
|
|
173
|
+
"query",
|
|
174
|
+
"execute",
|
|
175
|
+
"$count"
|
|
176
|
+
]);
|
|
177
|
+
const PUBLIC_SCHEMA = "public";
|
|
178
|
+
function tableLabel(table) {
|
|
179
|
+
const name = (0, drizzle_orm.getTableName)(table);
|
|
180
|
+
try {
|
|
181
|
+
const { schema } = (0, drizzle_orm_pg_core.getTableConfig)(table);
|
|
182
|
+
return schema && schema !== PUBLIC_SCHEMA ? `${schema}.${name}` : name;
|
|
183
|
+
} catch {
|
|
184
|
+
return name;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function keyOf(table) {
|
|
188
|
+
const keys = Object.entries((0, drizzle_orm.getTableColumns)(table)).filter(([, column]) => column.primary);
|
|
189
|
+
const [only] = keys;
|
|
190
|
+
return only && keys.length === 1 ? {
|
|
191
|
+
property: only[0],
|
|
192
|
+
column: only[1]
|
|
193
|
+
} : null;
|
|
194
|
+
}
|
|
195
|
+
const chunkText = (chunk) => (0, drizzle_orm.is)(chunk, drizzle_orm.StringChunk) ? [chunk.value].flat().join("") : "";
|
|
196
|
+
function idsOfParams(params) {
|
|
197
|
+
const ids = [];
|
|
198
|
+
for (const param of params) {
|
|
199
|
+
const id = require_track_writes.trackedId((0, drizzle_orm.is)(param, drizzle_orm.Param) ? param.value : param);
|
|
200
|
+
if (id === null) return null;
|
|
201
|
+
ids.push(id);
|
|
202
|
+
}
|
|
203
|
+
return ids;
|
|
204
|
+
}
|
|
205
|
+
/** The ids of exactly `key = x` or `key in (x, y)`; null for anything else. */
|
|
206
|
+
function idsOfWhere(where, key) {
|
|
207
|
+
if (!(0, drizzle_orm.is)(where, drizzle_orm.SQL)) return null;
|
|
208
|
+
const chunks = where.queryChunks.filter((chunk) => !((0, drizzle_orm.is)(chunk, drizzle_orm.StringChunk) && chunkText(chunk) === ""));
|
|
209
|
+
const [left, operator, right] = chunks;
|
|
210
|
+
if (chunks.length !== 3 || left !== key.column) return null;
|
|
211
|
+
const op = chunkText(operator).trim();
|
|
212
|
+
if (op === "=") return idsOfParams([right]);
|
|
213
|
+
return op === "in" && Array.isArray(right) ? idsOfParams(right) : null;
|
|
214
|
+
}
|
|
215
|
+
const sqlText = (part) => (0, drizzle_orm.is)(part, drizzle_orm.SQL) ? part.queryChunks.map(sqlText).join("") : chunkText(part);
|
|
216
|
+
/** Whether a conflict can only touch the row being inserted. */
|
|
217
|
+
function conflictKeepsId(onConflict, key) {
|
|
218
|
+
if (onConflict === void 0) return true;
|
|
219
|
+
const text = sqlText(onConflict);
|
|
220
|
+
return text.startsWith(`("${key.column.name}")`) || text.includes("do nothing");
|
|
221
|
+
}
|
|
222
|
+
function idsOfValues(values, key) {
|
|
223
|
+
return Array.isArray(values) ? idsOfParams(values.map((row) => row[key.property])) : null;
|
|
224
|
+
}
|
|
225
|
+
/** The ids the statement returned, under whatever name the host asked for. */
|
|
226
|
+
function idsOfResult(builder, result, key) {
|
|
227
|
+
const path = builder.config?.returning?.find(({ field }) => field === key.column)?.path;
|
|
228
|
+
if (!(path && Array.isArray(result))) return null;
|
|
229
|
+
const ids = [];
|
|
230
|
+
for (const row of result) {
|
|
231
|
+
let value = row;
|
|
232
|
+
for (const step of path) value = value?.[step];
|
|
233
|
+
const id = require_track_writes.trackedId(value);
|
|
234
|
+
if (id === null) return null;
|
|
235
|
+
ids.push(id);
|
|
236
|
+
}
|
|
237
|
+
return ids;
|
|
238
|
+
}
|
|
239
|
+
function idsOfStatement(kind, { config }, key) {
|
|
240
|
+
if (kind === "insert") return conflictKeepsId(config?.onConflict, key) ? idsOfValues(config?.values, key) : null;
|
|
241
|
+
return kind === "update" && config?.set?.[key.property] !== void 0 ? null : idsOfWhere(config?.where, key);
|
|
242
|
+
}
|
|
243
|
+
const watched = /* @__PURE__ */ new WeakMap();
|
|
244
|
+
function fixGeneratedIds(builder, key) {
|
|
245
|
+
const { config } = builder;
|
|
246
|
+
const { defaultFn } = key?.column ?? {};
|
|
247
|
+
const values = config?.values;
|
|
248
|
+
if (!(config && key && defaultFn && Array.isArray(values))) return () => void 0;
|
|
249
|
+
config.values = values.map((row) => {
|
|
250
|
+
const given = row[key.property];
|
|
251
|
+
if (!(given === void 0 || (0, drizzle_orm.is)(given, drizzle_orm.Param) && given.value === void 0)) return row;
|
|
252
|
+
const id = defaultFn();
|
|
253
|
+
return {
|
|
254
|
+
...row,
|
|
255
|
+
[key.property]: (0, drizzle_orm.is)(id, drizzle_orm.SQL) ? id : new drizzle_orm.Param(id, key.column)
|
|
256
|
+
};
|
|
257
|
+
});
|
|
258
|
+
return () => {
|
|
259
|
+
config.values = values;
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
function watchBuilder(builder, kind, table, sink) {
|
|
263
|
+
const key = safely(() => keyOf(table));
|
|
264
|
+
let ids = null;
|
|
265
|
+
const prepare = builder._prepare;
|
|
266
|
+
if (typeof prepare !== "function") return;
|
|
267
|
+
builder._prepare = (...args) => {
|
|
268
|
+
const undo = safely(() => fixGeneratedIds(builder, key)) ?? noop;
|
|
269
|
+
ids = key ? safely(() => idsOfStatement(kind, builder, key)) : null;
|
|
270
|
+
try {
|
|
271
|
+
return prepare.apply(builder, args);
|
|
272
|
+
} finally {
|
|
273
|
+
safely(undo);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const entry = { done(result) {
|
|
277
|
+
const known = (key && safely(() => idsOfResult(builder, result, key))) ?? ids;
|
|
278
|
+
return {
|
|
279
|
+
table: tableLabel(table),
|
|
280
|
+
key: key?.column.name ?? "",
|
|
281
|
+
deleted: kind === "delete",
|
|
282
|
+
ids: known ?? [],
|
|
283
|
+
complete: known !== null
|
|
284
|
+
};
|
|
285
|
+
} };
|
|
286
|
+
watched.set(builder, entry);
|
|
287
|
+
const { execute } = builder;
|
|
288
|
+
builder.execute = async (...args) => {
|
|
289
|
+
const result = await execute.apply(builder, args);
|
|
290
|
+
try {
|
|
291
|
+
sink(entry.done(result));
|
|
292
|
+
} catch {}
|
|
293
|
+
return result;
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
const isBuilder = (value) => typeof value?.execute === "function";
|
|
297
|
+
/** `db.insert` / `update` / `delete`, watching the builder each produces. */
|
|
298
|
+
function trackWriteMethod(kind, make, sink) {
|
|
299
|
+
return (...args) => {
|
|
300
|
+
const started = make(...args);
|
|
301
|
+
const table = args[0];
|
|
302
|
+
if (isBuilder(started)) {
|
|
303
|
+
safely(() => watchBuilder(started, kind, table, sink));
|
|
304
|
+
return started;
|
|
305
|
+
}
|
|
306
|
+
for (const step of [
|
|
307
|
+
"values",
|
|
308
|
+
"select",
|
|
309
|
+
"set"
|
|
310
|
+
]) {
|
|
311
|
+
const original = started?.[step];
|
|
312
|
+
if (typeof original === "function") started[step] = (...stepArgs) => {
|
|
313
|
+
const next = original.apply(started, stepArgs);
|
|
314
|
+
if (isBuilder(next)) safely(() => watchBuilder(next, kind, table, sink));
|
|
315
|
+
return next;
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
return started;
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function trackedWith(parts, sink) {
|
|
322
|
+
const out = { ...parts };
|
|
323
|
+
for (const kind of [
|
|
324
|
+
"insert",
|
|
325
|
+
"update",
|
|
326
|
+
"delete"
|
|
327
|
+
]) {
|
|
328
|
+
const make = out[kind];
|
|
329
|
+
if (typeof make === "function") out[kind] = trackWriteMethod(kind, make, sink);
|
|
330
|
+
}
|
|
331
|
+
return out;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* A callable that is also an object, such as the postgres.js client behind
|
|
335
|
+
* `$client` with its `end()`. Wrapping it would lose everything but the call.
|
|
336
|
+
*/
|
|
337
|
+
const carriesMembers = (value) => Object.keys(value).length > 0;
|
|
338
|
+
function trackClient(db, reporter, emit) {
|
|
339
|
+
const sink = (write) => emit([write]);
|
|
340
|
+
const transaction = (original) => async (work, ...rest) => {
|
|
341
|
+
const held = [];
|
|
342
|
+
const result = await original((tx) => work(trackClient(tx, reporter, (writes) => held.push(...writes))), ...rest);
|
|
343
|
+
emit(held);
|
|
344
|
+
return result;
|
|
345
|
+
};
|
|
346
|
+
const batch = (original) => async (queries, ...rest) => {
|
|
347
|
+
const results = await original(queries, ...rest);
|
|
348
|
+
try {
|
|
349
|
+
emit(queries.flatMap((query, i) => {
|
|
350
|
+
const entry = watched.get(query);
|
|
351
|
+
return entry ? [entry.done(results[i])] : [];
|
|
352
|
+
}));
|
|
353
|
+
} catch {}
|
|
354
|
+
return results;
|
|
355
|
+
};
|
|
356
|
+
return new Proxy(db, { get(target, property) {
|
|
357
|
+
const value = Reflect.get(target, property, target);
|
|
358
|
+
if (USES_DATABASE.has(property) || WRITES.has(property)) reporter.noteActivity();
|
|
359
|
+
if (typeof value !== "function" || carriesMembers(value)) return value;
|
|
360
|
+
const call = (...args) => value.apply(target, args);
|
|
361
|
+
if (WRITES.has(property)) return trackWriteMethod(property, call, sink);
|
|
362
|
+
switch (property) {
|
|
363
|
+
case "transaction": return transaction(call);
|
|
364
|
+
case "batch": return batch(call);
|
|
365
|
+
case "with": return (...args) => trackedWith(call(...args), sink);
|
|
366
|
+
case "constructor": return value;
|
|
367
|
+
default: return call;
|
|
368
|
+
}
|
|
369
|
+
} });
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Wrap a Drizzle client once, and every committed insert, update and delete it
|
|
373
|
+
* makes to a mapped table reaches Clivly right away.
|
|
374
|
+
*
|
|
375
|
+
* ```ts
|
|
376
|
+
* export const db = trackDrizzle(drizzle(process.env.DATABASE_URL!));
|
|
377
|
+
* ```
|
|
378
|
+
*
|
|
379
|
+
* The SQL, the results and the errors are the host's own: nothing is added to
|
|
380
|
+
* a query, so a policy or grant that allows the write today still does. A
|
|
381
|
+
* transaction is reported after it commits; one that rolls back is not. Raw
|
|
382
|
+
* SQL (`db.execute`) and `.prepare()`d statements are not seen, and reach
|
|
383
|
+
* Clivly with the next catch-up or pull.
|
|
384
|
+
*
|
|
385
|
+
* It finds the SDK your app creates, so the database module need not import
|
|
386
|
+
* your Clivly config. Wrapping twice is harmless.
|
|
387
|
+
*/
|
|
388
|
+
function trackDrizzle(db, options = {}) {
|
|
389
|
+
if (require_track_writes.isTracked(db)) return db;
|
|
390
|
+
const reporter = require_track_writes.createReporter(options);
|
|
391
|
+
const tracked = trackClient(db, reporter, (writes) => {
|
|
392
|
+
reporter.noteActivity();
|
|
393
|
+
reporter.report(writes);
|
|
394
|
+
});
|
|
395
|
+
require_track_writes.markTracked(tracked, db);
|
|
396
|
+
return tracked;
|
|
397
|
+
}
|
|
154
398
|
// ../sdk/src/drizzle.ts
|
|
155
399
|
function inferIdField(table) {
|
|
156
400
|
const columns = (0, drizzle_orm.getTableColumns)(table);
|
|
@@ -163,6 +407,10 @@ function inferIdField(table) {
|
|
|
163
407
|
const kind = primaryKeys.length === 0 ? "no" : "a composite";
|
|
164
408
|
throw new Error(`fromDrizzle: table "${(0, drizzle_orm.getTableName)(table)}" has ${kind} single-column primary key — pass { idField } explicitly.`);
|
|
165
409
|
}
|
|
410
|
+
function singlePrimaryKeyOrId(table) {
|
|
411
|
+
const primaryKeys = Object.entries((0, drizzle_orm.getTableColumns)(table)).filter(([, column]) => column.primary);
|
|
412
|
+
return primaryKeys.length === 1 ? primaryKeys[0]?.[0] ?? "id" : "id";
|
|
413
|
+
}
|
|
166
414
|
const AUTH_USERS_ALLOWED = require_sync.AUTH_USERS_COLUMNS;
|
|
167
415
|
const META_COLUMN = "raw_user_meta_data";
|
|
168
416
|
const { quoteIdent } = require_from_sql.postgresDialect;
|
|
@@ -222,7 +470,7 @@ function exactRead(column) {
|
|
|
222
470
|
return WITH_TIME_ZONE.test(type) ? drizzle_orm.sql`to_char(${column} at time zone 'UTC', ${format})` : drizzle_orm.sql`to_char(${column}, ${format})`;
|
|
223
471
|
}
|
|
224
472
|
function fromDrizzle(db, table, opts) {
|
|
225
|
-
const drizzleDb = db;
|
|
473
|
+
const drizzleDb = require_track_writes.untracked(db);
|
|
226
474
|
const idField = opts.idField ?? inferIdField(table);
|
|
227
475
|
const { columns, isAuthUsers } = readableColumns(table, {
|
|
228
476
|
cursorField: opts.cursorField,
|
|
@@ -236,6 +484,7 @@ function fromDrizzle(db, table, opts) {
|
|
|
236
484
|
for (const column of Object.values(columns)) projection[column.name] = isAuthUsers && column.name === META_COLUMN ? drizzle_orm.sql.raw(require_from_sql.narrowedMetaValue(quoteIdent(META_COLUMN))).mapWith(column) : column;
|
|
237
485
|
const cursorColumnName = cursorColumn.name;
|
|
238
486
|
const idColumnName = idColumn.name;
|
|
487
|
+
require_sync.assertNoStrayIdColumn("fromDrizzle", (0, drizzle_orm.getTableName)(table), Object.values(columns).map((column) => column.name), idColumnName);
|
|
239
488
|
const selected = {
|
|
240
489
|
...projection,
|
|
241
490
|
[require_from_sql.CURSOR_ALIAS]: exactRead(cursorColumn)
|
|
@@ -253,6 +502,7 @@ function fromDrizzle(db, table, opts) {
|
|
|
253
502
|
}, page))),
|
|
254
503
|
...opts.objectType === void 0 ? {} : { objectType: opts.objectType }
|
|
255
504
|
};
|
|
505
|
+
require_sync.withFetchByIds(source, async (ids) => require_from_sql.hideCursorAlias(await page(drizzle_orm.sql`${idColumn} in (${drizzle_orm.sql.join(ids.map((id) => drizzle_orm.sql`${id}`), drizzle_orm.sql`, `)})`, drizzle_orm.sql`${idColumn}`, ids.length)));
|
|
256
506
|
const uniqueConstraints = require_drizzle.buildUniqueConstraints(table);
|
|
257
507
|
const meta = {
|
|
258
508
|
columnKeys: Object.keys(columns),
|
|
@@ -320,7 +570,7 @@ function toColumnKeyedRow(table, row) {
|
|
|
320
570
|
return out;
|
|
321
571
|
}
|
|
322
572
|
function insertAndPush(clivly, db, table, opts) {
|
|
323
|
-
const insertableDb = db;
|
|
573
|
+
const insertableDb = require_track_writes.untracked(db);
|
|
324
574
|
return async (values, waitUntil) => {
|
|
325
575
|
const [created] = await insertableDb.insert(table).values(values).returning();
|
|
326
576
|
if (!created) throw new Error(`insertAndPush: insert into "${(0, drizzle_orm.getTableName)(table)}" returned no row.`);
|
|
@@ -344,7 +594,7 @@ const APP_ID_TABLE = require_app_id.APP_ID_TABLE;
|
|
|
344
594
|
* your drizzle config so push does not offer to drop the table.
|
|
345
595
|
*/
|
|
346
596
|
function drizzleAppIdStore(db) {
|
|
347
|
-
const client = db;
|
|
597
|
+
const client = require_track_writes.untracked(db);
|
|
348
598
|
const table = drizzle_orm.sql.identifier(APP_ID_TABLE);
|
|
349
599
|
let ready = null;
|
|
350
600
|
const ensureTable = () => {
|
|
@@ -372,7 +622,7 @@ function drizzleAppIdStore(db) {
|
|
|
372
622
|
* (`current_schema()`), so it reports exactly the tables the app can reach.
|
|
373
623
|
*/
|
|
374
624
|
function drizzleIntrospector(db) {
|
|
375
|
-
const client = db;
|
|
625
|
+
const client = require_track_writes.untracked(db);
|
|
376
626
|
return {
|
|
377
627
|
async listTables() {
|
|
378
628
|
return (await client.execute(drizzle_orm.sql`
|
|
@@ -414,7 +664,7 @@ function settle(pushed, waitUntil) {
|
|
|
414
664
|
* ```
|
|
415
665
|
*/
|
|
416
666
|
function updateAndPush(clivly, db, table, opts) {
|
|
417
|
-
const updatableDb = db;
|
|
667
|
+
const updatableDb = require_track_writes.untracked(db);
|
|
418
668
|
return async (where, values, waitUntil) => {
|
|
419
669
|
const updated = await updatableDb.update(table).set(values).where(where).returning();
|
|
420
670
|
await settle(Promise.all(updated.map((row) => clivly.pushOne(opts.entity, toColumnKeyedRow(table, row)))).then(() => void 0), waitUntil);
|
|
@@ -425,7 +675,8 @@ function updateAndPush(clivly, db, table, opts) {
|
|
|
425
675
|
* Delete rows of a mapped Drizzle table and tell Clivly at once, so their CRM
|
|
426
676
|
* records are archived (never deleted) within a second (continuous sync D9).
|
|
427
677
|
* Without it, a deletion reaches Clivly at its daily delete check. `idField`
|
|
428
|
-
* is the property holding the row id; it defaults to
|
|
678
|
+
* is the property holding the row id; it defaults to the table's
|
|
679
|
+
* single-column primary key, else `id`.
|
|
429
680
|
*
|
|
430
681
|
* ```ts
|
|
431
682
|
* const deleteUser = deleteAndPush(clivly, db, schema.user, { entity: "user" });
|
|
@@ -433,8 +684,8 @@ function updateAndPush(clivly, db, table, opts) {
|
|
|
433
684
|
* ```
|
|
434
685
|
*/
|
|
435
686
|
function deleteAndPush(clivly, db, table, opts) {
|
|
436
|
-
const deletableDb = db;
|
|
437
|
-
const idField = opts.idField ??
|
|
687
|
+
const deletableDb = require_track_writes.untracked(db);
|
|
688
|
+
const idField = opts.idField ?? singlePrimaryKeyOrId(table);
|
|
438
689
|
return async (where, waitUntil) => {
|
|
439
690
|
const deleted = await deletableDb.delete(table).where(where).returning();
|
|
440
691
|
const ids = deleted.map((row) => row[idField]).filter((id) => id !== null && id !== void 0).map((id) => typeof id === "number" ? id : String(id));
|
|
@@ -452,4 +703,5 @@ exports.fromDrizzle = fromDrizzle;
|
|
|
452
703
|
exports.fromDrizzleProjection = fromDrizzleProjection;
|
|
453
704
|
exports.insertAndPush = insertAndPush;
|
|
454
705
|
exports.toColumnKeyedRow = toColumnKeyedRow;
|
|
706
|
+
exports.trackDrizzle = trackDrizzle;
|
|
455
707
|
exports.updateAndPush = updateAndPush;
|
package/drizzle.d.cts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { R as ProjectionMapping, z as ProjectionRef } from "./entity-config-CXpGowQa.cjs";
|
|
2
|
-
import {
|
|
2
|
+
import { t as AppIdStore, w as NamespaceIntrospector, x as SyncSource } from "./types-BPG4l-oM.cjs";
|
|
3
3
|
import { n as buildUniqueConstraints, r as discoverFromDrizzle } from "./drizzle-BNHGQXiY.cjs";
|
|
4
4
|
import "./index-uFDiQEjj.cjs";
|
|
5
|
+
import { t as TrackOptions } from "./track-writes-4yyZbTZX.cjs";
|
|
5
6
|
import { Table } from "drizzle-orm";
|
|
6
7
|
/**
|
|
7
8
|
* Build a `SyncSource` that actually executes a projection's joins.
|
|
@@ -66,6 +67,24 @@ export declare function fromDrizzleProjection(db: unknown, projection: Projectio
|
|
|
66
67
|
objectType: string;
|
|
67
68
|
};
|
|
68
69
|
export declare function fromDrizzleProjection(db: unknown, projection: ProjectionMapping, opts: FromDrizzleProjectionOptions): SyncSource;
|
|
70
|
+
/**
|
|
71
|
+
* Wrap a Drizzle client once, and every committed insert, update and delete it
|
|
72
|
+
* makes to a mapped table reaches Clivly right away.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* export const db = trackDrizzle(drizzle(process.env.DATABASE_URL!));
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* The SQL, the results and the errors are the host's own: nothing is added to
|
|
79
|
+
* a query, so a policy or grant that allows the write today still does. A
|
|
80
|
+
* transaction is reported after it commits; one that rolls back is not. Raw
|
|
81
|
+
* SQL (`db.execute`) and `.prepare()`d statements are not seen, and reach
|
|
82
|
+
* Clivly with the next catch-up or pull.
|
|
83
|
+
*
|
|
84
|
+
* It finds the SDK your app creates, so the database module need not import
|
|
85
|
+
* your Clivly config. Wrapping twice is harmless.
|
|
86
|
+
*/
|
|
87
|
+
export declare function trackDrizzle<DB extends object>(db: DB, options?: TrackOptions): DB;
|
|
69
88
|
interface DrizzleDb {
|
|
70
89
|
select: (...args: any[]) => any;
|
|
71
90
|
}
|
|
@@ -209,7 +228,8 @@ export declare function updateAndPush(clivly: PushOneCapable, db: unknown, table
|
|
|
209
228
|
* Delete rows of a mapped Drizzle table and tell Clivly at once, so their CRM
|
|
210
229
|
* records are archived (never deleted) within a second (continuous sync D9).
|
|
211
230
|
* Without it, a deletion reaches Clivly at its daily delete check. `idField`
|
|
212
|
-
* is the property holding the row id; it defaults to
|
|
231
|
+
* is the property holding the row id; it defaults to the table's
|
|
232
|
+
* single-column primary key, else `id`.
|
|
213
233
|
*
|
|
214
234
|
* ```ts
|
|
215
235
|
* const deleteUser = deleteAndPush(clivly, db, schema.user, { entity: "user" });
|
|
@@ -220,4 +240,4 @@ export declare function deleteAndPush(clivly: PushDeleteCapable, db: unknown, ta
|
|
|
220
240
|
entity: string;
|
|
221
241
|
idField?: string;
|
|
222
242
|
}): (where: unknown, waitUntil?: (promise: Promise<unknown>) => void) => Promise<Record<string, unknown>[]>;
|
|
223
|
-
export { type FromDrizzleProjectionOptions, buildUniqueConstraints, discoverFromDrizzle };
|
|
243
|
+
export { type FromDrizzleProjectionOptions, type TrackOptions, buildUniqueConstraints, discoverFromDrizzle };
|
package/drizzle.d.mts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { R as ProjectionMapping, z as ProjectionRef } from "./entity-config-CXpGowQa.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { S as SyncSource, n as AppIdStore, t as NamespaceIntrospector } from "./namespace-probe-LKC4OU5X.mjs";
|
|
3
3
|
import { n as buildUniqueConstraints, r as discoverFromDrizzle } from "./drizzle-DFe7ILmd.mjs";
|
|
4
4
|
import "./index-DOoX2GgI.mjs";
|
|
5
|
+
import { t as TrackOptions } from "./track-writes-BJSWg8wg.mjs";
|
|
5
6
|
import { Table } from "drizzle-orm";
|
|
6
7
|
/**
|
|
7
8
|
* Build a `SyncSource` that actually executes a projection's joins.
|
|
@@ -66,6 +67,24 @@ export declare function fromDrizzleProjection(db: unknown, projection: Projectio
|
|
|
66
67
|
objectType: string;
|
|
67
68
|
};
|
|
68
69
|
export declare function fromDrizzleProjection(db: unknown, projection: ProjectionMapping, opts: FromDrizzleProjectionOptions): SyncSource;
|
|
70
|
+
/**
|
|
71
|
+
* Wrap a Drizzle client once, and every committed insert, update and delete it
|
|
72
|
+
* makes to a mapped table reaches Clivly right away.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* export const db = trackDrizzle(drizzle(process.env.DATABASE_URL!));
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* The SQL, the results and the errors are the host's own: nothing is added to
|
|
79
|
+
* a query, so a policy or grant that allows the write today still does. A
|
|
80
|
+
* transaction is reported after it commits; one that rolls back is not. Raw
|
|
81
|
+
* SQL (`db.execute`) and `.prepare()`d statements are not seen, and reach
|
|
82
|
+
* Clivly with the next catch-up or pull.
|
|
83
|
+
*
|
|
84
|
+
* It finds the SDK your app creates, so the database module need not import
|
|
85
|
+
* your Clivly config. Wrapping twice is harmless.
|
|
86
|
+
*/
|
|
87
|
+
export declare function trackDrizzle<DB extends object>(db: DB, options?: TrackOptions): DB;
|
|
69
88
|
interface DrizzleDb {
|
|
70
89
|
select: (...args: any[]) => any;
|
|
71
90
|
}
|
|
@@ -209,7 +228,8 @@ export declare function updateAndPush(clivly: PushOneCapable, db: unknown, table
|
|
|
209
228
|
* Delete rows of a mapped Drizzle table and tell Clivly at once, so their CRM
|
|
210
229
|
* records are archived (never deleted) within a second (continuous sync D9).
|
|
211
230
|
* Without it, a deletion reaches Clivly at its daily delete check. `idField`
|
|
212
|
-
* is the property holding the row id; it defaults to
|
|
231
|
+
* is the property holding the row id; it defaults to the table's
|
|
232
|
+
* single-column primary key, else `id`.
|
|
213
233
|
*
|
|
214
234
|
* ```ts
|
|
215
235
|
* const deleteUser = deleteAndPush(clivly, db, schema.user, { entity: "user" });
|
|
@@ -220,4 +240,4 @@ export declare function deleteAndPush(clivly: PushDeleteCapable, db: unknown, ta
|
|
|
220
240
|
entity: string;
|
|
221
241
|
idField?: string;
|
|
222
242
|
}): (where: unknown, waitUntil?: (promise: Promise<unknown>) => void) => Promise<Record<string, unknown>[]>;
|
|
223
|
-
export { type FromDrizzleProjectionOptions, buildUniqueConstraints, discoverFromDrizzle };
|
|
243
|
+
export { type FromDrizzleProjectionOptions, type TrackOptions, buildUniqueConstraints, discoverFromDrizzle };
|