@jcbuisson/express-x-drizzle 1.0.3 → 1.0.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 +14 -6
package/package.json
CHANGED
package/src/drizzle-plugins.mjs
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
+
import express from "express"
|
|
2
|
+
import { createServer } from "http"
|
|
3
|
+
import { Server } from "socket.io"
|
|
4
|
+
import bcrypt from 'bcryptjs'
|
|
5
|
+
|
|
1
6
|
import { and, eq, getTableName } from "drizzle-orm";
|
|
7
|
+
|
|
8
|
+
import { metadata } from '#root/src/db/schema.js';
|
|
2
9
|
import { Mutex, truncateString } from '@jcbuisson/express-x'
|
|
3
10
|
|
|
4
11
|
|
|
@@ -11,9 +18,8 @@ function whereToDrizzleFilters(table, filters) {
|
|
|
11
18
|
return conditions.length ? and(...conditions) : undefined;
|
|
12
19
|
}
|
|
13
20
|
|
|
14
|
-
////////////////////////// DRIZZLE OFFLINE PLUGIN //////////////////////////
|
|
15
21
|
|
|
16
|
-
|
|
22
|
+
////////////////////////// DRIZZLE OFFLINE PLUGIN //////////////////////////
|
|
17
23
|
|
|
18
24
|
export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
19
25
|
|
|
@@ -42,7 +48,7 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
42
48
|
|
|
43
49
|
updateWithMeta: async (uid, data, updated_at) => {
|
|
44
50
|
db.transaction(async (tx) => {
|
|
45
|
-
const value = await tx.update(model).
|
|
51
|
+
const value = await tx.update(model).set(data).where(eq(model.uid, uid)).returning();
|
|
46
52
|
const meta = await tx.update(metadata).set({ updated_at }).where(eq(metadata.uid, uid)).returning();
|
|
47
53
|
return [value, meta]
|
|
48
54
|
})
|
|
@@ -58,12 +64,14 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
58
64
|
})
|
|
59
65
|
}
|
|
60
66
|
|
|
67
|
+
const syncMutex = new Mutex()
|
|
68
|
+
|
|
61
69
|
// add a synchronization service
|
|
62
70
|
app.createService('sync', {
|
|
63
71
|
|
|
64
72
|
// AMÉLIORER : ne pas avoir une exclusion mutuelle globale, mais seulement par model/where
|
|
65
73
|
go: async (modelName, where, cutoffDate, clientMetadataDict) => {
|
|
66
|
-
await
|
|
74
|
+
await syncMutex.acquire()
|
|
67
75
|
try {
|
|
68
76
|
console.log('>>>>> SYNC', modelName, where, cutoffDate)
|
|
69
77
|
const databaseService = app.service(modelName)
|
|
@@ -166,7 +174,7 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
166
174
|
}
|
|
167
175
|
|
|
168
176
|
// STEP5: return to client the changes to perform on its cache, and create/update to perform on database with full data
|
|
169
|
-
//
|
|
177
|
+
// Database creations & updates are done later by the client with complete data (this function only has client values's meta-data)
|
|
170
178
|
return {
|
|
171
179
|
toAdd: addClient,
|
|
172
180
|
toUpdate: updateClient,
|
|
@@ -178,7 +186,7 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
178
186
|
} catch(err) {
|
|
179
187
|
console.log('*** err sync', err)
|
|
180
188
|
} finally {
|
|
181
|
-
|
|
189
|
+
syncMutex.release()
|
|
182
190
|
}
|
|
183
191
|
},
|
|
184
192
|
})
|