@jcbuisson/express-x-drizzle 3.1.11 → 3.1.25
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/CLAUDE.md +1 -1
- package/package.json +1 -1
- package/src/drizzle-plugins.mjs +42 -8
package/CLAUDE.md
CHANGED
|
@@ -24,6 +24,6 @@ The library exports one function: `drizzleOfflinePlugin(app, db, metadata, model
|
|
|
24
24
|
|
|
25
25
|
1. **Per-model services** — For each table in `models`, registers an `express-x` service named after the table. Each service exposes: `findUnique`, `findMany`, `createWithMeta`, `updateWithMeta`, `deleteWithMeta`. All mutation methods write to both the model table and the `metadata` table inside a transaction, keeping timestamps in sync.
|
|
26
26
|
|
|
27
|
-
2. **`sync` service** — Implements an offline-first reconciliation algorithm. The client sends its local metadata dictionary (`{ uid → { created_at, updated_at, deleted_at } }`)
|
|
27
|
+
2. **`sync` service** — Implements an offline-first reconciliation algorithm. The client sends its local metadata dictionary (`{ uid → { created_at, updated_at, deleted_at } }`). The server computes set intersections between client UIDs and database UIDs, then determines add/update/delete operations for both sides. An overlap-aware lock serializes sync calls whose `where` scopes can touch the same records. The return value tells the client which records to add, update, or delete locally, and which UIDs the client should push to the database with full data.
|
|
28
28
|
|
|
29
29
|
**Key invariant:** The sync algorithm compares `updated_at` (falling back to `created_at`) timestamps to decide which side wins a conflict — the most recently updated side wins. Records marked `deleted_at` on the client trigger a soft-delete propagation to the database.
|
package/package.json
CHANGED
package/src/drizzle-plugins.mjs
CHANGED
|
@@ -140,6 +140,14 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
140
140
|
createWithMeta: async (uid, data, created_at) => {
|
|
141
141
|
const ts = new Date(created_at)
|
|
142
142
|
return await db.transaction(async (tx) => {
|
|
143
|
+
const existingMeta = (await tx.select().from(metadata).where(eq(metadata.uid, uid)))[0] ?? null
|
|
144
|
+
const existingTime = existingMeta
|
|
145
|
+
? new Date(existingMeta.deleted_at || existingMeta.updated_at || existingMeta.created_at)
|
|
146
|
+
: null
|
|
147
|
+
if (existingTime && existingTime > ts) {
|
|
148
|
+
const value = (await tx.select().from(model).where(eq(model.uid, uid)))[0] ?? undefined
|
|
149
|
+
return [value, existingMeta]
|
|
150
|
+
}
|
|
143
151
|
// Upsert: if the model row already exists (e.g. a concurrent createWithMeta
|
|
144
152
|
// from the direct create() path landed before the sync's addDatabase step),
|
|
145
153
|
// update it instead of throwing a PK conflict that would rollback the
|
|
@@ -163,12 +171,28 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
163
171
|
updateWithMeta: async (uid, data, updated_at) => {
|
|
164
172
|
const ts = updated_at ? new Date(updated_at) : null
|
|
165
173
|
return await db.transaction(async (tx) => {
|
|
166
|
-
|
|
174
|
+
let [value] = await tx.update(model).set(data).where(eq(model.uid, uid)).returning();
|
|
175
|
+
const existingMeta = (await tx.select().from(metadata).where(eq(metadata.uid, uid)))[0] ?? null
|
|
176
|
+
if (!value && existingMeta?.deleted_at) {
|
|
177
|
+
const deletedAt = new Date(existingMeta.deleted_at)
|
|
178
|
+
if (ts && ts > deletedAt) {
|
|
179
|
+
;[value] = await tx.insert(model)
|
|
180
|
+
.values({ uid, ...data })
|
|
181
|
+
.onConflictDoUpdate({ target: model.uid, set: data })
|
|
182
|
+
.returning();
|
|
183
|
+
const [meta] = await tx.insert(metadata)
|
|
184
|
+
.values({ uid, updated_at: ts })
|
|
185
|
+
.onConflictDoUpdate({ target: metadata.uid, set: { updated_at: ts, deleted_at: null } })
|
|
186
|
+
.returning();
|
|
187
|
+
return [value, meta]
|
|
188
|
+
}
|
|
189
|
+
return [value, existingMeta]
|
|
190
|
+
}
|
|
167
191
|
// Upsert metadata: if the row is missing (data-integrity gap where the
|
|
168
192
|
// model row exists but no metadata row), create it so the loop stops.
|
|
169
193
|
const [meta] = await tx.insert(metadata)
|
|
170
194
|
.values({ uid, updated_at: ts })
|
|
171
|
-
.onConflictDoUpdate({ target: metadata.uid, set: { updated_at: ts } })
|
|
195
|
+
.onConflictDoUpdate({ target: metadata.uid, set: { updated_at: ts, deleted_at: null } })
|
|
172
196
|
.returning();
|
|
173
197
|
return [value, meta]
|
|
174
198
|
})
|
|
@@ -176,8 +200,14 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
176
200
|
|
|
177
201
|
deleteWithMeta: async (uid, deleted_at) => {
|
|
178
202
|
return await db.transaction(async (tx) => {
|
|
179
|
-
const [value] = await tx.delete(model).where(eq(model.uid, uid)).returning();
|
|
180
203
|
const ts = new Date(deleted_at)
|
|
204
|
+
const existingMeta = (await tx.select().from(metadata).where(eq(metadata.uid, uid)))[0] ?? null
|
|
205
|
+
const existingUpdatedAt = existingMeta ? new Date(existingMeta.updated_at || existingMeta.created_at) : null
|
|
206
|
+
if (existingUpdatedAt && existingUpdatedAt > ts) {
|
|
207
|
+
const value = (await tx.select().from(model).where(eq(model.uid, uid)))[0] ?? undefined
|
|
208
|
+
return [value, existingMeta]
|
|
209
|
+
}
|
|
210
|
+
const [value] = await tx.delete(model).where(eq(model.uid, uid)).returning();
|
|
181
211
|
const [meta] = await tx.insert(metadata)
|
|
182
212
|
.values({ uid, deleted_at: ts })
|
|
183
213
|
.onConflictDoUpdate({ target: metadata.uid, set: { deleted_at: ts } })
|
|
@@ -193,8 +223,7 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
193
223
|
// add a synchronization service
|
|
194
224
|
app.createService('sync', {
|
|
195
225
|
|
|
196
|
-
|
|
197
|
-
go: async (modelName, where, cutoffDate, clientMetadataDict) => {
|
|
226
|
+
go: async (modelName, where, clientMetadataDict) => {
|
|
198
227
|
|
|
199
228
|
// overlap-aware lock so independent scopes can still run in parallel, but overlapping where predicates do not
|
|
200
229
|
if (!syncLocks.has(modelName)) syncLocks.set(modelName, new OverlapLock())
|
|
@@ -202,7 +231,7 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
202
231
|
const releaseSyncLock = await syncLock.acquire(where)
|
|
203
232
|
|
|
204
233
|
try {
|
|
205
|
-
console.log('>>>>> SYNC', modelName, where
|
|
234
|
+
console.log('>>>>> SYNC', modelName, where)
|
|
206
235
|
const databaseService = app.service(modelName)
|
|
207
236
|
|
|
208
237
|
// STEP1: get existing database `where` values and build a dictionary
|
|
@@ -212,9 +241,14 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
212
241
|
return accu
|
|
213
242
|
}, {})
|
|
214
243
|
|
|
215
|
-
// STEP 2: fetch metadata for each database record
|
|
244
|
+
// STEP 2: fetch metadata for each database record and each client-sent uid.
|
|
245
|
+
// Client uids matter when the server row was deleted: only the tombstone remains.
|
|
216
246
|
const databaseMetadataDict = {}
|
|
217
|
-
|
|
247
|
+
const metadataUids = new Set([
|
|
248
|
+
...Object.keys(databaseValuesDict),
|
|
249
|
+
...Object.keys(clientMetadataDict ?? {}),
|
|
250
|
+
])
|
|
251
|
+
for (const uid of metadataUids) {
|
|
218
252
|
const meta = (await db.select().from(metadata).where(eq(metadata.uid, uid)))[0] ?? null
|
|
219
253
|
if (meta) databaseMetadataDict[uid] = meta
|
|
220
254
|
}
|