@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 +1 -1
- package/src/drizzle-plugins.mjs +17 -2
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)
|
|
@@ -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
|
|
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}:${
|
|
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()
|