pangea-server 1.0.35 → 1.0.36

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.
@@ -27,9 +27,11 @@ type FindManyOptions<BM extends BaseModel> = {
27
27
  search?: string | null;
28
28
  group?: Group;
29
29
  order?: Order;
30
+ paranoid?: 'active' | 'all' | 'deleted';
31
+ };
32
+ type FindManyPagedOptions<BM extends BaseModel> = FindManyOptions<BM> & {
30
33
  page?: number | null;
31
34
  pageSize?: number | null;
32
- paranoid?: 'active' | 'all' | 'deleted';
33
35
  };
34
36
  type Target<BM extends BaseModel> = Filters<BM> | BM;
35
37
  export declare class Db {
@@ -37,24 +39,27 @@ export declare class Db {
37
39
  constructor(tx: Tx);
38
40
  findOneOrNull<BM extends BaseModel>(model: BaseModelCtor<BM>, filters: Filters<BM>, options?: FindOneOptions): Promise<BM | null>;
39
41
  findOne<BM extends BaseModel>(model: BaseModelCtor<BM>, filters: Filters<BM>, options?: FindOneOptions): Promise<BM>;
40
- findMany<BM extends BaseModel>(model: BaseModelCtor<BM>, options?: FindManyOptions<BM>): Promise<{
42
+ findManyInstances<BM extends BaseModel>(model: BaseModelCtor<BM>, options?: FindManyPagedOptions<BM>): Promise<BM[]>;
43
+ count<BM extends BaseModel>(model: BaseModelCtor<BM>, options?: FindManyOptions<BM>): Promise<number>;
44
+ findMany<BM extends BaseModel>(model: BaseModelCtor<BM>, options?: FindManyPagedOptions<BM>): Promise<{
41
45
  instances: BM[];
42
46
  totalCount: number;
43
47
  }>;
48
+ private __getFindManyBaseOptions;
44
49
  insertOne<BM extends BaseModel>(model: BaseModelCtor<BM>, params: InsertParams<BM>): Promise<BM>;
45
50
  insertMany<BM extends BaseModel>(model: BaseModelCtor<BM>, data: InsertParams<BM>[]): Promise<number>;
51
+ upsertMany<BM extends BaseModel>(model: BaseModelCtor<BM>, fieldsToUpdate: (keyof BM)[], data: InsertParams<BM>[]): Promise<number>;
46
52
  updateOne<BM extends BaseModel>(model: BaseModelCtor<BM>, target: Target<BM>, params: UpdateParams<BM>): Promise<BM>;
47
53
  updateMany<BM extends BaseModel>(model: BaseModelCtor<BM>, where: Where<BM>, params: UpdateParams<BM>): Promise<number>;
48
- upsertMany<BM extends BaseModel>(model: BaseModelCtor<BM>, fieldsToUpdate: (keyof BM)[], data: InsertParams<BM>[]): Promise<number>;
49
54
  deleteOne<BM extends BaseModel>(model: BaseModelCtor<BM>, target: Target<BM>): Promise<BM | null>;
50
55
  deleteMany<BM extends BaseModel>(model: BaseModelCtor<BM>, where: Where<BM>): Promise<number>;
51
56
  destroyOne<BM extends BaseModel>(model: BaseModelCtor<BM>, target: Target<BM>): Promise<BM | null>;
52
57
  destroyMany<BM extends BaseModel>(model: BaseModelCtor<BM>, where: Where<BM>): Promise<number>;
58
+ private __deleteOne;
59
+ private __deleteMany;
53
60
  restoreOne<BM extends BaseModel>(model: BaseModelCtor<BM>, target: Target<BM>): Promise<BM>;
54
61
  restoreMany<BM extends BaseModel>(model: BaseModelCtor<BM>, where: Where<BM>): Promise<void>;
55
62
  private __getInstance;
56
- private __deleteOne;
57
- private __deleteMany;
58
63
  enableForeignKeyChecks(): Promise<[unknown[], unknown]>;
59
64
  disableForeignKeyChecks(): Promise<[unknown[], unknown]>;
60
65
  }
@@ -36,8 +36,41 @@ class Db {
36
36
  helpers_1.AppError.ThrowEntityNotFound();
37
37
  return instance;
38
38
  }
39
+ async findManyInstances(model, options = {}) {
40
+ const baseOptions = this.__getFindManyBaseOptions(model, options);
41
+ const { page, pageSize } = options;
42
+ if (page && pageSize) {
43
+ const idRows = await model.findAll({
44
+ ...baseOptions,
45
+ attributes: [[(0, sequelize_1.col)(`${model.name}.id`), 'id']],
46
+ group: [`${model.name}.id`],
47
+ raw: true,
48
+ offset: (page - 1) * pageSize,
49
+ limit: pageSize,
50
+ });
51
+ const ids = idRows.map((row) => row.id);
52
+ return model.findAll({ ...baseOptions, where: { id: { [_1.Ops.in]: ids } } });
53
+ }
54
+ return model.findAll(baseOptions);
55
+ }
56
+ async count(model, options = {}) {
57
+ const baseOptions = this.__getFindManyBaseOptions(model, options);
58
+ return model.count({ ...baseOptions, col: 'id', distinct: true });
59
+ }
39
60
  async findMany(model, options = {}) {
40
- const { attributes, include = [], where, searchFields, search, group, order, page, pageSize, paranoid = 'active', } = options;
61
+ const { page, pageSize } = options;
62
+ if (page && pageSize) {
63
+ const [instances, totalCount] = await Promise.all([
64
+ this.findManyInstances(model, options),
65
+ this.count(model, options),
66
+ ]);
67
+ return { instances, totalCount };
68
+ }
69
+ const instances = await this.findManyInstances(model, options);
70
+ return { instances, totalCount: instances.length };
71
+ }
72
+ __getFindManyBaseOptions(model, options = {}) {
73
+ const { attributes, include = [], where, searchFields, search, group, order, paranoid = 'active' } = options;
41
74
  let searchWhere;
42
75
  if (searchFields?.length && search) {
43
76
  const searchWords = search.split(/\s+/).filter(Boolean);
@@ -51,7 +84,7 @@ class Db {
51
84
  };
52
85
  }
53
86
  const deletedAtWhere = paranoid === 'deleted' ? { deletedAt: { [_1.Ops.not]: null } } : {};
54
- const baseOptions = {
87
+ return {
55
88
  attributes: getFinalAttributes(attributes),
56
89
  ...getIncludeAndWhere(model, include, { ...where, ...searchWhere, ...deletedAtWhere }),
57
90
  ...(group && { group: group.map(pangea_helpers_1.camelToSnake) }),
@@ -60,26 +93,6 @@ class Db {
60
93
  subQuery: false,
61
94
  transaction: this.__tx,
62
95
  };
63
- if (page && pageSize) {
64
- const [instances, totalCount] = await Promise.all([
65
- (async () => {
66
- const idRows = await model.findAll({
67
- ...baseOptions,
68
- attributes: [[(0, sequelize_1.col)(`${model.name}.id`), 'id']],
69
- group: [`${model.name}.id`],
70
- raw: true,
71
- offset: (page - 1) * pageSize,
72
- limit: pageSize,
73
- });
74
- const ids = idRows.map((row) => row.id);
75
- return model.findAll({ ...baseOptions, where: { id: { [_1.Ops.in]: ids } } });
76
- })(),
77
- model.count({ ...baseOptions, col: 'id', distinct: true }),
78
- ]);
79
- return { instances, totalCount };
80
- }
81
- const instances = await model.findAll(baseOptions);
82
- return { instances, totalCount: instances.length };
83
96
  }
84
97
  // insert methods
85
98
  insertOne(model, params) {
@@ -94,6 +107,16 @@ class Db {
94
107
  return newInstances.length;
95
108
  });
96
109
  }
110
+ upsertMany(model, fieldsToUpdate, data) {
111
+ return handleDbError(async () => {
112
+ const newInstances = await model.bulkCreate(data, {
113
+ updateOnDuplicate: fieldsToUpdate,
114
+ validate: true,
115
+ transaction: this.__tx,
116
+ });
117
+ return newInstances.length;
118
+ });
119
+ }
97
120
  // update methods
98
121
  updateOne(model, target, params) {
99
122
  return handleDbError(async () => {
@@ -108,17 +131,6 @@ class Db {
108
131
  return count;
109
132
  });
110
133
  }
111
- // upsert methods
112
- upsertMany(model, fieldsToUpdate, data) {
113
- return handleDbError(async () => {
114
- const newInstances = await model.bulkCreate(data, {
115
- updateOnDuplicate: fieldsToUpdate,
116
- validate: true,
117
- transaction: this.__tx,
118
- });
119
- return newInstances.length;
120
- });
121
- }
122
134
  // delete methods
123
135
  deleteOne(model, target) {
124
136
  return this.__deleteOne(model, target, false);
@@ -126,13 +138,24 @@ class Db {
126
138
  deleteMany(model, where) {
127
139
  return this.__deleteMany(model, where, false);
128
140
  }
129
- // destroy methods
130
141
  destroyOne(model, target) {
131
142
  return this.__deleteOne(model, target, true);
132
143
  }
133
144
  destroyMany(model, where) {
134
145
  return this.__deleteMany(model, where, true);
135
146
  }
147
+ __deleteOne(model, target, force) {
148
+ return handleDbError(async () => {
149
+ const instance = await this.__getInstance(model, target);
150
+ await instance.destroy({ force, transaction: this.__tx });
151
+ return this.findOneOrNull(model, instance.id, { paranoid: false });
152
+ });
153
+ }
154
+ __deleteMany(model, where, force) {
155
+ return handleDbError(() => {
156
+ return model.destroy({ where, force, transaction: this.__tx });
157
+ });
158
+ }
136
159
  // restore methods
137
160
  restoreOne(model, target) {
138
161
  return handleDbError(async () => {
@@ -152,18 +175,6 @@ class Db {
152
175
  return target;
153
176
  return this.findOne(model, target, { paranoid });
154
177
  }
155
- __deleteOne(model, target, force) {
156
- return handleDbError(async () => {
157
- const instance = await this.__getInstance(model, target);
158
- await instance.destroy({ force, transaction: this.__tx });
159
- return this.findOneOrNull(model, instance.id, { paranoid: false });
160
- });
161
- }
162
- __deleteMany(model, where, force) {
163
- return handleDbError(() => {
164
- return model.destroy({ where, force, transaction: this.__tx });
165
- });
166
- }
167
178
  // foreign key methods
168
179
  enableForeignKeyChecks() {
169
180
  const dbClient = (0, db_client_1.getDbClient)();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pangea-server",
3
3
  "description": "",
4
- "version": "1.0.35",
4
+ "version": "1.0.36",
5
5
  "files": [
6
6
  "dist"
7
7
  ],