@jcbuisson/express-x-drizzle 1.0.8 → 3.1.5

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": "1.0.8",
3
+ "version": "3.1.5",
4
4
  "description": "Drizzle ORM plugins for express-x framework",
5
5
  "main": "src/drizzle-plugins.mjs",
6
6
  "type": "module",
@@ -5,6 +5,17 @@ import { Mutex, truncateString, computeSyncResult } from '@jcbuisson/express-x'
5
5
 
6
6
  ////////////////////////// UTILITIES //////////////////////////
7
7
 
8
+ function stringifyWithSortedKeys(obj) {
9
+ return JSON.stringify(obj, (_, value) => {
10
+ if (value && typeof value === 'object' && !Array.isArray(value) && Object.prototype.toString.call(value) === '[object Object]') {
11
+ const sorted = {}
12
+ Object.keys(value).sort().forEach(k => { sorted[k] = value[k] })
13
+ return sorted
14
+ }
15
+ return value
16
+ })
17
+ }
18
+
8
19
  function whereToDrizzleFilters(table, where) {
9
20
  const conditions = Object.entries(where)
10
21
  .filter(([_, value]) => value !== undefined)
@@ -87,7 +98,11 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
87
98
  deleteWithMeta: async (uid, deleted_at) => {
88
99
  return await db.transaction(async (tx) => {
89
100
  const [value] = await tx.delete(model).where(eq(model.uid, uid)).returning();
90
- const [meta] = await tx.update(metadata).set({ deleted_at: new Date(deleted_at) }).where(eq(metadata.uid, uid)).returning();
101
+ const ts = new Date(deleted_at)
102
+ const [meta] = await tx.insert(metadata)
103
+ .values({ uid, deleted_at: ts })
104
+ .onConflictDoUpdate({ target: metadata.uid, set: { deleted_at: ts } })
105
+ .returning();
91
106
  return [value, meta]
92
107
  })
93
108
  },
@@ -103,7 +118,7 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
103
118
  go: async (modelName, where, cutoffDate, clientMetadataDict) => {
104
119
 
105
120
  // get or create a mutex specific to modelName + where
106
- const mutexKey = `${modelName}:${JSON.stringify(Object.fromEntries(Object.entries(where).sort()))}`
121
+ const mutexKey = `${modelName}:${stringifyWithSortedKeys(where)}`
107
122
  if (!syncMutexes.has(mutexKey)) syncMutexes.set(mutexKey, new Mutex())
108
123
  // acquire it: no other sync operation from another client on this model+where can occur in parallel
109
124
  await syncMutexes.get(mutexKey).acquire()