@sdflc/backend-helpers 1.0.120 → 1.0.121

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.
@@ -285,7 +285,7 @@ declare class BaseGateway {
285
285
  * Returns the Database instance from config.
286
286
  * All query builders are derived from this instance.
287
287
  */
288
- getDb(): Database;
288
+ getDb(): Database | undefined;
289
289
  /**
290
290
  * Returns a Knex query builder scoped to this gateway's table.
291
291
  * The builder is a fresh instance each time — it does not carry state
@@ -482,10 +482,11 @@ class BaseGateway {
482
482
  obj[constants_1.FIELDS.REMOVED_BY] = this.getContext()?.userId;
483
483
  }
484
484
  }
485
- if (hasLang && localizationForField) {
485
+ const db = this.getDb();
486
+ if (hasLang && localizationForField && db) {
486
487
  // Append removal timestamp to the id to free the original localisation key
487
488
  // for future re-use (e.g. if a new record with the same lang + slug is created).
488
- obj[this.getIdField()] = this.getDb().raw(`CONCAT(id, '-${removalTimeStr}')`);
489
+ obj[this.getIdField()] = db.raw(`CONCAT(id, '-${removalTimeStr}')`);
489
490
  }
490
491
  return obj;
491
492
  }
@@ -634,10 +635,11 @@ class BaseGateway {
634
635
  */
635
636
  async list(args) {
636
637
  const { filter, params, useCache } = args || {};
637
- const { limit, offset, sortBy } = this.getDb().createLimitOffsetSortBy(params, this.config.defaultSorting || []);
638
- if (!this.config.table) {
638
+ const db = this.getDb();
639
+ if (!this.config.table || !db) {
639
640
  return [];
640
641
  }
642
+ const { limit, offset, sortBy } = db.createLimitOffsetSortBy(params, this.config.defaultSorting || []);
641
643
  const shouldUseCache = useCache != null ? useCache : !this.config.noCache;
642
644
  const fieldsToUse = !shouldUseCache && Array.isArray(this.config.selectFields) ? this.config.selectFields : this.getIdField();
643
645
  const { query } = await this.buildListQuery({ filter, params, fieldsToUse, useCache: shouldUseCache });
@@ -285,7 +285,7 @@ declare class BaseGateway {
285
285
  * Returns the Database instance from config.
286
286
  * All query builders are derived from this instance.
287
287
  */
288
- getDb(): Database;
288
+ getDb(): Database | undefined;
289
289
  /**
290
290
  * Returns a Knex query builder scoped to this gateway's table.
291
291
  * The builder is a fresh instance each time — it does not carry state
@@ -476,10 +476,11 @@ class BaseGateway {
476
476
  obj[FIELDS.REMOVED_BY] = this.getContext()?.userId;
477
477
  }
478
478
  }
479
- if (hasLang && localizationForField) {
479
+ const db = this.getDb();
480
+ if (hasLang && localizationForField && db) {
480
481
  // Append removal timestamp to the id to free the original localisation key
481
482
  // for future re-use (e.g. if a new record with the same lang + slug is created).
482
- obj[this.getIdField()] = this.getDb().raw(`CONCAT(id, '-${removalTimeStr}')`);
483
+ obj[this.getIdField()] = db.raw(`CONCAT(id, '-${removalTimeStr}')`);
483
484
  }
484
485
  return obj;
485
486
  }
@@ -628,10 +629,11 @@ class BaseGateway {
628
629
  */
629
630
  async list(args) {
630
631
  const { filter, params, useCache } = args || {};
631
- const { limit, offset, sortBy } = this.getDb().createLimitOffsetSortBy(params, this.config.defaultSorting || []);
632
- if (!this.config.table) {
632
+ const db = this.getDb();
633
+ if (!this.config.table || !db) {
633
634
  return [];
634
635
  }
636
+ const { limit, offset, sortBy } = db.createLimitOffsetSortBy(params, this.config.defaultSorting || []);
635
637
  const shouldUseCache = useCache != null ? useCache : !this.config.noCache;
636
638
  const fieldsToUse = !shouldUseCache && Array.isArray(this.config.selectFields) ? this.config.selectFields : this.getIdField();
637
639
  const { query } = await this.buildListQuery({ filter, params, fieldsToUse, useCache: shouldUseCache });
@@ -1,6 +1,13 @@
1
1
  import { Database } from '../Database';
2
2
  export interface CacheInterface {
3
- db: Database;
3
+ /**
4
+ * Database instance used by the default batch loader (batchLoaderDefault).
5
+ * Optional when a custom `loaderFn` is provided — in that case the loader
6
+ * function is responsible for its own data fetching and `db` is not used.
7
+ * Runtime safety is enforced in DataCache.batchLoaderDefault(), which throws
8
+ * if `db` is missing and no `loaderFn` was supplied.
9
+ */
10
+ db?: Database;
4
11
  /**
5
12
  * Namespace prefix for DataLoader cache keys.
6
13
  * Resolved in BaseGateway as: context.keyPrefix → config.keyPrefix → table name.
@@ -1,6 +1,13 @@
1
1
  import { Database } from '../Database';
2
2
  export interface CacheInterface {
3
- db: Database;
3
+ /**
4
+ * Database instance used by the default batch loader (batchLoaderDefault).
5
+ * Optional when a custom `loaderFn` is provided — in that case the loader
6
+ * function is responsible for its own data fetching and `db` is not used.
7
+ * Runtime safety is enforced in DataCache.batchLoaderDefault(), which throws
8
+ * if `db` is missing and no `loaderFn` was supplied.
9
+ */
10
+ db?: Database;
4
11
  /**
5
12
  * Namespace prefix for DataLoader cache keys.
6
13
  * Resolved in BaseGateway as: context.keyPrefix → config.keyPrefix → table name.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.120",
3
+ "version": "1.0.121",
4
4
  "description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
5
5
  "type": "commonjs",
6
6
  "exports": {