@jcbuisson/express-x-drizzle 1.0.9 → 3.1.6

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.9",
3
+ "version": "3.1.6",
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)
@@ -107,7 +118,7 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
107
118
  go: async (modelName, where, cutoffDate, clientMetadataDict) => {
108
119
 
109
120
  // get or create a mutex specific to modelName + where
110
- const mutexKey = `${modelName}:${JSON.stringify(Object.fromEntries(Object.entries(where).sort()))}`
121
+ const mutexKey = `${modelName}:${stringifyWithSortedKeys(where)}`
111
122
  if (!syncMutexes.has(mutexKey)) syncMutexes.set(mutexKey, new Mutex())
112
123
  // acquire it: no other sync operation from another client on this model+where can occur in parallel
113
124
  await syncMutexes.get(mutexKey).acquire()
@@ -155,7 +166,10 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
155
166
  console.log('*** err sync', err)
156
167
  throw err
157
168
  } finally {
158
- syncMutexes.get(mutexKey).release()
169
+ const mutex = syncMutexes.get(mutexKey)
170
+ mutex.release()
171
+ // Remove idle mutexes so the Map stays bounded for dynamic where clauses.
172
+ if (mutex.queue.length === 0) syncMutexes.delete(mutexKey)
159
173
  }
160
174
  },
161
175
  })