liwi-mongo 12.0.0 → 13.0.1
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/README.md +7 -7
- package/dist/definitions/MongoBaseModel.d.ts +2 -2
- package/dist/definitions/MongoBaseModel.d.ts.map +1 -1
- package/dist/definitions/MongoConnection.d.ts +10 -3
- package/dist/definitions/MongoConnection.d.ts.map +1 -1
- package/dist/definitions/MongoCursor.d.ts +6 -6
- package/dist/definitions/MongoQueryCollection.d.ts +5 -5
- package/dist/definitions/MongoQueryCollection.d.ts.map +1 -1
- package/dist/definitions/MongoQuerySingleItem.d.ts +5 -5
- package/dist/definitions/MongoQuerySingleItem.d.ts.map +1 -1
- package/dist/definitions/MongoStore.d.ts +10 -10
- package/dist/definitions/MongoStore.d.ts.map +1 -1
- package/dist/definitions/createMongoSubscribeStore.d.ts +6 -5
- package/dist/definitions/createMongoSubscribeStore.d.ts.map +1 -1
- package/dist/definitions/index.d.ts +5 -5
- package/dist/definitions/index.d.ts.map +1 -1
- package/dist/index-node.mjs +546 -0
- package/dist/index-node.mjs.map +1 -0
- package/package.json +43 -38
- package/src/MongoBaseModel.ts +5 -4
- package/src/MongoConnection.ts +47 -43
- package/src/MongoCursor.ts +7 -7
- package/src/MongoQueryCollection.ts +25 -25
- package/src/MongoQuerySingleItem.ts +26 -26
- package/src/MongoStore.ts +33 -36
- package/src/createMongoSubscribeStore.ts +7 -5
- package/src/index.ts +11 -5
- package/CHANGELOG.md +0 -628
- package/dist/index-node18.mjs +0 -588
- package/dist/index-node18.mjs.map +0 -1
- package/src/.eslintrc.json +0 -38
package/package.json
CHANGED
|
@@ -1,67 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "liwi-mongo",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "13.0.1",
|
|
4
4
|
"description": "mongo implementation for liwi",
|
|
5
5
|
"keywords": [],
|
|
6
|
-
"
|
|
6
|
+
"homepage": "https://github.com/christophehurpeau/liwi",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/christophehurpeau/liwi/issues"
|
|
9
|
+
},
|
|
7
10
|
"license": "ISC",
|
|
11
|
+
"author": "Christophe Hurpeau <christophe@hurpeau.com> (https://christophe.hurpeau.com)",
|
|
8
12
|
"repository": {
|
|
9
13
|
"type": "git",
|
|
10
|
-
"url": "https://github.com/
|
|
14
|
+
"url": "https://github.com/christophehurpeau/liwi.git",
|
|
11
15
|
"directory": "packages/liwi-mongo"
|
|
12
16
|
},
|
|
13
|
-
"
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
14
21
|
"type": "module",
|
|
15
|
-
"engines": {
|
|
16
|
-
"node": ">=18.12.0"
|
|
17
|
-
},
|
|
18
22
|
"sideEffects": false,
|
|
19
|
-
"main": "./dist/index-
|
|
23
|
+
"main": "./dist/index-node.mjs",
|
|
20
24
|
"types": "./dist/definitions/index.d.ts",
|
|
21
25
|
"exports": {
|
|
22
26
|
"./package.json": "./package.json",
|
|
23
27
|
".": {
|
|
24
28
|
"types": "./dist/definitions/index.d.ts",
|
|
25
29
|
"node": {
|
|
26
|
-
"import": "./dist/index-
|
|
30
|
+
"import": "./dist/index-node.mjs"
|
|
27
31
|
}
|
|
28
32
|
}
|
|
29
33
|
},
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"mingo": "~6.2.7",
|
|
36
|
+
"mongodb": "^7.4.0",
|
|
37
|
+
"nightingale-logger": "^18.2.0",
|
|
38
|
+
"liwi-store": "13.0.1",
|
|
39
|
+
"liwi-subscribe-store": "13.0.1"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@pob/rollup-esbuild": "9.1.1",
|
|
43
|
+
"typescript": "6.0.3"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=22.18.0"
|
|
42
47
|
},
|
|
43
48
|
"pob": {
|
|
44
|
-
"
|
|
49
|
+
"bundler": "rollup-esbuild",
|
|
50
|
+
"entries": [
|
|
51
|
+
"index"
|
|
52
|
+
],
|
|
53
|
+
"envs": [
|
|
45
54
|
{
|
|
46
55
|
"target": "node",
|
|
47
|
-
"version": "
|
|
56
|
+
"version": "22",
|
|
57
|
+
"omitVersionInFileName": true
|
|
48
58
|
}
|
|
49
59
|
],
|
|
50
|
-
"
|
|
51
|
-
"index"
|
|
52
|
-
]
|
|
53
|
-
},
|
|
54
|
-
"prettier": "@pob/root/prettier-config",
|
|
55
|
-
"dependencies": {
|
|
56
|
-
"liwi-store": "12.0.0",
|
|
57
|
-
"liwi-subscribe-store": "12.0.0",
|
|
58
|
-
"mingo": "^6.2.7",
|
|
59
|
-
"mongodb": "^6.0.0",
|
|
60
|
-
"nightingale-logger": "^15.0.0"
|
|
60
|
+
"typescript": true
|
|
61
61
|
},
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "pnpm run clean:build && rollup --config rollup.config.mjs && pnpm run build:definitions",
|
|
64
|
+
"build:definitions": "tsc -p tsconfig.json",
|
|
65
|
+
"clean": "yarn clean:build",
|
|
66
|
+
"clean:build": "pob-esbuild-clean-out dist",
|
|
67
|
+
"lint": "pnpm run lint:eslint",
|
|
68
|
+
"lint:eslint": "pnpm run --dir '../..' eslint --quiet 'packages/liwi-mongo'",
|
|
69
|
+
"tsc": "tsc",
|
|
70
|
+
"watch": "yarn clean:build && rollup --config rollup.config.mjs --watch"
|
|
66
71
|
}
|
|
67
72
|
}
|
package/src/MongoBaseModel.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AllowedKeyValue, BaseModel, InsertType } from "liwi-store";
|
|
2
2
|
|
|
3
|
-
export type MongoKeyPath =
|
|
3
|
+
export type MongoKeyPath = "_id";
|
|
4
4
|
|
|
5
|
-
export interface MongoBaseModel<
|
|
6
|
-
extends
|
|
5
|
+
export interface MongoBaseModel<
|
|
6
|
+
KeyValue extends AllowedKeyValue = string,
|
|
7
|
+
> extends BaseModel {
|
|
7
8
|
_id: KeyValue;
|
|
8
9
|
}
|
|
9
10
|
|
package/src/MongoConnection.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import { AbstractConnection } from
|
|
2
|
-
import mongodb from
|
|
3
|
-
import { Logger } from
|
|
4
|
-
|
|
5
|
-
const logger = new Logger(
|
|
1
|
+
import { AbstractConnection } from "liwi-store";
|
|
2
|
+
import mongodb from "mongodb";
|
|
3
|
+
import { Logger } from "nightingale-logger";
|
|
4
|
+
|
|
5
|
+
const logger = new Logger("liwi:mongo:MongoConnection");
|
|
6
|
+
|
|
7
|
+
export interface MongoConfig {
|
|
8
|
+
host?: string;
|
|
9
|
+
port?: number | string;
|
|
10
|
+
database: string;
|
|
11
|
+
user?: string;
|
|
12
|
+
password?: string;
|
|
13
|
+
}
|
|
6
14
|
|
|
7
15
|
export default class MongoConnection extends AbstractConnection {
|
|
8
16
|
_connection?: mongodb.MongoClient;
|
|
@@ -12,36 +20,33 @@ export default class MongoConnection extends AbstractConnection {
|
|
|
12
20
|
connectionFailed?: boolean;
|
|
13
21
|
|
|
14
22
|
// TODO interface
|
|
15
|
-
constructor(
|
|
23
|
+
constructor({
|
|
24
|
+
host = "localhost",
|
|
25
|
+
port = "27017",
|
|
26
|
+
database,
|
|
27
|
+
user,
|
|
28
|
+
password,
|
|
29
|
+
}: MongoConfig) {
|
|
16
30
|
super();
|
|
17
31
|
|
|
18
|
-
if (!
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
if (!config.has('port')) {
|
|
22
|
-
config.set('port', '27017');
|
|
23
|
-
}
|
|
24
|
-
if (!config.has('database')) {
|
|
25
|
-
throw new Error('Missing config database');
|
|
32
|
+
if (!database) {
|
|
33
|
+
throw new Error("Missing config database");
|
|
26
34
|
}
|
|
27
35
|
|
|
28
36
|
const buildConnectionString = (redactCredentials: boolean): string =>
|
|
29
37
|
`mongodb://${
|
|
30
|
-
|
|
38
|
+
user
|
|
31
39
|
? `${
|
|
32
40
|
redactCredentials
|
|
33
|
-
? `${
|
|
34
|
-
: (
|
|
41
|
+
? `${user.slice(0, 2)}[redacted]`
|
|
42
|
+
: encodeURIComponent(user)
|
|
35
43
|
}:${
|
|
36
44
|
redactCredentials
|
|
37
|
-
?
|
|
38
|
-
: (
|
|
45
|
+
? "[redacted]"
|
|
46
|
+
: encodeURIComponent(password ?? "")
|
|
39
47
|
}@`
|
|
40
|
-
:
|
|
41
|
-
}
|
|
42
|
-
`${config.get('host') as string}:${config.get('port') as string}/${
|
|
43
|
-
config.get('database') as string
|
|
44
|
-
}`;
|
|
48
|
+
: ""
|
|
49
|
+
}${host}:${port}/${encodeURIComponent(database)}`;
|
|
45
50
|
|
|
46
51
|
const connectionString = buildConnectionString(false);
|
|
47
52
|
const connectionStringRedacted = buildConnectionString(true);
|
|
@@ -50,32 +55,32 @@ export default class MongoConnection extends AbstractConnection {
|
|
|
50
55
|
}
|
|
51
56
|
|
|
52
57
|
connect(connectionString: string, connectionStringRedacted: string): void {
|
|
53
|
-
logger.info(
|
|
58
|
+
logger.info("connecting", { connectionStringRedacted });
|
|
54
59
|
|
|
55
60
|
const connectPromise = mongodb.MongoClient.connect(connectionString)
|
|
56
61
|
.then((connection) => {
|
|
57
|
-
logger.info(
|
|
58
|
-
connection.on(
|
|
59
|
-
logger.warn(
|
|
62
|
+
logger.info("connected", { connectionStringRedacted });
|
|
63
|
+
connection.on("close", () => {
|
|
64
|
+
logger.warn("close", { connectionStringRedacted });
|
|
60
65
|
this.connectionFailed = true;
|
|
61
66
|
this.getConnection = () => {
|
|
62
|
-
throw new Error(
|
|
67
|
+
throw new Error("MongoDB connection closed");
|
|
63
68
|
};
|
|
64
69
|
});
|
|
65
|
-
connection.on(
|
|
66
|
-
logger.warn(
|
|
70
|
+
connection.on("timeout", () => {
|
|
71
|
+
logger.warn("timeout", { connectionStringRedacted });
|
|
67
72
|
this.connectionFailed = true;
|
|
68
73
|
this.getConnection = () => {
|
|
69
|
-
throw new Error(
|
|
74
|
+
throw new Error("MongoDB connection timeout");
|
|
70
75
|
};
|
|
71
76
|
});
|
|
72
|
-
connection.on(
|
|
73
|
-
logger.warn(
|
|
77
|
+
connection.on("reconnect", () => {
|
|
78
|
+
logger.warn("reconnect", { connectionStringRedacted });
|
|
74
79
|
this.connectionFailed = false;
|
|
75
80
|
this.getConnection = () => Promise.resolve(this._connection!);
|
|
76
81
|
});
|
|
77
|
-
connection.on(
|
|
78
|
-
logger.warn(
|
|
82
|
+
connection.on("error", (err) => {
|
|
83
|
+
logger.warn("error", { connectionStringRedacted, err });
|
|
79
84
|
});
|
|
80
85
|
|
|
81
86
|
this._connection = connection;
|
|
@@ -83,12 +88,12 @@ export default class MongoConnection extends AbstractConnection {
|
|
|
83
88
|
this.getConnection = () => Promise.resolve(this._connection!);
|
|
84
89
|
return connection;
|
|
85
90
|
})
|
|
86
|
-
.catch((error) => {
|
|
87
|
-
logger.info(
|
|
88
|
-
console.error(error.message || error);
|
|
91
|
+
.catch((error: unknown) => {
|
|
92
|
+
logger.info("not connected", { connectionStringRedacted });
|
|
93
|
+
console.error((error as Error).message || error);
|
|
89
94
|
// throw err;
|
|
90
95
|
process.nextTick(() => {
|
|
91
|
-
// eslint-disable-next-line unicorn/no-process-exit
|
|
96
|
+
// eslint-disable-next-line unicorn/no-process-exit
|
|
92
97
|
process.exit(1);
|
|
93
98
|
});
|
|
94
99
|
|
|
@@ -99,13 +104,12 @@ export default class MongoConnection extends AbstractConnection {
|
|
|
99
104
|
this._connecting = this.getConnection();
|
|
100
105
|
}
|
|
101
106
|
|
|
102
|
-
// eslint-disable-next-line @typescript-eslint/class-methods-use-this
|
|
103
107
|
getConnection(): Promise<mongodb.MongoClient> {
|
|
104
|
-
throw new Error(
|
|
108
|
+
throw new Error("call connect()");
|
|
105
109
|
}
|
|
106
110
|
|
|
107
111
|
async close(): Promise<void> {
|
|
108
|
-
this.getConnection = () => Promise.reject(new Error(
|
|
112
|
+
this.getConnection = () => Promise.reject(new Error("Connection closed"));
|
|
109
113
|
if (this._connection) {
|
|
110
114
|
await this._connection.close();
|
|
111
115
|
this._connection = undefined;
|
package/src/MongoCursor.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { AbstractStoreCursor } from
|
|
2
|
-
import type { AllowedKeyValue } from
|
|
3
|
-
import type { FindCursor } from
|
|
4
|
-
import type { MongoBaseModel } from
|
|
5
|
-
import type MongoStore from
|
|
1
|
+
import { AbstractStoreCursor } from "liwi-store";
|
|
2
|
+
import type { AllowedKeyValue } from "liwi-store";
|
|
3
|
+
import type { FindCursor } from "mongodb";
|
|
4
|
+
import type { MongoBaseModel } from "./MongoBaseModel";
|
|
5
|
+
import type MongoStore from "./MongoStore";
|
|
6
6
|
|
|
7
7
|
export default class MongoCursor<
|
|
8
8
|
Model extends MongoBaseModel<KeyValue>,
|
|
9
9
|
Result extends Partial<Model> = Model,
|
|
10
|
-
KeyValue extends AllowedKeyValue = Model[
|
|
10
|
+
KeyValue extends AllowedKeyValue = Model["_id"],
|
|
11
11
|
> extends AbstractStoreCursor<
|
|
12
12
|
MongoStore<Model, KeyValue>,
|
|
13
13
|
KeyValue,
|
|
@@ -51,7 +51,7 @@ export default class MongoCursor<
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
override result(): Promise<Result> {
|
|
54
|
-
if (!this._result) throw new Error(
|
|
54
|
+
if (!this._result) throw new Error("Cannot call result() before next()");
|
|
55
55
|
return Promise.resolve(this._result);
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
/* eslint-disable complexity
|
|
1
|
+
/* eslint-disable complexity */
|
|
2
2
|
import type {
|
|
3
|
-
|
|
4
|
-
SubscribeCallback,
|
|
5
|
-
QueryResult,
|
|
6
|
-
QueryParams,
|
|
3
|
+
AllowedKeyValue,
|
|
7
4
|
Changes,
|
|
8
5
|
QueryOptions,
|
|
6
|
+
QueryParams,
|
|
7
|
+
QueryResult,
|
|
8
|
+
QuerySubscription,
|
|
9
|
+
SubscribeCallback,
|
|
9
10
|
Transformer,
|
|
10
|
-
|
|
11
|
-
} from
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
import mingo from 'mingo';
|
|
11
|
+
} from "liwi-store";
|
|
12
|
+
import type { Actions } from "liwi-subscribe-store";
|
|
13
|
+
import { AbstractSubscribableStoreQuery } from "liwi-subscribe-store";
|
|
14
|
+
import mingo from "mingo";
|
|
15
15
|
import type {
|
|
16
16
|
MongoBaseModel,
|
|
17
17
|
MongoInsertType,
|
|
18
18
|
MongoKeyPath,
|
|
19
|
-
} from
|
|
20
|
-
import type MongoCursor from
|
|
21
|
-
import type MongoStore from
|
|
19
|
+
} from "./MongoBaseModel";
|
|
20
|
+
import type MongoCursor from "./MongoCursor";
|
|
21
|
+
import type MongoStore from "./MongoStore";
|
|
22
22
|
|
|
23
23
|
const identityTransformer = <
|
|
24
24
|
Model extends MongoBaseModel<any>,
|
|
@@ -32,7 +32,7 @@ type TestCriteria = (obj: any) => boolean;
|
|
|
32
32
|
export default class MongoQueryCollection<
|
|
33
33
|
Model extends MongoBaseModel<KeyValue>,
|
|
34
34
|
Params extends QueryParams<Params> = never,
|
|
35
|
-
KeyValue extends AllowedKeyValue = Model[
|
|
35
|
+
KeyValue extends AllowedKeyValue = Model["_id"],
|
|
36
36
|
Item extends Record<MongoKeyPath, KeyValue> = Model,
|
|
37
37
|
> extends AbstractSubscribableStoreQuery<
|
|
38
38
|
MongoKeyPath,
|
|
@@ -68,7 +68,7 @@ export default class MongoQueryCollection<
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
// criteria not supported by mingo: updates will not work
|
|
71
|
-
if (
|
|
71
|
+
if ("$text" in this.options.criteria) {
|
|
72
72
|
return () => false;
|
|
73
73
|
}
|
|
74
74
|
|
|
@@ -106,7 +106,7 @@ export default class MongoQueryCollection<
|
|
|
106
106
|
? this.fetch(({ result, meta, info }: QueryResult<Item[]>) => {
|
|
107
107
|
callback(null, [
|
|
108
108
|
{
|
|
109
|
-
type:
|
|
109
|
+
type: "initial",
|
|
110
110
|
initial: result,
|
|
111
111
|
queryInfo: info,
|
|
112
112
|
meta,
|
|
@@ -118,27 +118,27 @@ export default class MongoQueryCollection<
|
|
|
118
118
|
const unsubscribe = store.subscribe((action: Actions<Model>) => {
|
|
119
119
|
const changes: Changes<KeyValue, Item[]> = [];
|
|
120
120
|
switch (action.type) {
|
|
121
|
-
case
|
|
121
|
+
case "inserted": {
|
|
122
122
|
const filtered = action.next.filter(testCriteria);
|
|
123
123
|
if (filtered.length > 0) {
|
|
124
124
|
changes.push({
|
|
125
|
-
type:
|
|
125
|
+
type: "inserted",
|
|
126
126
|
result: filtered.map(this.transformer),
|
|
127
127
|
});
|
|
128
128
|
}
|
|
129
129
|
break;
|
|
130
130
|
}
|
|
131
|
-
case
|
|
131
|
+
case "deleted": {
|
|
132
132
|
const filtered = action.prev.filter(testCriteria);
|
|
133
133
|
if (filtered.length > 0) {
|
|
134
134
|
changes.push({
|
|
135
|
-
type:
|
|
135
|
+
type: "deleted",
|
|
136
136
|
keys: filtered.map((object) => object[this.store.keyPath]),
|
|
137
137
|
});
|
|
138
138
|
}
|
|
139
139
|
break;
|
|
140
140
|
}
|
|
141
|
-
case
|
|
141
|
+
case "updated": {
|
|
142
142
|
const {
|
|
143
143
|
deleted,
|
|
144
144
|
updated,
|
|
@@ -162,19 +162,19 @@ export default class MongoQueryCollection<
|
|
|
162
162
|
});
|
|
163
163
|
|
|
164
164
|
if (deleted.length > 0) {
|
|
165
|
-
changes.push({ type:
|
|
165
|
+
changes.push({ type: "deleted", keys: deleted });
|
|
166
166
|
}
|
|
167
167
|
if (updated.length > 0) {
|
|
168
|
-
changes.push({ type:
|
|
168
|
+
changes.push({ type: "updated", result: updated });
|
|
169
169
|
}
|
|
170
170
|
if (inserted.length > 0) {
|
|
171
|
-
changes.push({ type:
|
|
171
|
+
changes.push({ type: "inserted", result: inserted });
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
break;
|
|
175
175
|
}
|
|
176
176
|
default:
|
|
177
|
-
throw new Error(
|
|
177
|
+
throw new Error("Unsupported type");
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
if (changes.length === 0) return;
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
SubscribeCallback,
|
|
4
|
-
QueryResult,
|
|
5
|
-
QueryParams,
|
|
2
|
+
AllowedKeyValue,
|
|
6
3
|
Changes,
|
|
7
4
|
QueryOptions,
|
|
5
|
+
QueryParams,
|
|
6
|
+
QueryResult,
|
|
7
|
+
QuerySubscription,
|
|
8
|
+
SubscribeCallback,
|
|
8
9
|
Transformer,
|
|
9
|
-
|
|
10
|
-
} from
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import mingo from 'mingo';
|
|
10
|
+
} from "liwi-store";
|
|
11
|
+
import type { Actions } from "liwi-subscribe-store";
|
|
12
|
+
import { AbstractSubscribableStoreQuery } from "liwi-subscribe-store";
|
|
13
|
+
import mingo from "mingo";
|
|
14
14
|
import type {
|
|
15
15
|
MongoBaseModel,
|
|
16
16
|
MongoInsertType,
|
|
17
17
|
MongoKeyPath,
|
|
18
|
-
} from
|
|
19
|
-
import type MongoCursor from
|
|
20
|
-
import type MongoStore from
|
|
18
|
+
} from "./MongoBaseModel";
|
|
19
|
+
import type MongoCursor from "./MongoCursor";
|
|
20
|
+
import type MongoStore from "./MongoStore";
|
|
21
21
|
|
|
22
22
|
const identityTransformer = <
|
|
23
23
|
Model extends MongoBaseModel<any>,
|
|
@@ -32,7 +32,7 @@ export default class MongoQuerySingleItem<
|
|
|
32
32
|
Model extends MongoBaseModel<KeyValue>,
|
|
33
33
|
Params extends QueryParams<Params> = never,
|
|
34
34
|
Result extends Record<MongoKeyPath, KeyValue> | null = Model | null,
|
|
35
|
-
KeyValue extends AllowedKeyValue = Model[
|
|
35
|
+
KeyValue extends AllowedKeyValue = Model["_id"],
|
|
36
36
|
> extends AbstractSubscribableStoreQuery<
|
|
37
37
|
MongoKeyPath,
|
|
38
38
|
KeyValue,
|
|
@@ -78,7 +78,7 @@ export default class MongoQuerySingleItem<
|
|
|
78
78
|
await cursor.limit(1);
|
|
79
79
|
return cursor.toArray().then((result: Model[]) => {
|
|
80
80
|
const item: Result =
|
|
81
|
-
result.length === 0 ? (null as Result) : this.transformer(result[0]);
|
|
81
|
+
result.length === 0 ? (null as Result) : this.transformer(result[0]!);
|
|
82
82
|
return onFulfilled({
|
|
83
83
|
result: item,
|
|
84
84
|
meta: { total: result === null ? 0 : 1 },
|
|
@@ -101,7 +101,7 @@ export default class MongoQuerySingleItem<
|
|
|
101
101
|
? this.fetch(({ result, meta, info }: QueryResult<Result>) => {
|
|
102
102
|
callback(null, [
|
|
103
103
|
{
|
|
104
|
-
type:
|
|
104
|
+
type: "initial",
|
|
105
105
|
initial: result,
|
|
106
106
|
queryInfo: info,
|
|
107
107
|
meta,
|
|
@@ -113,27 +113,27 @@ export default class MongoQuerySingleItem<
|
|
|
113
113
|
const unsubscribe = store.subscribe(async (action: Actions<Model>) => {
|
|
114
114
|
const changes: Changes<KeyValue, Result> = [];
|
|
115
115
|
switch (action.type) {
|
|
116
|
-
case
|
|
116
|
+
case "inserted": {
|
|
117
117
|
const filtered = action.next.filter(testCriteria);
|
|
118
118
|
if (filtered.length > 0) {
|
|
119
119
|
changes.push({
|
|
120
|
-
type:
|
|
121
|
-
result: this.transformer(filtered[0]),
|
|
120
|
+
type: "updated",
|
|
121
|
+
result: this.transformer(filtered[0]!),
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
124
|
break;
|
|
125
125
|
}
|
|
126
|
-
case
|
|
126
|
+
case "deleted": {
|
|
127
127
|
const filtered = action.prev.filter(testCriteria);
|
|
128
128
|
if (filtered.length > 0) {
|
|
129
129
|
changes.push({
|
|
130
|
-
type:
|
|
130
|
+
type: "deleted",
|
|
131
131
|
keys: filtered.map((object) => object[this.store.keyPath]),
|
|
132
132
|
});
|
|
133
133
|
}
|
|
134
134
|
break;
|
|
135
135
|
}
|
|
136
|
-
case
|
|
136
|
+
case "updated": {
|
|
137
137
|
const filtered = action.changes.filter(([prev, next]) =>
|
|
138
138
|
testCriteria(prev),
|
|
139
139
|
);
|
|
@@ -141,17 +141,17 @@ export default class MongoQuerySingleItem<
|
|
|
141
141
|
if (this.options.sort) {
|
|
142
142
|
const { result } = await this.fetch((res) => res);
|
|
143
143
|
changes.push({
|
|
144
|
-
type:
|
|
144
|
+
type: "updated",
|
|
145
145
|
result,
|
|
146
146
|
});
|
|
147
147
|
} else if (filtered.length !== 1) {
|
|
148
148
|
throw new Error(
|
|
149
|
-
|
|
149
|
+
"should not match more than 1, use sort if you can have multiple match",
|
|
150
150
|
);
|
|
151
151
|
} else {
|
|
152
|
-
const [, next] = filtered[0]
|
|
152
|
+
const [, next] = filtered[0]!;
|
|
153
153
|
changes.push({
|
|
154
|
-
type:
|
|
154
|
+
type: "updated",
|
|
155
155
|
result: testCriteria(next) ? this.transformer(next) : null!,
|
|
156
156
|
});
|
|
157
157
|
}
|
|
@@ -160,7 +160,7 @@ export default class MongoQuerySingleItem<
|
|
|
160
160
|
break;
|
|
161
161
|
}
|
|
162
162
|
default:
|
|
163
|
-
throw new Error(
|
|
163
|
+
throw new Error("Unsupported type");
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
if (changes.length === 0) return;
|