@rpcbase/server 0.364.0-mongoosetsindex.1 → 0.365.0
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/queue/register_queue_listener.js +7 -6
- package/rts/index.js +13 -14
package/package.json
CHANGED
|
@@ -9,6 +9,7 @@ const queue = require("./index")
|
|
|
9
9
|
const dispatch_worker_queue = require("./dispatch_worker_queue")
|
|
10
10
|
const dispatch_indexer_queue = require("./dispatch_indexer_queue")
|
|
11
11
|
|
|
12
|
+
|
|
12
13
|
const log = debug("rb:queue:listener")
|
|
13
14
|
|
|
14
15
|
const {RB_APP_NAME, RB_TENANT_ID} = process.env
|
|
@@ -30,10 +31,12 @@ const mongoose_delete_plugin = (schema) => {
|
|
|
30
31
|
})
|
|
31
32
|
|
|
32
33
|
// TODO: add other delete operations
|
|
34
|
+
// TODO: this should be deleteOne ?
|
|
33
35
|
// https://mongoosejs.com/docs/queries.html
|
|
34
36
|
schema.post("findOneAndDelete", function(doc) {
|
|
35
37
|
log("queue:findOneAndDelete", "dispatch_doc_change NYI")
|
|
36
38
|
log("del PLUGIN", doc)
|
|
39
|
+
const change = null // TMP
|
|
37
40
|
const coll_name = change.ns.coll
|
|
38
41
|
|
|
39
42
|
const model_name = Object.keys(mongoose.models).find((k) => {
|
|
@@ -79,8 +82,6 @@ const dispatch_change_handler = (change) => {
|
|
|
79
82
|
return mongoose.models[k].collection.collectionName === coll_name
|
|
80
83
|
})
|
|
81
84
|
|
|
82
|
-
console.log("O3333K", Object.keys(require("../mongoose").models), Object.keys(mongoose.models))
|
|
83
|
-
|
|
84
85
|
if (!model_name) {
|
|
85
86
|
return
|
|
86
87
|
}
|
|
@@ -107,8 +108,8 @@ const register_db_emitter = () => {
|
|
|
107
108
|
|
|
108
109
|
// Set up the change stream with a filter to only listen to the specific database
|
|
109
110
|
const pipeline = [
|
|
110
|
-
{ $match: {
|
|
111
|
-
{ $match: { operationType: { $in: ["insert", "update"
|
|
111
|
+
{ $match: { "ns.db": RB_APP_NAME } },
|
|
112
|
+
{ $match: { operationType: { $in: ["insert", "update" /* "delete"*/] } } },
|
|
112
113
|
]
|
|
113
114
|
|
|
114
115
|
// https://www.mongodb.com/docs/manual/reference/method/Mongo.watch/
|
|
@@ -154,8 +155,8 @@ const register_queue_listener = () => new Promise((resolve, reject) => {
|
|
|
154
155
|
mongoose.plugin(mongoose_delete_plugin)
|
|
155
156
|
|
|
156
157
|
mongoose.connection.once("open", () => {
|
|
157
|
-
const
|
|
158
|
-
resolve(
|
|
158
|
+
const db_emitter = register_db_emitter()
|
|
159
|
+
resolve(db_emitter)
|
|
159
160
|
})
|
|
160
161
|
|
|
161
162
|
mongoose.connection.on("error", (err) => {
|
package/rts/index.js
CHANGED
|
@@ -37,7 +37,7 @@ const get_query_options = (ctx, options) => {
|
|
|
37
37
|
// https://mongoosejs.com/docs/api/query.html#Query.prototype.setOptions()
|
|
38
38
|
const query_options = {
|
|
39
39
|
ctx,
|
|
40
|
-
is_client: true
|
|
40
|
+
is_client: true,
|
|
41
41
|
//
|
|
42
42
|
}
|
|
43
43
|
|
|
@@ -46,8 +46,7 @@ const get_query_options = (ctx, options) => {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
const limit = typeof options.limit === "number" ? Math.min(QUERY_MAX_LIMIT, Math.abs(options.limit)) : QUERY_MAX_LIMIT
|
|
49
|
-
|
|
50
|
-
query_options.limit = options.limit
|
|
49
|
+
query_options.limit = limit
|
|
51
50
|
|
|
52
51
|
return query_options
|
|
53
52
|
}
|
|
@@ -99,15 +98,15 @@ const dispatch_doc_change = (model, doc) => {
|
|
|
99
98
|
return
|
|
100
99
|
}
|
|
101
100
|
|
|
102
|
-
|
|
103
101
|
const query_options = get_query_options(ctx, options)
|
|
102
|
+
// TODO: do not redo query here, use document from change event
|
|
104
103
|
const query_promise = model.find(query, options.projection, query_options)
|
|
105
104
|
if (query_options.sort) {
|
|
106
105
|
query_promise.sort(query_options.sort)
|
|
107
106
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
107
|
+
|
|
108
|
+
query_promise.limit(query_options.limit)
|
|
109
|
+
|
|
111
110
|
// WARNING: DANGER
|
|
112
111
|
// TODO: we should not be doing a read here, use doc from event
|
|
113
112
|
const data = await query_promise
|
|
@@ -179,8 +178,8 @@ const mongoose_txn_plugin = (schema, options) => {
|
|
|
179
178
|
|
|
180
179
|
|
|
181
180
|
// responds to a change stream "change" event
|
|
182
|
-
const get_dispatch_change_handler = (
|
|
183
|
-
const queries = _queries[model_name]
|
|
181
|
+
const get_dispatch_change_handler = (model_name) => async(change) => {
|
|
182
|
+
// const queries = _queries[model_name]
|
|
184
183
|
|
|
185
184
|
const model = mongoose.model(model_name)
|
|
186
185
|
|
|
@@ -203,7 +202,7 @@ const get_dispatch_change_handler = (mongoose, model_name) => async(change) => {
|
|
|
203
202
|
}
|
|
204
203
|
|
|
205
204
|
|
|
206
|
-
const add_change_stream = (
|
|
205
|
+
const add_change_stream = (socket_id, {model_name, query, query_key, options}) => {
|
|
207
206
|
if (!model_name) throw new Error("empty model name")
|
|
208
207
|
|
|
209
208
|
if (!_change_streams[model_name]) {
|
|
@@ -225,7 +224,7 @@ const add_change_stream = (mongoose, socket_id, {model_name, query, query_key, o
|
|
|
225
224
|
fullDocumentBeforeChange: "whenAvailable",
|
|
226
225
|
})
|
|
227
226
|
|
|
228
|
-
emitter.on("change", get_dispatch_change_handler(
|
|
227
|
+
emitter.on("change", get_dispatch_change_handler(model_name))
|
|
229
228
|
|
|
230
229
|
emitter.on("error", (err) => {
|
|
231
230
|
console.log("change listener emitter got error", err)
|
|
@@ -261,7 +260,7 @@ const add_change_stream = (mongoose, socket_id, {model_name, query, query_key, o
|
|
|
261
260
|
}
|
|
262
261
|
|
|
263
262
|
|
|
264
|
-
const run_query = async(
|
|
263
|
+
const run_query = async(socket_id, {model_name, query, query_key, options}) => {
|
|
265
264
|
const model = mongoose.model(model_name)
|
|
266
265
|
|
|
267
266
|
log("run_query", {model_name, query, query_key, options})
|
|
@@ -399,7 +398,7 @@ const rts_server = async(server) => {
|
|
|
399
398
|
console.log("socket connected", socket.id)
|
|
400
399
|
//
|
|
401
400
|
socket.on("register_query", (payload) => {
|
|
402
|
-
add_change_stream(
|
|
401
|
+
add_change_stream(socket.id, payload)
|
|
403
402
|
})
|
|
404
403
|
|
|
405
404
|
socket.on("remove_query", (payload) => {
|
|
@@ -408,7 +407,7 @@ const rts_server = async(server) => {
|
|
|
408
407
|
|
|
409
408
|
socket.on("run_query", (payload) => {
|
|
410
409
|
try {
|
|
411
|
-
run_query(
|
|
410
|
+
run_query(socket.id, payload)
|
|
412
411
|
} catch (err) {
|
|
413
412
|
console.log("run_query caught error", err)
|
|
414
413
|
}
|