@livestore/common 0.0.0-snapshot-f0abb7780ec63d606e59654d2c8a98d4a7f53569 → 0.0.0-snapshot-2411da6706c12365b5aa4533b551b9b6554d4617

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 (38) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/devtools/devtools-messages-client-session.d.ts +21 -21
  3. package/dist/devtools/devtools-messages-common.d.ts +6 -6
  4. package/dist/devtools/devtools-messages-leader.d.ts +28 -28
  5. package/dist/leader-thread/LeaderSyncProcessor.d.ts.map +1 -1
  6. package/dist/leader-thread/LeaderSyncProcessor.js +6 -7
  7. package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -1
  8. package/dist/leader-thread/apply-mutation.d.ts.map +1 -1
  9. package/dist/leader-thread/apply-mutation.js +4 -4
  10. package/dist/leader-thread/apply-mutation.js.map +1 -1
  11. package/dist/rehydrate-from-mutationlog.js +3 -3
  12. package/dist/rehydrate-from-mutationlog.js.map +1 -1
  13. package/dist/schema/MutationEvent.js +2 -2
  14. package/dist/schema/MutationEvent.js.map +1 -1
  15. package/dist/schema/mutations.d.ts +4 -1
  16. package/dist/schema/mutations.d.ts.map +1 -1
  17. package/dist/schema/mutations.js.map +1 -1
  18. package/dist/schema/schema.d.ts +1 -0
  19. package/dist/schema/schema.d.ts.map +1 -1
  20. package/dist/schema/schema.js +19 -8
  21. package/dist/schema/schema.js.map +1 -1
  22. package/dist/schema-management/validate-mutation-defs.js +2 -2
  23. package/dist/schema-management/validate-mutation-defs.js.map +1 -1
  24. package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -1
  25. package/dist/sync/ClientSessionSyncProcessor.js +3 -5
  26. package/dist/sync/ClientSessionSyncProcessor.js.map +1 -1
  27. package/dist/version.d.ts +1 -1
  28. package/dist/version.js +1 -1
  29. package/package.json +2 -2
  30. package/src/leader-thread/LeaderSyncProcessor.ts +7 -9
  31. package/src/leader-thread/apply-mutation.ts +4 -3
  32. package/src/rehydrate-from-mutationlog.ts +2 -2
  33. package/src/schema/MutationEvent.ts +2 -2
  34. package/src/schema/mutations.ts +4 -1
  35. package/src/schema/schema.ts +20 -8
  36. package/src/schema-management/validate-mutation-defs.ts +2 -2
  37. package/src/sync/ClientSessionSyncProcessor.ts +4 -6
  38. package/src/version.ts +1 -1
@@ -72,29 +72,32 @@ export const makeSchema = <TInputSchema extends InputSchema>(
72
72
  tables.set(tableDef.sqliteDef.name, tableDef)
73
73
  }
74
74
 
75
- const mutations: MutationDefMap = new Map()
75
+ const mutations: MutationDefMap = {
76
+ map: new Map(),
77
+ wasProvided: inputSchema.mutations !== undefined,
78
+ }
76
79
 
77
80
  if (isReadonlyArray(inputSchema.mutations)) {
78
81
  for (const mutation of inputSchema.mutations) {
79
- mutations.set(mutation.name, mutation)
82
+ mutations.map.set(mutation.name, mutation)
80
83
  }
81
84
  } else {
82
85
  for (const mutation of Object.values(inputSchema.mutations ?? {})) {
83
- if (mutations.has(mutation.name)) {
86
+ if (mutations.map.has(mutation.name)) {
84
87
  shouldNeverHappen(`Duplicate mutation name: ${mutation.name}. Please use unique names for mutations.`)
85
88
  }
86
- mutations.set(mutation.name, mutation)
89
+ mutations.map.set(mutation.name, mutation)
87
90
  }
88
91
  }
89
92
 
90
- mutations.set(rawSqlMutation.name, rawSqlMutation)
93
+ mutations.map.set(rawSqlMutation.name, rawSqlMutation)
91
94
 
92
95
  for (const tableDef of tables.values()) {
93
96
  if (tableHasDerivedMutations(tableDef)) {
94
97
  const derivedMutationDefs = makeDerivedMutationDefsForTable(tableDef)
95
- mutations.set(derivedMutationDefs.insert.name, derivedMutationDefs.insert)
96
- mutations.set(derivedMutationDefs.update.name, derivedMutationDefs.update)
97
- mutations.set(derivedMutationDefs.delete.name, derivedMutationDefs.delete)
98
+ mutations.map.set(derivedMutationDefs.insert.name, derivedMutationDefs.insert)
99
+ mutations.map.set(derivedMutationDefs.update.name, derivedMutationDefs.update)
100
+ mutations.map.set(derivedMutationDefs.delete.name, derivedMutationDefs.delete)
98
101
  }
99
102
  }
100
103
 
@@ -114,6 +117,15 @@ export const makeSchema = <TInputSchema extends InputSchema>(
114
117
  } satisfies LiveStoreSchema
115
118
  }
116
119
 
120
+ export const getMutationDef = <TSchema extends LiveStoreSchema>(schema: TSchema, mutationName: string) => {
121
+ const mutationDef = schema.mutations.map.get(mutationName)
122
+ if (mutationDef === undefined) {
123
+ const extraInfo = schema.mutations.wasProvided ? '' : ' Please provide \`mutations\` in the schema options.'
124
+ return shouldNeverHappen(`No mutation definition found for \`${mutationName}\`.${extraInfo}`)
125
+ }
126
+ return mutationDef
127
+ }
128
+
117
129
  export namespace FromInputSchema {
118
130
  export type DeriveSchema<TInputSchema extends InputSchema> = LiveStoreSchema<
119
131
  DbSchemaFromInputSchemaTables<TInputSchema['tables']>,
@@ -11,7 +11,7 @@ export const validateSchema = (schema: LiveStoreSchema, schemaManager: SchemaMan
11
11
  const registeredMutationDefInfos = schemaManager.getMutationDefInfos()
12
12
 
13
13
  const missingMutationDefs = registeredMutationDefInfos.filter(
14
- (registeredMutationDefInfo) => !schema.mutations.has(registeredMutationDefInfo.mutationName),
14
+ (registeredMutationDefInfo) => !schema.mutations.map.has(registeredMutationDefInfo.mutationName),
15
15
  )
16
16
 
17
17
  if (missingMutationDefs.length > 0) {
@@ -20,7 +20,7 @@ export const validateSchema = (schema: LiveStoreSchema, schemaManager: SchemaMan
20
20
  })
21
21
  }
22
22
 
23
- for (const [, mutationDef] of schema.mutations) {
23
+ for (const [, mutationDef] of schema.mutations.map) {
24
24
  const registeredMutationDefInfo = registeredMutationDefInfos.find(
25
25
  (info) => info.mutationName === mutationDef.name,
26
26
  )
@@ -5,7 +5,7 @@ import * as otel from '@opentelemetry/api'
5
5
 
6
6
  import type { ClientSession, UnexpectedError } from '../adapter-types.js'
7
7
  import * as EventId from '../schema/EventId.js'
8
- import { type LiveStoreSchema } from '../schema/mod.js'
8
+ import { getMutationDef, type LiveStoreSchema } from '../schema/mod.js'
9
9
  import * as MutationEvent from '../schema/MutationEvent.js'
10
10
  import * as SyncState from './syncstate.js'
11
11
 
@@ -58,10 +58,8 @@ export const makeClientSessionSyncProcessor = ({
58
58
  }
59
59
 
60
60
  const syncStateUpdateQueue = Queue.unbounded<SyncState.SyncState>().pipe(Effect.runSync)
61
- const isLocalEvent = (mutationEventEncoded: MutationEvent.EncodedWithMeta) => {
62
- const mutationDef = schema.mutations.get(mutationEventEncoded.mutation)!
63
- return mutationDef.options.clientOnly
64
- }
61
+ const isLocalEvent = (mutationEventEncoded: MutationEvent.EncodedWithMeta) =>
62
+ getMutationDef(schema, mutationEventEncoded.mutation).options.clientOnly
65
63
 
66
64
  /** We're queuing push requests to reduce the number of messages sent to the leader by batching them */
67
65
  const leaderPushQueue = BucketQueue.make<MutationEvent.EncodedWithMeta>().pipe(Effect.runSync)
@@ -71,7 +69,7 @@ export const makeClientSessionSyncProcessor = ({
71
69
 
72
70
  let baseEventId = syncStateRef.current.localHead
73
71
  const encodedMutationEvents = batch.map((mutationEvent) => {
74
- const mutationDef = schema.mutations.get(mutationEvent.mutation)!
72
+ const mutationDef = getMutationDef(schema, mutationEvent.mutation)
75
73
  const nextIdPair = EventId.nextPair(baseEventId, mutationDef.options.clientOnly)
76
74
  baseEventId = nextIdPair.id
77
75
  return new MutationEvent.EncodedWithMeta(
package/src/version.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // import packageJson from '../package.json' with { type: 'json' }
3
3
  // export const liveStoreVersion = packageJson.version
4
4
 
5
- export const liveStoreVersion = '0.3.0-dev.16' as const
5
+ export const liveStoreVersion = '0.3.0-dev.17' as const
6
6
 
7
7
  /**
8
8
  * This version number is incremented whenever the internal storage format changes in a breaking way.