pangea-server 1.0.60 → 1.0.62

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
@@ -1 +1 @@
1
- # PANGEA BACKEND
1
+ # PANGEA SERVER
@@ -9,16 +9,16 @@ type Group = string[];
9
9
  type Order = Record<string, 'asc' | 'desc'>;
10
10
  type FindOneOptions = {
11
11
  scopes?: string[];
12
- attributes?: Attributes;
13
12
  order?: Order;
14
13
  paranoid?: boolean;
15
14
  };
15
+ type AggregateOneOptions = {
16
+ paranoid?: boolean;
17
+ };
16
18
  type FindManyOptions<BM extends BaseModel> = {
17
- attributes?: Attributes;
18
19
  where?: Where<BM>;
19
20
  searchFields?: string[];
20
21
  search?: string | null;
21
- group?: Group;
22
22
  order?: Order;
23
23
  paranoid?: boolean;
24
24
  };
@@ -26,18 +26,25 @@ type FindManyPagedOptions<BM extends BaseModel> = FindManyOptions<BM> & {
26
26
  page?: number | null;
27
27
  pageSize?: number | null;
28
28
  };
29
+ type AggregateManyConfig = {
30
+ attributes: Attributes;
31
+ group: Group;
32
+ };
29
33
  type Target<BM extends BaseModel> = Filters<BM> | BM;
30
34
  export declare class Db {
31
35
  private __tx?;
32
36
  constructor(tx: Tx);
33
37
  findOneOrNull<BM extends BaseModel>(model: BaseModelCtor<BM>, filters: Filters<BM>, options?: FindOneOptions): Promise<BM | null>;
34
38
  findOne<BM extends BaseModel>(model: BaseModelCtor<BM>, filters: Filters<BM>, options?: FindOneOptions): Promise<BM>;
39
+ aggregateOne<BM extends BaseModel>(model: BaseModelCtor<BM>, attributes: Attributes, options?: AggregateOneOptions): Promise<BM | null>;
40
+ private __getFindOneBaseOptions;
35
41
  findMany<BM extends BaseModel>(model: BaseModelCtor<BM>, options?: FindManyPagedOptions<BM>): Promise<BM[]>;
36
42
  count<BM extends BaseModel>(model: BaseModelCtor<BM>, options?: FindManyOptions<BM>): Promise<number>;
37
43
  findManyWithCount<BM extends BaseModel>(model: BaseModelCtor<BM>, options?: FindManyPagedOptions<BM>): Promise<{
38
44
  instances: BM[];
39
45
  totalCount: number;
40
46
  }>;
47
+ aggregateMany<BM extends BaseModel>(model: BaseModelCtor<BM>, config: AggregateManyConfig, options?: Omit<FindManyOptions<BM>, 'order'>): Promise<BM[]>;
41
48
  private __getFindManyBaseOptions;
42
49
  insertOne<BM extends BaseModel>(model: BaseModelCtor<BM>, params: InsertParams<BM>): Promise<BM>;
43
50
  insertMany<BM extends BaseModel>(model: BaseModelCtor<BM>, data: InsertParams<BM>[]): Promise<number>;
@@ -13,20 +13,15 @@ class Db {
13
13
  }
14
14
  // find methods
15
15
  findOneOrNull(model, filters, options = {}) {
16
- const { scopes, attributes, order, paranoid = true } = options;
16
+ const { scopes, order, paranoid = true } = options;
17
17
  const scopedModel = scopes?.length ? model.scope(...scopes) : model;
18
- const baseOptions = {
19
- attributes: getFinalAttributes(model, attributes),
20
- paranoid,
21
- subQuery: false,
22
- transaction: this.__tx,
23
- };
18
+ const baseOptions = this.__getFindOneBaseOptions(paranoid);
24
19
  if (typeof filters === 'number') {
25
20
  return scopedModel.findByPk(filters, { ...baseOptions, ...getInclude(model) });
26
21
  }
27
22
  return scopedModel.findOne({
28
23
  ...baseOptions,
29
- ...getInclude(model, { attributes, where: filters }),
24
+ ...getInclude(model, { where: filters }),
30
25
  order: getFinalOrder(order),
31
26
  });
32
27
  }
@@ -36,6 +31,18 @@ class Db {
36
31
  helpers_1.AppError.ThrowEntityNotFound();
37
32
  return instance;
38
33
  }
34
+ aggregateOne(model, attributes, options = {}) {
35
+ const { paranoid = true } = options;
36
+ const baseOptions = this.__getFindOneBaseOptions(paranoid);
37
+ return model.findOne({
38
+ ...baseOptions,
39
+ attributes: getFinalAttributes(model, attributes),
40
+ ...getInclude(model, { attributes }),
41
+ });
42
+ }
43
+ __getFindOneBaseOptions(paranoid) {
44
+ return { paranoid, subQuery: false, transaction: this.__tx };
45
+ }
39
46
  async findMany(model, options = {}) {
40
47
  const baseOptions = this.__getFindManyBaseOptions(model, options);
41
48
  const { page, pageSize } = options;
@@ -66,8 +73,18 @@ class Db {
66
73
  const instances = await this.findMany(model, options);
67
74
  return { instances, totalCount: instances.length };
68
75
  }
76
+ async aggregateMany(model, config, options = {}) {
77
+ const baseOptions = this.__getFindManyBaseOptions(model, options);
78
+ return model.findAll({
79
+ ...baseOptions,
80
+ attributes: getFinalAttributes(model, config.attributes),
81
+ ...(config.group && {
82
+ group: config.group.map((field) => (field.includes('.') ? field : `${model.name}.${(0, pangea_helpers_1.camelToSnake)(field)}`)),
83
+ }),
84
+ });
85
+ }
69
86
  __getFindManyBaseOptions(model, options = {}) {
70
- const { attributes, where, searchFields, search, group, order, paranoid = true } = options;
87
+ const { where, searchFields, search, order, paranoid = true } = options;
71
88
  let searchWhere;
72
89
  if (searchFields?.length && search) {
73
90
  const searchWords = search.split(/\s+/).filter(Boolean);
@@ -81,9 +98,7 @@ class Db {
81
98
  };
82
99
  }
83
100
  return {
84
- attributes: getFinalAttributes(model, attributes),
85
- ...getInclude(model, { attributes, where: { ...where, ...searchWhere } }),
86
- ...(group && { group: group.map(pangea_helpers_1.camelToSnake) }),
101
+ ...getInclude(model, { where: { ...where, ...searchWhere } }),
87
102
  order: getFinalOrder(order),
88
103
  paranoid,
89
104
  subQuery: false,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pangea-server",
3
3
  "description": "",
4
- "version": "1.0.60",
4
+ "version": "1.0.62",
5
5
  "files": [
6
6
  "dist"
7
7
  ],