@sdflc/backend-helpers 1.0.122 → 1.0.124

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.
@@ -41,6 +41,21 @@ declare class Database {
41
41
  * DB error message directly or log it and return a generic message.
42
42
  */
43
43
  isDbError(ex: any): [boolean, string];
44
+ /**
45
+ * Returns the SQL LIKE operator appropriate for the connected database engine.
46
+ *
47
+ * PostgreSQL supports ILIKE natively — a case-insensitive variant of LIKE.
48
+ * MS Azure SQL Server and MySQL do not have ILIKE; they use plain LIKE, which
49
+ * is case-insensitive by default under the collations used in this project
50
+ * (e.g. SQL_Latin1_General_CP1_CI_AS on MSSQL, utf8_general_ci on MySQL).
51
+ *
52
+ * Use this wherever a case-insensitive pattern match is needed so the
53
+ * correct operator is chosen automatically without branching in gateway code.
54
+ *
55
+ * Knex client strings: 'pg' | 'pg-native' for PostgreSQL; 'mssql' | 'tedious'
56
+ * for SQL Server; 'mysql' | 'mysql2' for MySQL.
57
+ */
58
+ getSqlLikeOperator(): string;
44
59
  /**
45
60
  * Creates a new Knex transaction.
46
61
  * If `trx` is provided, creates a nested transaction on the existing one.
@@ -1,4 +1,3 @@
1
- // ./src/Database.ts (capitalised to match class name convention)
2
1
  import camelCase from 'lodash/camelCase';
3
2
  import knex from 'knex';
4
3
  import { buildKey as defaultBuildKey, Logger, LoggerLevels, isLogger } from '@sdflc/utils';
@@ -100,6 +99,23 @@ class Database {
100
99
  return [false, ''];
101
100
  }
102
101
  }
102
+ /**
103
+ * Returns the SQL LIKE operator appropriate for the connected database engine.
104
+ *
105
+ * PostgreSQL supports ILIKE natively — a case-insensitive variant of LIKE.
106
+ * MS Azure SQL Server and MySQL do not have ILIKE; they use plain LIKE, which
107
+ * is case-insensitive by default under the collations used in this project
108
+ * (e.g. SQL_Latin1_General_CP1_CI_AS on MSSQL, utf8_general_ci on MySQL).
109
+ *
110
+ * Use this wherever a case-insensitive pattern match is needed so the
111
+ * correct operator is chosen automatically without branching in gateway code.
112
+ *
113
+ * Knex client strings: 'pg' | 'pg-native' for PostgreSQL; 'mssql' | 'tedious'
114
+ * for SQL Server; 'mysql' | 'mysql2' for MySQL.
115
+ */
116
+ getSqlLikeOperator() {
117
+ return this.dbType === 'pg' || this.dbType === 'pg-native' ? 'ilike' : 'like';
118
+ }
103
119
  /**
104
120
  * Creates a new Knex transaction.
105
121
  * If `trx` is provided, creates a nested transaction on the existing one.
@@ -1,6 +1,7 @@
1
1
  import { Logger } from '@sdflc/utils';
2
2
  import { ApiContext } from '.';
3
3
  import { CacheInterface } from '..';
4
+ import { GatewayFilterRule } from './GatewayFilterRule';
4
5
  import { LoaderInterface } from './LoaderInterface';
5
6
  import { SortItemInterface } from './SortItemInterface';
6
7
  export interface BaseGatewayPropsInterface extends CacheInterface {
@@ -111,4 +112,74 @@ export interface BaseGatewayPropsInterface extends CacheInterface {
111
112
  * between production and non-production environments.
112
113
  */
113
114
  isProduction?: boolean;
115
+ /**
116
+ * Declarative WHERE rules applied by onListFilter() after base logic.
117
+ *
118
+ * All filter types are supported. Rules are applied against the filter
119
+ * object passed to list() via args.filter.
120
+ *
121
+ * Example:
122
+ * listFilters: [
123
+ * { field: 'partyId', column: 'Party_Guest.Party_ID', type: 'array' },
124
+ * { field: 'isStarred', column: 'Party_Guest.Is_Starred', type: 'bool' },
125
+ * { field: 'wasInvited', column: 'Party_Guest.Invited_At', type: 'nullableBool' },
126
+ * { field: 'rsvpAt', column: 'Party_Guest.Rsvp_At', type: 'nullableDateRange' },
127
+ * {
128
+ * field: 'search', column: '', type: 'search',
129
+ * columns: ['Person.First_Name', 'Person.Email'],
130
+ * },
131
+ * ]
132
+ */
133
+ listFilters?: GatewayFilterRule[];
134
+ /**
135
+ * Declarative WHERE rules applied by onUpdateFilter() after base logic.
136
+ *
137
+ * Intended for simple equality / array filters on the update path.
138
+ * Each rule that matches increments the filtersAppliedQty counter,
139
+ * contributing to the safety guard that prevents unfiltered UPDATEs.
140
+ *
141
+ * 'search', 'dateRange', and 'nullableDateRange' are NOT supported here —
142
+ * update filters should be precise identifiers, not fuzzy searches or ranges.
143
+ * Rules with unsupported types are silently skipped and logged as a warning.
144
+ *
145
+ * Example:
146
+ * updateFilters: [
147
+ * { field: 'partyId', column: 'Party_ID', type: 'array' },
148
+ * ]
149
+ */
150
+ updateFilters?: GatewayFilterRule[];
151
+ /**
152
+ * Declarative WHERE rules applied by onRemoveFilter() after base logic.
153
+ *
154
+ * The remove path operates on castArray(whereParams) — an array of filter
155
+ * objects. For 'array' rules, values are collected across ALL entries in
156
+ * the array and applied as a single whereIn, matching the bulk-remove
157
+ * pattern established in PartyGuestGw.onRemoveFilter.
158
+ *
159
+ * As with updateFilters, 'search', 'dateRange', and 'nullableDateRange'
160
+ * are not supported and will be skipped with a warning.
161
+ *
162
+ * Example:
163
+ * removeFilters: [
164
+ * { field: 'partyId', column: 'Party_ID', type: 'array' },
165
+ * ]
166
+ */
167
+ removeFilters?: GatewayFilterRule[];
168
+ /**
169
+ * Declarative WHERE rules applied by count() after base scoping logic.
170
+ *
171
+ * When not explicitly set, count() falls back to listFilters — in most
172
+ * cases count and list should be scoped identically for the same filter
173
+ * params. Set countFilters explicitly only when count needs different
174
+ * scoping than list (e.g. counting across statuses that list excludes).
175
+ *
176
+ * All filter types are supported ('search', 'dateRange', 'nullableDateRange'
177
+ * included) — count() is a read operation, same as list().
178
+ *
179
+ * Example:
180
+ * countFilters: [
181
+ * { field: 'partyId', column: 'Party_Guest.Party_ID', type: 'array' },
182
+ * ]
183
+ */
184
+ countFilters?: GatewayFilterRule[];
114
185
  }
@@ -1,2 +1 @@
1
- // ./src/interfaces/BaseGatewayPropsInterface.ts
2
1
  export {};
@@ -0,0 +1,109 @@
1
+ /**
2
+ * GatewayFilterRule
3
+ *
4
+ * Discriminator for the declarative filter rules accepted by BaseGateway.
5
+ * Each type maps to a specific WHERE clause strategy:
6
+ *
7
+ * 'array' — whereIn(column, values) for arrays; where(column, value)
8
+ * for scalars. Skipped when value is null / undefined / ''.
9
+ *
10
+ * 'eq' — where(column, value). Skipped when value is null/undefined.
11
+ * Use when you want a strict single-value equality and never
12
+ * want array expansion (e.g. an enum code, a boolean stored
13
+ * as a number).
14
+ *
15
+ * 'bool' — where(column, true|false). Only fires when the filter value
16
+ * is strictly true or false — null and undefined are ignored.
17
+ * Use for boolean columns where you want to filter on the
18
+ * value itself (e.g. IS_STARRED = true).
19
+ *
20
+ * 'nullableBool' — true → whereNotNull(column)
21
+ * false → whereNull(column)
22
+ * null/undefined → skipped (no constraint added).
23
+ * Use when a nullable datetime/column encodes presence:
24
+ * e.g. wasInvited ↔ Invited_At IS [NOT] NULL.
25
+ *
26
+ * 'dateRange' — Reads filterParams[field + 'From'] and filterParams[field + 'To'].
27
+ * Applies where(column, '>=', from) and/or where(column, '<=', to).
28
+ * Either bound is independently optional.
29
+ * The 'field' on the rule is the base name (e.g. 'rsvpAt');
30
+ * the actual filter keys are 'rsvpAtFrom' / 'rsvpAtTo'.
31
+ *
32
+ * 'nullableDateRange' — Extends 'dateRange' with a null-check phase.
33
+ * Also reads filterParams[field + 'Null'] (boolean):
34
+ * true → whereNull(column); from/to skipped
35
+ * false → whereNotNull(column); from/to still applied
36
+ * null/undefined → null-ness unconstrained; from/to applied
37
+ *
38
+ * 'search' — LIKE / ILIKE across one or more columns (OR semantics).
39
+ * Reads filterParams[field] (string). Trims whitespace; skipped
40
+ * when the result is empty.
41
+ * Requires the 'columns' array on the rule — must be non-empty.
42
+ * The operator ('like' vs 'ilike') is resolved per-database
43
+ * via Database.getSqlLikeOperator() so behaviour is correct on
44
+ * both PostgreSQL (ilike) and MS Azure SQL Server (like, which
45
+ * is case-insensitive under _CI_ collations).
46
+ */
47
+ export type GatewayFilterType = 'array' | 'eq' | 'bool' | 'nullableBool' | 'dateRange' | 'nullableDateRange' | 'search';
48
+ /**
49
+ * GatewayFilterRule
50
+ *
51
+ * A single declarative filter rule for use in BaseGatewayPropsInterface.listFilters,
52
+ * updateFilters, and removeFilters.
53
+ *
54
+ * Rules are applied in declaration order by BaseGateway after the standard
55
+ * base-class filter logic runs. They supplement — never replace — the existing
56
+ * onListFilter / onUpdateFilter / onRemoveFilter override chain.
57
+ *
58
+ * Example — array filter:
59
+ * { field: 'partyId', column: `${TABLES.PARTY_GUEST}.${FIELDS.PARTY_ID}`, type: 'array' }
60
+ *
61
+ * Example — nullable bool (wasInvited ↔ Invited_At IS [NOT] NULL):
62
+ * { field: 'wasInvited', column: `${TABLES.PARTY_GUEST}.${FIELDS.INVITED_AT}`, type: 'nullableBool' }
63
+ *
64
+ * Example — nullable date range:
65
+ * { field: 'rsvpAt', column: `${TABLES.PARTY_GUEST}.${FIELDS.RSVP_AT}`, type: 'nullableDateRange' }
66
+ * reads: filter.rsvpAtNull, filter.rsvpAtFrom, filter.rsvpAtTo
67
+ *
68
+ * Example — full-text search across Person columns:
69
+ * {
70
+ * field: 'search',
71
+ * column: '', // unused for 'search' — columns[] drives the OR
72
+ * type: 'search',
73
+ * columns: [
74
+ * `${TABLES.PERSON}.${FIELDS.FIRST_NAME}`,
75
+ * `${TABLES.PERSON}.${FIELDS.EMAIL}`,
76
+ * ],
77
+ * }
78
+ */
79
+ export interface GatewayFilterRule {
80
+ /**
81
+ * The key read from filterParams.
82
+ *
83
+ * For 'dateRange' / 'nullableDateRange': this is the base name; the actual
84
+ * keys read are field+'From', field+'To', and (for nullableDateRange) field+'Null'.
85
+ *
86
+ * For 'search': this is the key that holds the search string.
87
+ */
88
+ field: string;
89
+ /**
90
+ * The fully-qualified database column the filter is applied to.
91
+ * Should include the table name to prevent ambiguity in joined queries
92
+ * (e.g. 'Party_Guest.Party_ID', not just 'Party_ID').
93
+ *
94
+ * Unused for 'search' (which uses the 'columns' array instead) but
95
+ * must still be present as an empty string for type safety.
96
+ */
97
+ column: string;
98
+ /**
99
+ * The filter strategy. See GatewayFilterType for full documentation.
100
+ */
101
+ type: GatewayFilterType;
102
+ /**
103
+ * Required for type: 'search' — the list of columns to OR across.
104
+ * Must contain at least one entry; the rule is silently skipped otherwise.
105
+ * Each entry should be table-qualified for joined query safety.
106
+ * Ignored for all other filter types.
107
+ */
108
+ columns?: string[];
109
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -30,3 +30,4 @@ export * from './BaseControllerPropsInterface';
30
30
  export * from './BaseRestApiCrudControllerPropsInterface';
31
31
  export * from './BaseExternalApiGatewayPropsInterface';
32
32
  export * from './DataCachePropsInterface';
33
+ export * from './GatewayFilterRule';
@@ -29,3 +29,4 @@ export * from './BaseControllerPropsInterface';
29
29
  export * from './BaseRestApiCrudControllerPropsInterface';
30
30
  export * from './BaseExternalApiGatewayPropsInterface';
31
31
  export * from './DataCachePropsInterface';
32
+ export * from './GatewayFilterRule';
@@ -1,6 +1,7 @@
1
1
  import { Logger } from '@sdflc/utils';
2
2
  import { ApiContext } from '.';
3
3
  import { CacheInterface } from '..';
4
+ import { GatewayFilterRule } from './GatewayFilterRule';
4
5
  import { LoaderInterface } from './LoaderInterface';
5
6
  import { SortItemInterface } from './SortItemInterface';
6
7
  export interface BaseGatewayPropsInterface extends CacheInterface {
@@ -111,4 +112,74 @@ export interface BaseGatewayPropsInterface extends CacheInterface {
111
112
  * between production and non-production environments.
112
113
  */
113
114
  isProduction?: boolean;
115
+ /**
116
+ * Declarative WHERE rules applied by onListFilter() after base logic.
117
+ *
118
+ * All filter types are supported. Rules are applied against the filter
119
+ * object passed to list() via args.filter.
120
+ *
121
+ * Example:
122
+ * listFilters: [
123
+ * { field: 'partyId', column: 'Party_Guest.Party_ID', type: 'array' },
124
+ * { field: 'isStarred', column: 'Party_Guest.Is_Starred', type: 'bool' },
125
+ * { field: 'wasInvited', column: 'Party_Guest.Invited_At', type: 'nullableBool' },
126
+ * { field: 'rsvpAt', column: 'Party_Guest.Rsvp_At', type: 'nullableDateRange' },
127
+ * {
128
+ * field: 'search', column: '', type: 'search',
129
+ * columns: ['Person.First_Name', 'Person.Email'],
130
+ * },
131
+ * ]
132
+ */
133
+ listFilters?: GatewayFilterRule[];
134
+ /**
135
+ * Declarative WHERE rules applied by onUpdateFilter() after base logic.
136
+ *
137
+ * Intended for simple equality / array filters on the update path.
138
+ * Each rule that matches increments the filtersAppliedQty counter,
139
+ * contributing to the safety guard that prevents unfiltered UPDATEs.
140
+ *
141
+ * 'search', 'dateRange', and 'nullableDateRange' are NOT supported here —
142
+ * update filters should be precise identifiers, not fuzzy searches or ranges.
143
+ * Rules with unsupported types are silently skipped and logged as a warning.
144
+ *
145
+ * Example:
146
+ * updateFilters: [
147
+ * { field: 'partyId', column: 'Party_ID', type: 'array' },
148
+ * ]
149
+ */
150
+ updateFilters?: GatewayFilterRule[];
151
+ /**
152
+ * Declarative WHERE rules applied by onRemoveFilter() after base logic.
153
+ *
154
+ * The remove path operates on castArray(whereParams) — an array of filter
155
+ * objects. For 'array' rules, values are collected across ALL entries in
156
+ * the array and applied as a single whereIn, matching the bulk-remove
157
+ * pattern established in PartyGuestGw.onRemoveFilter.
158
+ *
159
+ * As with updateFilters, 'search', 'dateRange', and 'nullableDateRange'
160
+ * are not supported and will be skipped with a warning.
161
+ *
162
+ * Example:
163
+ * removeFilters: [
164
+ * { field: 'partyId', column: 'Party_ID', type: 'array' },
165
+ * ]
166
+ */
167
+ removeFilters?: GatewayFilterRule[];
168
+ /**
169
+ * Declarative WHERE rules applied by count() after base scoping logic.
170
+ *
171
+ * When not explicitly set, count() falls back to listFilters — in most
172
+ * cases count and list should be scoped identically for the same filter
173
+ * params. Set countFilters explicitly only when count needs different
174
+ * scoping than list (e.g. counting across statuses that list excludes).
175
+ *
176
+ * All filter types are supported ('search', 'dateRange', 'nullableDateRange'
177
+ * included) — count() is a read operation, same as list().
178
+ *
179
+ * Example:
180
+ * countFilters: [
181
+ * { field: 'partyId', column: 'Party_Guest.Party_ID', type: 'array' },
182
+ * ]
183
+ */
184
+ countFilters?: GatewayFilterRule[];
114
185
  }
@@ -1,3 +1,2 @@
1
1
  "use strict";
2
- // ./src/interfaces/BaseGatewayPropsInterface.ts
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,109 @@
1
+ /**
2
+ * GatewayFilterRule
3
+ *
4
+ * Discriminator for the declarative filter rules accepted by BaseGateway.
5
+ * Each type maps to a specific WHERE clause strategy:
6
+ *
7
+ * 'array' — whereIn(column, values) for arrays; where(column, value)
8
+ * for scalars. Skipped when value is null / undefined / ''.
9
+ *
10
+ * 'eq' — where(column, value). Skipped when value is null/undefined.
11
+ * Use when you want a strict single-value equality and never
12
+ * want array expansion (e.g. an enum code, a boolean stored
13
+ * as a number).
14
+ *
15
+ * 'bool' — where(column, true|false). Only fires when the filter value
16
+ * is strictly true or false — null and undefined are ignored.
17
+ * Use for boolean columns where you want to filter on the
18
+ * value itself (e.g. IS_STARRED = true).
19
+ *
20
+ * 'nullableBool' — true → whereNotNull(column)
21
+ * false → whereNull(column)
22
+ * null/undefined → skipped (no constraint added).
23
+ * Use when a nullable datetime/column encodes presence:
24
+ * e.g. wasInvited ↔ Invited_At IS [NOT] NULL.
25
+ *
26
+ * 'dateRange' — Reads filterParams[field + 'From'] and filterParams[field + 'To'].
27
+ * Applies where(column, '>=', from) and/or where(column, '<=', to).
28
+ * Either bound is independently optional.
29
+ * The 'field' on the rule is the base name (e.g. 'rsvpAt');
30
+ * the actual filter keys are 'rsvpAtFrom' / 'rsvpAtTo'.
31
+ *
32
+ * 'nullableDateRange' — Extends 'dateRange' with a null-check phase.
33
+ * Also reads filterParams[field + 'Null'] (boolean):
34
+ * true → whereNull(column); from/to skipped
35
+ * false → whereNotNull(column); from/to still applied
36
+ * null/undefined → null-ness unconstrained; from/to applied
37
+ *
38
+ * 'search' — LIKE / ILIKE across one or more columns (OR semantics).
39
+ * Reads filterParams[field] (string). Trims whitespace; skipped
40
+ * when the result is empty.
41
+ * Requires the 'columns' array on the rule — must be non-empty.
42
+ * The operator ('like' vs 'ilike') is resolved per-database
43
+ * via Database.getSqlLikeOperator() so behaviour is correct on
44
+ * both PostgreSQL (ilike) and MS Azure SQL Server (like, which
45
+ * is case-insensitive under _CI_ collations).
46
+ */
47
+ export type GatewayFilterType = 'array' | 'eq' | 'bool' | 'nullableBool' | 'dateRange' | 'nullableDateRange' | 'search';
48
+ /**
49
+ * GatewayFilterRule
50
+ *
51
+ * A single declarative filter rule for use in BaseGatewayPropsInterface.listFilters,
52
+ * updateFilters, and removeFilters.
53
+ *
54
+ * Rules are applied in declaration order by BaseGateway after the standard
55
+ * base-class filter logic runs. They supplement — never replace — the existing
56
+ * onListFilter / onUpdateFilter / onRemoveFilter override chain.
57
+ *
58
+ * Example — array filter:
59
+ * { field: 'partyId', column: `${TABLES.PARTY_GUEST}.${FIELDS.PARTY_ID}`, type: 'array' }
60
+ *
61
+ * Example — nullable bool (wasInvited ↔ Invited_At IS [NOT] NULL):
62
+ * { field: 'wasInvited', column: `${TABLES.PARTY_GUEST}.${FIELDS.INVITED_AT}`, type: 'nullableBool' }
63
+ *
64
+ * Example — nullable date range:
65
+ * { field: 'rsvpAt', column: `${TABLES.PARTY_GUEST}.${FIELDS.RSVP_AT}`, type: 'nullableDateRange' }
66
+ * reads: filter.rsvpAtNull, filter.rsvpAtFrom, filter.rsvpAtTo
67
+ *
68
+ * Example — full-text search across Person columns:
69
+ * {
70
+ * field: 'search',
71
+ * column: '', // unused for 'search' — columns[] drives the OR
72
+ * type: 'search',
73
+ * columns: [
74
+ * `${TABLES.PERSON}.${FIELDS.FIRST_NAME}`,
75
+ * `${TABLES.PERSON}.${FIELDS.EMAIL}`,
76
+ * ],
77
+ * }
78
+ */
79
+ export interface GatewayFilterRule {
80
+ /**
81
+ * The key read from filterParams.
82
+ *
83
+ * For 'dateRange' / 'nullableDateRange': this is the base name; the actual
84
+ * keys read are field+'From', field+'To', and (for nullableDateRange) field+'Null'.
85
+ *
86
+ * For 'search': this is the key that holds the search string.
87
+ */
88
+ field: string;
89
+ /**
90
+ * The fully-qualified database column the filter is applied to.
91
+ * Should include the table name to prevent ambiguity in joined queries
92
+ * (e.g. 'Party_Guest.Party_ID', not just 'Party_ID').
93
+ *
94
+ * Unused for 'search' (which uses the 'columns' array instead) but
95
+ * must still be present as an empty string for type safety.
96
+ */
97
+ column: string;
98
+ /**
99
+ * The filter strategy. See GatewayFilterType for full documentation.
100
+ */
101
+ type: GatewayFilterType;
102
+ /**
103
+ * Required for type: 'search' — the list of columns to OR across.
104
+ * Must contain at least one entry; the rule is silently skipped otherwise.
105
+ * Each entry should be table-qualified for joined query safety.
106
+ * Ignored for all other filter types.
107
+ */
108
+ columns?: string[];
109
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -30,3 +30,4 @@ export * from './BaseControllerPropsInterface';
30
30
  export * from './BaseRestApiCrudControllerPropsInterface';
31
31
  export * from './BaseExternalApiGatewayPropsInterface';
32
32
  export * from './DataCachePropsInterface';
33
+ export * from './GatewayFilterRule';
@@ -45,3 +45,4 @@ __exportStar(require("./BaseControllerPropsInterface"), exports);
45
45
  __exportStar(require("./BaseRestApiCrudControllerPropsInterface"), exports);
46
46
  __exportStar(require("./BaseExternalApiGatewayPropsInterface"), exports);
47
47
  __exportStar(require("./DataCachePropsInterface"), exports);
48
+ __exportStar(require("./GatewayFilterRule"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.122",
3
+ "version": "1.0.124",
4
4
  "description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
5
5
  "type": "commonjs",
6
6
  "exports": {