@livequery/mongoose 2.0.53 → 2.0.55

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,13 +1,13 @@
1
1
  import { LivequeryRequest, LivequeryBaseEntity, WebsocketSyncPayload } from '@livequery/types';
2
2
  import mongoose, { Connection, Schema } from 'mongoose';
3
- import { Subject } from 'rxjs/internal/Subject';
4
- import { Observable } from 'rxjs';
5
- export type LivequeryDatasource<RouteOptions> = Observable<WebsocketSyncPayload<LivequeryBaseEntity>> & {
6
- init: (routes: Array<{
3
+ import { Subject } from 'rxjs';
4
+ export type LivequeryDatasource<Config, RouteOptions> = Subject<WebsocketSyncPayload<LivequeryBaseEntity>> & {
5
+ init(config: Config, routes: Array<{
7
6
  path: string;
7
+ method: number;
8
8
  options: RouteOptions;
9
- }>) => any;
10
- query?: (query: LivequeryRequest, options: RouteOptions) => any;
9
+ }>): Promise<void>;
10
+ query: (query: LivequeryRequest, options: RouteOptions) => Promise<any>;
11
11
  };
12
12
  export type MongooseDatasourceConfig = {
13
13
  connections: {
@@ -21,16 +21,17 @@ export type RouteOptions<T = any> = {
21
21
  db?: string | ((req: LivequeryRequest) => Promise<string> | string);
22
22
  connection?: string | ((req: LivequeryRequest) => Promise<string> | string);
23
23
  };
24
- export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any>> implements LivequeryDatasource<RouteOptions> {
24
+ export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<LivequeryBaseEntity>> implements LivequeryDatasource<MongooseDatasourceConfig, RouteOptions> {
25
25
  #private;
26
- private config;
27
26
  readonly refs: Map<string, Set<string>>;
28
- constructor(config: MongooseDatasourceConfig);
29
- init(routes: {
27
+ config: MongooseDatasourceConfig;
28
+ routes: Map<string, RouteOptions<any>>;
29
+ init(config: MongooseDatasourceConfig, routes: Array<{
30
30
  path: string;
31
- options: RouteOptions<any>;
32
- }[]): Promise<void>;
33
- query(req: LivequeryRequest, options: RouteOptions): Promise<mongoose.UpdateWriteOpResult | mongoose.mongo.DeleteResult | {
31
+ method: number;
32
+ options: RouteOptions;
33
+ }>): Promise<void>;
34
+ query(req: LivequeryRequest, options: RouteOptions): Promise<mongoose.mongo.DeleteResult | mongoose.UpdateWriteOpResult | {
34
35
  items: any[];
35
36
  summary: any;
36
37
  has: {
@@ -54,8 +55,4 @@ export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any
54
55
  } | {
55
56
  item: any;
56
57
  }>;
57
- link(watcher: (routes: Array<{
58
- path: string;
59
- options: RouteOptions;
60
- }>, config: MongooseDatasourceConfig) => Observable<WebsocketSyncPayload>): import("rxjs").Subscription;
61
58
  }
@@ -2,20 +2,22 @@ import { Cursor } from './Cursor.js';
2
2
  import { MongoQuery } from "./MongoQuery.js";
3
3
  import { ObjectId } from 'bson';
4
4
  import { SmartCache } from './SmartCache.js';
5
- import { Subject } from 'rxjs/internal/Subject';
6
- import { BehaviorSubject, switchMap } from 'rxjs';
5
+ import { Subject } from 'rxjs';
7
6
  export class MongooseDatasource extends Subject {
8
- config;
9
7
  #schemas = new SmartCache();
10
8
  #models = new SmartCache();
11
9
  refs = new Map();
12
- constructor(config) {
13
- super();
10
+ config;
11
+ routes;
12
+ async init(config, routes) {
14
13
  this.config = config;
15
- }
16
- #routes$ = new BehaviorSubject([]);
17
- async init(routes) {
18
- this.#routes$.next(routes);
14
+ this.routes = routes.reduce((p, c) => {
15
+ const set = p.get(c.path);
16
+ if (set && set.schema != c.options.schema)
17
+ throw new Error('Schema mismatch for route path "' + c.path + '"');
18
+ p.set(c.path, c.options);
19
+ return p;
20
+ }, new Map());
19
21
  }
20
22
  async query(req, options) {
21
23
  const connection_name = typeof options.connection == 'function' ? await options.connection(req) : (options.connection || Object.keys(this.config.connections)[0] || 'default');
@@ -171,7 +173,4 @@ export class MongooseDatasource extends Subject {
171
173
  ...req.body ? { body: this.#convert(req.body, fields) } : {}
172
174
  };
173
175
  }
174
- link(watcher) {
175
- return this.#routes$.pipe(switchMap(routes => watcher(routes, this.config))).subscribe();
176
- }
177
176
  }
@@ -1,2 +1,2 @@
1
- export { MongooseDatasource, LivequeryDatasource, MongooseDatasourceConfig, RouteOptions } from './MongooseDatasource.js';
1
+ export * from './MongooseDatasource.js';
2
2
  export * from './DataChangePayload.js';
@@ -1,2 +1,2 @@
1
- export { MongooseDatasource } from './MongooseDatasource.js';
1
+ export * from './MongooseDatasource.js';
2
2
  export * from './DataChangePayload.js';
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.53",
9
+ "version": "2.0.55",
10
10
  "description": "Mongoose datasource mapping for @livequery ecosystem",
11
11
  "main": "./build/src/index.js",
12
12
  "types": "./build/src/index.d.ts",