@koalarx/nest 1.0.1 → 1.2.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.
@@ -1,16 +1,17 @@
1
+ import { PaginationDto } from '@koalarx/nest/core/dtos/pagination.dto';
1
2
  import { CreatedRegistreResponseBase } from '../../core/controllers/created-registre-response.base';
2
3
  import { ListResponseBase } from '../../core/controllers/list-response.base';
3
4
  import { EntityBase } from '../../core/database/entity.base';
4
- import { PaginationParams } from '../../core/models/pagination-params';
5
5
  import { IComparableId } from '../../core/utils/interfaces/icomparable';
6
6
  export declare abstract class InMemoryBaseRepository<TClass extends EntityBase<any>> {
7
7
  private readonly typeId;
8
8
  protected items: TClass[];
9
9
  constructor(typeId?: 'number' | 'string');
10
10
  protected findById(id: IComparableId): Promise<TClass | null>;
11
- protected findMany<T extends PaginationParams>(query: T, predicate?: (value: TClass, index: number, array: TClass[]) => unknown): Promise<TClass[]>;
12
- protected findManyAndCount<T extends PaginationParams>(query: T, predicate?: (value: TClass, index: number, array: TClass[]) => unknown): Promise<ListResponseBase<TClass>>;
13
- protected saveChanges(item: TClass): Promise<CreatedRegistreResponseBase<any>>;
11
+ protected findMany<T extends PaginationDto>(query: T, predicate?: (value: TClass, index: number, array: TClass[]) => unknown): Promise<TClass[]>;
12
+ protected findManyAndCount<T extends PaginationDto>(query: T, predicate?: (value: TClass, index: number, array: TClass[]) => unknown): Promise<ListResponseBase<TClass>>;
13
+ protected insert(item: TClass): Promise<CreatedRegistreResponseBase<any>>;
14
+ protected edit(item: TClass, updateWhere?: (item: TClass) => boolean): Promise<void>;
14
15
  protected remove(predicate: (value: TClass, index: number, obj: TClass[]) => unknown): Promise<void>;
15
16
  private getNewId;
16
17
  }
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InMemoryBaseRepository = void 0;
4
+ const query_params_1 = require("../../core/constants/query-params");
4
5
  const array_1 = require("@koalarx/utils/operators/array");
5
- const pagination_params_1 = require("../../core/models/pagination-params");
6
6
  const node_crypto_1 = require("node:crypto");
7
7
  class InMemoryBaseRepository {
8
8
  typeId;
@@ -14,8 +14,8 @@ class InMemoryBaseRepository {
14
14
  return this.items.find((item) => item._id === id) ?? null;
15
15
  }
16
16
  async findMany(query, predicate) {
17
- const page = query.page ?? pagination_params_1.QueryFilterParams.page;
18
- const limit = query.limit ?? pagination_params_1.QueryFilterParams.limit;
17
+ const page = query.page ?? query_params_1.QUERY_FILTER_PARAMS.page;
18
+ const limit = query.limit ?? query_params_1.QUERY_FILTER_PARAMS.limit;
19
19
  return (0, array_1.klArray)(predicate ? this.items.filter(predicate) : this.items)
20
20
  .orderBy(query.orderBy ?? '', query.direction === 'desc')
21
21
  .getValue()
@@ -28,18 +28,19 @@ class InMemoryBaseRepository {
28
28
  count: items.length,
29
29
  };
30
30
  }
31
- async saveChanges(item) {
32
- const itemIndex = this.items.findIndex((itemDB) => itemDB.equals(item));
33
- if (itemIndex > -1) {
34
- this.items[itemIndex] = item;
35
- }
36
- const id = this.typeId === 'number'
37
- ? this.getNewId()
38
- : (0, node_crypto_1.randomUUID)();
31
+ async insert(item) {
32
+ const id = this.typeId === 'number' ? this.getNewId() : (0, node_crypto_1.randomUUID)();
39
33
  item.automap({ ...item, id });
40
34
  this.items.push(item);
41
35
  return { id: item._id };
42
36
  }
37
+ async edit(item, updateWhere) {
38
+ const predicate = updateWhere ?? ((itemDB) => itemDB.equals(item));
39
+ const itemIndex = this.items.findIndex(predicate);
40
+ if (itemIndex > -1) {
41
+ this.items[itemIndex] = item;
42
+ }
43
+ }
43
44
  async remove(predicate) {
44
45
  const itemIndex = this.items.findIndex(predicate);
45
46
  if (itemIndex > -1) {
@@ -48,7 +49,8 @@ class InMemoryBaseRepository {
48
49
  }
49
50
  getNewId() {
50
51
  return this.typeId === 'number'
51
- ? (0, array_1.klArray)(this.items).orderBy('_id', true).getValue()[0]?._id ?? 0 + 1
52
+ ? (0, array_1.klArray)(this.items).orderBy('_id', true).getValue()[0]
53
+ ?._id ?? 0 + 1
52
54
  : (0, node_crypto_1.randomUUID)();
53
55
  }
54
56
  }
@@ -13,7 +13,7 @@ function generateUniqueDatabaseURL() {
13
13
  url.searchParams.set('schema', schemaId);
14
14
  return {
15
15
  url: url.toString(),
16
- schemaId
16
+ schemaId,
17
17
  };
18
18
  }
19
19
  function createE2EDatabase() {