@livequery/mongoose 2.0.50 → 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.
- package/build/src/MongooseDatasource.d.ts +22 -7
- package/build/src/MongooseDatasource.js +10 -19
- package/build/src/index.d.ts +1 -2
- package/build/src/index.js +0 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/build/src/RouteOptions.d.ts +0 -7
- package/build/src/RouteOptions.js +0 -1
|
@@ -1,14 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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
|
-
|
|
6
|
-
|
|
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
|
|
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(
|
|
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: {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Cursor } from './Cursor.js';
|
|
2
2
|
import { MongoQuery } from "./MongoQuery.js";
|
|
3
|
-
import mongoose from 'mongoose';
|
|
4
3
|
import { ObjectId } from 'bson';
|
|
5
4
|
import { SmartCache } from './SmartCache.js';
|
|
6
5
|
import { Subject } from 'rxjs/internal/Subject';
|
|
@@ -8,18 +7,16 @@ export class MongooseDatasource extends Subject {
|
|
|
8
7
|
#schemas = new SmartCache();
|
|
9
8
|
#models = new SmartCache();
|
|
10
9
|
refs = new Map();
|
|
11
|
-
async query(
|
|
12
|
-
const db = typeof
|
|
13
|
-
const 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;
|
|
14
13
|
const collection_name = schema.options.collection;
|
|
15
14
|
const model = await this.#models.get(`${db}|${collection_name}`, async () => {
|
|
16
|
-
return
|
|
15
|
+
return config.connections[db].model(collection_name, schema, collection_name);
|
|
17
16
|
});
|
|
18
|
-
const query = await this.normalizeRef(
|
|
19
|
-
if (query.method == 'get')
|
|
20
|
-
config.realtime && this.#log(query, model);
|
|
17
|
+
const query = await this.normalizeRef(req, schema);
|
|
18
|
+
if (query.method == 'get')
|
|
21
19
|
return await this.#get(query, model);
|
|
22
|
-
}
|
|
23
20
|
if (query.method == 'post')
|
|
24
21
|
return this.#post(query, model);
|
|
25
22
|
if (query.method == 'put')
|
|
@@ -30,12 +27,6 @@ export class MongooseDatasource extends Subject {
|
|
|
30
27
|
return this.#del(query, model);
|
|
31
28
|
throw { status: 500, code: 'INVAILD_METHOD', message: 'Invaild method' };
|
|
32
29
|
}
|
|
33
|
-
#log(req, model) {
|
|
34
|
-
const _ = this.refs.get(model.collection.name);
|
|
35
|
-
const set = _ || new Set();
|
|
36
|
-
set.add(req.collection_ref);
|
|
37
|
-
!_ && this.refs.set(model.collection.name, set);
|
|
38
|
-
}
|
|
39
30
|
async #get(req, model) {
|
|
40
31
|
const { limit, items, count, has, summary } = await MongoQuery.query(req, model);
|
|
41
32
|
const current = items.length;
|
|
@@ -142,10 +133,10 @@ export class MongooseDatasource extends Subject {
|
|
|
142
133
|
return {
|
|
143
134
|
...obj,
|
|
144
135
|
...[...fields].reduce((p, c) => {
|
|
145
|
-
if (obj[c] && typeof obj[c] == 'string' &&
|
|
136
|
+
if (obj[c] && typeof obj[c] == 'string' && ObjectId.isValid(obj[c])) {
|
|
146
137
|
return {
|
|
147
138
|
...p,
|
|
148
|
-
[c]:
|
|
139
|
+
[c]: ObjectId.createFromHexString(obj[c])
|
|
149
140
|
};
|
|
150
141
|
}
|
|
151
142
|
return p;
|
|
@@ -155,8 +146,8 @@ export class MongooseDatasource extends Subject {
|
|
|
155
146
|
async normalizeRef(req, schema) {
|
|
156
147
|
const fields = await this.#schemas.get(schema, async () => {
|
|
157
148
|
return new Set(Object.entries(schema.paths).filter(([k, v]) => {
|
|
158
|
-
return (v.instance == 'Array' ? v.getEmbeddedSchemaType().instance : v.instance) == '
|
|
159
|
-
}).map(([k
|
|
149
|
+
return (v.instance == 'Array' ? v.getEmbeddedSchemaType().instance : v.instance) == 'ObjectId';
|
|
150
|
+
}).map(([k]) => k));
|
|
160
151
|
});
|
|
161
152
|
if (fields.size == 0)
|
|
162
153
|
return req;
|
package/build/src/index.d.ts
CHANGED
package/build/src/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/cursor.ts","../src/datachangepayload.ts","../src/mongoquery.ts","../src/mongoosedatasource.ts","../src/
|
|
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.
|
|
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
|
-
"
|
|
35
|
+
"peerDependencies": {
|
|
36
36
|
"bson": "*",
|
|
37
37
|
"mongoose": "^8.6.2"
|
|
38
38
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|