@livequery/mongoose 2.0.52 → 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.
@@ -2,12 +2,12 @@ import { LivequeryRequest, LivequeryBaseEntity, WebsocketSyncPayload } from '@li
2
2
  import mongoose, { Connection, Schema } from 'mongoose';
3
3
  import { Subject } from 'rxjs/internal/Subject';
4
4
  import { Observable } from 'rxjs';
5
- export type LivequeryDatasource<Config, RouteOptions> = Observable<WebsocketSyncPayload<LivequeryBaseEntity>> & {
6
- init?: (config: Config, options: Array<{
5
+ export type LivequeryDatasource<RouteOptions> = Observable<WebsocketSyncPayload<LivequeryBaseEntity>> & {
6
+ init: (routes: Array<{
7
7
  path: string;
8
8
  options: RouteOptions;
9
- }>) => Promise<void>;
10
- query?: (query: LivequeryRequest, config: Config, options: RouteOptions) => any;
9
+ }>) => any;
10
+ query?: (query: LivequeryRequest, options: RouteOptions) => any;
11
11
  };
12
12
  export type MongooseDatasourceConfig = {
13
13
  connections: {
@@ -19,11 +19,18 @@ export type RouteOptions<T = any> = {
19
19
  realtime?: boolean;
20
20
  schema: Schema<T>;
21
21
  db?: string | ((req: LivequeryRequest) => Promise<string> | string);
22
+ connection?: string | ((req: LivequeryRequest) => Promise<string> | string);
22
23
  };
23
- export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any>> implements LivequeryDatasource<MongooseDatasourceConfig, RouteOptions> {
24
+ export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any>> implements LivequeryDatasource<RouteOptions> {
24
25
  #private;
26
+ private config;
25
27
  readonly refs: Map<string, Set<string>>;
26
- query(req: LivequeryRequest, config: MongooseDatasourceConfig, options: RouteOptions): 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 | {
27
34
  items: any[];
28
35
  summary: any;
29
36
  has: {
@@ -47,5 +54,8 @@ export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any
47
54
  } | {
48
55
  item: any;
49
56
  }>;
50
- 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;
51
61
  }
@@ -3,18 +3,32 @@ 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(req, config, options) {
11
- const db = typeof options.db == 'function' ? await options.db(req) : options.db || process.env.DB_NAME || 'main';
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');
12
23
  const schema = options.schema;
13
24
  const collection_name = schema.options.collection;
14
- const model = await this.#models.get(`${db}|${collection_name}`, async () => {
15
- return config.connections[db].model(collection_name, schema, collection_name);
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` };
29
+ return connection.model(collection_name, schema, collection_name);
16
30
  });
17
- const query = await this.normalizeRef(req, schema);
31
+ const query = await this.#normalizeRef(req, schema);
18
32
  if (query.method == 'get')
19
33
  return await this.#get(query, model);
20
34
  if (query.method == 'post')
@@ -143,7 +157,7 @@ export class MongooseDatasource extends Subject {
143
157
  }, {})
144
158
  };
145
159
  }
146
- async normalizeRef(req, schema) {
160
+ async #normalizeRef(req, schema) {
147
161
  const fields = await this.#schemas.get(schema, async () => {
148
162
  return new Set(Object.entries(schema.paths).filter(([k, v]) => {
149
163
  return (v.instance == 'Array' ? v.getEmbeddedSchemaType().instance : v.instance) == 'ObjectId';
@@ -157,4 +171,7 @@ export class MongooseDatasource extends Subject {
157
171
  ...req.body ? { body: this.#convert(req.body, fields) } : {}
158
172
  };
159
173
  }
174
+ link(watcher) {
175
+ return this.#routes$.pipe(switchMap(routes => watcher(routes, this.config))).subscribe();
176
+ }
160
177
  }
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.52",
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",