liwi-mongo 12.0.0 → 13.1.0

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.
Files changed (34) hide show
  1. package/README.md +7 -7
  2. package/dist/definitions/MongoBaseModel.d.ts +2 -2
  3. package/dist/definitions/MongoBaseModel.d.ts.map +1 -1
  4. package/dist/definitions/MongoConnection.d.ts +10 -3
  5. package/dist/definitions/MongoConnection.d.ts.map +1 -1
  6. package/dist/definitions/MongoCursor.d.ts +6 -6
  7. package/dist/definitions/MongoQueryCollection.d.ts +5 -5
  8. package/dist/definitions/MongoQueryCollection.d.ts.map +1 -1
  9. package/dist/definitions/MongoQueryCollection.test.d.ts +2 -0
  10. package/dist/definitions/MongoQueryCollection.test.d.ts.map +1 -0
  11. package/dist/definitions/MongoQuerySingleItem.d.ts +5 -5
  12. package/dist/definitions/MongoQuerySingleItem.d.ts.map +1 -1
  13. package/dist/definitions/MongoStore.d.ts +12 -12
  14. package/dist/definitions/MongoStore.d.ts.map +1 -1
  15. package/dist/definitions/createMongoSubscribeStore.d.ts +6 -5
  16. package/dist/definitions/createMongoSubscribeStore.d.ts.map +1 -1
  17. package/dist/definitions/index.d.ts +5 -5
  18. package/dist/definitions/index.d.ts.map +1 -1
  19. package/dist/index-node.mjs +555 -0
  20. package/dist/index-node.mjs.map +1 -0
  21. package/package.json +43 -38
  22. package/src/MongoBaseModel.ts +5 -4
  23. package/src/MongoConnection.ts +47 -43
  24. package/src/MongoCursor.ts +7 -7
  25. package/src/MongoQueryCollection.test.ts +108 -0
  26. package/src/MongoQueryCollection.ts +26 -25
  27. package/src/MongoQuerySingleItem.ts +27 -26
  28. package/src/MongoStore.ts +52 -43
  29. package/src/createMongoSubscribeStore.ts +7 -5
  30. package/src/index.ts +11 -5
  31. package/CHANGELOG.md +0 -628
  32. package/dist/index-node18.mjs +0 -588
  33. package/dist/index-node18.mjs.map +0 -1
  34. package/src/.eslintrc.json +0 -38
@@ -1,23 +1,23 @@
1
1
  import type {
2
- QuerySubscription,
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
- AllowedKeyValue,
10
- } from 'liwi-store';
11
- import type { Actions } from 'liwi-subscribe-store';
12
- import { AbstractSubscribableStoreQuery } from 'liwi-subscribe-store';
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 './MongoBaseModel';
19
- import type MongoCursor from './MongoCursor';
20
- import type MongoStore from './MongoStore';
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['_id'],
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: 'initial',
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 'inserted': {
116
+ case "inserted": {
117
117
  const filtered = action.next.filter(testCriteria);
118
118
  if (filtered.length > 0) {
119
119
  changes.push({
120
- type: 'updated',
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 'deleted': {
126
+ case "deleted": {
127
127
  const filtered = action.prev.filter(testCriteria);
128
128
  if (filtered.length > 0) {
129
129
  changes.push({
130
- type: 'deleted',
130
+ type: "deleted",
131
131
  keys: filtered.map((object) => object[this.store.keyPath]),
132
132
  });
133
133
  }
134
134
  break;
135
135
  }
136
- case 'updated': {
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: 'updated',
144
+ type: "updated",
145
145
  result,
146
146
  });
147
147
  } else if (filtered.length !== 1) {
148
148
  throw new Error(
149
- 'should not match more than 1, use sort if you can have multiple match',
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: 'updated',
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('Unsupported type');
163
+ throw new Error("Unsupported type");
164
164
  }
165
165
 
166
166
  if (changes.length === 0) return;
@@ -184,6 +184,7 @@ export default class MongoQuerySingleItem<
184
184
  const cursor = await this.store.cursor(
185
185
  this.options.criteria,
186
186
  this.options.sort,
187
+ this.options.fields,
187
188
  );
188
189
 
189
190
  if (this.options.limit) {
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,
3
+ CreateQueryOptions,
7
4
  Criteria,
5
+ Fields,
6
+ OptionalBaseModelKeysForInsert,
7
+ QueryParams,
8
8
  Sort,
9
+ SubscribableStore,
9
10
  Update,
10
- QueryOptions,
11
- Transformer,
12
- AllowedKeyValue,
13
- OptionalBaseModelKeysForInsert,
14
- } from 'liwi-store';
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);
@@ -86,10 +83,15 @@ export default class MongoStore<
86
83
  createQuerySingleItem<
87
84
  Result extends Record<MongoKeyPath, KeyValue> = Model,
88
85
  Params extends QueryParams<Params> = never,
89
- >(
90
- options: QueryOptions<Model>,
91
- transformer?: Transformer<Model, Result>,
92
- ): MongoQuerySingleItem<Model, Params, Result, KeyValue> {
86
+ >({
87
+ transformer,
88
+ ...options
89
+ }: CreateQueryOptions<Model, Result>): MongoQuerySingleItem<
90
+ Model,
91
+ Params,
92
+ Result,
93
+ KeyValue
94
+ > {
93
95
  return new MongoQuerySingleItem<Model, Params, Result, KeyValue>(
94
96
  this,
95
97
  options,
@@ -100,10 +102,15 @@ export default class MongoStore<
100
102
  createQueryCollection<
101
103
  Item extends Record<MongoKeyPath, KeyValue> = Model,
102
104
  Params extends QueryParams<Params> = never,
103
- >(
104
- options: QueryOptions<Model>,
105
- transformer?: Transformer<Model, Item>,
106
- ): MongoQueryCollection<Model, Params, Model['_id'], Item> {
105
+ >({
106
+ transformer,
107
+ ...options
108
+ }: CreateQueryOptions<Model, Item>): MongoQueryCollection<
109
+ Model,
110
+ Params,
111
+ Model["_id"],
112
+ Item
113
+ > {
107
114
  return new MongoQueryCollection<Model, Params, KeyValue, Item>(
108
115
  this,
109
116
  options,
@@ -113,7 +120,7 @@ export default class MongoStore<
113
120
 
114
121
  async insertOne(object: MongoInsertType<Model>): Promise<Model> {
115
122
  if (!object._id) {
116
- object._id = new mongodb.ObjectId().toString() as Model['_id'];
123
+ object._id = new mongodb.ObjectId().toString() as Model["_id"];
117
124
  }
118
125
 
119
126
  if (!object.created) object.created = new Date();
@@ -125,7 +132,7 @@ export default class MongoStore<
125
132
  object as any,
126
133
  );
127
134
  if (!isAcknowledged) {
128
- throw new Error('Fail to insert');
135
+ throw new Error("Fail to insert");
129
136
  }
130
137
 
131
138
  return object as Model;
@@ -146,7 +153,7 @@ export default class MongoStore<
146
153
  >,
147
154
  >(
148
155
  object: UpsertPartialObject<MongoKeyPath, KeyValue, Model, K>,
149
- setOnInsertPartialObject?: Update<Model>['$setOnInsert'],
156
+ setOnInsertPartialObject?: Update<Model>["$setOnInsert"],
150
157
  ): Promise<Model> {
151
158
  const result = await this.upsertOneWithInfo(
152
159
  object,
@@ -162,9 +169,9 @@ export default class MongoStore<
162
169
  >,
163
170
  >(
164
171
  object: UpsertPartialObject<MongoKeyPath, KeyValue, Model, K>,
165
- setOnInsertPartialObject?: Update<Model>['$setOnInsert'],
172
+ setOnInsertPartialObject?: Update<Model>["$setOnInsert"],
166
173
  ): Promise<MongoUpsertResult<KeyValue, Model>> {
167
- const $setOnInsert: Update<Model>['$setOnInsert'] = {
174
+ const $setOnInsert: Update<Model>["$setOnInsert"] = {
168
175
  // @ts-expect-error -- created is Date as set in BaseModel
169
176
  created: object.created || new Date(),
170
177
  ...setOnInsertPartialObject,
@@ -208,7 +215,7 @@ export default class MongoStore<
208
215
  );
209
216
  if (!commandResult.acknowledged) {
210
217
  console.error(commandResult);
211
- throw new Error('Update failed');
218
+ throw new Error("Update failed");
212
219
  }
213
220
  const object = await this.findByKey(key);
214
221
  return object!;
@@ -261,12 +268,14 @@ export default class MongoStore<
261
268
  async cursor<Result extends Partial<Model> = Model>(
262
269
  filter?: Criteria<Model>,
263
270
  sort?: Sort<Model>,
271
+ fields?: Fields<Model>,
264
272
  ): Promise<MongoCursor<Model, Result, KeyValue>> {
265
273
  const collection = await this.collection;
266
274
  const findCursor = filter
267
275
  ? collection.find<Result>(filter as Filter<Model>)
268
276
  : (collection.find() as unknown as FindCursor<Result>);
269
277
  if (sort) findCursor.sort(sort);
278
+ if (fields) findCursor.project(fields);
270
279
  return new MongoCursor<Model, Result, KeyValue>(this, findCursor);
271
280
  }
272
281
 
@@ -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";