@livequery/mongoose 2.0.51 → 2.0.52

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,29 @@
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<Config, RouteOptions> = Observable<WebsocketSyncPayload<LivequeryBaseEntity>> & {
6
+ init?: (config: Config, options: Array<{
7
+ path: string;
8
+ options: RouteOptions;
9
+ }>) => Promise<void>;
10
+ query?: (query: LivequeryRequest, config: Config, options: RouteOptions) => any;
7
11
  };
8
- export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any>> implements LivequeryDatasource<RouteOptions> {
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
+ };
23
+ export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any>> implements LivequeryDatasource<MongooseDatasourceConfig, RouteOptions> {
9
24
  #private;
10
25
  readonly refs: Map<string, Set<string>>;
11
- query(_: LivequeryRequest, config: RouteOptions, connection: Connection): Promise<mongoose.mongo.DeleteResult | mongoose.UpdateWriteOpResult | {
26
+ query(req: LivequeryRequest, config: MongooseDatasourceConfig, options: RouteOptions): Promise<mongoose.mongo.DeleteResult | mongoose.UpdateWriteOpResult | {
12
27
  items: any[];
13
28
  summary: any;
14
29
  has: {
@@ -7,18 +7,16 @@ export class MongooseDatasource extends Subject {
7
7
  #schemas = new SmartCache();
8
8
  #models = new SmartCache();
9
9
  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;
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
+ const schema = options.schema;
13
13
  const collection_name = schema.options.collection;
14
14
  const model = await this.#models.get(`${db}|${collection_name}`, async () => {
15
- return connection.model(collection_name, schema, collection_name);
15
+ return config.connections[db].model(collection_name, schema, collection_name);
16
16
  });
17
- const query = await this.normalizeRef(_, schema);
18
- if (query.method == 'get') {
19
- config.realtime && this.#log(query, model);
17
+ const query = await this.normalizeRef(req, schema);
18
+ if (query.method == 'get')
20
19
  return await this.#get(query, model);
21
- }
22
20
  if (query.method == 'post')
23
21
  return this.#post(query, model);
24
22
  if (query.method == 'put')
@@ -29,12 +27,6 @@ export class MongooseDatasource extends Subject {
29
27
  return this.#del(query, model);
30
28
  throw { status: 500, code: 'INVAILD_METHOD', message: 'Invaild method' };
31
29
  }
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
30
  async #get(req, model) {
39
31
  const { limit, items, count, has, summary } = await MongoQuery.query(req, model);
40
32
  const current = items.length;
@@ -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.52",
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 {};