@livequery/mongoose 2.0.4 → 2.0.5
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,28 +1,3 @@
|
|
|
1
|
-
/// <reference types="mongoose/types/aggregate.js" />
|
|
2
|
-
/// <reference types="mongoose/types/callback.js" />
|
|
3
|
-
/// <reference types="mongoose/types/collection.js" />
|
|
4
|
-
/// <reference types="mongoose/types/connection.js" />
|
|
5
|
-
/// <reference types="mongoose/types/cursor.js" />
|
|
6
|
-
/// <reference types="mongoose/types/document.js" />
|
|
7
|
-
/// <reference types="mongoose/types/error.js" />
|
|
8
|
-
/// <reference types="mongoose/types/expressions.js" />
|
|
9
|
-
/// <reference types="mongoose/types/helpers.js" />
|
|
10
|
-
/// <reference types="mongoose/types/middlewares.js" />
|
|
11
|
-
/// <reference types="mongoose/types/indexes.js" />
|
|
12
|
-
/// <reference types="mongoose/types/models.js" />
|
|
13
|
-
/// <reference types="mongoose/types/mongooseoptions.js" />
|
|
14
|
-
/// <reference types="mongoose/types/pipelinestage.js" />
|
|
15
|
-
/// <reference types="mongoose/types/populate.js" />
|
|
16
|
-
/// <reference types="mongoose/types/query.js" />
|
|
17
|
-
/// <reference types="mongoose/types/schemaoptions.js" />
|
|
18
|
-
/// <reference types="mongoose/types/schematypes.js" />
|
|
19
|
-
/// <reference types="mongoose/types/session.js" />
|
|
20
|
-
/// <reference types="mongoose/types/types.js" />
|
|
21
|
-
/// <reference types="mongoose/types/utility.js" />
|
|
22
|
-
/// <reference types="mongoose/types/validation.js" />
|
|
23
|
-
/// <reference types="mongoose/types/virtuals.js" />
|
|
24
|
-
/// <reference types="mongoose/types/inferschematype.js" />
|
|
25
|
-
/// <reference types="mongoose/types/inferrawdoctype.js" />
|
|
26
1
|
import { LivequeryBaseEntity, LivequeryRequest } from "@livequery/types";
|
|
27
2
|
import { Model } from "mongoose";
|
|
28
3
|
export declare class MongoQuery {
|
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
/// <reference types="mongoose/types/aggregate.js" />
|
|
2
|
-
/// <reference types="mongoose/types/callback.js" />
|
|
3
|
-
/// <reference types="mongoose/types/collection.js" />
|
|
4
|
-
/// <reference types="mongoose/types/connection.js" />
|
|
5
|
-
/// <reference types="mongoose/types/cursor.js" />
|
|
6
|
-
/// <reference types="mongoose/types/document.js" />
|
|
7
|
-
/// <reference types="mongoose/types/error.js" />
|
|
8
|
-
/// <reference types="mongoose/types/expressions.js" />
|
|
9
|
-
/// <reference types="mongoose/types/helpers.js" />
|
|
10
|
-
/// <reference types="mongoose/types/middlewares.js" />
|
|
11
|
-
/// <reference types="mongoose/types/indexes.js" />
|
|
12
|
-
/// <reference types="mongoose/types/models.js" />
|
|
13
|
-
/// <reference types="mongoose/types/mongooseoptions.js" />
|
|
14
|
-
/// <reference types="mongoose/types/pipelinestage.js" />
|
|
15
|
-
/// <reference types="mongoose/types/populate.js" />
|
|
16
|
-
/// <reference types="mongoose/types/query.js" />
|
|
17
|
-
/// <reference types="mongoose/types/schemaoptions.js" />
|
|
18
|
-
/// <reference types="mongoose/types/schematypes.js" />
|
|
19
|
-
/// <reference types="mongoose/types/session.js" />
|
|
20
|
-
/// <reference types="mongoose/types/types.js" />
|
|
21
|
-
/// <reference types="mongoose/types/utility.js" />
|
|
22
|
-
/// <reference types="mongoose/types/validation.js" />
|
|
23
|
-
/// <reference types="mongoose/types/virtuals.js" />
|
|
24
|
-
/// <reference types="mongoose/types/inferschematype.js" />
|
|
25
|
-
/// <reference types="mongoose/types/inferrawdoctype.js" />
|
|
26
1
|
import { RouteOptions } from "./RouteOptions.js";
|
|
27
2
|
import { LivequeryRequest, LivequeryBaseEntity, WebsocketSyncPayload, DatabaseEvent } from '@livequery/types';
|
|
28
3
|
import { Observable } from "rxjs";
|
|
@@ -43,7 +18,7 @@ export declare class MongooseDatasource implements LivequeryDatasource<RouteOpti
|
|
|
43
18
|
[name: string]: Connection;
|
|
44
19
|
}): Promise<void>;
|
|
45
20
|
query(query: LivequeryRequest): Promise<import("mongodb").DeleteResult | import("mongoose").UpdateWriteOpResult | {
|
|
46
|
-
items:
|
|
21
|
+
items: any[];
|
|
47
22
|
summary: any;
|
|
48
23
|
has: {
|
|
49
24
|
next: boolean;
|
|
@@ -3,34 +3,26 @@ import { map, mergeAll } from "rxjs";
|
|
|
3
3
|
import { MongoQuery } from "./MongoQuery.js";
|
|
4
4
|
import { ObjectId } from 'bson';
|
|
5
5
|
export class MongooseDatasource {
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
6
|
+
#refs = new Map();
|
|
7
|
+
#routes = new Map();
|
|
8
|
+
#realtime_collections = new Map();
|
|
9
|
+
#conections = {};
|
|
10
10
|
async init(routes, connections) {
|
|
11
|
-
|
|
11
|
+
this.#conections = { ...connections };
|
|
12
|
+
for (const { refs, ...options } of routes) {
|
|
12
13
|
for (const ref of refs) {
|
|
13
|
-
const connection = Object.keys(connections).length == 1 ? connections[Object.keys(connections)[0]] : connections[connection_name];
|
|
14
|
-
if (!connection)
|
|
15
|
-
throw new Error(`Can not find [${connection_name}] datasource`);
|
|
16
|
-
if (!schema.options.collection) {
|
|
17
|
-
console.error(`Missing schema collection name at refs`, refs);
|
|
18
|
-
throw new Error(`Missing collection name of some schema, please carefully check`);
|
|
19
|
-
}
|
|
20
|
-
const model = this.#entities_map.get(schema) ?? connection.model(schema.options.collection, schema);
|
|
21
|
-
this.#entities_map.set(schema, model);
|
|
22
14
|
const schema_ref = ref.replaceAll(':', '');
|
|
23
|
-
this.#
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
this.#routes.set(schema_ref, options);
|
|
16
|
+
this.#refs.set(options.schema, new Set([
|
|
17
|
+
...this.#refs.get(options.schema) || [],
|
|
18
|
+
ref
|
|
26
19
|
]));
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const table_name = model.collection.collectionName;
|
|
20
|
+
if (options.realtime) {
|
|
21
|
+
const table_name = options.schema.options.collection.name;
|
|
30
22
|
const $ = schema_ref.split('/');
|
|
31
23
|
const is_collection_ref = $.length % 2 == 1;
|
|
32
|
-
this.#
|
|
33
|
-
...this.#
|
|
24
|
+
this.#realtime_collections.set(table_name, new Set([
|
|
25
|
+
...this.#realtime_collections.get(table_name) || [],
|
|
34
26
|
is_collection_ref ? schema_ref : $.slice(0, -1).join('/')
|
|
35
27
|
]));
|
|
36
28
|
}
|
|
@@ -38,22 +30,27 @@ export class MongooseDatasource {
|
|
|
38
30
|
}
|
|
39
31
|
}
|
|
40
32
|
async query(query) {
|
|
41
|
-
const config = this.#
|
|
33
|
+
const config = this.#routes.get(query.schema_ref);
|
|
42
34
|
if (!config)
|
|
43
35
|
throw { status: 500, code: 'REF_NOT_FOUND', message: 'Missing ref config in livequery system' };
|
|
36
|
+
const db = await config.db?.(query) || process.env.DB_NAME;
|
|
37
|
+
const connection = config.connection ? this.#conections[await config.connection(query)] : Object.values(this.#conections)[0];
|
|
38
|
+
const schema = config.schema;
|
|
39
|
+
const collection_name = schema.options.collection.name;
|
|
40
|
+
const model = connection.useDb(db).model(collection_name, schema, collection_name, schema.options);
|
|
44
41
|
if (query.method == 'get')
|
|
45
|
-
return await this.#get(query,
|
|
42
|
+
return await this.#get(query, model);
|
|
46
43
|
if (query.method == 'post')
|
|
47
|
-
return this.#post(query,
|
|
44
|
+
return this.#post(query, model);
|
|
48
45
|
if (query.method == 'put')
|
|
49
|
-
return this.#put(query,
|
|
46
|
+
return this.#put(query, model);
|
|
50
47
|
if (query.method == 'patch')
|
|
51
|
-
return this.#patch(query,
|
|
48
|
+
return this.#patch(query, model);
|
|
52
49
|
if (query.method == 'delete')
|
|
53
|
-
return this.#del(query,
|
|
50
|
+
return this.#del(query, model);
|
|
54
51
|
throw { status: 500, code: 'INVAILD_METHOD', message: 'Invaild method' };
|
|
55
52
|
}
|
|
56
|
-
async #get(req,
|
|
53
|
+
async #get(req, model) {
|
|
57
54
|
const { limit, items, count, has, summary } = await MongoQuery.query(req, model);
|
|
58
55
|
const current = items.length;
|
|
59
56
|
const total = current + count.next + count.prev;
|
|
@@ -102,7 +99,7 @@ export class MongooseDatasource {
|
|
|
102
99
|
item,
|
|
103
100
|
};
|
|
104
101
|
}
|
|
105
|
-
async #post(req,
|
|
102
|
+
async #post(req, model) {
|
|
106
103
|
const merged = {
|
|
107
104
|
...req.keys,
|
|
108
105
|
...req.body
|
|
@@ -110,39 +107,54 @@ export class MongooseDatasource {
|
|
|
110
107
|
const [data] = await model.insertMany([merged]);
|
|
111
108
|
return {
|
|
112
109
|
item: {
|
|
113
|
-
...data,
|
|
110
|
+
...data['_doc'],
|
|
114
111
|
_id: undefined,
|
|
115
112
|
id: data._id.toString()
|
|
116
113
|
}
|
|
117
114
|
};
|
|
118
115
|
}
|
|
119
|
-
async #put(req,
|
|
120
|
-
const keys = {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
116
|
+
async #put(req, model) {
|
|
117
|
+
const keys = Object.entries(req.keys).reduce((p, [k, c]) => {
|
|
118
|
+
return {
|
|
119
|
+
...p,
|
|
120
|
+
...k == 'id' ? {
|
|
121
|
+
_id: ObjectId.createFromHexString(req.keys.id)
|
|
122
|
+
} : {
|
|
123
|
+
[k]: c
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}, {});
|
|
125
127
|
return await model.updateOne(keys, req.body);
|
|
126
128
|
}
|
|
127
|
-
async #patch(req,
|
|
128
|
-
const keys = {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
129
|
+
async #patch(req, model) {
|
|
130
|
+
const keys = Object.entries(req.keys).reduce((p, [k, c]) => {
|
|
131
|
+
return {
|
|
132
|
+
...p,
|
|
133
|
+
...k == 'id' ? {
|
|
134
|
+
_id: ObjectId.createFromHexString(req.keys.id)
|
|
135
|
+
} : {
|
|
136
|
+
[k]: c
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}, {});
|
|
133
140
|
return await model.updateOne(keys, req.body);
|
|
134
141
|
}
|
|
135
|
-
async #del(req,
|
|
136
|
-
const keys = {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
142
|
+
async #del(req, model) {
|
|
143
|
+
const keys = Object.entries(req.keys).reduce((p, [k, c]) => {
|
|
144
|
+
return {
|
|
145
|
+
...p,
|
|
146
|
+
...k == 'id' ? {
|
|
147
|
+
_id: ObjectId.createFromHexString(req.keys.id)
|
|
148
|
+
} : {
|
|
149
|
+
[k]: c
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}, {});
|
|
141
153
|
return await model.deleteOne(keys);
|
|
142
154
|
}
|
|
143
155
|
enable_realtime(d) {
|
|
144
156
|
return d.pipe(map((event) => {
|
|
145
|
-
const refs = this.#
|
|
157
|
+
const refs = this.#realtime_collections.get(event.table);
|
|
146
158
|
if (!refs)
|
|
147
159
|
return [];
|
|
148
160
|
const data = {
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
import { LivequeryRequest } from "@livequery/types";
|
|
2
|
+
import { Schema } from "mongoose";
|
|
1
3
|
export type RouteOptions<T = any> = {
|
|
2
4
|
realtime?: boolean;
|
|
3
|
-
schema: T
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
};
|
|
8
|
-
connection_name?: string;
|
|
5
|
+
schema: Schema<T>;
|
|
6
|
+
connection?: (req: LivequeryRequest) => Promise<string> | string;
|
|
7
|
+
db?: (req: LivequeryRequest) => Promise<string> | string;
|
|
9
8
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../src/cursor.ts","../src/datachangepayload.ts","../src/mongoquery.ts","../src/mongoosedatasource.ts","../src/routeoptions.ts","../src/index.ts"],"version":"5.6.2"}
|
package/package.json
CHANGED