@jcbuisson/express-x-drizzle 1.0.2 → 1.0.4
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 +2 -2
- package/src/drizzle-plugins.mjs +47 -5
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,42 @@ function whereToDrizzleFilters(table, filters) {
|
|
|
11
18
|
return conditions.length ? and(...conditions) : undefined;
|
|
12
19
|
}
|
|
13
20
|
|
|
14
|
-
////////////////////////// DRIZZLE
|
|
21
|
+
// ////////////////////////// DRIZZLE CRUD DATABSE PLUGIN //////////////////////////
|
|
22
|
+
|
|
23
|
+
// export function drizzleDatabasePlugin(app, db, models) {
|
|
24
|
+
|
|
25
|
+
// // add a database service for each model
|
|
26
|
+
// for (const model of models) {
|
|
27
|
+
// const modelName = getTableName(model)
|
|
28
|
+
|
|
29
|
+
// app.createService(modelName, {
|
|
30
|
+
|
|
31
|
+
// findUnique: async (where) => {
|
|
32
|
+
// const rows = await db.select().from(model).where(whereToDrizzleFilters(model, where));
|
|
33
|
+
// return rows[0] ?? null;
|
|
34
|
+
// },
|
|
15
35
|
|
|
16
|
-
|
|
36
|
+
// findMany: async (where) => {
|
|
37
|
+
// return await db.select().from(model).where(whereToDrizzleFilters(model, where));
|
|
38
|
+
// },
|
|
39
|
+
|
|
40
|
+
// create: async (data) => {
|
|
41
|
+
// return await db.insert(model).values(data).returning();
|
|
42
|
+
// },
|
|
43
|
+
|
|
44
|
+
// update: async (uid, data) => {
|
|
45
|
+
// return await db.update(model).where(eq(model.uid, uid)).values(data).returning();
|
|
46
|
+
// },
|
|
47
|
+
|
|
48
|
+
// remove: async (uid) => {
|
|
49
|
+
// return await db.delete(model).where(eq(model.uid, uid)).returning();
|
|
50
|
+
// }
|
|
51
|
+
// })
|
|
52
|
+
// }
|
|
53
|
+
// }
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
////////////////////////// DRIZZLE OFFLINE PLUGIN //////////////////////////
|
|
17
57
|
|
|
18
58
|
export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
19
59
|
|
|
@@ -58,12 +98,14 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
58
98
|
})
|
|
59
99
|
}
|
|
60
100
|
|
|
101
|
+
const syncMutex = new Mutex()
|
|
102
|
+
|
|
61
103
|
// add a synchronization service
|
|
62
104
|
app.createService('sync', {
|
|
63
105
|
|
|
64
106
|
// AMÉLIORER : ne pas avoir une exclusion mutuelle globale, mais seulement par model/where
|
|
65
107
|
go: async (modelName, where, cutoffDate, clientMetadataDict) => {
|
|
66
|
-
await
|
|
108
|
+
await syncMutex.acquire()
|
|
67
109
|
try {
|
|
68
110
|
console.log('>>>>> SYNC', modelName, where, cutoffDate)
|
|
69
111
|
const databaseService = app.service(modelName)
|
|
@@ -166,7 +208,7 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
166
208
|
}
|
|
167
209
|
|
|
168
210
|
// STEP5: return to client the changes to perform on its cache, and create/update to perform on database with full data
|
|
169
|
-
//
|
|
211
|
+
// Database creations & updates are done later by the client with complete data (this function only has client values's meta-data)
|
|
170
212
|
return {
|
|
171
213
|
toAdd: addClient,
|
|
172
214
|
toUpdate: updateClient,
|
|
@@ -178,7 +220,7 @@ export function drizzleOfflinePlugin(app, db, metadata, models) {
|
|
|
178
220
|
} catch(err) {
|
|
179
221
|
console.log('*** err sync', err)
|
|
180
222
|
} finally {
|
|
181
|
-
|
|
223
|
+
syncMutex.release()
|
|
182
224
|
}
|
|
183
225
|
},
|
|
184
226
|
})
|