@livequery/mongoose 2.0.5 → 2.0.11
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.
|
@@ -41,5 +41,12 @@ export declare class MongooseDatasource implements LivequeryDatasource<RouteOpti
|
|
|
41
41
|
} | {
|
|
42
42
|
item: any;
|
|
43
43
|
}>;
|
|
44
|
-
enable_realtime<T extends LivequeryBaseEntity = LivequeryBaseEntity>(d: Observable<DatabaseEvent>): Observable<
|
|
44
|
+
enable_realtime<T extends LivequeryBaseEntity = LivequeryBaseEntity>(d: Observable<DatabaseEvent>): Observable<{
|
|
45
|
+
new_data: LivequeryBaseEntity;
|
|
46
|
+
old_data: LivequeryBaseEntity;
|
|
47
|
+
table: string;
|
|
48
|
+
type: "added" | "removed" | "modified";
|
|
49
|
+
old_ref: string;
|
|
50
|
+
new_ref: string;
|
|
51
|
+
}>;
|
|
45
52
|
}
|
|
@@ -18,12 +18,13 @@ export class MongooseDatasource {
|
|
|
18
18
|
ref
|
|
19
19
|
]));
|
|
20
20
|
if (options.realtime) {
|
|
21
|
-
const table_name = options.schema.options.collection
|
|
21
|
+
const table_name = options.schema.options.collection;
|
|
22
22
|
const $ = schema_ref.split('/');
|
|
23
23
|
const is_collection_ref = $.length % 2 == 1;
|
|
24
24
|
this.#realtime_collections.set(table_name, new Set([
|
|
25
25
|
...this.#realtime_collections.get(table_name) || [],
|
|
26
|
-
is_collection_ref ? schema_ref : $.slice(0, -1).join('/')
|
|
26
|
+
is_collection_ref ? schema_ref : $.slice(0, -1).join('/'),
|
|
27
|
+
schema_ref
|
|
27
28
|
]));
|
|
28
29
|
}
|
|
29
30
|
}
|
|
@@ -36,7 +37,7 @@ export class MongooseDatasource {
|
|
|
36
37
|
const db = await config.db?.(query) || process.env.DB_NAME;
|
|
37
38
|
const connection = config.connection ? this.#conections[await config.connection(query)] : Object.values(this.#conections)[0];
|
|
38
39
|
const schema = config.schema;
|
|
39
|
-
const collection_name = schema.options.collection
|
|
40
|
+
const collection_name = schema.options.collection;
|
|
40
41
|
const model = connection.useDb(db).model(collection_name, schema, collection_name, schema.options);
|
|
41
42
|
if (query.method == 'get')
|
|
42
43
|
return await this.#get(query, model);
|
|
@@ -153,26 +154,63 @@ export class MongooseDatasource {
|
|
|
153
154
|
return await model.deleteOne(keys);
|
|
154
155
|
}
|
|
155
156
|
enable_realtime(d) {
|
|
157
|
+
function parse_refs(ref, o, n) {
|
|
158
|
+
const keys = ref.split('/').map((k, i) => {
|
|
159
|
+
if (i % 2 == 0)
|
|
160
|
+
return { v: { old_ref: k, new_ref: k } };
|
|
161
|
+
if (Array.isArray(o[k]) || Array.isArray(n[k])) {
|
|
162
|
+
return [
|
|
163
|
+
...Array.isArray(o[k]) ? o[k] : [],
|
|
164
|
+
...Array.isArray(n[k]) ? n[k] : []
|
|
165
|
+
].reduce((p, c) => {
|
|
166
|
+
p.set(c, {
|
|
167
|
+
old_ref: Array.isArray(o[k]) && o[k].includes(c) ? c : null,
|
|
168
|
+
new_ref: Array.isArray(n[k]) && n[k].includes(c) ? c : null,
|
|
169
|
+
});
|
|
170
|
+
return p;
|
|
171
|
+
}, new Map());
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
v: {
|
|
175
|
+
old_ref: o[k],
|
|
176
|
+
new_ref: n[k]
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
});
|
|
180
|
+
function parse(arr) {
|
|
181
|
+
const v = arr[0];
|
|
182
|
+
const x = v instanceof Map ? [...v] : Object.entries(v);
|
|
183
|
+
return x.map(([k, w]) => {
|
|
184
|
+
const { old_ref, new_ref } = w;
|
|
185
|
+
const next = arr.slice(1);
|
|
186
|
+
if (next.length == 0)
|
|
187
|
+
return { old_ref, new_ref };
|
|
188
|
+
const n = parse(next);
|
|
189
|
+
return n.map(nn => {
|
|
190
|
+
return {
|
|
191
|
+
old_ref: `${old_ref}/${nn.old_ref}`,
|
|
192
|
+
new_ref: `${new_ref}/${nn.new_ref}`
|
|
193
|
+
};
|
|
194
|
+
});
|
|
195
|
+
}).flat(2);
|
|
196
|
+
}
|
|
197
|
+
return parse(keys);
|
|
198
|
+
}
|
|
156
199
|
return d.pipe(map((event) => {
|
|
157
200
|
const refs = this.#realtime_collections.get(event.table);
|
|
158
201
|
if (!refs)
|
|
159
202
|
return [];
|
|
160
|
-
const data = {
|
|
161
|
-
...event.old_data || {},
|
|
162
|
-
...event.new_data || {}
|
|
163
|
-
};
|
|
164
203
|
return [...refs].map(ref => {
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
204
|
+
const refs_list = parse_refs(ref, event.old_data, event.new_data);
|
|
205
|
+
return refs_list.map(({ new_ref, old_ref }) => ({
|
|
206
|
+
new_data: event.new_data,
|
|
207
|
+
old_data: event.old_data,
|
|
169
208
|
table: event.table,
|
|
209
|
+
type: event.type,
|
|
170
210
|
old_ref,
|
|
171
|
-
new_ref
|
|
172
|
-
|
|
173
|
-
new_data: event.new_data
|
|
174
|
-
};
|
|
211
|
+
new_ref
|
|
212
|
+
}));
|
|
175
213
|
});
|
|
176
|
-
}), mergeAll());
|
|
214
|
+
}), mergeAll(), mergeAll());
|
|
177
215
|
}
|
|
178
216
|
}
|
package/package.json
CHANGED