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/src/MongoStore.ts
CHANGED
|
@@ -1,34 +1,33 @@
|
|
|
1
|
-
/* eslint-disable max-lines */
|
|
2
1
|
import type {
|
|
3
|
-
|
|
4
|
-
SubscribableStore,
|
|
5
|
-
QueryParams,
|
|
6
|
-
UpsertPartialObject,
|
|
2
|
+
AllowedKeyValue,
|
|
7
3
|
Criteria,
|
|
8
|
-
|
|
9
|
-
Update,
|
|
4
|
+
OptionalBaseModelKeysForInsert,
|
|
10
5
|
QueryOptions,
|
|
6
|
+
QueryParams,
|
|
7
|
+
Sort,
|
|
8
|
+
SubscribableStore,
|
|
11
9
|
Transformer,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
Update,
|
|
11
|
+
UpsertPartialObject,
|
|
12
|
+
UpsertResult,
|
|
13
|
+
} from "liwi-store";
|
|
15
14
|
import type {
|
|
16
15
|
Collection,
|
|
17
16
|
Filter,
|
|
18
17
|
FindCursor,
|
|
19
18
|
MongoClient,
|
|
20
19
|
UpdateFilter,
|
|
21
|
-
} from
|
|
22
|
-
import mongodb from
|
|
20
|
+
} from "mongodb";
|
|
21
|
+
import mongodb from "mongodb";
|
|
23
22
|
import type {
|
|
24
23
|
MongoBaseModel,
|
|
25
|
-
MongoKeyPath,
|
|
26
24
|
MongoInsertType,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
import
|
|
30
|
-
import
|
|
31
|
-
import
|
|
25
|
+
MongoKeyPath,
|
|
26
|
+
} from "./MongoBaseModel.ts";
|
|
27
|
+
import type MongoConnection from "./MongoConnection.ts";
|
|
28
|
+
import MongoCursor from "./MongoCursor.ts";
|
|
29
|
+
import MongoQueryCollection from "./MongoQueryCollection.ts";
|
|
30
|
+
import MongoQuerySingleItem from "./MongoQuerySingleItem.ts";
|
|
32
31
|
|
|
33
32
|
export interface MongoUpsertResult<
|
|
34
33
|
KeyValue extends AllowedKeyValue,
|
|
@@ -41,16 +40,14 @@ export interface MongoUpsertResult<
|
|
|
41
40
|
export default class MongoStore<
|
|
42
41
|
Model extends MongoBaseModel<KeyValue>,
|
|
43
42
|
KeyValue extends AllowedKeyValue = Model[MongoKeyPath],
|
|
44
|
-
> implements
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
{
|
|
53
|
-
readonly keyPath: MongoKeyPath = '_id';
|
|
43
|
+
> implements SubscribableStore<
|
|
44
|
+
MongoKeyPath,
|
|
45
|
+
KeyValue,
|
|
46
|
+
Model,
|
|
47
|
+
MongoInsertType<Model>,
|
|
48
|
+
MongoConnection
|
|
49
|
+
> {
|
|
50
|
+
readonly keyPath: MongoKeyPath = "_id";
|
|
54
51
|
|
|
55
52
|
readonly connection: MongoConnection;
|
|
56
53
|
|
|
@@ -77,7 +74,7 @@ export default class MongoStore<
|
|
|
77
74
|
|
|
78
75
|
get collection(): Promise<Collection<Model>> {
|
|
79
76
|
if (this.connection.connectionFailed) {
|
|
80
|
-
return Promise.reject(new Error(
|
|
77
|
+
return Promise.reject(new Error("MongoDB connection failed"));
|
|
81
78
|
}
|
|
82
79
|
|
|
83
80
|
return Promise.resolve(this._collection);
|
|
@@ -103,7 +100,7 @@ export default class MongoStore<
|
|
|
103
100
|
>(
|
|
104
101
|
options: QueryOptions<Model>,
|
|
105
102
|
transformer?: Transformer<Model, Item>,
|
|
106
|
-
): MongoQueryCollection<Model, Params, Model[
|
|
103
|
+
): MongoQueryCollection<Model, Params, Model["_id"], Item> {
|
|
107
104
|
return new MongoQueryCollection<Model, Params, KeyValue, Item>(
|
|
108
105
|
this,
|
|
109
106
|
options,
|
|
@@ -113,7 +110,7 @@ export default class MongoStore<
|
|
|
113
110
|
|
|
114
111
|
async insertOne(object: MongoInsertType<Model>): Promise<Model> {
|
|
115
112
|
if (!object._id) {
|
|
116
|
-
object._id = new mongodb.ObjectId().toString() as Model[
|
|
113
|
+
object._id = new mongodb.ObjectId().toString() as Model["_id"];
|
|
117
114
|
}
|
|
118
115
|
|
|
119
116
|
if (!object.created) object.created = new Date();
|
|
@@ -125,7 +122,7 @@ export default class MongoStore<
|
|
|
125
122
|
object as any,
|
|
126
123
|
);
|
|
127
124
|
if (!isAcknowledged) {
|
|
128
|
-
throw new Error(
|
|
125
|
+
throw new Error("Fail to insert");
|
|
129
126
|
}
|
|
130
127
|
|
|
131
128
|
return object as Model;
|
|
@@ -146,7 +143,7 @@ export default class MongoStore<
|
|
|
146
143
|
>,
|
|
147
144
|
>(
|
|
148
145
|
object: UpsertPartialObject<MongoKeyPath, KeyValue, Model, K>,
|
|
149
|
-
setOnInsertPartialObject?: Update<Model>[
|
|
146
|
+
setOnInsertPartialObject?: Update<Model>["$setOnInsert"],
|
|
150
147
|
): Promise<Model> {
|
|
151
148
|
const result = await this.upsertOneWithInfo(
|
|
152
149
|
object,
|
|
@@ -162,9 +159,9 @@ export default class MongoStore<
|
|
|
162
159
|
>,
|
|
163
160
|
>(
|
|
164
161
|
object: UpsertPartialObject<MongoKeyPath, KeyValue, Model, K>,
|
|
165
|
-
setOnInsertPartialObject?: Update<Model>[
|
|
162
|
+
setOnInsertPartialObject?: Update<Model>["$setOnInsert"],
|
|
166
163
|
): Promise<MongoUpsertResult<KeyValue, Model>> {
|
|
167
|
-
const $setOnInsert: Update<Model>[
|
|
164
|
+
const $setOnInsert: Update<Model>["$setOnInsert"] = {
|
|
168
165
|
// @ts-expect-error -- created is Date as set in BaseModel
|
|
169
166
|
created: object.created || new Date(),
|
|
170
167
|
...setOnInsertPartialObject,
|
|
@@ -208,7 +205,7 @@ export default class MongoStore<
|
|
|
208
205
|
);
|
|
209
206
|
if (!commandResult.acknowledged) {
|
|
210
207
|
console.error(commandResult);
|
|
211
|
-
throw new Error(
|
|
208
|
+
throw new Error("Update failed");
|
|
212
209
|
}
|
|
213
210
|
const object = await this.findByKey(key);
|
|
214
211
|
return object!;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type { AllowedKeyValue } from
|
|
2
|
-
import { SubscribeStore } from
|
|
1
|
+
import type { AllowedKeyValue } from "liwi-store";
|
|
2
|
+
import { SubscribeStore } from "liwi-subscribe-store";
|
|
3
3
|
import type {
|
|
4
4
|
MongoBaseModel,
|
|
5
5
|
MongoInsertType,
|
|
6
6
|
MongoKeyPath,
|
|
7
|
-
} from
|
|
8
|
-
import type MongoConnection from
|
|
9
|
-
import type MongoStore from
|
|
7
|
+
} from "./MongoBaseModel";
|
|
8
|
+
import type MongoConnection from "./MongoConnection";
|
|
9
|
+
import type MongoStore from "./MongoStore";
|
|
10
10
|
|
|
11
11
|
export default function createMongoSubscribeStore<
|
|
12
12
|
Model extends MongoBaseModel<KeyValue>,
|
|
@@ -23,3 +23,5 @@ export default function createMongoSubscribeStore<
|
|
|
23
23
|
> {
|
|
24
24
|
return new SubscribeStore(mongoStore);
|
|
25
25
|
}
|
|
26
|
+
|
|
27
|
+
export { type SubscribeStore } from "liwi-subscribe-store";
|
package/src/index.ts
CHANGED
|
@@ -2,10 +2,16 @@ export type {
|
|
|
2
2
|
MongoBaseModel,
|
|
3
3
|
MongoKeyPath,
|
|
4
4
|
MongoInsertType,
|
|
5
|
-
} from
|
|
5
|
+
} from "./MongoBaseModel";
|
|
6
6
|
|
|
7
|
-
export type { Update } from
|
|
7
|
+
export type { Update } from "liwi-store";
|
|
8
8
|
|
|
9
|
-
export { default as MongoStore } from
|
|
10
|
-
export {
|
|
11
|
-
|
|
9
|
+
export { default as MongoStore } from "./MongoStore.ts";
|
|
10
|
+
export {
|
|
11
|
+
default as MongoConnection,
|
|
12
|
+
type MongoConfig,
|
|
13
|
+
} from "./MongoConnection.ts";
|
|
14
|
+
export {
|
|
15
|
+
default as createMongoSubscribeStore,
|
|
16
|
+
type SubscribeStore,
|
|
17
|
+
} from "./createMongoSubscribeStore.ts";
|