@livequery/mongoose 2.0.51 → 2.0.53

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,14 +1,36 @@
1
- import { RouteOptions } from "./RouteOptions.js";
2
- import { LivequeryRequest, WebsocketSyncPayload } from '@livequery/types';
3
- import mongoose, { Connection } from 'mongoose';
1
+ import { LivequeryRequest, LivequeryBaseEntity, WebsocketSyncPayload } from '@livequery/types';
2
+ import mongoose, { Connection, Schema } from 'mongoose';
4
3
  import { Subject } from 'rxjs/internal/Subject';
5
- export type LivequeryDatasource<T> = {
6
- query(query: LivequeryRequest, options: T, connection: any): any;
4
+ import { Observable } from 'rxjs';
5
+ export type LivequeryDatasource<RouteOptions> = Observable<WebsocketSyncPayload<LivequeryBaseEntity>> & {
6
+ init: (routes: Array<{
7
+ path: string;
8
+ options: RouteOptions;
9
+ }>) => any;
10
+ query?: (query: LivequeryRequest, options: RouteOptions) => any;
11
+ };
12
+ export type MongooseDatasourceConfig = {
13
+ connections: {
14
+ [key: string]: Connection;
15
+ };
16
+ databases: string[];
17
+ };
18
+ export type RouteOptions<T = any> = {
19
+ realtime?: boolean;
20
+ schema: Schema<T>;
21
+ db?: string | ((req: LivequeryRequest) => Promise<string> | string);
22
+ connection?: string | ((req: LivequeryRequest) => Promise<string> | string);
7
23
  };
8
24
  export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any>> implements LivequeryDatasource<RouteOptions> {
9
25
  #private;
26
+ private config;
10
27
  readonly refs: Map<string, Set<string>>;
11
- query(_: LivequeryRequest, config: RouteOptions, connection: Connection): Promise<mongoose.mongo.DeleteResult | mongoose.UpdateWriteOpResult | {
28
+ constructor(config: MongooseDatasourceConfig);
29
+ init(routes: {
30
+ path: string;
31
+ options: RouteOptions<any>;
32
+ }[]): Promise<void>;
33
+ query(req: LivequeryRequest, options: RouteOptions): Promise<mongoose.UpdateWriteOpResult | mongoose.mongo.DeleteResult | {
12
34
  items: any[];
13
35
  summary: any;
14
36
  has: {
@@ -32,5 +54,8 @@ export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any
32
54
  } | {
33
55
  item: any;
34
56
  }>;
35
- normalizeRef(req: LivequeryRequest, schema: mongoose.Schema): Promise<LivequeryRequest>;
57
+ link(watcher: (routes: Array<{
58
+ path: string;
59
+ options: RouteOptions;
60
+ }>, config: MongooseDatasourceConfig) => Observable<WebsocketSyncPayload>): import("rxjs").Subscription;
36
61
  }
@@ -3,22 +3,34 @@ import { MongoQuery } from "./MongoQuery.js";
3
3
  import { ObjectId } from 'bson';
4
4
  import { SmartCache } from './SmartCache.js';
5
5
  import { Subject } from 'rxjs/internal/Subject';
6
+ import { BehaviorSubject, switchMap } from 'rxjs';
6
7
  export class MongooseDatasource extends Subject {
8
+ config;
7
9
  #schemas = new SmartCache();
8
10
  #models = new SmartCache();
9
11
  refs = new Map();
10
- async query(_, config, connection) {
11
- const db = typeof config.db == 'function' ? await config.db(_) : config.db || process.env.DB_NAME || 'main';
12
- const schema = config.schema;
12
+ constructor(config) {
13
+ super();
14
+ this.config = config;
15
+ }
16
+ #routes$ = new BehaviorSubject([]);
17
+ async init(routes) {
18
+ this.#routes$.next(routes);
19
+ }
20
+ async query(req, options) {
21
+ const connection_name = typeof options.connection == 'function' ? await options.connection(req) : (options.connection || Object.keys(this.config.connections)[0] || 'default');
22
+ const db = typeof options.db == 'function' ? await options.db(req) : (options.db || process.env.DB_NAME || 'main');
23
+ const schema = options.schema;
13
24
  const collection_name = schema.options.collection;
14
- const model = await this.#models.get(`${db}|${collection_name}`, async () => {
25
+ const model = await this.#models.get(`${collection_name}|${db}|${collection_name}`, async () => {
26
+ const connection = this.config.connections[connection_name];
27
+ if (!connection)
28
+ throw { status: 500, code: 'DB_CONNECTION_NOT_FOUND', message: `Database connection "${connection_name}" not found` };
15
29
  return connection.model(collection_name, schema, collection_name);
16
30
  });
17
- const query = await this.normalizeRef(_, schema);
18
- if (query.method == 'get') {
19
- config.realtime && this.#log(query, model);
31
+ const query = await this.#normalizeRef(req, schema);
32
+ if (query.method == 'get')
20
33
  return await this.#get(query, model);
21
- }
22
34
  if (query.method == 'post')
23
35
  return this.#post(query, model);
24
36
  if (query.method == 'put')
@@ -29,12 +41,6 @@ export class MongooseDatasource extends Subject {
29
41
  return this.#del(query, model);
30
42
  throw { status: 500, code: 'INVAILD_METHOD', message: 'Invaild method' };
31
43
  }
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
- }
38
44
  async #get(req, model) {
39
45
  const { limit, items, count, has, summary } = await MongoQuery.query(req, model);
40
46
  const current = items.length;
@@ -151,7 +157,7 @@ export class MongooseDatasource extends Subject {
151
157
  }, {})
152
158
  };
153
159
  }
154
- async normalizeRef(req, schema) {
160
+ async #normalizeRef(req, schema) {
155
161
  const fields = await this.#schemas.get(schema, async () => {
156
162
  return new Set(Object.entries(schema.paths).filter(([k, v]) => {
157
163
  return (v.instance == 'Array' ? v.getEmbeddedSchemaType().instance : v.instance) == 'ObjectId';
@@ -165,4 +171,7 @@ export class MongooseDatasource extends Subject {
165
171
  ...req.body ? { body: this.#convert(req.body, fields) } : {}
166
172
  };
167
173
  }
174
+ link(watcher) {
175
+ return this.#routes$.pipe(switchMap(routes => watcher(routes, this.config))).subscribe();
176
+ }
168
177
  }
@@ -1,3 +1,2 @@
1
- export { MongooseDatasource } from './MongooseDatasource.js';
1
+ export { MongooseDatasource, LivequeryDatasource, MongooseDatasourceConfig, RouteOptions } from './MongooseDatasource.js';
2
2
  export * from './DataChangePayload.js';
3
- export * from './RouteOptions.js';
@@ -1,3 +1,2 @@
1
1
  export { MongooseDatasource } from './MongooseDatasource.js';
2
2
  export * from './DataChangePayload.js';
3
- export * from './RouteOptions.js';
@@ -1 +1 @@
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"}
1
+ {"root":["../src/cursor.ts","../src/datachangepayload.ts","../src/mongoquery.ts","../src/mongoosedatasource.ts","../src/smartcache.ts","../src/index.ts"],"version":"5.9.3"}
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.51",
9
+ "version": "2.0.53",
10
10
  "description": "Mongoose datasource mapping for @livequery ecosystem",
11
11
  "main": "./build/src/index.js",
12
12
  "types": "./build/src/index.d.ts",
@@ -32,7 +32,7 @@
32
32
  "build": "rm -rf build; tsc -b .",
33
33
  "deploy": "rm -rf build && yarn build; git add .; git commit -m \"Update\"; git push origin master; npm publish --access public"
34
34
  },
35
- "dependencies": {
35
+ "peerDependencies": {
36
36
  "bson": "*",
37
37
  "mongoose": "^8.6.2"
38
38
  }
@@ -1,7 +0,0 @@
1
- import { LivequeryRequest } from "@livequery/types";
2
- import { Schema } from "mongoose";
3
- export type RouteOptions<T = any> = {
4
- realtime?: boolean;
5
- schema: Schema<T>;
6
- db?: string | ((req: LivequeryRequest) => Promise<string> | string);
7
- };
@@ -1 +0,0 @@
1
- export {};