@livestore/common 0.0.56-dev.0 → 0.0.56-dev.1

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 (44) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/adapter-types.d.ts +9 -12
  3. package/dist/adapter-types.d.ts.map +1 -1
  4. package/dist/adapter-types.js +2 -2
  5. package/dist/adapter-types.js.map +1 -1
  6. package/dist/devtools/devtools-bridge.d.ts +12 -0
  7. package/dist/devtools/devtools-bridge.d.ts.map +1 -0
  8. package/dist/devtools/devtools-bridge.js +2 -0
  9. package/dist/devtools/devtools-bridge.js.map +1 -0
  10. package/dist/devtools/devtools-messages.d.ts +8 -8
  11. package/dist/devtools/index.d.ts +3 -2
  12. package/dist/devtools/index.d.ts.map +1 -1
  13. package/dist/devtools/index.js +3 -2
  14. package/dist/devtools/index.js.map +1 -1
  15. package/dist/init-singleton-tables.d.ts +2 -2
  16. package/dist/init-singleton-tables.d.ts.map +1 -1
  17. package/dist/init-singleton-tables.js.map +1 -1
  18. package/dist/mutation.d.ts +2 -0
  19. package/dist/mutation.d.ts.map +1 -1
  20. package/dist/mutation.js +14 -0
  21. package/dist/mutation.js.map +1 -1
  22. package/dist/rehydrate-from-mutationlog.d.ts +3 -3
  23. package/dist/rehydrate-from-mutationlog.d.ts.map +1 -1
  24. package/dist/rehydrate-from-mutationlog.js.map +1 -1
  25. package/dist/schema/mutations.d.ts +1 -1
  26. package/dist/schema-management/common.d.ts +3 -3
  27. package/dist/schema-management/common.d.ts.map +1 -1
  28. package/dist/schema-management/common.js.map +1 -1
  29. package/dist/schema-management/migrations.d.ts +4 -4
  30. package/dist/schema-management/migrations.d.ts.map +1 -1
  31. package/dist/schema-management/migrations.js.map +1 -1
  32. package/package.json +3 -3
  33. package/src/adapter-types.ts +9 -10
  34. package/src/devtools/devtools-bridge.ts +13 -0
  35. package/src/devtools/index.ts +4 -3
  36. package/src/init-singleton-tables.ts +2 -2
  37. package/src/mutation.ts +19 -0
  38. package/src/rehydrate-from-mutationlog.ts +3 -3
  39. package/src/schema-management/common.ts +3 -3
  40. package/src/schema-management/migrations.ts +4 -4
  41. package/dist/schema/parse-utils.d.ts +0 -4
  42. package/dist/schema/parse-utils.d.ts.map +0 -1
  43. package/dist/schema/parse-utils.js +0 -22
  44. package/dist/schema/parse-utils.js.map +0 -1
@@ -0,0 +1,13 @@
1
+ import type { Effect, PubSub } from '@livestore/utils/effect'
2
+
3
+ import type * as Devtools from './devtools-messages.js'
4
+
5
+ export type PrepareDevtoolsBridge = {
6
+ /** Messages coming from the app host (usually responses to requests) */
7
+ responsePubSub: PubSub.PubSub<Devtools.MessageFromAppHostCoordinator | Devtools.MessageFromAppHostStore>
8
+ sendToAppHost: (msg: Devtools.MessageToAppHostCoordinator | Devtools.MessageToAppHostStore) => Effect.Effect<void>
9
+ channelId: string
10
+ copyToClipboard: (text: string) => Effect.Effect<void>
11
+ sendEscapeKey?: Effect.Effect<void>
12
+ isLeaderTab: boolean
13
+ }
@@ -1,8 +1,9 @@
1
1
  import type { Effect, Scope } from '@livestore/utils/effect'
2
- import { BrowserChannel, Schema } from '@livestore/utils/effect'
2
+ import { Schema, WebChannel } from '@livestore/utils/effect'
3
3
 
4
4
  export * from './devtools-messages.js'
5
5
  export * from './devtools-window-message.js'
6
+ export * from './devtools-bridge.js'
6
7
 
7
8
  export namespace WebBridge {
8
9
  export class AppHostReady extends Schema.TaggedStruct('LSD.WebBridge.AppHostReady', {
@@ -37,8 +38,8 @@ export namespace WebBridge {
37
38
 
38
39
  export const makeBroadcastChannel = (
39
40
  key?: string,
40
- ): Effect.Effect<BrowserChannel.BrowserChannel<typeof All.Type, typeof All.Type>, never, Scope.Scope> =>
41
- BrowserChannel.broadcastChannel({
41
+ ): Effect.Effect<WebChannel.WebChannel<typeof All.Type, typeof All.Type>, never, Scope.Scope> =>
42
+ WebChannel.broadcastChannel({
42
43
  channelName: `livestore-web-bridge-devtools${key ? `-${key}` : ''}`,
43
44
  listenSchema: All,
44
45
  sendSchema: All,
@@ -1,9 +1,9 @@
1
- import type { InMemoryDatabase } from './adapter-types.js'
1
+ import type { SynchronousDatabase } from './adapter-types.js'
2
2
  import type { LiveStoreSchema } from './schema/index.js'
3
3
  import { getDefaultValuesEncoded } from './schema/schema-helpers.js'
4
4
  import { prepareBindValues, sql } from './util.js'
5
5
 
6
- export const initializeSingletonTables = (schema: LiveStoreSchema, db: InMemoryDatabase) => {
6
+ export const initializeSingletonTables = (schema: LiveStoreSchema, db: SynchronousDatabase) => {
7
7
  for (const [, tableDef] of schema.tables) {
8
8
  if (tableDef.options.isSingleton) {
9
9
  const defaultValues = getDefaultValuesEncoded(tableDef, undefined)
package/src/mutation.ts CHANGED
@@ -1,5 +1,7 @@
1
+ import { memoizeByRef, shouldNeverHappen } from '@livestore/utils'
1
2
  import { Schema } from '@livestore/utils/effect'
2
3
 
4
+ import type { LiveStoreSchema } from './schema/index.js'
3
5
  import type { MutationDef, MutationEvent } from './schema/mutations.js'
4
6
  import type { PreparedBindValues } from './util.js'
5
7
  import { prepareBindValues } from './util.js'
@@ -48,3 +50,20 @@ export const getExecArgsFromMutation = ({
48
50
  return { statementSql, bindValues: prepareBindValues(bindValues ?? {}, statementSql), writeTables }
49
51
  })
50
52
  }
53
+
54
+ export const makeShouldExcludeMutationFromLog = memoizeByRef((schema: LiveStoreSchema) => {
55
+ const migrationOptions = schema.migrationOptions
56
+ const mutationLogExclude =
57
+ migrationOptions.strategy === 'from-mutation-log'
58
+ ? (migrationOptions.excludeMutations ?? new Set(['livestore.RawSql']))
59
+ : new Set(['livestore.RawSql'])
60
+
61
+ return (mutationName: string, mutationEventDecoded: MutationEvent.Any): boolean => {
62
+ if (mutationLogExclude.has(mutationName)) return true
63
+
64
+ const mutationDef = schema.mutations.get(mutationName) ?? shouldNeverHappen(`Unknown mutation: ${mutationName}`)
65
+ const execArgsArr = getExecArgsFromMutation({ mutationDef, mutationEventDecoded })
66
+
67
+ return execArgsArr.some((_) => _.statementSql.includes('__livestore'))
68
+ }
69
+ })
@@ -2,9 +2,9 @@ import { memoizeByRef, shouldNeverHappen } from '@livestore/utils'
2
2
  import { Chunk, Effect, Option, Schema, Stream } from '@livestore/utils/effect'
3
3
 
4
4
  import {
5
- type InMemoryDatabase,
6
5
  type MigrationOptionsFromMutationLog,
7
6
  SqliteError,
7
+ type SynchronousDatabase,
8
8
  UnexpectedError,
9
9
  } from './adapter-types.js'
10
10
  import { getExecArgsFromMutation } from './mutation.js'
@@ -20,8 +20,8 @@ export const rehydrateFromMutationLog = ({
20
20
  migrationOptions,
21
21
  onProgress,
22
22
  }: {
23
- logDb: InMemoryDatabase
24
- db: InMemoryDatabase
23
+ logDb: SynchronousDatabase
24
+ db: SynchronousDatabase
25
25
  schema: LiveStoreSchema
26
26
  migrationOptions: MigrationOptionsFromMutationLog
27
27
  onProgress: (_: { done: number; total: number }) => Effect.Effect<void>
@@ -1,4 +1,4 @@
1
- import type { InMemoryDatabase } from '../adapter-types.js'
1
+ import type { SynchronousDatabase } from '../adapter-types.js'
2
2
  import type { ParamsObject } from '../util.js'
3
3
  import { prepareBindValues } from '../util.js'
4
4
 
@@ -6,7 +6,7 @@ import { prepareBindValues } from '../util.js'
6
6
  // will require proper scope-aware cleanup etc (for testing and apps with multiple LiveStore instances)
7
7
  // const cachedStmts = new Map<string, PreparedStatement>()
8
8
 
9
- export const dbExecute = (db: InMemoryDatabase, queryStr: string, bindValues?: ParamsObject) => {
9
+ export const dbExecute = (db: SynchronousDatabase, queryStr: string, bindValues?: ParamsObject) => {
10
10
  // let stmt = cachedStmts.get(queryStr)
11
11
  // if (!stmt) {
12
12
  const stmt = db.prepare(queryStr)
@@ -18,7 +18,7 @@ export const dbExecute = (db: InMemoryDatabase, queryStr: string, bindValues?: P
18
18
  stmt.execute(preparedBindValues)
19
19
  }
20
20
 
21
- export const dbSelect = <T>(db: InMemoryDatabase, queryStr: string, bindValues?: ParamsObject) => {
21
+ export const dbSelect = <T>(db: SynchronousDatabase, queryStr: string, bindValues?: ParamsObject) => {
22
22
  // let stmt = cachedStmts.get(queryStr)
23
23
  // if (!stmt) {
24
24
  const stmt = db.prepare(queryStr)
@@ -2,7 +2,7 @@ import { memoizeByStringifyArgs } from '@livestore/utils'
2
2
  import { Effect, Schema as EffectSchema } from '@livestore/utils/effect'
3
3
  import { SqliteAst, SqliteDsl } from 'effect-db-schema'
4
4
 
5
- import type { InMemoryDatabase } from '../adapter-types.js'
5
+ import type { SynchronousDatabase } from '../adapter-types.js'
6
6
  import type { LiveStoreSchema } from '../schema/index.js'
7
7
  import type { SchemaMetaRow, SchemaMutationsMetaRow } from '../schema/system-tables.js'
8
8
  import {
@@ -19,7 +19,7 @@ import { validateSchema } from './validate-mutation-defs.js'
19
19
 
20
20
  const getMemoizedTimestamp = memoizeByStringifyArgs(() => new Date().toISOString())
21
21
 
22
- export const makeSchemaManager = (db: InMemoryDatabase): Effect.Effect<SchemaManager> =>
22
+ export const makeSchemaManager = (db: SynchronousDatabase): Effect.Effect<SchemaManager> =>
23
23
  Effect.gen(function* () {
24
24
  yield* migrateTable({
25
25
  db,
@@ -56,7 +56,7 @@ export const migrateDb = ({
56
56
  schema,
57
57
  onProgress,
58
58
  }: {
59
- db: InMemoryDatabase
59
+ db: SynchronousDatabase
60
60
  schema: LiveStoreSchema
61
61
  onProgress?: (opts: { done: number; total: number }) => Effect.Effect<void>
62
62
  }) =>
@@ -121,7 +121,7 @@ export const migrateTable = ({
121
121
  behaviour,
122
122
  skipMetaTable = false,
123
123
  }: {
124
- db: InMemoryDatabase
124
+ db: SynchronousDatabase
125
125
  tableAst: SqliteAst.Table
126
126
  schemaHash?: number
127
127
  behaviour: 'drop-and-recreate' | 'create-if-not-exists'
@@ -1,4 +0,0 @@
1
- import { type FromColumns, type FromTable, type TableDef } from './table-def.js';
2
- export declare const many: <TTableDef extends TableDef>(table: TTableDef) => ((rawRows: ReadonlyArray<any>) => ReadonlyArray<FromTable.RowDecoded<TTableDef>>);
3
- export declare const first: <TTableDef extends TableDef>(table: TTableDef, fallback?: FromColumns.InsertRowDecoded<TTableDef["sqliteDef"]["columns"]>) => (rawRows: ReadonlyArray<any>) => any;
4
- //# sourceMappingURL=parse-utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"parse-utils.d.ts","sourceRoot":"","sources":["../../src/schema/parse-utils.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,KAAK,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAEhF,eAAO,MAAM,IAAI,GAAI,SAAS,SAAS,QAAQ,SACtC,SAAS,KACf,CAAC,CAAC,OAAO,EAAE,aAAa,CAAC,GAAG,CAAC,KAAK,aAAa,CAAC,SAAS,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAElF,CAAA;AAED,eAAO,MAAM,KAAK,GACf,SAAS,SAAS,QAAQ,SAClB,SAAS,aACL,WAAW,CAAC,gBAAgB,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,SAAS,CAAC,CAAC,eAElE,aAAa,CAAC,GAAG,CAAC,QAuB3B,CAAA"}
@@ -1,22 +0,0 @@
1
- import { shouldNeverHappen } from '@livestore/utils';
2
- import { pipe, ReadonlyRecord, Schema, TreeFormatter } from '@livestore/utils/effect';
3
- import { getDefaultValuesDecoded } from './schema-helpers.js';
4
- export const many = (table) => {
5
- return Schema.decodeSync(Schema.Array(table.schema));
6
- };
7
- export const first = (table, fallback) => (rawRows) => {
8
- const rows = Schema.decodeSync(Schema.Array(table.schema))(rawRows);
9
- if (rows.length === 0) {
10
- const schemaDefaultValues = getDefaultValuesDecoded(table);
11
- const defaultValuesResult = pipe(table.sqliteDef.columns, ReadonlyRecord.map((_column, columnName) => fallback?.[columnName] ?? schemaDefaultValues[columnName]), Schema.validateEither(table.schema));
12
- if (defaultValuesResult._tag === 'Right') {
13
- return defaultValuesResult.right;
14
- }
15
- else {
16
- console.error('decode error', TreeFormatter.formatErrorSync(defaultValuesResult.left));
17
- return shouldNeverHappen(`Expected query (for table ${table.sqliteDef.name}) to return at least one result but found none. Also can't fallback to default values as some were not provided.`);
18
- }
19
- }
20
- return rows[0];
21
- };
22
- //# sourceMappingURL=parse-utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"parse-utils.js","sourceRoot":"","sources":["../../src/schema/parse-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAEpD,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAGrF,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAA;AAG7D,MAAM,CAAC,MAAM,IAAI,GAAG,CAClB,KAAgB,EACmE,EAAE;IACrF,OAAO,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAS,CAAA;AAC9D,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,KAAK,GAChB,CACE,KAAgB,EAChB,QAA0E,EAC1E,EAAE,CACJ,CAAC,OAA2B,EAAE,EAAE;IAC9B,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;IAEnE,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtB,MAAM,mBAAmB,GAAG,uBAAuB,CAAC,KAAK,CAAC,CAAA;QAE1D,MAAM,mBAAmB,GAAG,IAAI,CAC9B,KAAK,CAAC,SAAS,CAAC,OAAO,EACvB,cAAc,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,UAAU,EAAE,EAAE,CAAE,QAAgB,EAAE,CAAC,UAAU,CAAC,IAAI,mBAAmB,CAAC,UAAU,CAAC,CAAC,EAC/G,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,MAAM,CAAC,CACpC,CAAA;QAED,IAAI,mBAAmB,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;YACzC,OAAO,mBAAmB,CAAC,KAAK,CAAA;QAClC,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KAAK,CAAC,cAAc,EAAE,aAAa,CAAC,eAAe,CAAC,mBAAmB,CAAC,IAAI,CAAC,CAAC,CAAA;YACtF,OAAO,iBAAiB,CACtB,6BAA6B,KAAK,CAAC,SAAS,CAAC,IAAI,kHAAkH,CACpK,CAAA;QACH,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC,CAAC,CAAE,CAAA;AACjB,CAAC,CAAA"}