@livequery/mongoose 2.0.24 → 2.0.27
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/build/src/MongoQuery.js
CHANGED
|
@@ -231,7 +231,7 @@ export class MongoQuery {
|
|
|
231
231
|
},
|
|
232
232
|
in: () => ({ $in: JSON.parse(value) }),
|
|
233
233
|
nin: () => ({ $nin: JSON.parse(value) }),
|
|
234
|
-
'eq-string': ({ $eq: value })
|
|
234
|
+
'eq-string': () => ({ $eq: value })
|
|
235
235
|
};
|
|
236
236
|
const fn = map[expression || 'eq-string'];
|
|
237
237
|
if (!fn)
|
|
@@ -334,6 +334,8 @@ export class MongoQuery {
|
|
|
334
334
|
];
|
|
335
335
|
}
|
|
336
336
|
static #build_cursor_paging($sort, req) {
|
|
337
|
+
if (req.options[':after'] || req.options[':before'] || req.options[':around']) {
|
|
338
|
+
}
|
|
337
339
|
const { pipelines, summary } = this.#parse_summary(req);
|
|
338
340
|
const limit = this.#get_limit(req);
|
|
339
341
|
return [
|
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
import { RouteOptions } from "./RouteOptions.js";
|
|
2
|
-
import { LivequeryRequest,
|
|
3
|
-
import { Observable } from "rxjs";
|
|
2
|
+
import { LivequeryRequest, WebsocketSyncPayload, DatabaseEvent } from '@livequery/types';
|
|
3
|
+
import { Observable, Subject } from "rxjs";
|
|
4
4
|
import { Connection } from 'mongoose';
|
|
5
|
-
export type
|
|
5
|
+
export type LivequeryDatasourceOptions<T> = Array<T & {
|
|
6
6
|
refs: string[];
|
|
7
7
|
}>;
|
|
8
|
-
export type
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
export type LivequeryDatasourceProps<OptionsType = {}, RawDataChangeType = {}, InjectType = any> = {
|
|
9
|
+
routes: LivequeryDatasourceOptions<OptionsType>;
|
|
10
|
+
deps: {
|
|
11
|
+
[name: string]: InjectType;
|
|
12
|
+
};
|
|
13
|
+
rawChanges: Observable<RawDataChangeType>;
|
|
14
|
+
};
|
|
15
|
+
export type LivequeryDatasource<OptionsType = {}, RawDataChangeType = {}, InjectType = any> = {
|
|
16
|
+
init(props: LivequeryDatasourceProps<OptionsType, RawDataChangeType, InjectType>): any;
|
|
12
17
|
query(query: LivequeryRequest): any;
|
|
13
|
-
|
|
18
|
+
$: Observable<WebsocketSyncPayload>;
|
|
14
19
|
};
|
|
15
20
|
export declare class MongooseDatasource implements LivequeryDatasource<RouteOptions, DatabaseEvent, Connection> {
|
|
16
21
|
#private;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}): Promise<void>;
|
|
22
|
+
readonly $: Subject<WebsocketSyncPayload>;
|
|
23
|
+
init({ deps, rawChanges, routes }: LivequeryDatasourceProps<RouteOptions, DatabaseEvent, Connection>): void;
|
|
20
24
|
query(query: LivequeryRequest): Promise<import("mongodb").DeleteResult | import("mongoose").UpdateWriteOpResult | {
|
|
21
25
|
items: any[];
|
|
22
26
|
summary: any;
|
|
@@ -41,12 +45,4 @@ export declare class MongooseDatasource implements LivequeryDatasource<RouteOpti
|
|
|
41
45
|
} | {
|
|
42
46
|
item: any;
|
|
43
47
|
}>;
|
|
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
|
-
}>;
|
|
52
48
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Cursor } from './Cursor.js';
|
|
2
|
-
import { map, mergeAll } from "rxjs";
|
|
2
|
+
import { map, mergeAll, Subject, pipe } from "rxjs";
|
|
3
3
|
import { MongoQuery } from "./MongoQuery.js";
|
|
4
4
|
import { ObjectId } from 'bson';
|
|
5
5
|
export class MongooseDatasource {
|
|
@@ -7,8 +7,10 @@ export class MongooseDatasource {
|
|
|
7
7
|
#routes = new Map();
|
|
8
8
|
#realtime_collections = new Map();
|
|
9
9
|
#conections = {};
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
$ = new Subject;
|
|
11
|
+
init({ deps, rawChanges, routes }) {
|
|
12
|
+
this.#conections = { ...deps };
|
|
13
|
+
rawChanges.pipe(this.#stream()).subscribe(this.$);
|
|
12
14
|
for (const { refs, ...options } of routes) {
|
|
13
15
|
for (const ref of refs) {
|
|
14
16
|
const schema_ref = ref.replaceAll(':', '');
|
|
@@ -153,55 +155,55 @@ export class MongooseDatasource {
|
|
|
153
155
|
}, {});
|
|
154
156
|
return await model.deleteOne(keys);
|
|
155
157
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
-
}
|
|
158
|
+
#caculateRef(arr) {
|
|
159
|
+
const v = arr[0];
|
|
160
|
+
const x = v instanceof Map ? [...v] : Object.entries(v);
|
|
161
|
+
return x.map(([k, w]) => {
|
|
162
|
+
const { old_ref, new_ref } = w;
|
|
163
|
+
const next = arr.slice(1);
|
|
164
|
+
if (next.length == 0)
|
|
165
|
+
return { old_ref, new_ref };
|
|
166
|
+
const n = this.#caculateRef(next);
|
|
167
|
+
return n.map(nn => {
|
|
173
168
|
return {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
new_ref: n[k]
|
|
177
|
-
}
|
|
169
|
+
old_ref: `${old_ref}/${nn.old_ref}`,
|
|
170
|
+
new_ref: `${new_ref}/${nn.new_ref}`
|
|
178
171
|
};
|
|
179
172
|
});
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
173
|
+
}).flat(2);
|
|
174
|
+
}
|
|
175
|
+
#parse(ref, o, n) {
|
|
176
|
+
const keys = ref.split('/').map((k, i) => {
|
|
177
|
+
if (i % 2 == 0)
|
|
178
|
+
return { v: { old_ref: k, new_ref: k } };
|
|
179
|
+
if (Array.isArray(o[k]) || Array.isArray(n[k])) {
|
|
180
|
+
return [
|
|
181
|
+
...Array.isArray(o[k]) ? o[k] : [],
|
|
182
|
+
...Array.isArray(n[k]) ? n[k] : []
|
|
183
|
+
].reduce((p, c) => {
|
|
184
|
+
p.set(c, {
|
|
185
|
+
old_ref: Array.isArray(o[k]) && o[k].includes(c) ? c : null,
|
|
186
|
+
new_ref: Array.isArray(n[k]) && n[k].includes(c) ? c : null,
|
|
194
187
|
});
|
|
195
|
-
|
|
188
|
+
return p;
|
|
189
|
+
}, new Map());
|
|
196
190
|
}
|
|
197
|
-
return
|
|
198
|
-
|
|
199
|
-
|
|
191
|
+
return {
|
|
192
|
+
v: {
|
|
193
|
+
old_ref: o[k],
|
|
194
|
+
new_ref: n[k]
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
});
|
|
198
|
+
return this.#caculateRef(keys);
|
|
199
|
+
}
|
|
200
|
+
#stream() {
|
|
201
|
+
return pipe(map((event) => {
|
|
200
202
|
const refs = this.#realtime_collections.get(event.table);
|
|
201
203
|
if (!refs)
|
|
202
204
|
return [];
|
|
203
205
|
return [...refs].map(ref => {
|
|
204
|
-
const refs_list =
|
|
206
|
+
const refs_list = this.#parse(ref, event.old_data, event.new_data);
|
|
205
207
|
return refs_list.map(({ new_ref, old_ref }) => ({
|
|
206
208
|
new_data: event.new_data,
|
|
207
209
|
old_data: event.old_data,
|
package/package.json
CHANGED