@tachybase/database 1.6.0-alpha.4 → 1.6.0-alpha.5

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.
@@ -135,6 +135,10 @@ export declare class Repository<TModelAttributes extends {} = any, TCreationAttr
135
135
  collection: Collection;
136
136
  model: ModelStatic<Model>;
137
137
  constructor(collection: Collection);
138
+ /**
139
+ * Check if database connection is closed before executing query
140
+ */
141
+ protected checkConnection(): void;
138
142
  static valuesToFilter(values: Values, filterKeys: Array<string>): {
139
143
  $and: any[];
140
144
  };
package/lib/repository.js CHANGED
@@ -146,6 +146,16 @@ const _Repository = class _Repository {
146
146
  this.collection = collection;
147
147
  this.model = collection.model;
148
148
  }
149
+ /**
150
+ * Check if database connection is closed before executing query
151
+ */
152
+ checkConnection() {
153
+ if (this.database.closed()) {
154
+ throw new Error(
155
+ `Database connection is closed. Cannot execute query on collection "${this.collection.name}". The application may be reloading or restarting.`
156
+ );
157
+ }
158
+ }
149
159
  static valuesToFilter(values, filterKeys) {
150
160
  const filterAnd = [];
151
161
  const flattedValues = (0, import_flat.flatten)(values);
@@ -186,6 +196,7 @@ const _Repository = class _Repository {
186
196
  */
187
197
  async count(countOptions) {
188
198
  var _a;
199
+ this.checkConnection();
189
200
  let options = countOptions ? import_lodash.default.clone(countOptions) : {};
190
201
  const transaction2 = await this.getTransaction(options);
191
202
  if (countOptions == null ? void 0 : countOptions.filter) {
@@ -219,6 +230,7 @@ const _Repository = class _Repository {
219
230
  }
220
231
  async aggregate(options) {
221
232
  var _a;
233
+ this.checkConnection();
222
234
  const { method, field } = options;
223
235
  const queryOptions = this.buildQueryOptions({
224
236
  ...options,
@@ -269,6 +281,7 @@ const _Repository = class _Repository {
269
281
  if ((options == null ? void 0 : options.targetCollection) && (options == null ? void 0 : options.targetCollection) !== this.collection.name) {
270
282
  return await this.database.getCollection(options.targetCollection).repository.find(options);
271
283
  }
284
+ this.checkConnection();
272
285
  const model = this.collection.model;
273
286
  const transaction2 = await this.getTransaction(options);
274
287
  const opts = {
@@ -357,6 +370,7 @@ const _Repository = class _Repository {
357
370
  return this.create({ values, transaction: transaction2, hooks });
358
371
  }
359
372
  async create(options) {
373
+ this.checkConnection();
360
374
  if (Array.isArray(options.values)) {
361
375
  return this.createMany({
362
376
  ...options,
@@ -405,6 +419,7 @@ const _Repository = class _Repository {
405
419
  return instances;
406
420
  }
407
421
  async update(options) {
422
+ this.checkConnection();
408
423
  if (Array.isArray(options.values)) {
409
424
  return this.updateMany({
410
425
  ...options,
@@ -481,6 +496,7 @@ const _Repository = class _Repository {
481
496
  return instances;
482
497
  }
483
498
  async destroy(options) {
499
+ this.checkConnection();
484
500
  const transaction2 = await this.getTransaction(options);
485
501
  const modelFilterKey = this.collection.filterTargetKey;
486
502
  options = options;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tachybase/database",
3
- "version": "1.6.0-alpha.4",
3
+ "version": "1.6.0-alpha.5",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./lib/index.js",
@@ -22,9 +22,9 @@
22
22
  "semver": "7.7.2",
23
23
  "sequelize": "^6.37.7",
24
24
  "umzug": "^3.8.2",
25
- "@tachybase/globals": "1.6.0-alpha.4",
26
- "@tachybase/logger": "1.6.0-alpha.4",
27
- "@tachybase/utils": "1.6.0-alpha.4"
25
+ "@tachybase/globals": "1.6.0-alpha.5",
26
+ "@tachybase/utils": "1.6.0-alpha.5",
27
+ "@tachybase/logger": "1.6.0-alpha.5"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/flat": "^5.0.5",