velocious 1.0.502 → 1.0.504

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.
Files changed (81) hide show
  1. package/build/application.js +4 -0
  2. package/build/configuration-types.js +8 -8
  3. package/build/configuration.js +2 -2
  4. package/build/src/application.d.ts.map +1 -1
  5. package/build/src/application.js +5 -1
  6. package/build/src/configuration-types.d.ts +17 -17
  7. package/build/src/configuration-types.d.ts.map +1 -1
  8. package/build/src/configuration-types.js +9 -9
  9. package/build/src/configuration.d.ts +4 -4
  10. package/build/src/configuration.d.ts.map +1 -1
  11. package/build/src/configuration.js +3 -3
  12. package/build/src/sync/sync-change-fanout.d.ts +71 -0
  13. package/build/src/sync/sync-change-fanout.d.ts.map +1 -0
  14. package/build/src/sync/sync-change-fanout.js +54 -0
  15. package/build/src/sync/sync-channel-name.d.ts +9 -0
  16. package/build/src/sync/sync-channel-name.d.ts.map +1 -0
  17. package/build/src/sync/sync-channel-name.js +10 -0
  18. package/build/src/sync/sync-client-types.d.ts +13 -6
  19. package/build/src/sync/sync-client-types.d.ts.map +1 -1
  20. package/build/src/sync/sync-client-types.js +1 -1
  21. package/build/src/sync/sync-client.d.ts +1 -1
  22. package/build/src/sync/sync-client.d.ts.map +1 -1
  23. package/build/src/sync/sync-client.js +8 -4
  24. package/build/src/sync/sync-envelope-replay-service.d.ts +15 -4
  25. package/build/src/sync/sync-envelope-replay-service.d.ts.map +1 -1
  26. package/build/src/sync/sync-envelope-replay-service.js +60 -42
  27. package/build/src/sync/sync-model-change-feed-service.d.ts +6 -1
  28. package/build/src/sync/sync-model-change-feed-service.d.ts.map +1 -1
  29. package/build/src/sync/sync-model-change-feed-service.js +15 -4
  30. package/build/src/sync/sync-publish-suppression.d.ts +31 -0
  31. package/build/src/sync/sync-publish-suppression.d.ts.map +1 -0
  32. package/build/src/sync/sync-publish-suppression.js +54 -0
  33. package/build/src/sync/sync-publisher-types.d.ts +178 -0
  34. package/build/src/sync/sync-publisher-types.d.ts.map +1 -0
  35. package/build/src/sync/sync-publisher-types.js +3 -0
  36. package/build/src/sync/sync-publisher.d.ts +160 -0
  37. package/build/src/sync/sync-publisher.d.ts.map +1 -0
  38. package/build/src/sync/sync-publisher.js +438 -0
  39. package/build/src/sync/sync-realtime-bridge.d.ts +33 -10
  40. package/build/src/sync/sync-realtime-bridge.d.ts.map +1 -1
  41. package/build/src/sync/sync-realtime-bridge.js +51 -12
  42. package/build/src/sync/sync-scope-attributes.d.ts +15 -0
  43. package/build/src/sync/sync-scope-attributes.d.ts.map +1 -0
  44. package/build/src/sync/sync-scope-attributes.js +24 -0
  45. package/build/src/sync/sync-websocket-channel.d.ts +58 -0
  46. package/build/src/sync/sync-websocket-channel.d.ts.map +1 -0
  47. package/build/src/sync/sync-websocket-channel.js +131 -0
  48. package/build/src/testing/test-runner.d.ts +8 -0
  49. package/build/src/testing/test-runner.d.ts.map +1 -1
  50. package/build/src/testing/test-runner.js +64 -2
  51. package/build/sync/sync-change-fanout.js +58 -0
  52. package/build/sync/sync-channel-name.js +10 -0
  53. package/build/sync/sync-client-types.js +12 -5
  54. package/build/sync/sync-client.js +8 -3
  55. package/build/sync/sync-envelope-replay-service.js +60 -41
  56. package/build/sync/sync-model-change-feed-service.js +17 -3
  57. package/build/sync/sync-publish-suppression.js +59 -0
  58. package/build/sync/sync-publisher-types.js +84 -0
  59. package/build/sync/sync-publisher.js +502 -0
  60. package/build/sync/sync-realtime-bridge.js +56 -11
  61. package/build/sync/sync-scope-attributes.js +26 -0
  62. package/build/sync/sync-websocket-channel.js +146 -0
  63. package/build/testing/test-runner.js +69 -1
  64. package/build/tsconfig.tsbuildinfo +1 -1
  65. package/package.json +1 -1
  66. package/src/application.js +4 -0
  67. package/src/configuration-types.js +8 -8
  68. package/src/configuration.js +2 -2
  69. package/src/sync/sync-change-fanout.js +58 -0
  70. package/src/sync/sync-channel-name.js +10 -0
  71. package/src/sync/sync-client-types.js +12 -5
  72. package/src/sync/sync-client.js +8 -3
  73. package/src/sync/sync-envelope-replay-service.js +60 -41
  74. package/src/sync/sync-model-change-feed-service.js +17 -3
  75. package/src/sync/sync-publish-suppression.js +59 -0
  76. package/src/sync/sync-publisher-types.js +84 -0
  77. package/src/sync/sync-publisher.js +502 -0
  78. package/src/sync/sync-realtime-bridge.js +56 -11
  79. package/src/sync/sync-scope-attributes.js +26 -0
  80. package/src/sync/sync-websocket-channel.js +146 -0
  81. package/src/testing/test-runner.js +69 -1
@@ -0,0 +1,502 @@
1
+ // @ts-check
2
+
3
+ import Configuration from "../configuration.js"
4
+ import Logger from "../logger.js"
5
+ import restArgsError from "../utils/rest-args-error.js"
6
+
7
+ import {declaredSyncScopeAttributes} from "./sync-scope-attributes.js"
8
+ import {deliverDeclaredBroadcasts, upsertSyncRow} from "./sync-change-fanout.js"
9
+ import {isPublishingSuppressed} from "./sync-publish-suppression.js"
10
+ import {VELOCIOUS_SYNC_CHANNEL} from "./sync-channel-name.js"
11
+
12
+ /** @type {{create: "afterCreate", update: "afterUpdate", destroy: "afterDestroy"}} */
13
+ const PUBLISHED_CALLBACK_NAMES = {create: "afterCreate", destroy: "afterDestroy", update: "afterUpdate"}
14
+
15
+ /**
16
+ * Operations published by default for models declaring `static sync` publish
17
+ * without an `operations` key: server-side creates and updates publish
18
+ * automatically. Destroys are not published by default because a server
19
+ * destroy is often cleanup rather than a synced delete; opt in with an
20
+ * operations list.
21
+ * @type {Array<"create" | "update" | "destroy">} */
22
+ const DEFAULT_PUBLISHED_OPERATIONS = ["create", "update"]
23
+
24
+ /** @type {WeakMap<Configuration, SyncPublisher>} */
25
+ const startedPublishersByConfiguration = new WeakMap()
26
+
27
+ /**
28
+ * Declarative server-side sync publisher — the server mirror of the client's
29
+ * track-by-default mutation tracking.
30
+ *
31
+ * Server models declare what to publish through `static sync`'s `publish`
32
+ * key, and Velocious writes every committed server-side change to the sync
33
+ * change feed (model-backed Sync-row upsert with server re-sequencing) and
34
+ * broadcasts the standard sync envelope (`{echoOrigin, syncs: [...]}`) on the
35
+ * framework sync channel ({@link VELOCIOUS_SYNC_CHANNEL}) scoped by the
36
+ * change's derived scope-partition values, so devices receive server-origin
37
+ * changes without app code declaring channels or calling manual
38
+ * upsert/broadcast helpers:
39
+ *
40
+ * static sync = {publish: true} // default payload (attributes) + default scope partition
41
+ * static sync = {publish: {serialize: (record) => ({id: record.id(), pin: record.pin()})}}
42
+ *
43
+ * The scope partition comes from the sync model's `static
44
+ * syncScopeAttributes` declaration (for example `["eventId"]` or
45
+ * `["accountId"]` — Velocious has no built-in partition name): each declared
46
+ * scope attribute reads the record's attribute of the same name when the
47
+ * model has one, else the record's own id (scope-root models), overridable
48
+ * per model through `publish: {scopeAttributes: {accountId: "ownerId"}}`.
49
+ * The pre-framework-channel `broadcasts` list and the `eventId`
50
+ * string/resolver-function declaration forms keep working but are deprecated.
51
+ *
52
+ * Replayed device mutations never double-publish: the framework's routed
53
+ * replay apply marks its written records through `markServerApply(record)`
54
+ * (see sync-publish-suppression.js), and app code applying already-synced
55
+ * data can use `markServerApply`/`withoutPublishing` the same way.
56
+ */
57
+ export default class SyncPublisher {
58
+ /**
59
+ * Builds the sync publisher by deriving published resources from the
60
+ * configuration's registered models: every model declaring `static sync`
61
+ * with a `publish` declaration becomes a published resource
62
+ * (`publish: false` opts out). The sync/change model is the registered
63
+ * "Sync" model and broadcasts default to the configuration's channel
64
+ * broadcast.
65
+ * @param {import("./sync-publisher-types.js").SyncPublisherOptions} [options] - Optional overrides.
66
+ */
67
+ constructor(options = {}) {
68
+ const {actorForeignKeyColumn = "authentication_token_id", broadcaster, configuration = Configuration.current(), onError, syncModel, ...restOptions} = options
69
+
70
+ restArgsError(restOptions)
71
+
72
+ const modelClasses = configuration.getModelClasses()
73
+ const publishingModelClasses = Object.values(modelClasses).filter((modelClass) => publishDeclarationFor(modelClass))
74
+
75
+ if (publishingModelClasses.length === 0) {
76
+ throw new Error("SyncPublisher found no registered models declaring static sync publish - declare `static sync = {publish: {serialize}}` on the models whose server-side changes should publish to the sync feed")
77
+ }
78
+
79
+ const resolvedSyncModel = syncModel || modelClasses.Sync
80
+
81
+ if (!resolvedSyncModel) {
82
+ throw new Error("SyncPublisher requires a registered \"Sync\" model for published sync change rows (or pass options.syncModel)")
83
+ }
84
+
85
+ const scopeAttributes = declaredSyncScopeAttributes(resolvedSyncModel)
86
+ /** @type {Record<string, import("./sync-publisher-types.js").SyncPublisherResourceConfig>} */
87
+ const resources = {}
88
+
89
+ for (const modelClass of publishingModelClasses) {
90
+ const publish = publishDeclarationFor(modelClass)
91
+ const resourceConfig = resourceConfigFromPublishDeclaration({modelClass, publish, scopeAttributes, syncModel: resolvedSyncModel})
92
+
93
+ resources[resourceConfig.resourceType] = resourceConfig
94
+ }
95
+
96
+ /** @type {{actorForeignKeyColumn: string, broadcaster: import("./sync-publisher-types.js").SyncPublisherOptions["broadcaster"], configuration: Configuration, onError: import("./sync-publisher-types.js").SyncPublisherOptions["onError"], resources: Record<string, import("./sync-publisher-types.js").SyncPublisherResourceConfig>, syncModel: ?}} */
97
+ this.config = {actorForeignKeyColumn, broadcaster, configuration, onError, resources, syncModel: resolvedSyncModel}
98
+ /** @type {Array<{callback: (record: ?) => Promise<void>, callbackName: "afterCreate" | "afterUpdate" | "afterDestroy", modelClass: ?}>} */
99
+ this._publishedCallbacks = []
100
+ /** @type {Logger | null} */
101
+ this._logger = null
102
+ this._started = false
103
+ }
104
+
105
+ /**
106
+ * Builds a sync publisher derived from the given configuration. Alias for
107
+ * `new SyncPublisher({configuration, ...options})`.
108
+ * @param {Configuration} [configuration] - Configuration owning the registered models. Defaults to the current configuration.
109
+ * @param {Omit<import("./sync-publisher-types.js").SyncPublisherOptions, "configuration">} [options] - Optional overrides.
110
+ * @returns {SyncPublisher} Sync publisher derived from the configuration.
111
+ */
112
+ static fromConfiguration(configuration = Configuration.current(), options = {}) {
113
+ return new SyncPublisher({...options, configuration})
114
+ }
115
+
116
+ /**
117
+ * Starts (and memoizes per configuration) the sync publisher for a server
118
+ * boot: no-op when no registered model declares a publish config, guarded so
119
+ * repeated boots with the same configuration register the publish callbacks
120
+ * only once.
121
+ * @param {Configuration} configuration - Configuration owning the registered models.
122
+ * @returns {Promise<SyncPublisher | null>} Started publisher, or null when no models declare publish.
123
+ */
124
+ static async startFromConfiguration(configuration) {
125
+ const startedPublisher = startedPublishersByConfiguration.get(configuration)
126
+
127
+ if (startedPublisher) return startedPublisher
128
+
129
+ if (!Object.values(configuration.getModelClasses()).some((modelClass) => publishDeclarationFor(modelClass))) return null
130
+
131
+ const publisher = new SyncPublisher({configuration})
132
+
133
+ startedPublishersByConfiguration.set(configuration, publisher)
134
+ await publisher.start()
135
+
136
+ return publisher
137
+ }
138
+
139
+ /**
140
+ * Registers the publish callbacks for every published resource: server-side
141
+ * creates and updates (destroys when opted in) upsert a sync change row and
142
+ * fan out the declared broadcasts once their transaction commits.
143
+ * @returns {Promise<void>}
144
+ */
145
+ async start() {
146
+ if (this._started) return
147
+
148
+ this._started = true
149
+
150
+ for (const resourceConfig of Object.values(this.config.resources)) {
151
+ for (const operation of resourceConfig.operations) {
152
+ const callbackName = PUBLISHED_CALLBACK_NAMES[operation]
153
+ const callback = this.publishedMutationCallback({operation, resourceConfig})
154
+
155
+ resourceConfig.modelClass[callbackName](callback)
156
+ this._publishedCallbacks.push({callback, callbackName, modelClass: resourceConfig.modelClass})
157
+ }
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Unregisters all publish callbacks (tests, shutdown).
163
+ * @returns {void}
164
+ */
165
+ stop() {
166
+ for (const {callback, callbackName, modelClass} of this._publishedCallbacks) {
167
+ modelClass.unregisterLifecycleCallback(callbackName, callback)
168
+ }
169
+
170
+ this._publishedCallbacks = []
171
+ this._started = false
172
+ }
173
+
174
+ /**
175
+ * Builds the lifecycle callback publishing one server-side mutation. The
176
+ * published payload (declaration `serialize`), event scope, and sync type
177
+ * are snapshotted at mutation-callback time, so afterSave hooks assigning
178
+ * unsaved attributes (or any later drift on the record) cannot change what
179
+ * gets published vs what was committed. Persisting and broadcasting are
180
+ * deferred through the model connection's afterCommit hook so they only run
181
+ * once the mutation's transaction has committed (immediately when no
182
+ * transaction is open) - rolled-back mutations never publish. Post-commit
183
+ * publish failures are reported without rethrowing into the driver's
184
+ * afterCommit chain (see reportAfterCommitError).
185
+ * @param {{operation: "create" | "update" | "destroy", resourceConfig: import("./sync-publisher-types.js").SyncPublisherResourceConfig}} args - Operation and resource config.
186
+ * @returns {(record: ?) => Promise<void>} Lifecycle callback.
187
+ */
188
+ publishedMutationCallback({operation, resourceConfig}) {
189
+ return async (record) => {
190
+ if (isPublishingSuppressed(record)) return
191
+
192
+ const data = await resourceConfig.serialize(record)
193
+ const resourceId = String(record.id())
194
+ const syncType = operation === "destroy" ? "delete" : "update"
195
+ const scopeValues = await this.publishedScopeValues({record, resourceConfig})
196
+ /** @type {Record<string, ?>} */
197
+ const attributes = {
198
+ [this.config.actorForeignKeyColumn]: null,
199
+ client_updated_at: new Date(),
200
+ data: JSON.stringify(data),
201
+ resource_id: resourceId,
202
+ resource_type: resourceConfig.resourceType,
203
+ sync_type: syncType,
204
+ ...scopeValues.columns
205
+ }
206
+
207
+ await resourceConfig.modelClass.connection().afterCommit(async () => {
208
+ try {
209
+ const syncRow = await this.upsertPublishedSyncRow(attributes)
210
+
211
+ await this.broadcaster()({
212
+ body: {
213
+ echoOrigin: null,
214
+ syncs: [{data, resourceId, resourceType: resourceConfig.resourceType, syncType}]
215
+ },
216
+ channel: VELOCIOUS_SYNC_CHANNEL,
217
+ params: {...scopeValues.params, resourceType: resourceConfig.resourceType}
218
+ })
219
+
220
+ if (resourceConfig.broadcasts) {
221
+ await deliverDeclaredBroadcasts({
222
+ args: {data, operation, record, resourceId, resourceType: resourceConfig.resourceType, syncRow, syncType},
223
+ broadcaster: this.broadcaster(),
224
+ broadcasts: resourceConfig.broadcasts
225
+ })
226
+ }
227
+ } catch (error) {
228
+ await this.reportAfterCommitError(/** @type {Error} */ (error))
229
+ }
230
+ })
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Resolves the scope-partition values for one published mutation from the
236
+ * resource's derived scope plan: each entry reads its record attribute (or
237
+ * the record's own id for scope-root models, or the deprecated resolver
238
+ * function). The values are persisted onto the sync row's partition columns
239
+ * and broadcast as the framework sync channel's scoping params.
240
+ * @param {{record: ?, resourceConfig: import("./sync-publisher-types.js").SyncPublisherResourceConfig}} args - Mutated record and resource config.
241
+ * @returns {Promise<{columns: Record<string, string | null>, params: Record<string, string | null>}>} Scope values keyed by sync-row column and by scope attribute.
242
+ */
243
+ async publishedScopeValues({record, resourceConfig}) {
244
+ /** @type {Record<string, string | null>} */
245
+ const columns = {}
246
+ /** @type {Record<string, string | null>} */
247
+ const params = {}
248
+
249
+ for (const scopePlanEntry of resourceConfig.scopePlan) {
250
+ /** @type {?} */
251
+ let rawValue
252
+
253
+ if (scopePlanEntry.resolver) {
254
+ rawValue = await scopePlanEntry.resolver(record)
255
+ } else if (scopePlanEntry.recordAttribute) {
256
+ rawValue = record.readAttribute(scopePlanEntry.recordAttribute)
257
+ } else {
258
+ rawValue = record.id()
259
+ }
260
+
261
+ const value = rawValue === undefined || rawValue === null ? null : String(rawValue)
262
+
263
+ columns[scopePlanEntry.columnName] = value
264
+ params[scopePlanEntry.scopeAttribute] = value
265
+ }
266
+
267
+ return {columns, params}
268
+ }
269
+
270
+ /**
271
+ * Upserts the published server-origin sync row for a resource identity:
272
+ * server-origin rows carry a null actor column (no device to echo the
273
+ * change back to), so repeated server changes to one resource reuse and
274
+ * re-sequence one feed row.
275
+ * @param {Record<string, ?>} attributes - Snapshotted sync row attributes.
276
+ * @returns {Promise<?>} Upserted sync row.
277
+ */
278
+ async upsertPublishedSyncRow(attributes) {
279
+ const existingSync = await this.config.syncModel
280
+ .where({
281
+ [this.config.actorForeignKeyColumn]: null,
282
+ resource_id: attributes.resource_id,
283
+ resource_type: attributes.resource_type
284
+ })
285
+ .first()
286
+
287
+ return await upsertSyncRow({attributes, existingSync, syncModel: this.config.syncModel})
288
+ }
289
+
290
+ /**
291
+ * Returns the broadcaster delivering declared broadcasts: the injected one,
292
+ * or the configuration's channel broadcast awaited through the pending
293
+ * broadcast queue.
294
+ * @returns {NonNullable<import("./sync-publisher-types.js").SyncPublisherOptions["broadcaster"]>} Broadcast deliverer.
295
+ */
296
+ broadcaster() {
297
+ if (this.config.broadcaster) return this.config.broadcaster
298
+
299
+ return async ({body, channel, params}) => {
300
+ this.config.configuration.broadcastToChannel(channel, params, body)
301
+ await this.config.configuration.awaitPendingBroadcasts()
302
+ }
303
+ }
304
+
305
+ /**
306
+ * Reports a post-commit publish failure. The transaction has already
307
+ * committed when afterCommit callbacks run, so rethrowing here would poison
308
+ * the driver's awaited afterCommit chain (breaking unrelated callbacks) -
309
+ * instead the failure goes to the configured onError hook, or is emitted on
310
+ * the configuration's framework-error/all-error channels (so production bug
311
+ * reporting via `configuration.getErrorEvents()` sees a broken publish
312
+ * path) and logged loudly through the publisher's logger when none is
313
+ * configured.
314
+ * @param {Error} error - Post-commit publish failure.
315
+ * @returns {Promise<void>}
316
+ */
317
+ async reportAfterCommitError(error) {
318
+ if (this.config.onError) {
319
+ this.config.onError(error)
320
+
321
+ return
322
+ }
323
+
324
+ const errorEvents = this.config.configuration.getErrorEvents()
325
+ const payload = {context: {stage: "sync-publish-after-commit"}, error}
326
+
327
+ errorEvents.emit("framework-error", payload)
328
+ errorEvents.emit("all-error", {...payload, errorType: "framework-error"})
329
+
330
+ await this.logger().error("SyncPublisher failed to publish a server-side sync change after commit", error)
331
+ }
332
+
333
+ /**
334
+ * Returns the lazily built publisher logger.
335
+ * @returns {Logger} Publisher logger.
336
+ */
337
+ logger() {
338
+ this._logger ||= new Logger("SyncPublisher", {configuration: this.config.configuration})
339
+
340
+ return this._logger
341
+ }
342
+ }
343
+
344
+ /**
345
+ * Resolves a model class's active publish declaration from `static sync`.
346
+ * Opted-out (`publish: false`) and undeclared models resolve to null; every
347
+ * other declared value flows into loud declaration validation.
348
+ * @param {?} modelClass - Registered model class.
349
+ * @returns {import("./sync-publisher-types.js").SyncPublishDeclaration | null} Active publish declaration, or null.
350
+ */
351
+ function publishDeclarationFor(modelClass) {
352
+ const declaration = modelClass.sync
353
+
354
+ if (!declaration || typeof declaration !== "object" || declaration.publish === undefined || declaration.publish === false) return null
355
+
356
+ return declaration.publish
357
+ }
358
+
359
+ /**
360
+ * Builds one published resource config from a model's `static sync` publish
361
+ * declaration. `publish: true` opts in with all defaults (attribute payload,
362
+ * derived scope partition, created/updated operations).
363
+ * @param {{modelClass: ?, publish: import("./sync-publisher-types.js").SyncPublishDeclaration | null, scopeAttributes: string[] | null, syncModel: ?}} args - Declaration args plus the sync model's declared scope attributes.
364
+ * @returns {import("./sync-publisher-types.js").SyncPublisherResourceConfig} Derived resource config.
365
+ */
366
+ function resourceConfigFromPublishDeclaration({modelClass, publish, scopeAttributes: syncScopeAttributes, syncModel}) {
367
+ const modelName = modelClass.getModelName()
368
+ const normalizedPublish = publish === true ? {} : publish
369
+
370
+ if (!normalizedPublish || typeof normalizedPublish !== "object" || Array.isArray(normalizedPublish)) {
371
+ throw new Error(`${modelName} static sync publish must be true, false or a publish declaration object, got: ${String(publish)}`)
372
+ }
373
+
374
+ const {broadcasts, eventId, operations, resourceType, scopeAttributes, serialize, ...restDeclaration} = normalizedPublish
375
+ const unknownKeys = Object.keys(restDeclaration)
376
+
377
+ if (unknownKeys.length > 0) {
378
+ throw new Error(`${modelName} static sync publish received unknown keys: ${unknownKeys.join(", ")} (supported: broadcasts, eventId (deprecated), operations, resourceType, scopeAttributes, serialize)`)
379
+ }
380
+ if (serialize !== undefined && typeof serialize !== "function") {
381
+ throw new Error(`${modelName} static sync publish serialize must be a function building the published payload, got: ${String(serialize)}`)
382
+ }
383
+ if (operations !== undefined) {
384
+ if (!Array.isArray(operations) || operations.length === 0) {
385
+ throw new Error(`${modelName} static sync publish operations must be a non-empty array of create/update/destroy`)
386
+ }
387
+
388
+ for (const operation of operations) {
389
+ if (!(operation in PUBLISHED_CALLBACK_NAMES)) {
390
+ throw new Error(`${modelName} static sync publish operations must be create/update/destroy, got: ${String(operation)}`)
391
+ }
392
+ }
393
+ }
394
+
395
+ return {
396
+ broadcasts,
397
+ modelClass,
398
+ operations: operations === undefined ? DEFAULT_PUBLISHED_OPERATIONS : operations,
399
+ resourceType: resourceType === undefined ? modelName : resourceType,
400
+ scopePlan: scopePlanFor({eventId, modelClass, modelName, scopeAttributes, syncModel, syncScopeAttributes}),
401
+ serialize: serialize === undefined ? defaultSerializedAttributes : serialize
402
+ }
403
+ }
404
+
405
+ /**
406
+ * Derives the scope plan partitioning a published model's changes: one entry
407
+ * per scope attribute declared on the sync model (`static
408
+ * syncScopeAttributes`), each reading the record attribute named like the
409
+ * scope attribute (overridable through the declaration's `scopeAttributes`
410
+ * name map), or the record's own id when the model has no such attribute
411
+ * (scope-root models). The deprecated `eventId` declaration forms map to a
412
+ * fixed `eventId`/`event_id` plan for 1.0.503 compatibility.
413
+ * @param {{eventId: import("./sync-publisher-types.js").SyncPublishDeclarationConfig["eventId"], modelClass: ?, modelName: string, scopeAttributes: Record<string, string> | undefined, syncModel: ?, syncScopeAttributes: string[] | null}} args - Declaration and sync-model scope args.
414
+ * @returns {Array<import("./sync-publisher-types.js").SyncPublisherScopePlanEntry>} Derived scope plan.
415
+ */
416
+ function scopePlanFor({eventId, modelClass, modelName, scopeAttributes, syncModel, syncScopeAttributes}) {
417
+ const attributeNames = Object.values(modelClass.getColumnNameToAttributeNameMap())
418
+
419
+ if (eventId !== undefined) {
420
+ if (scopeAttributes !== undefined) {
421
+ throw new Error(`${modelName} static sync publish can't declare both scopeAttributes and the deprecated eventId form`)
422
+ }
423
+ if (typeof eventId === "function") {
424
+ return [{columnName: "event_id", recordAttribute: null, resolver: eventId, scopeAttribute: "eventId"}]
425
+ }
426
+ if (typeof eventId !== "string") {
427
+ throw new Error(`${modelName} static sync publish eventId must be an attribute-name string (or a deprecated resolver function), got: ${String(eventId)}`)
428
+ }
429
+ if (!attributeNames.includes(eventId)) {
430
+ throw new Error(`${modelName} static sync publish eventId attribute doesn't exist on the model: ${eventId}`)
431
+ }
432
+
433
+ return [{columnName: "event_id", recordAttribute: eventId, resolver: undefined, scopeAttribute: "eventId"}]
434
+ }
435
+
436
+ if (scopeAttributes !== undefined && !syncScopeAttributes) {
437
+ throw new Error(`${modelName} static sync publish declares scopeAttributes but the sync model declares no static syncScopeAttributes`)
438
+ }
439
+
440
+ if (!syncScopeAttributes) return []
441
+
442
+ if (scopeAttributes !== undefined && (typeof scopeAttributes !== "object" || Array.isArray(scopeAttributes))) {
443
+ throw new Error(`${modelName} static sync publish scopeAttributes must be an object mapping scope attributes to record attribute names, got: ${String(scopeAttributes)}`)
444
+ }
445
+
446
+ for (const scopeAttribute of Object.keys(scopeAttributes || {})) {
447
+ if (!syncScopeAttributes.includes(scopeAttribute)) {
448
+ throw new Error(`${modelName} static sync publish scopeAttributes received unknown scope attribute: ${scopeAttribute} (the sync model declares: ${syncScopeAttributes.join(", ")})`)
449
+ }
450
+ }
451
+
452
+ return syncScopeAttributes.map((scopeAttribute) => {
453
+ const declaredRecordAttribute = scopeAttributes?.[scopeAttribute]
454
+
455
+ if (declaredRecordAttribute !== undefined) {
456
+ if (typeof declaredRecordAttribute !== "string" || !attributeNames.includes(declaredRecordAttribute)) {
457
+ throw new Error(`${modelName} static sync publish scopeAttributes.${scopeAttribute} must name an existing record attribute, got: ${String(declaredRecordAttribute)}`)
458
+ }
459
+
460
+ return {columnName: syncScopeColumnName({scopeAttribute, syncModel}), recordAttribute: declaredRecordAttribute, resolver: undefined, scopeAttribute}
461
+ }
462
+
463
+ return {
464
+ columnName: syncScopeColumnName({scopeAttribute, syncModel}),
465
+ recordAttribute: attributeNames.includes(scopeAttribute) ? scopeAttribute : null,
466
+ resolver: undefined,
467
+ scopeAttribute
468
+ }
469
+ })
470
+ }
471
+
472
+ /**
473
+ * Resolves the sync-row column persisting a declared scope attribute.
474
+ * @param {{scopeAttribute: string, syncModel: ?}} args - Scope attribute and sync model.
475
+ * @returns {string} Sync-row column name.
476
+ */
477
+ function syncScopeColumnName({scopeAttribute, syncModel}) {
478
+ const columnName = syncModel.getAttributeNameToColumnNameMap()[scopeAttribute]
479
+
480
+ if (!columnName) {
481
+ throw new Error(`${syncModel.name} declares the sync scope attribute ${scopeAttribute} but has no matching column for it`)
482
+ }
483
+
484
+ return columnName
485
+ }
486
+
487
+ /**
488
+ * Default publish serializer: the record's attributes with Date values
489
+ * serialized to ISO strings.
490
+ * @param {?} record - Mutated server model record.
491
+ * @returns {Record<string, ?>} Serialized attributes payload.
492
+ */
493
+ function defaultSerializedAttributes(record) {
494
+ /** @type {Record<string, ?>} */
495
+ const attributes = {...record.attributes()}
496
+
497
+ for (const [attributeName, value] of Object.entries(attributes)) {
498
+ if (value instanceof Date) attributes[attributeName] = value.toISOString()
499
+ }
500
+
501
+ return attributes
502
+ }
@@ -1,17 +1,22 @@
1
1
  // @ts-check
2
2
 
3
3
  import SyncApiClient from "./sync-api-client.js"
4
+ import {VELOCIOUS_SYNC_CHANNEL} from "./sync-channel-name.js"
4
5
 
5
6
  /** @typedef {import("../configuration-types.js").VelociousSyncRealtimeChannelDescriptor} VelociousSyncRealtimeChannelDescriptor */
6
7
  /** @typedef {import("../configuration-types.js").VelociousSyncRealtimeWebsocketClient} VelociousSyncRealtimeWebsocketClient */
7
8
 
8
9
  /**
9
- * Derived realtime push bridge for the sync client. Subscribes the declared
10
- * websocket channels (config `sync.client.realtime.channels` callback plus
11
- * model-level `static sync = {realtime: {channel}}` declarations), applies
12
- * pushed changes through the same derived resource applier as pulls (with echo
13
- * suppression against tracked re-queueing), drops own-device messages by echo
14
- * origin, and fires a coalesced `pull()` when subscriptions become ready or
10
+ * Derived realtime push bridge for the sync client. Subscribes every declared
11
+ * pull scope to the framework sync channel ({@link VELOCIOUS_SYNC_CHANNEL})
12
+ * automatically the subscribe params mirror the scope's
13
+ * `{resourceType, conditions}` so the server authorizes them through the same
14
+ * sync resource authorization as pulls plus any deprecated legacy channels
15
+ * (config `sync.client.realtime.channels` callback and model-level
16
+ * `static sync = {realtime: {channel}}` declarations). Pushed changes apply
17
+ * through the same derived resource applier as pulls (with echo suppression
18
+ * against tracked re-queueing), own-device messages are dropped by echo
19
+ * origin, and a coalesced `pull()` fires when subscriptions become ready or
15
20
  * resume so offline gaps close.
16
21
  */
17
22
  export default class SyncRealtimeBridge {
@@ -41,7 +46,7 @@ export default class SyncRealtimeBridge {
41
46
  * Subscribes the derived realtime channels (idempotent and single-flighted):
42
47
  * an active subscription is kept as-is and a concurrent subscribe awaits the
43
48
  * in-flight attempt. Call `unsubscribe()` first to change the context.
44
- * @param {?} [context] - App context passed to the `sync.client.realtime.channels` callback (runtime values like eventId).
49
+ * @param {?} [context] - App context passed to the deprecated `sync.client.realtime.channels` callback (runtime scope values).
45
50
  * @returns {Promise<void>}
46
51
  */
47
52
  async subscribe(context) {
@@ -204,9 +209,12 @@ export default class SyncRealtimeBridge {
204
209
  }
205
210
 
206
211
  /**
207
- * Derives the channel descriptors to subscribe: model-level static realtime
208
- * declarations plus the config channels callback, failing loudly when none exist.
209
- * @param {?} context - App context passed to the channels callback.
212
+ * Derives the channel descriptors to subscribe: one framework sync channel
213
+ * subscription per declared pull scope (the params mirror the scope's
214
+ * `{resourceType, conditions}`), plus the deprecated legacy paths
215
+ * model-level static realtime declarations and the config channels callback.
216
+ * Fails loudly when nothing is subscribable.
217
+ * @param {?} context - App context passed to the deprecated channels callback.
210
218
  * @returns {Promise<Array<VelociousSyncRealtimeChannelDescriptor>>} Channel descriptors.
211
219
  */
212
220
  async channelDescriptors(context) {
@@ -214,6 +222,13 @@ export default class SyncRealtimeBridge {
214
222
  /** @type {Array<VelociousSyncRealtimeChannelDescriptor>} */
215
223
  const channelDescriptors = []
216
224
 
225
+ for (const scopeRow of await this.syncClient.scopeStore().activeScopes()) {
226
+ channelDescriptors.push({
227
+ channel: VELOCIOUS_SYNC_CHANNEL,
228
+ params: {conditions: this.attributeNamedConditions(scopeRow), resourceType: scopeRow.resourceType}
229
+ })
230
+ }
231
+
217
232
  for (const [resourceType, resourceConfig] of Object.entries(this.syncClient.config.resources)) {
218
233
  if (!resourceConfig.realtime) continue
219
234
 
@@ -225,12 +240,42 @@ export default class SyncRealtimeBridge {
225
240
  }
226
241
 
227
242
  if (channelDescriptors.length === 0) {
228
- throw new Error("subscribeRealtime found no realtime channels - declare sync.client.realtime.channels or static sync = {realtime: {channel}} on a model")
243
+ throw new Error("subscribeRealtime found no channels to subscribe - declare a sync scope (syncClient().sync(query)) so its framework sync channel subscription can be derived, or the deprecated sync.client.realtime.channels callback")
229
244
  }
230
245
 
231
246
  return channelDescriptors
232
247
  }
233
248
 
249
+ /**
250
+ * Translates a persisted scope's condition keys to the model's attribute
251
+ * names so the framework channel subscription matches the publisher's
252
+ * attribute-named scoping params: `serializedScopeFromQuery` persists the
253
+ * query's model-normalized column names (for example `project_id`), while
254
+ * scope-partition broadcasts carry attribute names (`projectId`). Keys
255
+ * without a column mapping are already attribute names and pass through;
256
+ * scopes on models without a declared sync resource fail loudly because no
257
+ * attribute mapping exists for them.
258
+ * @param {{conditions: Record<string, ?>, resourceType: string}} scopeRow - Active scope row.
259
+ * @returns {Record<string, ?>} Attribute-named scope conditions.
260
+ */
261
+ attributeNamedConditions(scopeRow) {
262
+ const resourceConfig = this.syncClient.config.resources[scopeRow.resourceType]
263
+
264
+ if (!resourceConfig) {
265
+ throw new Error(`subscribeRealtime can't derive attribute names for the sync scope declared on ${scopeRow.resourceType} - declare static sync on that model so its resource is registered`)
266
+ }
267
+
268
+ const columnNameToAttributeName = resourceConfig.modelClass.getColumnNameToAttributeNameMap()
269
+ /** @type {Record<string, ?>} */
270
+ const conditions = {}
271
+
272
+ for (const [conditionName, conditionValue] of Object.entries(scopeRow.conditions)) {
273
+ conditions[columnNameToAttributeName[conditionName] || conditionName] = conditionValue
274
+ }
275
+
276
+ return conditions
277
+ }
278
+
234
279
  /**
235
280
  * Chains one pushed message onto the serialized apply queue so changes apply
236
281
  * in arrival order; failures go to the sync client's error reporting.
@@ -0,0 +1,26 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * Reads and validates a sync/change model's declared scope-partition
5
+ * attributes (`static syncScopeAttributes`).
6
+ *
7
+ * The declaration names the attribute(s) partitioning the app's sync feed —
8
+ * the same attribute names client pull scopes use as conditions (for example
9
+ * `["eventId"]` or `["accountId"]`). The publisher persists them onto every
10
+ * published sync row and broadcasts them as the framework sync channel's
11
+ * scoping params, and the change feed serializes them onto every changes row
12
+ * under their own names. Velocious itself has no built-in partition name.
13
+ * @param {?} syncModel - Sync/change model class.
14
+ * @returns {string[] | null} Declared scope attributes, or null when the model declares none.
15
+ */
16
+ export function declaredSyncScopeAttributes(syncModel) {
17
+ const declared = syncModel.syncScopeAttributes
18
+
19
+ if (declared === undefined || declared === null) return null
20
+
21
+ if (!Array.isArray(declared) || declared.length === 0 || declared.some((attributeName) => typeof attributeName !== "string" || !attributeName)) {
22
+ throw new Error(`${syncModel.name} static syncScopeAttributes must be a non-empty array of attribute-name strings, got: ${String(declared)}`)
23
+ }
24
+
25
+ return declared
26
+ }