velocious 1.0.495 → 1.0.497
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.
- package/build/configuration-types.js +21 -0
- package/build/configuration.js +48 -0
- package/build/database/column-types.js +19 -0
- package/build/database/record/index.js +11 -0
- package/build/database/record/validation-messages.js +50 -0
- package/build/database/record/validators/length.js +52 -0
- package/build/database/record/validators/presence.js +4 -1
- package/build/database/record/validators/uniqueness.js +4 -1
- package/build/frontend-model-resource/base-resource.js +175 -5
- package/build/frontend-models/resource-definition.js +46 -0
- package/build/src/configuration-types.d.ts +68 -0
- package/build/src/configuration-types.d.ts.map +1 -1
- package/build/src/configuration-types.js +20 -1
- package/build/src/configuration.d.ts +6 -0
- package/build/src/configuration.d.ts.map +1 -1
- package/build/src/configuration.js +45 -1
- package/build/src/database/column-types.d.ts +8 -0
- package/build/src/database/column-types.d.ts.map +1 -0
- package/build/src/database/column-types.js +18 -0
- package/build/src/database/record/index.d.ts +8 -0
- package/build/src/database/record/index.d.ts.map +1 -1
- package/build/src/database/record/index.js +11 -1
- package/build/src/database/record/validation-messages.d.ts +38 -0
- package/build/src/database/record/validation-messages.d.ts.map +1 -0
- package/build/src/database/record/validation-messages.js +45 -0
- package/build/src/database/record/validators/length.d.ts +12 -0
- package/build/src/database/record/validators/length.d.ts.map +1 -0
- package/build/src/database/record/validators/length.js +45 -0
- package/build/src/database/record/validators/presence.d.ts.map +1 -1
- package/build/src/database/record/validators/presence.js +4 -2
- package/build/src/database/record/validators/uniqueness.d.ts.map +1 -1
- package/build/src/database/record/validators/uniqueness.js +4 -2
- package/build/src/frontend-model-resource/base-resource.d.ts +184 -5
- package/build/src/frontend-model-resource/base-resource.d.ts.map +1 -1
- package/build/src/frontend-model-resource/base-resource.js +155 -6
- package/build/src/frontend-models/resource-definition.d.ts +41 -0
- package/build/src/frontend-models/resource-definition.d.ts.map +1 -1
- package/build/src/frontend-models/resource-definition.js +42 -1
- package/build/src/sync/sync-api-client.d.ts +4 -1
- package/build/src/sync/sync-api-client.d.ts.map +1 -1
- package/build/src/sync/sync-api-client.js +9 -2
- package/build/src/sync/sync-client-types.d.ts +88 -10
- package/build/src/sync/sync-client-types.d.ts.map +1 -1
- package/build/src/sync/sync-client-types.js +1 -1
- package/build/src/sync/sync-client.d.ts +38 -11
- package/build/src/sync/sync-client.d.ts.map +1 -1
- package/build/src/sync/sync-client.js +193 -29
- package/build/src/sync/sync-envelope-replay-service.d.ts +166 -3
- package/build/src/sync/sync-envelope-replay-service.d.ts.map +1 -1
- package/build/src/sync/sync-envelope-replay-service.js +304 -12
- package/build/src/sync/sync-replay-upsert-applier.d.ts +1 -0
- package/build/src/sync/sync-replay-upsert-applier.d.ts.map +1 -1
- package/build/src/sync/sync-replay-upsert-applier.js +2 -1
- package/build/src/sync/sync-resource-base.d.ts +16 -32
- package/build/src/sync/sync-resource-base.d.ts.map +1 -1
- package/build/src/sync/sync-resource-base.js +31 -43
- package/build/src/testing/test-runner.d.ts +12 -9
- package/build/src/testing/test-runner.d.ts.map +1 -1
- package/build/src/testing/test-runner.js +30 -9
- package/build/sync/sync-api-client.js +7 -1
- package/build/sync/sync-client-types.js +34 -5
- package/build/sync/sync-client.js +223 -27
- package/build/sync/sync-envelope-replay-service.js +350 -10
- package/build/sync/sync-replay-upsert-applier.js +1 -0
- package/build/sync/sync-resource-base.js +32 -45
- package/build/testing/test-runner.js +30 -8
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/scripts/run-tests.js +6 -0
- package/src/configuration-types.js +21 -0
- package/src/configuration.js +48 -0
- package/src/database/column-types.js +19 -0
- package/src/database/record/index.js +11 -0
- package/src/database/record/validation-messages.js +50 -0
- package/src/database/record/validators/length.js +52 -0
- package/src/database/record/validators/presence.js +4 -1
- package/src/database/record/validators/uniqueness.js +4 -1
- package/src/frontend-model-resource/base-resource.js +175 -5
- package/src/frontend-models/resource-definition.js +46 -0
- package/src/sync/sync-api-client.js +7 -1
- package/src/sync/sync-client-types.js +34 -5
- package/src/sync/sync-client.js +223 -27
- package/src/sync/sync-envelope-replay-service.js +350 -10
- package/src/sync/sync-replay-upsert-applier.js +1 -0
- package/src/sync/sync-resource-base.js +32 -45
- package/src/testing/test-runner.js +30 -8
- package/build/src/sync/sync-attribute-normalizer.d.ts +0 -105
- package/build/src/sync/sync-attribute-normalizer.d.ts.map +0 -1
- package/build/src/sync/sync-attribute-normalizer.js +0 -172
- package/build/sync/sync-attribute-normalizer.js +0 -186
- package/src/sync/sync-attribute-normalizer.js +0 -186
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
import {snakeCaseKey} from "typanic"
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Builds the error thrown for a failed sync attribute validation.
|
|
7
|
-
* @callback SyncAttributeErrorFactory
|
|
8
|
-
* @param {string} message - Human-readable validation message.
|
|
9
|
-
* @param {{cause?: Error, code: string}} details - Stable machine-readable code and optional cause.
|
|
10
|
-
* @returns {Error} Error to throw.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Declarative validation schema for one writable sync attribute.
|
|
15
|
-
*
|
|
16
|
-
* Keys are camelCase attribute names; each attribute also accepts its
|
|
17
|
-
* snake_case column key on input (the camelCase key wins when both are given)
|
|
18
|
-
* and is written under the snake_case column key on output. Error messages
|
|
19
|
-
* default to `<label> ...` phrasings with `sync-<label>-...` codes, where the
|
|
20
|
-
* label defaults to the camelCase attribute name; apps pin exact messages per
|
|
21
|
-
* attribute through the `*Message` overrides.
|
|
22
|
-
* @typedef {object} SyncAttributeSchemaEntry
|
|
23
|
-
* @property {"date" | "json" | "raw" | "string"} type - Attribute value type.
|
|
24
|
-
* @property {boolean} [required] - Whether present blank/absent values are rejected (strings reject blank-after-trim, dates reject null).
|
|
25
|
-
* @property {number} [maxLength] - Maximum string length, checked after trimming.
|
|
26
|
-
* @property {string} [label] - Label used in derived messages and codes.
|
|
27
|
-
* @property {string} [column] - Output column key. Defaults to the snake_cased attribute name.
|
|
28
|
-
* @property {string} [requiredMessage] - Pinned message for required rejections.
|
|
29
|
-
* @property {string} [tooLongMessage] - Pinned message for too-long rejections.
|
|
30
|
-
* @property {string} [invalidMessage] - Pinned message for invalid dates and non-object/non-string json values.
|
|
31
|
-
* @property {string} [invalidJsonMessage] - Pinned message for unparseable json strings.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Normalizes writable sync attributes against a declarative schema.
|
|
36
|
-
*
|
|
37
|
-
* Only present keys are validated, so required attributes may be absent —
|
|
38
|
-
* create defaults own that concern. Input keys not declared in the schema are
|
|
39
|
-
* rejected by default so misspelled or read-only attributes fail loudly
|
|
40
|
-
* instead of being silently discarded (`unknownAttributes: "ignore"` opts
|
|
41
|
-
* out). Validation failures throw through the given error factory so apps
|
|
42
|
-
* control the raised error class.
|
|
43
|
-
* @param {object} args - Options.
|
|
44
|
-
* @param {Record<string, ?>} args.attributes - Raw incoming attributes (camelCase and/or snake_case keys).
|
|
45
|
-
* @param {SyncAttributeErrorFactory} args.errorFactory - Maps validation failures to thrown errors.
|
|
46
|
-
* @param {Record<string, SyncAttributeSchemaEntry>} args.schema - Attribute schema keyed by camelCase attribute name.
|
|
47
|
-
* @param {"error" | "ignore"} [args.unknownAttributes] - Unknown input-key handling. Defaults to "error".
|
|
48
|
-
* @returns {Record<string, ?>} Normalized values keyed by snake_case column names.
|
|
49
|
-
*/
|
|
50
|
-
export default function normalizeAttributesWithSchema({attributes, errorFactory, schema, unknownAttributes = "error"}) {
|
|
51
|
-
if (!attributes || typeof attributes != "object") throw new Error(`normalizeAttributesWithSchema requires an attributes object, got: ${String(attributes)}`)
|
|
52
|
-
if (typeof errorFactory != "function") throw new Error("normalizeAttributesWithSchema requires an errorFactory function")
|
|
53
|
-
if (!schema || typeof schema != "object") throw new Error(`normalizeAttributesWithSchema requires a schema object, got: ${String(schema)}`)
|
|
54
|
-
if (unknownAttributes != "error" && unknownAttributes != "ignore") {
|
|
55
|
-
throw new Error(`normalizeAttributesWithSchema unknownAttributes must be "error" or "ignore", got: ${String(unknownAttributes)}`)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** @type {Record<string, ?>} */
|
|
59
|
-
const normalized = {}
|
|
60
|
-
/** @type {Set<string>} */
|
|
61
|
-
const knownInputKeys = new Set()
|
|
62
|
-
|
|
63
|
-
for (const [attributeName, entry] of Object.entries(schema)) {
|
|
64
|
-
const column = entry.column ?? snakeCaseKey(attributeName)
|
|
65
|
-
const inputKeys = column == attributeName ? [attributeName] : [column, attributeName]
|
|
66
|
-
|
|
67
|
-
for (const inputKey of inputKeys) {
|
|
68
|
-
knownInputKeys.add(inputKey)
|
|
69
|
-
|
|
70
|
-
if (!Object.prototype.hasOwnProperty.call(attributes, inputKey)) continue
|
|
71
|
-
|
|
72
|
-
normalized[column] = normalizeAttributeValue({attributeName, entry, errorFactory, value: attributes[inputKey]})
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (unknownAttributes == "error") {
|
|
77
|
-
for (const inputKey of Object.keys(attributes)) {
|
|
78
|
-
if (!knownInputKeys.has(inputKey)) {
|
|
79
|
-
throw errorFactory(`Unknown attribute: ${inputKey}.`, {code: "sync-unknown-attribute"})
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return normalized
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Normalizes one present attribute value per its schema entry.
|
|
89
|
-
* @param {object} args - Options.
|
|
90
|
-
* @param {string} args.attributeName - camelCase attribute name.
|
|
91
|
-
* @param {SyncAttributeSchemaEntry} args.entry - Schema entry.
|
|
92
|
-
* @param {SyncAttributeErrorFactory} args.errorFactory - Error factory.
|
|
93
|
-
* @param {?} args.value - Raw value.
|
|
94
|
-
* @returns {?} Normalized value.
|
|
95
|
-
*/
|
|
96
|
-
function normalizeAttributeValue({attributeName, entry, errorFactory, value}) {
|
|
97
|
-
const label = entry.label ?? attributeName
|
|
98
|
-
|
|
99
|
-
if (entry.type == "string") return normalizeString({entry, errorFactory, label, value})
|
|
100
|
-
if (entry.type == "date") return normalizeDate({entry, errorFactory, label, value})
|
|
101
|
-
if (entry.type == "json") return normalizeJson({entry, errorFactory, label, value})
|
|
102
|
-
if (entry.type == "raw") return value
|
|
103
|
-
|
|
104
|
-
throw new Error(`Unknown sync attribute schema type for ${attributeName}: ${String(entry.type)}`)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Normalizes a string attribute: trims, rejects blank required values after trimming, maps empty optional values to null and bounds the trimmed length.
|
|
109
|
-
* @param {object} args - Options.
|
|
110
|
-
* @param {SyncAttributeSchemaEntry} args.entry - Schema entry.
|
|
111
|
-
* @param {SyncAttributeErrorFactory} args.errorFactory - Error factory.
|
|
112
|
-
* @param {string} args.label - Message/code label.
|
|
113
|
-
* @param {?} args.value - Raw value.
|
|
114
|
-
* @returns {string | null} Normalized string.
|
|
115
|
-
*/
|
|
116
|
-
function normalizeString({entry, errorFactory, label, value}) {
|
|
117
|
-
const requiredError = () => errorFactory(entry.requiredMessage ?? `${label} is required.`, {code: `sync-${label}-required`})
|
|
118
|
-
|
|
119
|
-
if (value === null || value === undefined || value === "") {
|
|
120
|
-
if (entry.required) throw requiredError()
|
|
121
|
-
|
|
122
|
-
return null
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const stringValue = String(value).trim()
|
|
126
|
-
|
|
127
|
-
if (stringValue === "") {
|
|
128
|
-
if (entry.required) throw requiredError()
|
|
129
|
-
|
|
130
|
-
return null
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (entry.maxLength !== undefined && stringValue.length > entry.maxLength) {
|
|
134
|
-
throw errorFactory(entry.tooLongMessage ?? `${label} is too long (maximum is ${entry.maxLength} characters).`, {code: `sync-${label}-too-long`})
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return stringValue
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Normalizes a date attribute: Date instances pass through, strings/numbers are parsed and invalid or missing-required values are rejected.
|
|
142
|
-
* @param {object} args - Options.
|
|
143
|
-
* @param {SyncAttributeSchemaEntry} args.entry - Schema entry.
|
|
144
|
-
* @param {SyncAttributeErrorFactory} args.errorFactory - Error factory.
|
|
145
|
-
* @param {string} args.label - Message/code label.
|
|
146
|
-
* @param {?} args.value - Raw value.
|
|
147
|
-
* @returns {Date | null} Normalized date.
|
|
148
|
-
*/
|
|
149
|
-
function normalizeDate({entry, errorFactory, label, value}) {
|
|
150
|
-
if (!entry.required && (value === null || value === undefined || value === "")) return null
|
|
151
|
-
|
|
152
|
-
const dateValue = value instanceof Date ? value : typeof value == "string" || typeof value == "number" ? new Date(value) : null
|
|
153
|
-
|
|
154
|
-
if (!dateValue || Number.isNaN(dateValue.getTime())) {
|
|
155
|
-
throw errorFactory(entry.invalidMessage ?? `${label} must be a valid datetime.`, {code: `sync-${label}-invalid-datetime`})
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return dateValue
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Normalizes a json attribute: empty values become null, objects pass through, strings must parse as JSON and other types are rejected.
|
|
163
|
-
* @param {object} args - Options.
|
|
164
|
-
* @param {SyncAttributeSchemaEntry} args.entry - Schema entry.
|
|
165
|
-
* @param {SyncAttributeErrorFactory} args.errorFactory - Error factory.
|
|
166
|
-
* @param {string} args.label - Message/code label.
|
|
167
|
-
* @param {?} args.value - Raw value.
|
|
168
|
-
* @returns {?} Normalized json value.
|
|
169
|
-
*/
|
|
170
|
-
function normalizeJson({entry, errorFactory, label, value}) {
|
|
171
|
-
if (value === null || value === undefined || value === "") return null
|
|
172
|
-
if (typeof value == "object") return value
|
|
173
|
-
|
|
174
|
-
if (typeof value == "string") {
|
|
175
|
-
try {
|
|
176
|
-
return JSON.parse(value)
|
|
177
|
-
} catch (error) {
|
|
178
|
-
throw errorFactory(entry.invalidJsonMessage ?? `${label} must be valid JSON.`, {
|
|
179
|
-
cause: /** @type {Error} */ (error),
|
|
180
|
-
code: `sync-${label}-invalid-json`
|
|
181
|
-
})
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
throw errorFactory(entry.invalidMessage ?? `${label} must be an object or JSON string.`, {code: `sync-${label}-invalid`})
|
|
186
|
-
}
|