@livequery/mongoose 2.0.47 → 2.0.50
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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Cursor } from "./Cursor.js";
|
|
2
2
|
import { ObjectId } from "bson";
|
|
3
|
+
import { Types } from 'mongoose';
|
|
3
4
|
export class MongoQuery {
|
|
4
5
|
static #is_operator(c) {
|
|
5
6
|
return ['+', '-', '*', '/', '(', ')', '~'].indexOf(c) !== -1;
|
|
@@ -203,37 +204,26 @@ export class MongoQuery {
|
|
|
203
204
|
return p;
|
|
204
205
|
const [key, expression] = k.split(':');
|
|
205
206
|
const map = {
|
|
206
|
-
eq: () => {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
if (value.toLowerCase() == 'false')
|
|
212
|
-
return { $eq: false };
|
|
213
|
-
if (!isNaN(Number(value)))
|
|
214
|
-
return { $eq: Number(value) };
|
|
215
|
-
return { $eq: value };
|
|
216
|
-
},
|
|
217
|
-
lt: () => ({ $lt: Number(value) }),
|
|
218
|
-
lte: () => ({ $lte: Number(value) }),
|
|
219
|
-
gt: () => ({ $gt: Number(value) }),
|
|
220
|
-
gte: () => ({ $gte: Number(value) }),
|
|
207
|
+
eq: () => ({ $eq: value }),
|
|
208
|
+
lt: () => ({ $lt: !isNaN(Number(value)) ? Number(value) : 0 }),
|
|
209
|
+
lte: () => ({ $lte: !isNaN(Number(value)) ? Number(value) : 0 }),
|
|
210
|
+
gt: () => ({ $gt: !isNaN(Number(value)) ? Number(value) : 0 }),
|
|
211
|
+
gte: () => ({ $gte: !isNaN(Number(value)) ? Number(value) : 0 }),
|
|
221
212
|
ne: () => {
|
|
222
|
-
if (value.toLowerCase() == 'null')
|
|
223
|
-
return { $ne: null };
|
|
224
|
-
if (value.toLowerCase() == 'true')
|
|
225
|
-
return { $ne: true };
|
|
226
|
-
if (value.toLowerCase() == 'false')
|
|
227
|
-
return { $ne: false };
|
|
228
|
-
if (!isNaN(Number(value)))
|
|
229
|
-
return { $ne: Number(value) };
|
|
230
213
|
return { $ne: value };
|
|
231
214
|
},
|
|
232
215
|
in: () => ({ $in: JSON.parse(value) }),
|
|
233
216
|
nin: () => ({ $nin: JSON.parse(value) }),
|
|
234
|
-
'eq-
|
|
217
|
+
'eq-number': () => ({ $eq: !isNaN(Number(value)) ? Number(value) : 0 }),
|
|
218
|
+
'neq-number': () => ({ $ne: !isNaN(Number(value)) ? Number(value) : 0 }),
|
|
219
|
+
'eq-boolean': () => ({ $eq: `${value}`.toLowerCase() == 'true' ? true : false }),
|
|
220
|
+
'neq-boolean': () => ({ $ne: `${value}`.toLowerCase() == 'false' ? false : true }),
|
|
221
|
+
'eq-null': () => ({ $eq: null }),
|
|
222
|
+
'neq-null': () => ({ $ne: null }),
|
|
223
|
+
'eq-oid': () => ({ $eq: Types.ObjectId.isValid(value) ? new Types.ObjectId(value) : value }),
|
|
224
|
+
'neq-oid': () => ({ $ne: Types.ObjectId.isValid(value) ? new Types.ObjectId(value) : value }),
|
|
235
225
|
};
|
|
236
|
-
const fn = map[expression || 'eq
|
|
226
|
+
const fn = map[expression || 'eq'];
|
|
237
227
|
if (!fn)
|
|
238
228
|
return p;
|
|
239
229
|
return {
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { RouteOptions } from "./RouteOptions.js";
|
|
2
|
-
import { LivequeryRequest,
|
|
2
|
+
import { LivequeryRequest, WebsocketSyncPayload } from '@livequery/types';
|
|
3
3
|
import mongoose, { Connection } from 'mongoose';
|
|
4
|
+
import { Subject } from 'rxjs/internal/Subject';
|
|
4
5
|
export type LivequeryDatasource<T> = {
|
|
5
6
|
query(query: LivequeryRequest, options: T, connection: any): any;
|
|
6
7
|
};
|
|
7
|
-
export declare class MongooseDatasource implements LivequeryDatasource<RouteOptions> {
|
|
8
|
+
export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any>> implements LivequeryDatasource<RouteOptions> {
|
|
8
9
|
#private;
|
|
10
|
+
readonly refs: Map<string, Set<string>>;
|
|
9
11
|
query(_: LivequeryRequest, config: RouteOptions, connection: Connection): Promise<mongoose.mongo.DeleteResult | mongoose.UpdateWriteOpResult | {
|
|
10
12
|
items: any[];
|
|
11
13
|
summary: any;
|
|
@@ -30,15 +32,5 @@ export declare class MongooseDatasource implements LivequeryDatasource<RouteOpti
|
|
|
30
32
|
} | {
|
|
31
33
|
item: any;
|
|
32
34
|
}>;
|
|
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
|
-
}>;
|
|
35
|
+
normalizeRef(req: LivequeryRequest, schema: mongoose.Schema): Promise<LivequeryRequest>;
|
|
44
36
|
}
|
|
@@ -3,19 +3,23 @@ import { MongoQuery } from "./MongoQuery.js";
|
|
|
3
3
|
import mongoose from 'mongoose';
|
|
4
4
|
import { ObjectId } from 'bson';
|
|
5
5
|
import { SmartCache } from './SmartCache.js';
|
|
6
|
-
|
|
6
|
+
import { Subject } from 'rxjs/internal/Subject';
|
|
7
|
+
export class MongooseDatasource extends Subject {
|
|
7
8
|
#schemas = new SmartCache();
|
|
8
9
|
#models = new SmartCache();
|
|
10
|
+
refs = new Map();
|
|
9
11
|
async query(_, config, connection) {
|
|
10
12
|
const db = typeof config.db == 'function' ? await config.db(_) : config.db || process.env.DB_NAME || 'main';
|
|
11
13
|
const schema = config.schema;
|
|
12
14
|
const collection_name = schema.options.collection;
|
|
13
|
-
const model = await this.#models.get(`${db}
|
|
15
|
+
const model = await this.#models.get(`${db}|${collection_name}`, async () => {
|
|
14
16
|
return connection.model(collection_name, schema, collection_name);
|
|
15
17
|
});
|
|
16
18
|
const query = await this.normalizeRef(_, schema);
|
|
17
|
-
if (query.method == 'get')
|
|
19
|
+
if (query.method == 'get') {
|
|
20
|
+
config.realtime && this.#log(query, model);
|
|
18
21
|
return await this.#get(query, model);
|
|
22
|
+
}
|
|
19
23
|
if (query.method == 'post')
|
|
20
24
|
return this.#post(query, model);
|
|
21
25
|
if (query.method == 'put')
|
|
@@ -26,6 +30,12 @@ export class MongooseDatasource {
|
|
|
26
30
|
return this.#del(query, model);
|
|
27
31
|
throw { status: 500, code: 'INVAILD_METHOD', message: 'Invaild method' };
|
|
28
32
|
}
|
|
33
|
+
#log(req, model) {
|
|
34
|
+
const _ = this.refs.get(model.collection.name);
|
|
35
|
+
const set = _ || new Set();
|
|
36
|
+
set.add(req.collection_ref);
|
|
37
|
+
!_ && this.refs.set(model.collection.name, set);
|
|
38
|
+
}
|
|
29
39
|
async #get(req, model) {
|
|
30
40
|
const { limit, items, count, has, summary } = await MongoQuery.query(req, model);
|
|
31
41
|
const current = items.length;
|
|
@@ -145,15 +155,15 @@ export class MongooseDatasource {
|
|
|
145
155
|
async normalizeRef(req, schema) {
|
|
146
156
|
const fields = await this.#schemas.get(schema, async () => {
|
|
147
157
|
return new Set(Object.entries(schema.paths).filter(([k, v]) => {
|
|
148
|
-
return v.instance == 'ObjectID';
|
|
158
|
+
return (v.instance == 'Array' ? v.getEmbeddedSchemaType().instance : v.instance) == 'ObjectID';
|
|
149
159
|
}).map(([k, v]) => k));
|
|
150
160
|
});
|
|
151
161
|
if (fields.size == 0)
|
|
152
162
|
return req;
|
|
153
163
|
return {
|
|
154
164
|
...req,
|
|
155
|
-
keys: this.#convert(req.keys, fields),
|
|
156
|
-
body: this.#convert(req.body, fields)
|
|
165
|
+
...req.keys ? { keys: this.#convert(req.keys, fields) } : {},
|
|
166
|
+
...req.body ? { body: this.#convert(req.body, fields) } : {}
|
|
157
167
|
};
|
|
158
168
|
}
|
|
159
169
|
}
|
package/package.json
CHANGED