@livequery/mongoose 2.0.27 → 2.0.47
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.
|
@@ -1,27 +1,12 @@
|
|
|
1
1
|
import { RouteOptions } from "./RouteOptions.js";
|
|
2
|
-
import { LivequeryRequest,
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
refs: string[];
|
|
7
|
-
}>;
|
|
8
|
-
export type LivequeryDatasourceProps<OptionsType = {}, RawDataChangeType = {}, InjectType = any> = {
|
|
9
|
-
routes: LivequeryDatasourceOptions<OptionsType>;
|
|
10
|
-
deps: {
|
|
11
|
-
[name: string]: InjectType;
|
|
12
|
-
};
|
|
13
|
-
rawChanges: Observable<RawDataChangeType>;
|
|
2
|
+
import { LivequeryRequest, LivequeryBaseEntity } from '@livequery/types';
|
|
3
|
+
import mongoose, { Connection } from 'mongoose';
|
|
4
|
+
export type LivequeryDatasource<T> = {
|
|
5
|
+
query(query: LivequeryRequest, options: T, connection: any): any;
|
|
14
6
|
};
|
|
15
|
-
export
|
|
16
|
-
init(props: LivequeryDatasourceProps<OptionsType, RawDataChangeType, InjectType>): any;
|
|
17
|
-
query(query: LivequeryRequest): any;
|
|
18
|
-
$: Observable<WebsocketSyncPayload>;
|
|
19
|
-
};
|
|
20
|
-
export declare class MongooseDatasource implements LivequeryDatasource<RouteOptions, DatabaseEvent, Connection> {
|
|
7
|
+
export declare class MongooseDatasource implements LivequeryDatasource<RouteOptions> {
|
|
21
8
|
#private;
|
|
22
|
-
|
|
23
|
-
init({ deps, rawChanges, routes }: LivequeryDatasourceProps<RouteOptions, DatabaseEvent, Connection>): void;
|
|
24
|
-
query(query: LivequeryRequest): Promise<import("mongodb").DeleteResult | import("mongoose").UpdateWriteOpResult | {
|
|
9
|
+
query(_: LivequeryRequest, config: RouteOptions, connection: Connection): Promise<mongoose.mongo.DeleteResult | mongoose.UpdateWriteOpResult | {
|
|
25
10
|
items: any[];
|
|
26
11
|
summary: any;
|
|
27
12
|
has: {
|
|
@@ -45,4 +30,15 @@ export declare class MongooseDatasource implements LivequeryDatasource<RouteOpti
|
|
|
45
30
|
} | {
|
|
46
31
|
item: any;
|
|
47
32
|
}>;
|
|
33
|
+
normalizeRef(req: LivequeryRequest, schema: mongoose.Schema): Promise<LivequeryRequest | {
|
|
34
|
+
keys: any;
|
|
35
|
+
body: any;
|
|
36
|
+
ref: string;
|
|
37
|
+
is_collection: boolean;
|
|
38
|
+
collection_ref: string;
|
|
39
|
+
schema_collection_ref: string;
|
|
40
|
+
doc_id?: string;
|
|
41
|
+
options: import("@livequery/types").QueryOption<LivequeryBaseEntity>;
|
|
42
|
+
method?: string;
|
|
43
|
+
}>;
|
|
48
44
|
}
|
|
@@ -1,46 +1,19 @@
|
|
|
1
1
|
import { Cursor } from './Cursor.js';
|
|
2
|
-
import { map, mergeAll, Subject, pipe } from "rxjs";
|
|
3
2
|
import { MongoQuery } from "./MongoQuery.js";
|
|
3
|
+
import mongoose from 'mongoose';
|
|
4
4
|
import { ObjectId } from 'bson';
|
|
5
|
+
import { SmartCache } from './SmartCache.js';
|
|
5
6
|
export class MongooseDatasource {
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
$ = new Subject;
|
|
11
|
-
init({ deps, rawChanges, routes }) {
|
|
12
|
-
this.#conections = { ...deps };
|
|
13
|
-
rawChanges.pipe(this.#stream()).subscribe(this.$);
|
|
14
|
-
for (const { refs, ...options } of routes) {
|
|
15
|
-
for (const ref of refs) {
|
|
16
|
-
const schema_ref = ref.replaceAll(':', '');
|
|
17
|
-
this.#routes.set(schema_ref, options);
|
|
18
|
-
this.#refs.set(options.schema, new Set([
|
|
19
|
-
...this.#refs.get(options.schema) || [],
|
|
20
|
-
ref
|
|
21
|
-
]));
|
|
22
|
-
if (options.realtime) {
|
|
23
|
-
const table_name = options.schema.options.collection;
|
|
24
|
-
const $ = schema_ref.split('/');
|
|
25
|
-
const is_collection_ref = $.length % 2 == 1;
|
|
26
|
-
this.#realtime_collections.set(table_name, new Set([
|
|
27
|
-
...this.#realtime_collections.get(table_name) || [],
|
|
28
|
-
is_collection_ref ? schema_ref : $.slice(0, -1).join('/'),
|
|
29
|
-
schema_ref
|
|
30
|
-
]));
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
async query(query) {
|
|
36
|
-
const config = this.#routes.get(query.schema_ref);
|
|
37
|
-
if (!config)
|
|
38
|
-
throw { status: 500, code: 'REF_NOT_FOUND', message: 'Missing ref config in livequery system' };
|
|
39
|
-
const db = await config.db?.(query) || process.env.DB_NAME;
|
|
40
|
-
const connection = config.connection ? this.#conections[await config.connection(query)] : Object.values(this.#conections)[0];
|
|
7
|
+
#schemas = new SmartCache();
|
|
8
|
+
#models = new SmartCache();
|
|
9
|
+
async query(_, config, connection) {
|
|
10
|
+
const db = typeof config.db == 'function' ? await config.db(_) : config.db || process.env.DB_NAME || 'main';
|
|
41
11
|
const schema = config.schema;
|
|
42
12
|
const collection_name = schema.options.collection;
|
|
43
|
-
const model =
|
|
13
|
+
const model = await this.#models.get(`${db}::${collection_name}`, async () => {
|
|
14
|
+
return connection.model(collection_name, schema, collection_name);
|
|
15
|
+
});
|
|
16
|
+
const query = await this.normalizeRef(_, schema);
|
|
44
17
|
if (query.method == 'get')
|
|
45
18
|
return await this.#get(query, model);
|
|
46
19
|
if (query.method == 'post')
|
|
@@ -155,64 +128,32 @@ export class MongooseDatasource {
|
|
|
155
128
|
}, {});
|
|
156
129
|
return await model.deleteOne(keys);
|
|
157
130
|
}
|
|
158
|
-
#
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return n.map(nn => {
|
|
168
|
-
return {
|
|
169
|
-
old_ref: `${old_ref}/${nn.old_ref}`,
|
|
170
|
-
new_ref: `${new_ref}/${nn.new_ref}`
|
|
171
|
-
};
|
|
172
|
-
});
|
|
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,
|
|
187
|
-
});
|
|
188
|
-
return p;
|
|
189
|
-
}, new Map());
|
|
190
|
-
}
|
|
191
|
-
return {
|
|
192
|
-
v: {
|
|
193
|
-
old_ref: o[k],
|
|
194
|
-
new_ref: n[k]
|
|
131
|
+
#convert(obj, fields) {
|
|
132
|
+
return {
|
|
133
|
+
...obj,
|
|
134
|
+
...[...fields].reduce((p, c) => {
|
|
135
|
+
if (obj[c] && typeof obj[c] == 'string' && mongoose.Types.ObjectId.isValid(obj[c])) {
|
|
136
|
+
return {
|
|
137
|
+
...p,
|
|
138
|
+
[c]: new mongoose.Types.ObjectId(obj[c])
|
|
139
|
+
};
|
|
195
140
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
141
|
+
return p;
|
|
142
|
+
}, {})
|
|
143
|
+
};
|
|
199
144
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
new_ref
|
|
214
|
-
}));
|
|
215
|
-
});
|
|
216
|
-
}), mergeAll(), mergeAll());
|
|
145
|
+
async normalizeRef(req, schema) {
|
|
146
|
+
const fields = await this.#schemas.get(schema, async () => {
|
|
147
|
+
return new Set(Object.entries(schema.paths).filter(([k, v]) => {
|
|
148
|
+
return v.instance == 'ObjectID';
|
|
149
|
+
}).map(([k, v]) => k));
|
|
150
|
+
});
|
|
151
|
+
if (fields.size == 0)
|
|
152
|
+
return req;
|
|
153
|
+
return {
|
|
154
|
+
...req,
|
|
155
|
+
keys: this.#convert(req.keys, fields),
|
|
156
|
+
body: this.#convert(req.body, fields)
|
|
157
|
+
};
|
|
217
158
|
}
|
|
218
159
|
}
|
|
@@ -3,6 +3,6 @@ import { Schema } from "mongoose";
|
|
|
3
3
|
export type RouteOptions<T = any> = {
|
|
4
4
|
realtime?: boolean;
|
|
5
5
|
schema: Schema<T>;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
db?: string | ((req: LivequeryRequest) => Promise<string> | string);
|
|
7
|
+
object_ids_fields: string[];
|
|
8
8
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/cursor.ts","../src/datachangepayload.ts","../src/mongoquery.ts","../src/mongoosedatasource.ts","../src/routeoptions.ts","../src/index.ts"],"version":"5.
|
|
1
|
+
{"root":["../src/cursor.ts","../src/datachangepayload.ts","../src/mongoquery.ts","../src/mongoosedatasource.ts","../src/routeoptions.ts","../src/smartcache.ts","../src/index.ts"],"version":"5.9.3"}
|
package/package.json
CHANGED