@livequery/mongoose 2.0.48 → 2.0.51
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,5 +1,5 @@
|
|
|
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
4
|
import { Subject } from 'rxjs/internal/Subject';
|
|
5
5
|
export type LivequeryDatasource<T> = {
|
|
@@ -7,6 +7,7 @@ export type LivequeryDatasource<T> = {
|
|
|
7
7
|
};
|
|
8
8
|
export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any>> implements LivequeryDatasource<RouteOptions> {
|
|
9
9
|
#private;
|
|
10
|
+
readonly refs: Map<string, Set<string>>;
|
|
10
11
|
query(_: LivequeryRequest, config: RouteOptions, connection: Connection): Promise<mongoose.mongo.DeleteResult | mongoose.UpdateWriteOpResult | {
|
|
11
12
|
items: any[];
|
|
12
13
|
summary: any;
|
|
@@ -31,16 +32,5 @@ export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any
|
|
|
31
32
|
} | {
|
|
32
33
|
item: any;
|
|
33
34
|
}>;
|
|
34
|
-
normalizeRef(req: LivequeryRequest, schema: mongoose.Schema): Promise<LivequeryRequest
|
|
35
|
-
keys: any;
|
|
36
|
-
body: any;
|
|
37
|
-
ref: string;
|
|
38
|
-
is_collection: boolean;
|
|
39
|
-
collection_ref: string;
|
|
40
|
-
schema_collection_ref: string;
|
|
41
|
-
doc_id?: string;
|
|
42
|
-
options: import("@livequery/types").QueryOption<LivequeryBaseEntity>;
|
|
43
|
-
method?: string;
|
|
44
|
-
}>;
|
|
45
|
-
broadcast(): Promise<void>;
|
|
35
|
+
normalizeRef(req: LivequeryRequest, schema: mongoose.Schema): Promise<LivequeryRequest>;
|
|
46
36
|
}
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import { Cursor } from './Cursor.js';
|
|
2
2
|
import { MongoQuery } from "./MongoQuery.js";
|
|
3
|
-
import mongoose from 'mongoose';
|
|
4
3
|
import { ObjectId } from 'bson';
|
|
5
4
|
import { SmartCache } from './SmartCache.js';
|
|
6
5
|
import { Subject } from 'rxjs/internal/Subject';
|
|
7
6
|
export class MongooseDatasource extends Subject {
|
|
8
7
|
#schemas = new SmartCache();
|
|
9
8
|
#models = new SmartCache();
|
|
9
|
+
refs = new Map();
|
|
10
10
|
async query(_, config, connection) {
|
|
11
11
|
const db = typeof config.db == 'function' ? await config.db(_) : config.db || process.env.DB_NAME || 'main';
|
|
12
12
|
const schema = config.schema;
|
|
13
13
|
const collection_name = schema.options.collection;
|
|
14
|
-
const model = await this.#models.get(`${db}
|
|
14
|
+
const model = await this.#models.get(`${db}|${collection_name}`, async () => {
|
|
15
15
|
return connection.model(collection_name, schema, collection_name);
|
|
16
16
|
});
|
|
17
17
|
const query = await this.normalizeRef(_, schema);
|
|
18
|
-
if (query.method == 'get')
|
|
18
|
+
if (query.method == 'get') {
|
|
19
|
+
config.realtime && this.#log(query, model);
|
|
19
20
|
return await this.#get(query, model);
|
|
21
|
+
}
|
|
20
22
|
if (query.method == 'post')
|
|
21
23
|
return this.#post(query, model);
|
|
22
24
|
if (query.method == 'put')
|
|
@@ -27,6 +29,12 @@ export class MongooseDatasource extends Subject {
|
|
|
27
29
|
return this.#del(query, model);
|
|
28
30
|
throw { status: 500, code: 'INVAILD_METHOD', message: 'Invaild method' };
|
|
29
31
|
}
|
|
32
|
+
#log(req, model) {
|
|
33
|
+
const _ = this.refs.get(model.collection.name);
|
|
34
|
+
const set = _ || new Set();
|
|
35
|
+
set.add(req.collection_ref);
|
|
36
|
+
!_ && this.refs.set(model.collection.name, set);
|
|
37
|
+
}
|
|
30
38
|
async #get(req, model) {
|
|
31
39
|
const { limit, items, count, has, summary } = await MongoQuery.query(req, model);
|
|
32
40
|
const current = items.length;
|
|
@@ -133,10 +141,10 @@ export class MongooseDatasource extends Subject {
|
|
|
133
141
|
return {
|
|
134
142
|
...obj,
|
|
135
143
|
...[...fields].reduce((p, c) => {
|
|
136
|
-
if (obj[c] && typeof obj[c] == 'string' &&
|
|
144
|
+
if (obj[c] && typeof obj[c] == 'string' && ObjectId.isValid(obj[c])) {
|
|
137
145
|
return {
|
|
138
146
|
...p,
|
|
139
|
-
[c]:
|
|
147
|
+
[c]: ObjectId.createFromHexString(obj[c])
|
|
140
148
|
};
|
|
141
149
|
}
|
|
142
150
|
return p;
|
|
@@ -146,17 +154,15 @@ export class MongooseDatasource extends Subject {
|
|
|
146
154
|
async normalizeRef(req, schema) {
|
|
147
155
|
const fields = await this.#schemas.get(schema, async () => {
|
|
148
156
|
return new Set(Object.entries(schema.paths).filter(([k, v]) => {
|
|
149
|
-
return v.instance == '
|
|
150
|
-
}).map(([k
|
|
157
|
+
return (v.instance == 'Array' ? v.getEmbeddedSchemaType().instance : v.instance) == 'ObjectId';
|
|
158
|
+
}).map(([k]) => k));
|
|
151
159
|
});
|
|
152
160
|
if (fields.size == 0)
|
|
153
161
|
return req;
|
|
154
162
|
return {
|
|
155
163
|
...req,
|
|
156
|
-
keys: this.#convert(req.keys, fields),
|
|
157
|
-
body: this.#convert(req.body, fields)
|
|
164
|
+
...req.keys ? { keys: this.#convert(req.keys, fields) } : {},
|
|
165
|
+
...req.body ? { body: this.#convert(req.body, fields) } : {}
|
|
158
166
|
};
|
|
159
167
|
}
|
|
160
|
-
async broadcast() {
|
|
161
|
-
}
|
|
162
168
|
}
|
package/package.json
CHANGED