@goatlab/fluent 0.7.29 → 0.7.31

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.
@@ -12,12 +12,13 @@ export interface TypeOrmConnectorParams<Input, Output> {
12
12
  outputSchema?: z.ZodType<Output>;
13
13
  }
14
14
  export declare class TypeOrmConnector<ModelDTO = AnyObject, InputDTO = ModelDTO, OutputDTO = InputDTO> extends BaseConnector<ModelDTO, InputDTO, OutputDTO> implements FluentConnectorInterface<ModelDTO, InputDTO, OutputDTO> {
15
- private readonly repository;
15
+ private repository;
16
16
  private readonly dataSource;
17
17
  private readonly inputSchema;
18
18
  private readonly outputSchema;
19
19
  private readonly entity;
20
20
  constructor({ entity, dataSource, inputSchema, outputSchema }: TypeOrmConnectorParams<InputDTO, OutputDTO>);
21
+ initDB(): number;
21
22
  insert(data: InputDTO): Promise<OutputDTO>;
22
23
  insertMany(data: InputDTO[]): Promise<OutputDTO[]>;
23
24
  findMany<T extends FluentQuery<ModelDTO>>(query?: T): Promise<QueryOutput<T, ModelDTO>[]>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TypeOrmConnector = void 0;
4
+ const tslib_1 = require("tslib");
4
5
  const js_utils_1 = require("@goatlab/js-utils");
5
6
  const BaseConnector_1 = require("../BaseConnector");
6
7
  const outputKeys_1 = require("../outputKeys");
@@ -21,18 +22,22 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
21
22
  this.outputSchema =
22
23
  outputSchema || inputSchema;
23
24
  this.entity = entity;
24
- this.repository = this.dataSource.getRepository(entity);
25
+ }
26
+ initDB() {
27
+ this.repository = this.dataSource.getRepository(this.entity);
25
28
  this.isMongoDB =
26
29
  this.repository.metadata.connection.driver.options.type === 'mongodb';
27
30
  if (this.isMongoDB) {
28
- this.repository = this.dataSource.getMongoRepository(entity);
31
+ this.repository = this.dataSource.getMongoRepository(this.entity);
29
32
  }
30
- const relationShipBuilder = generatorDatasource_1.modelGeneratorDataSource.getRepository(entity);
33
+ const relationShipBuilder = generatorDatasource_1.modelGeneratorDataSource.getRepository(this.entity);
31
34
  const { relations } = (0, getRelationsFromModelGenerator_1.getRelationsFromModelGenerator)(relationShipBuilder);
32
35
  this.modelRelations = relations;
33
36
  this.outputKeys = (0, outputKeys_1.getOutputKeys)(relationShipBuilder) || [];
37
+ return 1;
34
38
  }
35
39
  async insert(data) {
40
+ this.initDB();
36
41
  const validatedData = this.inputSchema.parse(data);
37
42
  if (this.isMongoDB && validatedData['id']) {
38
43
  validatedData['_id'] = js_utils_1.Ids.objectID(validatedData['id']);
@@ -45,6 +50,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
45
50
  return this.outputSchema.parse((0, clearEmpties_1.clearEmpties)(js_utils_1.Objects.deleteNulls(datum)));
46
51
  }
47
52
  async insertMany(data) {
53
+ this.initDB();
48
54
  const validatedData = this.inputSchema.array().parse(data);
49
55
  const inserted = await this.repository.save(validatedData, {
50
56
  chunk: data.length / 300
@@ -57,6 +63,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
57
63
  }));
58
64
  }
59
65
  async findMany(query) {
66
+ this.initDB();
60
67
  const requiresCustomQuery = query?.include && Object.keys(query.include).length;
61
68
  if (this.isMongoDB) {
62
69
  const results = await this.customMongoRelatedFind(query);
@@ -99,6 +106,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
99
106
  return this.outputSchema?.array().parse(found);
100
107
  }
101
108
  async updateById(id, data) {
109
+ this.initDB();
102
110
  const dataToInsert = this.outputKeys.includes('updated')
103
111
  ? {
104
112
  ...data,
@@ -110,6 +118,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
110
118
  return (await this.requireById(id));
111
119
  }
112
120
  async replaceById(id, data) {
121
+ this.initDB();
113
122
  const idFieldName = this.isMongoDB ? '_id' : 'id';
114
123
  const value = this.requireById(id);
115
124
  const flatValue = js_utils_1.Objects.flatten(JSON.parse(JSON.stringify(value)));
@@ -133,6 +142,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
133
142
  return (await this.requireById(id));
134
143
  }
135
144
  async deleteById(id) {
145
+ this.initDB();
136
146
  const parsedId = this.isMongoDB
137
147
  ? js_utils_1.Ids.objectID(id)
138
148
  : id;
@@ -140,10 +150,12 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
140
150
  return id;
141
151
  }
142
152
  async clear() {
153
+ this.initDB();
143
154
  await this.repository.clear();
144
155
  return true;
145
156
  }
146
157
  loadFirst(query) {
158
+ this.initDB();
147
159
  const newInstance = this.clone();
148
160
  newInstance.setRelatedQuery({
149
161
  entity: this.entity,
@@ -156,6 +168,7 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
156
168
  return newInstance;
157
169
  }
158
170
  loadById(id) {
171
+ this.initDB();
159
172
  const newInstance = this.clone();
160
173
  newInstance.setRelatedQuery({
161
174
  entity: this.entity,
@@ -169,12 +182,15 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
169
182
  return newInstance;
170
183
  }
171
184
  raw() {
185
+ this.initDB();
172
186
  return this.repository;
173
187
  }
174
188
  mongoRaw() {
189
+ this.initDB();
175
190
  return this.repository;
176
191
  }
177
192
  clone() {
193
+ this.initDB();
178
194
  return new this.constructor();
179
195
  }
180
196
  generateTypeOrmQuery(query) {
@@ -367,4 +383,10 @@ class TypeOrmConnector extends BaseConnector_1.BaseConnector {
367
383
  return this.outputSchema?.array().parse(raw);
368
384
  }
369
385
  }
386
+ tslib_1.__decorate([
387
+ js_utils_1.Memo.syncMethod(),
388
+ tslib_1.__metadata("design:type", Function),
389
+ tslib_1.__metadata("design:paramtypes", []),
390
+ tslib_1.__metadata("design:returntype", void 0)
391
+ ], TypeOrmConnector.prototype, "initDB", null);
370
392
  exports.TypeOrmConnector = TypeOrmConnector;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getMongoBaseAggregation = void 0;
4
4
  const getMongoWhere_1 = require("./getMongoWhere");
5
5
  const getMongoBaseAggregation = ({ include, self }) => {
6
+ self.initDB();
6
7
  if (!include) {
7
8
  return [];
8
9
  }
@@ -6,6 +6,7 @@ const getMongoOrderBy_1 = require("./getMongoOrderBy");
6
6
  const getMongoSelect_1 = require("./getMongoSelect");
7
7
  const getMongoWhere_1 = require("./getMongoWhere");
8
8
  const getMongoFindAggregatedQuery = ({ query, self }) => {
9
+ self.initDB();
9
10
  const selected = (0, getMongoSelect_1.getMongoSelect)(query?.select);
10
11
  const orderBy = (0, getMongoOrderBy_1.getMongoOrderBy)(query?.orderBy);
11
12
  const where = (0, getMongoWhere_1.getMongoWhere)({