@jcbuisson/express-x-drizzle 1.0.9 → 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 +1 -1
- package/src/drizzle-plugins.mjs +12 -1
package/package.json
CHANGED
package/src/drizzle-plugins.mjs
CHANGED
|
@@ -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}:${
|
|
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()
|