@jcbuisson/express-x-drizzle 3.1.12 → 3.1.28

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x-drizzle",
3
- "version": "3.1.12",
3
+ "version": "3.1.28",
4
4
  "description": "Drizzle ORM plugins for express-x framework",
5
5
  "main": "src/drizzle-plugins.mjs",
6
6
  "type": "module",
@@ -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
- const [value] = await tx.update(model).set(data).where(eq(model.uid, uid)).returning();
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,16 @@ 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 existingTime = existingMeta
206
+ ? new Date(existingMeta.deleted_at || existingMeta.updated_at || existingMeta.created_at)
207
+ : null
208
+ if (existingTime && existingTime > ts) {
209
+ const value = (await tx.select().from(model).where(eq(model.uid, uid)))[0] ?? undefined
210
+ return [value, existingMeta]
211
+ }
212
+ const [value] = await tx.delete(model).where(eq(model.uid, uid)).returning();
181
213
  const [meta] = await tx.insert(metadata)
182
214
  .values({ uid, deleted_at: ts })
183
215
  .onConflictDoUpdate({ target: metadata.uid, set: { deleted_at: ts } })
@@ -211,9 +243,14 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
211
243
  return accu
212
244
  }, {})
213
245
 
214
- // STEP 2: fetch metadata for each database record
246
+ // STEP 2: fetch metadata for each database record and each client-sent uid.
247
+ // Client uids matter when the server row was deleted: only the tombstone remains.
215
248
  const databaseMetadataDict = {}
216
- for (const uid of Object.keys(databaseValuesDict)) {
249
+ const metadataUids = new Set([
250
+ ...Object.keys(databaseValuesDict),
251
+ ...Object.keys(clientMetadataDict ?? {}),
252
+ ])
253
+ for (const uid of metadataUids) {
217
254
  const meta = (await db.select().from(metadata).where(eq(metadata.uid, uid)))[0] ?? null
218
255
  if (meta) databaseMetadataDict[uid] = meta
219
256
  }