velocious 1.0.493 → 1.0.494

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.
@@ -3,7 +3,11 @@
3
3
  import {forcedNonBlankStringParam} from "typanic"
4
4
 
5
5
  import FrontendModelBaseResource from "../frontend-model-resource/base-resource.js"
6
+ import normalizeAttributesWithSchema from "./sync-attribute-normalizer.js"
6
7
  import SyncModelChangeFeedService from "./sync-model-change-feed-service.js"
8
+ import VelociousError from "../velocious-error.js"
9
+
10
+ const QUICK_SEARCH_COLUMN = "quickSearch"
7
11
 
8
12
  /**
9
13
  * Optional client-declared sync scope carried on a changes request.
@@ -22,6 +26,105 @@ import SyncModelChangeFeedService from "./sync-model-change-feed-service.js"
22
26
  * @augments {FrontendModelBaseResource<TModelClass>}
23
27
  */
24
28
  export default class SyncResourceBase extends FrontendModelBaseResource {
29
+ /**
30
+ * Declarative quick-search text columns. When declared, an index search on
31
+ * the pseudo-column `quickSearch` expands to an OR of LIKE conditions over
32
+ * these root-table columns instead of hitting the controller default.
33
+ * @type {string[] | null} */
34
+ static quickSearchColumns = null
35
+
36
+ /**
37
+ * Declarative writable-attribute schema consumed by
38
+ * {@link SyncResourceBase#normalizeWritableAttributes}, keyed by camelCase
39
+ * attribute name.
40
+ * @type {Record<string, import("./sync-attribute-normalizer.js").SyncAttributeSchemaEntry> | null} */
41
+ static writableAttributes = null
42
+
43
+ /**
44
+ * Applies frontend-model index searches, expanding declared quick searches.
45
+ * @param {object} args - Search args.
46
+ * @param {import("../frontend-model-resource/base-resource.js").FrontendModelResourceController} args.controller - Controller handling the query.
47
+ * @param {import("../frontend-model-resource/base-resource.js").FrontendModelResourceAnyQuery} args.query - Query instance.
48
+ * @param {import("../frontend-model-resource/base-resource.js").FrontendModelResourceSearch} args.search - Search params.
49
+ * @returns {void}
50
+ */
51
+ applyFrontendModelIndexSearch({controller, query, search}) {
52
+ if (this.applyQuickSearch({query, search})) return
53
+
54
+ super.applyFrontendModelIndexSearch({controller, query, search})
55
+ }
56
+
57
+ /**
58
+ * Expands a `quickSearch` pseudo-column search into an OR of LIKE conditions
59
+ * over the declared {@link SyncResourceBase.quickSearchColumns}.
60
+ * @param {object} args - Search args.
61
+ * @param {import("../frontend-model-resource/base-resource.js").FrontendModelResourceAnyQuery} args.query - Query to filter.
62
+ * @param {import("../frontend-model-resource/base-resource.js").FrontendModelResourceSearch} args.search - Search payload.
63
+ * @returns {boolean} Whether the search was handled as a quick search.
64
+ */
65
+ applyQuickSearch({query, search}) {
66
+ const quickSearchColumns = /** @type {typeof SyncResourceBase} */ (this.constructor).quickSearchColumns
67
+
68
+ if (!quickSearchColumns || quickSearchColumns.length === 0) return false
69
+ if (search.path.length > 0 || search.column !== QUICK_SEARCH_COLUMN) return false
70
+
71
+ if (search.operator !== "like") {
72
+ throw VelociousError.safe("Sync quick search must use the like operator.", {code: "sync-invalid-quick-search"})
73
+ }
74
+
75
+ if (typeof search.value !== "string") {
76
+ throw VelociousError.safe("Sync quick search must be a string.", {code: "sync-invalid-quick-search"})
77
+ }
78
+
79
+ const trimmedValue = search.value.trim()
80
+
81
+ if (!trimmedValue) return true
82
+
83
+ const tableSql = query.driver.quoteTable(query.getTableReferenceForJoin())
84
+ const likeValue = `%${trimmedValue}%`
85
+ const conditions = quickSearchColumns.map((columnName) => (
86
+ `${tableSql}.${query.driver.quoteColumn(columnName)} LIKE ${query.driver.quote(likeValue)}`
87
+ ))
88
+
89
+ query.where(`(${conditions.join(" OR ")})`)
90
+
91
+ return true
92
+ }
93
+
94
+ /**
95
+ * Normalizes incoming writable attributes through the declared
96
+ * {@link SyncResourceBase.writableAttributes} schema: camelCase and
97
+ * snake_case input keys are accepted, values are validated per type and the
98
+ * normalized values are written under snake_case column keys. Validation
99
+ * failures throw client-safe errors built by
100
+ * {@link SyncResourceBase#writableAttributeError}.
101
+ * @param {Record<string, ?>} attributes - Raw incoming attributes.
102
+ * @param {{unknownAttributes?: "error" | "ignore"}} [options] - Unknown input-key handling. Defaults to "error".
103
+ * @returns {Record<string, ?>} Normalized attributes keyed by column names.
104
+ */
105
+ normalizeWritableAttributes(attributes, options = {}) {
106
+ const schema = /** @type {typeof SyncResourceBase} */ (this.constructor).writableAttributes
107
+
108
+ if (!schema) throw new Error(`${this.constructor.name} must define static writableAttributes to use normalizeWritableAttributes`)
109
+
110
+ return normalizeAttributesWithSchema({
111
+ attributes,
112
+ errorFactory: (message, details) => this.writableAttributeError(message, details),
113
+ schema,
114
+ unknownAttributes: options.unknownAttributes
115
+ })
116
+ }
117
+
118
+ /**
119
+ * Builds the client-safe error thrown for a failed writable-attribute validation.
120
+ * @param {string} message - Human-readable validation message.
121
+ * @param {{cause?: Error, code: string}} details - Stable machine-readable code and optional cause.
122
+ * @returns {Error} Client-safe error.
123
+ */
124
+ writableAttributeError(message, {cause, code}) {
125
+ return VelociousError.safe(message, cause ? {cause, code} : {code})
126
+ }
127
+
25
128
  /**
26
129
  * Returns a stable change-feed page after app authorization.
27
130
  * @returns {Promise<Record<string, ?>>} Change-feed page result.