@livequery/mongoose 2.0.52 → 2.0.54
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
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
init?: (config: Config, options: 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
|
-
}>)
|
|
10
|
-
query
|
|
9
|
+
}>): Promise<void>;
|
|
10
|
+
query: (query: LivequeryRequest, options: RouteOptions) => Promise<any>;
|
|
11
11
|
};
|
|
12
12
|
export type MongooseDatasourceConfig = {
|
|
13
13
|
connections: {
|
|
@@ -19,11 +19,19 @@ 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<
|
|
24
|
+
export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<LivequeryBaseEntity>> implements LivequeryDatasource<MongooseDatasourceConfig, RouteOptions> {
|
|
24
25
|
#private;
|
|
25
26
|
readonly refs: Map<string, Set<string>>;
|
|
26
|
-
|
|
27
|
+
config: MongooseDatasourceConfig;
|
|
28
|
+
routes: Map<string, RouteOptions<any>>;
|
|
29
|
+
init(config: MongooseDatasourceConfig, routes: Array<{
|
|
30
|
+
path: string;
|
|
31
|
+
method: number;
|
|
32
|
+
options: RouteOptions;
|
|
33
|
+
}>): Promise<void>;
|
|
34
|
+
query(req: LivequeryRequest, options: RouteOptions): Promise<mongoose.mongo.DeleteResult | mongoose.UpdateWriteOpResult | {
|
|
27
35
|
items: any[];
|
|
28
36
|
summary: any;
|
|
29
37
|
has: {
|
|
@@ -47,5 +55,4 @@ export declare class MongooseDatasource extends Subject<WebsocketSyncPayload<any
|
|
|
47
55
|
} | {
|
|
48
56
|
item: any;
|
|
49
57
|
}>;
|
|
50
|
-
normalizeRef(req: LivequeryRequest, schema: mongoose.Schema): Promise<LivequeryRequest>;
|
|
51
58
|
}
|
|
@@ -2,19 +2,35 @@ 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
|
|
5
|
+
import { Subject } from 'rxjs';
|
|
6
6
|
export class MongooseDatasource extends Subject {
|
|
7
7
|
#schemas = new SmartCache();
|
|
8
8
|
#models = new SmartCache();
|
|
9
9
|
refs = new Map();
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
config;
|
|
11
|
+
routes;
|
|
12
|
+
async init(config, routes) {
|
|
13
|
+
this.config = config;
|
|
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());
|
|
21
|
+
}
|
|
22
|
+
async query(req, options) {
|
|
23
|
+
const connection_name = typeof options.connection == 'function' ? await options.connection(req) : (options.connection || Object.keys(this.config.connections)[0] || 'default');
|
|
24
|
+
const db = typeof options.db == 'function' ? await options.db(req) : (options.db || process.env.DB_NAME || 'main');
|
|
12
25
|
const schema = options.schema;
|
|
13
26
|
const collection_name = schema.options.collection;
|
|
14
|
-
const model = await this.#models.get(`${db}|${collection_name}`, async () => {
|
|
15
|
-
|
|
27
|
+
const model = await this.#models.get(`${collection_name}|${db}|${collection_name}`, async () => {
|
|
28
|
+
const connection = this.config.connections[connection_name];
|
|
29
|
+
if (!connection)
|
|
30
|
+
throw { status: 500, code: 'DB_CONNECTION_NOT_FOUND', message: `Database connection "${connection_name}" not found` };
|
|
31
|
+
return connection.model(collection_name, schema, collection_name);
|
|
16
32
|
});
|
|
17
|
-
const query = await this
|
|
33
|
+
const query = await this.#normalizeRef(req, schema);
|
|
18
34
|
if (query.method == 'get')
|
|
19
35
|
return await this.#get(query, model);
|
|
20
36
|
if (query.method == 'post')
|
|
@@ -143,7 +159,7 @@ export class MongooseDatasource extends Subject {
|
|
|
143
159
|
}, {})
|
|
144
160
|
};
|
|
145
161
|
}
|
|
146
|
-
async normalizeRef(req, schema) {
|
|
162
|
+
async #normalizeRef(req, schema) {
|
|
147
163
|
const fields = await this.#schemas.get(schema, async () => {
|
|
148
164
|
return new Set(Object.entries(schema.paths).filter(([k, v]) => {
|
|
149
165
|
return (v.instance == 'Array' ? v.getEmbeddedSchemaType().instance : v.instance) == 'ObjectId';
|
package/build/src/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './MongooseDatasource.js';
|
|
2
2
|
export * from './DataChangePayload.js';
|
package/build/src/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './MongooseDatasource.js';
|
|
2
2
|
export * from './DataChangePayload.js';
|
package/package.json
CHANGED