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/src/MongoStore.ts CHANGED
@@ -1,34 +1,33 @@
1
- /* eslint-disable max-lines */
2
1
  import type {
3
- UpsertResult,
4
- SubscribableStore,
5
- QueryParams,
6
- UpsertPartialObject,
2
+ AllowedKeyValue,
7
3
  Criteria,
8
- Sort,
9
- Update,
4
+ OptionalBaseModelKeysForInsert,
10
5
  QueryOptions,
6
+ QueryParams,
7
+ Sort,
8
+ SubscribableStore,
11
9
  Transformer,
12
- AllowedKeyValue,
13
- OptionalBaseModelKeysForInsert,
14
- } from 'liwi-store';
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 'mongodb';
22
- import mongodb from 'mongodb';
20
+ } from "mongodb";
21
+ import mongodb from "mongodb";
23
22
  import type {
24
23
  MongoBaseModel,
25
- MongoKeyPath,
26
24
  MongoInsertType,
27
- } from './MongoBaseModel';
28
- import type MongoConnection from './MongoConnection';
29
- import MongoCursor from './MongoCursor';
30
- import MongoQueryCollection from './MongoQueryCollection';
31
- import MongoQuerySingleItem from './MongoQuerySingleItem';
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
- SubscribableStore<
46
- MongoKeyPath,
47
- KeyValue,
48
- Model,
49
- MongoInsertType<Model>,
50
- MongoConnection
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('MongoDB connection failed'));
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['_id'], Item> {
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['_id'];
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('Fail to insert');
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>['$setOnInsert'],
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>['$setOnInsert'],
162
+ setOnInsertPartialObject?: Update<Model>["$setOnInsert"],
166
163
  ): Promise<MongoUpsertResult<KeyValue, Model>> {
167
- const $setOnInsert: Update<Model>['$setOnInsert'] = {
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('Update failed');
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 'liwi-store';
2
- import { SubscribeStore } from 'liwi-subscribe-store';
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 './MongoBaseModel';
8
- import type MongoConnection from './MongoConnection';
9
- import type MongoStore from './MongoStore';
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 './MongoBaseModel';
5
+ } from "./MongoBaseModel";
6
6
 
7
- export type { Update } from 'liwi-store';
7
+ export type { Update } from "liwi-store";
8
8
 
9
- export { default as MongoStore } from './MongoStore';
10
- export { default as MongoConnection } from './MongoConnection';
11
- export { default as createMongoSubscribeStore } from './createMongoSubscribeStore';
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";