@livequery/mongoose 2.0.48 → 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.
@@ -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
- if (value.toLowerCase() == 'null')
208
- return { $eq: null };
209
- if (value.toLowerCase() == 'true')
210
- return { $eq: true };
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-string': () => ({ $eq: value })
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-string'];
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, LivequeryBaseEntity, WebsocketSyncPayload } from '@livequery/types';
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
  }
@@ -7,16 +7,19 @@ import { Subject } from 'rxjs/internal/Subject';
7
7
  export class MongooseDatasource extends Subject {
8
8
  #schemas = new SmartCache();
9
9
  #models = new SmartCache();
10
+ refs = new Map();
10
11
  async query(_, config, connection) {
11
12
  const db = typeof config.db == 'function' ? await config.db(_) : config.db || process.env.DB_NAME || 'main';
12
13
  const schema = config.schema;
13
14
  const collection_name = schema.options.collection;
14
- const model = await this.#models.get(`${db}::${collection_name}`, async () => {
15
+ const model = await this.#models.get(`${db}|${collection_name}`, async () => {
15
16
  return connection.model(collection_name, schema, collection_name);
16
17
  });
17
18
  const query = await this.normalizeRef(_, schema);
18
- if (query.method == 'get')
19
+ if (query.method == 'get') {
20
+ config.realtime && this.#log(query, model);
19
21
  return await this.#get(query, model);
22
+ }
20
23
  if (query.method == 'post')
21
24
  return this.#post(query, model);
22
25
  if (query.method == 'put')
@@ -27,6 +30,12 @@ export class MongooseDatasource extends Subject {
27
30
  return this.#del(query, model);
28
31
  throw { status: 500, code: 'INVAILD_METHOD', message: 'Invaild method' };
29
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
+ }
30
39
  async #get(req, model) {
31
40
  const { limit, items, count, has, summary } = await MongoQuery.query(req, model);
32
41
  const current = items.length;
@@ -146,17 +155,15 @@ export class MongooseDatasource extends Subject {
146
155
  async normalizeRef(req, schema) {
147
156
  const fields = await this.#schemas.get(schema, async () => {
148
157
  return new Set(Object.entries(schema.paths).filter(([k, v]) => {
149
- return v.instance == 'ObjectID';
158
+ return (v.instance == 'Array' ? v.getEmbeddedSchemaType().instance : v.instance) == 'ObjectID';
150
159
  }).map(([k, v]) => k));
151
160
  });
152
161
  if (fields.size == 0)
153
162
  return req;
154
163
  return {
155
164
  ...req,
156
- keys: this.#convert(req.keys, fields),
157
- body: this.#convert(req.body, fields)
165
+ ...req.keys ? { keys: this.#convert(req.keys, fields) } : {},
166
+ ...req.body ? { body: this.#convert(req.body, fields) } : {}
158
167
  };
159
168
  }
160
- async broadcast() {
161
- }
162
169
  }
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "repository": {
7
7
  "url": "git@github.com:livequery/mongoose.git"
8
8
  },
9
- "version": "2.0.48",
9
+ "version": "2.0.50",
10
10
  "description": "Mongoose datasource mapping for @livequery ecosystem",
11
11
  "main": "./build/src/index.js",
12
12
  "types": "./build/src/index.d.ts",