pangea-server 1.0.57 → 1.0.59

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.
@@ -4,30 +4,23 @@ type AttributeOp = 'COUNT' | 'SUM';
4
4
  type AttributeColumn = AttributeField | [AttributeOp, AttributeField];
5
5
  type AttributeAlias = string;
6
6
  type Attributes = Record<AttributeAlias, AttributeColumn>;
7
- type IncludeItem = {
8
- relation?: string;
9
- active?: boolean;
10
- include?: IncludeItem[];
11
- };
12
7
  type Filters<BM extends BaseModel> = ModelId | Where<BM> | undefined;
13
8
  type Group = string[];
14
9
  type Order = Record<string, 'asc' | 'desc'>;
15
10
  type FindOneOptions = {
16
11
  scopes?: string[];
17
12
  attributes?: Attributes;
18
- include?: IncludeItem[];
19
13
  order?: Order;
20
14
  paranoid?: boolean;
21
15
  };
22
16
  type FindManyOptions<BM extends BaseModel> = {
23
17
  attributes?: Attributes;
24
- include?: IncludeItem[];
25
18
  where?: Where<BM>;
26
19
  searchFields?: string[];
27
20
  search?: string | null;
28
21
  group?: Group;
29
22
  order?: Order;
30
- paranoid?: 'active' | 'all' | 'deleted';
23
+ paranoid?: boolean;
31
24
  };
32
25
  type FindManyPagedOptions<BM extends BaseModel> = FindManyOptions<BM> & {
33
26
  page?: number | null;
@@ -13,8 +13,8 @@ class Db {
13
13
  }
14
14
  // find methods
15
15
  findOneOrNull(model, filters, options = {}) {
16
- const { scopes = [], attributes, include = [], order, paranoid = true } = options;
17
- const scopedModel = scopes.length ? model.scope(...scopes) : model;
16
+ const { scopes, attributes, order, paranoid = true } = options;
17
+ const scopedModel = scopes?.length ? model.scope(...scopes) : model;
18
18
  const baseOptions = {
19
19
  attributes: getFinalAttributes(attributes),
20
20
  paranoid,
@@ -22,11 +22,11 @@ class Db {
22
22
  transaction: this.__tx,
23
23
  };
24
24
  if (typeof filters === 'number') {
25
- return scopedModel.findByPk(filters, { ...baseOptions, ...getIncludeAndWhere(model, include) });
25
+ return scopedModel.findByPk(filters, { ...baseOptions, ...getInclude(model) });
26
26
  }
27
27
  return scopedModel.findOne({
28
28
  ...baseOptions,
29
- ...getIncludeAndWhere(model, include, { withoutAttributes: !!attributes }, filters),
29
+ ...getInclude(model, { attributes, where: filters }),
30
30
  order: getFinalOrder(order),
31
31
  });
32
32
  }
@@ -67,7 +67,7 @@ class Db {
67
67
  return { instances, totalCount: instances.length };
68
68
  }
69
69
  __getFindManyBaseOptions(model, options = {}) {
70
- const { attributes, include = [], where, searchFields, search, group, order, paranoid = 'active' } = options;
70
+ const { attributes, where, searchFields, search, group, order, paranoid = true } = options;
71
71
  let searchWhere;
72
72
  if (searchFields?.length && search) {
73
73
  const searchWords = search.split(/\s+/).filter(Boolean);
@@ -80,13 +80,12 @@ class Db {
80
80
  })),
81
81
  };
82
82
  }
83
- const deletedAtWhere = paranoid === 'deleted' ? { deletedAt: { [_1.Ops.not]: null } } : {};
84
83
  return {
85
84
  attributes: getFinalAttributes(attributes),
86
- ...getIncludeAndWhere(model, include, { withoutAttributes: !!attributes }, { ...where, ...searchWhere, ...deletedAtWhere }),
85
+ ...getInclude(model, { attributes, where: { ...where, ...searchWhere } }),
87
86
  ...(group && { group: group.map(pangea_helpers_1.camelToSnake) }),
88
- order: [...getFinalOrder(order), ...(!group ? [['createdAt', 'DESC']] : [])],
89
- paranoid: paranoid === 'active',
87
+ order: getFinalOrder(order),
88
+ paranoid,
90
89
  subQuery: false,
91
90
  transaction: this.__tx,
92
91
  };
@@ -223,17 +222,16 @@ function convertWhereKeys(obj) {
223
222
  }
224
223
  return obj;
225
224
  }
226
- function getIncludeAndWhere(model, includeItems, config = {}, where, relDepth = new Map()) {
225
+ function getInclude(model, config = {}, relDepth = new Map()) {
227
226
  const includeOptions = [];
228
- const cleanWhere = { ...where };
227
+ const cleanWhere = { ...config.where };
229
228
  for (const [relName, rel] of Object.entries(model.Relations)) {
230
229
  const relKey = `${model.name}.${relName}`;
231
230
  const currentDepth = relDepth.get(relKey) || 0;
232
231
  const maxDepth = rel.joinDepth;
233
232
  if (currentDepth >= maxDepth)
234
233
  continue;
235
- const { active = true, include: nestedIncludeItems = [] } = includeItems.find((i) => i.relation === relName) || {};
236
- if (!rel.eager || !active)
234
+ if (!rel.eager)
237
235
  continue;
238
236
  const relWhere = cleanWhere[relName];
239
237
  delete cleanWhere[relName];
@@ -242,11 +240,11 @@ function getIncludeAndWhere(model, includeItems, config = {}, where, relDepth =
242
240
  const relModel = rel.getModelFn();
243
241
  includeOptions.push({
244
242
  model: relModel,
245
- as: rel.as,
246
- attributes: !config.withoutAttributes ? undefined : [],
243
+ as: rel.alias,
244
+ attributes: !config.attributes ? undefined : [],
247
245
  required: rel.required,
248
246
  paranoid: rel.paranoid,
249
- ...getIncludeAndWhere(relModel, nestedIncludeItems, config, relWhere, newRelDepth),
247
+ ...getInclude(relModel, { ...config, where: relWhere }, newRelDepth),
250
248
  });
251
249
  }
252
250
  return {
@@ -254,12 +252,15 @@ function getIncludeAndWhere(model, includeItems, config = {}, where, relDepth =
254
252
  ...(Reflect.ownKeys(cleanWhere).length && { where: convertWhereKeys(cleanWhere) }),
255
253
  };
256
254
  }
257
- function getFinalOrder(order) {
258
- return Object.entries(order || {}).map(([key, value]) => {
255
+ function getFinalOrder(order, config = {}) {
256
+ if (!order)
257
+ return;
258
+ const finalOrder = Object.entries(order).map(([key, value]) => {
259
259
  const parts = key.split('.');
260
260
  const direction = value.toUpperCase();
261
261
  return [...parts, direction];
262
262
  });
263
+ return [...finalOrder, ...(!config.attributes ? [['createdAt', 'DESC']] : [])];
263
264
  }
264
265
  async function handleDbError(operation) {
265
266
  try {
@@ -35,11 +35,11 @@ function getRelation(relationFn) {
35
35
  return function (getModelFn, options = {}) {
36
36
  return function (target, propertyName) {
37
37
  const { foreignKey, ...relOptions } = options;
38
- const as = (0, pangea_helpers_1.camelToSnake)(propertyName);
39
- relationFn(getModelFn, { as, ...(foreignKey && { foreignKey }) })(target, propertyName);
38
+ const alias = (0, pangea_helpers_1.camelToSnake)(propertyName);
39
+ relationFn(getModelFn, { as: alias, ...(foreignKey && { foreignKey }) })(target, propertyName);
40
40
  const model = target.constructor;
41
41
  const { eager = relationFn !== seq.HasMany, required = relationFn === seq.BelongsTo ? !model.Columns[`${propertyName}Id`].allowNull : false, paranoid = relationFn !== seq.BelongsTo, joinDepth = 1, ...restOptions } = relOptions;
42
- model.AddRelation(propertyName, { as, getModelFn, eager, required, paranoid, joinDepth, ...restOptions });
42
+ model.AddRelation(propertyName, { alias, getModelFn, eager, required, paranoid, joinDepth, ...restOptions });
43
43
  };
44
44
  };
45
45
  }
@@ -35,17 +35,13 @@ function Table(tableName, options = {}) {
35
35
  const allIndexes = [...tableIndexes, ...columnIndexes];
36
36
  seq.Table({
37
37
  tableName,
38
- // modelName: camelToSnake(target.name), // 'as' in SQL query
39
38
  charset: 'utf8mb4',
40
39
  collate: 'utf8mb4_general_ci',
41
40
  underscored: true,
42
41
  timestamps: true,
43
42
  paranoid: true,
44
43
  indexes: allIndexes.map(({ field, fields, ...restIndex }) => {
45
- return {
46
- ...restIndex,
47
- fields: fields ? fields.map(pangea_helpers_1.camelToSnake) : field ? [(0, pangea_helpers_1.camelToSnake)(field)] : [],
48
- };
44
+ return { ...restIndex, fields: fields ? fields.map(pangea_helpers_1.camelToSnake) : field ? [(0, pangea_helpers_1.camelToSnake)(field)] : [] };
49
45
  }),
50
46
  })(target);
51
47
  const excludedAttributes = target.ExcludedAttributes;
@@ -6,7 +6,7 @@ type Column = {
6
6
  };
7
7
  type Columns = Record<string, Column>;
8
8
  type Relation = {
9
- as: string;
9
+ alias: string;
10
10
  getModelFn: GetModelFn;
11
11
  eager: boolean;
12
12
  required: boolean;
@@ -28,17 +28,13 @@ async function seedTables(models, seeds) {
28
28
  (0, helpers_1.printInfo)('database', 'seeding tables...');
29
29
  const modelsToSeed = Object.values(models)
30
30
  .filter(() => (0, helpers_1.getEnvBool)('DB_INCLUDE_TESTING_SEEDS'))
31
- .map((model) => {
32
- console.log(model.name);
33
- return model.name;
34
- });
31
+ .map((model) => model.name);
35
32
  const modelsWithData = modelsToSeed.filter((modelName) => seeds[modelName]?.length);
36
33
  const tx = await (0, db_client_1.getDbClient)().transaction();
37
34
  const db = new db_class_1.Db(tx);
38
35
  try {
39
36
  await db.disableForeignKeyChecks();
40
37
  await Promise.all(modelsWithData.map((modelName) => db.insertMany(models[modelName], seeds[modelName])));
41
- console.log('hola');
42
38
  await db.enableForeignKeyChecks();
43
39
  await tx.commit();
44
40
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pangea-server",
3
3
  "description": "",
4
- "version": "1.0.57",
4
+ "version": "1.0.59",
5
5
  "files": [
6
6
  "dist"
7
7
  ],