@abejarano/ts-mongodb-criteria 1.11.1 → 1.11.2

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 CHANGED
@@ -182,7 +182,7 @@ Use the `collection` argument inside `ensureIndexes` to avoid recursion.
182
182
  MongoRepository provides ready-to-use public methods for repositories that extend it:
183
183
 
184
184
  - `list(criteria, fieldsToExclude?)` for paginated queries
185
- - `many(filter, options?)` to fetch multiple entities matching a filter, with optional `{ transaction?, sort }`
185
+ - `many(filter, options?)` to fetch multiple entities matching a filter, with optional `{ transaction?, fieldsToExclude?, sort? }`
186
186
  - `one(filter, transaction?)` to fetch a single entity
187
187
  - `upsert(entity, transaction?)` to persist an aggregate
188
188
  Internal helpers are private, so repositories should call these public methods
@@ -220,7 +220,7 @@ await MongoTransaction.run(async (tx) => {
220
220
  const persistedUser = await userRepository.one({ id: user.getId() }, tx)
221
221
  const activeUsers = await userRepository.many(
222
222
  { status: "active" },
223
- { transaction: tx, sort: { name: 1 } }
223
+ { transaction: tx, sort: Order.asc("name") }
224
224
  )
225
225
  })
226
226
  ```
@@ -25,16 +25,23 @@ class MongoRepository {
25
25
  id: result._id.toString(),
26
26
  });
27
27
  }
28
+ /**
29
+ *
30
+ * @param filter
31
+ * @param options
32
+ */
28
33
  async many(filter, options) {
29
34
  const collection = await this.collection();
30
35
  const session = options?.transaction
31
36
  ? MongoTransaction_1.MongoTransaction.sessionFor(options?.transaction)
32
37
  : undefined;
33
- const hasFields = options?.fields && options.fields.length > 0;
34
- const projection = hasFields ? {} : undefined;
38
+ const hasFields = options?.fieldsToExclude && options.fieldsToExclude.length > 0;
39
+ const projection = hasFields
40
+ ? {}
41
+ : undefined;
35
42
  if (hasFields) {
36
- options.fields.forEach((field) => {
37
- projection[field] = 1;
43
+ options.fieldsToExclude.forEach((field) => {
44
+ projection[field] = 0;
38
45
  });
39
46
  }
40
47
  const findOptions = session
@@ -42,9 +49,19 @@ class MongoRepository {
42
49
  : projection
43
50
  ? { projection }
44
51
  : undefined;
52
+ let order = { _id: -1 };
53
+ if (options?.sort?.hasOrder()) {
54
+ order = {
55
+ [options?.sort.orderBy.value === "id"
56
+ ? "_id"
57
+ : options?.sort.orderBy.value]: options.sort.orderType.isAsc()
58
+ ? 1
59
+ : -1,
60
+ };
61
+ }
45
62
  const documents = await collection
46
63
  .find(filter, findOptions)
47
- .sort(options?.sort ? options?.sort : { _id: -1 })
64
+ .sort(order)
48
65
  .toArray();
49
66
  return documents.map((document) => this.aggregateRootClass.fromPrimitives({
50
67
  ...document,
@@ -76,6 +93,7 @@ class MongoRepository {
76
93
  *
77
94
  * @param {object} filter - The query to match documents that should be deleted.
78
95
  * @param {DeleteOptions} [options] - Optional parameters that modify the behavior of the delete operation.
96
+ * @param transaction
79
97
  * @return {Promise<void>} A promise that resolves when the deletion is complete, or rejects if an error occurs.
80
98
  */
81
99
  async delete(filter, options, transaction) {
@@ -22,16 +22,23 @@ export class MongoRepository {
22
22
  id: result._id.toString(),
23
23
  });
24
24
  }
25
+ /**
26
+ *
27
+ * @param filter
28
+ * @param options
29
+ */
25
30
  async many(filter, options) {
26
31
  const collection = await this.collection();
27
32
  const session = options?.transaction
28
33
  ? MongoTransaction.sessionFor(options?.transaction)
29
34
  : undefined;
30
- const hasFields = options?.fields && options.fields.length > 0;
31
- const projection = hasFields ? {} : undefined;
35
+ const hasFields = options?.fieldsToExclude && options.fieldsToExclude.length > 0;
36
+ const projection = hasFields
37
+ ? {}
38
+ : undefined;
32
39
  if (hasFields) {
33
- options.fields.forEach((field) => {
34
- projection[field] = 1;
40
+ options.fieldsToExclude.forEach((field) => {
41
+ projection[field] = 0;
35
42
  });
36
43
  }
37
44
  const findOptions = session
@@ -39,9 +46,19 @@ export class MongoRepository {
39
46
  : projection
40
47
  ? { projection }
41
48
  : undefined;
49
+ let order = { _id: -1 };
50
+ if (options?.sort?.hasOrder()) {
51
+ order = {
52
+ [options?.sort.orderBy.value === "id"
53
+ ? "_id"
54
+ : options?.sort.orderBy.value]: options.sort.orderType.isAsc()
55
+ ? 1
56
+ : -1,
57
+ };
58
+ }
42
59
  const documents = await collection
43
60
  .find(filter, findOptions)
44
- .sort(options?.sort ? options?.sort : { _id: -1 })
61
+ .sort(order)
45
62
  .toArray();
46
63
  return documents.map((document) => this.aggregateRootClass.fromPrimitives({
47
64
  ...document,
@@ -73,6 +90,7 @@ export class MongoRepository {
73
90
  *
74
91
  * @param {object} filter - The query to match documents that should be deleted.
75
92
  * @param {DeleteOptions} [options] - Optional parameters that modify the behavior of the delete operation.
93
+ * @param transaction
76
94
  * @return {Promise<void>} A promise that resolves when the deletion is complete, or rejects if an error occurs.
77
95
  */
78
96
  async delete(filter, options, transaction) {
@@ -1,4 +1,3 @@
1
1
  export * from "./criteria";
2
2
  export * from "./mongo";
3
3
  export * from "./AggregateRoot";
4
- export type { MongoSort, MongoDirection } from "./types";
@@ -2,12 +2,12 @@ import { Criteria, Paginate } from "../criteria";
2
2
  import { AggregateRoot } from "../AggregateRoot";
3
3
  import { DeleteOptions } from "mongodb";
4
4
  import { MongoTransaction } from "./MongoTransaction";
5
- import { MongoSort } from "../types";
5
+ import { Order } from "../criteria";
6
6
  export interface IRepository<T extends AggregateRoot> {
7
7
  many(filter: object, options?: {
8
8
  transaction?: MongoTransaction;
9
- fields?: string[];
10
- sort?: MongoSort;
9
+ fieldsToExclude?: string[];
10
+ sort?: Order;
11
11
  }): Promise<T[]>;
12
12
  one(filter: object, transaction?: MongoTransaction): Promise<T | null>;
13
13
  list<D>(criteria: Criteria, fieldsToExclude?: string[], transaction?: MongoTransaction): Promise<Paginate<D>>;
@@ -1,8 +1,7 @@
1
1
  import { MongoTransaction } from "./MongoTransaction";
2
- import { Criteria, Paginate } from "../criteria";
2
+ import { Criteria, Order, Paginate } from "../criteria";
3
3
  import { AggregateRoot, AggregateRootClass } from "../AggregateRoot";
4
4
  import { Collection, DeleteOptions, Document, UpdateFilter } from "mongodb";
5
- import { MongoSort } from "../types";
6
5
  export declare abstract class MongoRepository<T extends AggregateRoot> {
7
6
  private readonly aggregateRootClass;
8
7
  private static indexRegistry;
@@ -13,10 +12,15 @@ export declare abstract class MongoRepository<T extends AggregateRoot> {
13
12
  abstract collectionName(): string;
14
13
  /** Finds a single entity and hydrates it via the aggregate's fromPrimitives. */
15
14
  one(filter: object, transaction?: MongoTransaction): Promise<T | null>;
15
+ /**
16
+ *
17
+ * @param filter
18
+ * @param options
19
+ */
16
20
  many(filter: object, options?: {
17
21
  transaction?: MongoTransaction;
18
- fields?: string[];
19
- sort?: MongoSort;
22
+ fieldsToExclude?: string[];
23
+ sort?: Order;
20
24
  }): Promise<T[]>;
21
25
  /** Upserts an aggregate by delegating to persist with its id. */
22
26
  upsert(entity: T, transaction?: MongoTransaction): Promise<void>;
@@ -27,6 +31,7 @@ export declare abstract class MongoRepository<T extends AggregateRoot> {
27
31
  *
28
32
  * @param {object} filter - The query to match documents that should be deleted.
29
33
  * @param {DeleteOptions} [options] - Optional parameters that modify the behavior of the delete operation.
34
+ * @param transaction
30
35
  * @return {Promise<void>} A promise that resolves when the deletion is complete, or rejects if an error occurs.
31
36
  */
32
37
  delete(filter: object, options?: DeleteOptions, transaction?: MongoTransaction): Promise<void>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@abejarano/ts-mongodb-criteria",
3
3
  "author": "angel bejarano / angel.bejarano@jaspesoft.com",
4
- "version": "1.11.1",
4
+ "version": "1.11.2",
5
5
  "description": "Patrón Criteria para consultas MongoDB en TypeScript",
6
6
  "main": "dist/cjs/index.js",
7
7
  "module": "dist/esm/index.js",