@livestore/common 0.3.2-dev.9 → 0.4.0-dev.0

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 (162) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/ClientSessionLeaderThreadProxy.d.ts +2 -2
  3. package/dist/ClientSessionLeaderThreadProxy.d.ts.map +1 -1
  4. package/dist/adapter-types.d.ts +4 -4
  5. package/dist/adapter-types.d.ts.map +1 -1
  6. package/dist/debug-info.d.ts +17 -17
  7. package/dist/devtools/devtools-messages-client-session.d.ts +38 -38
  8. package/dist/devtools/devtools-messages-common.d.ts +6 -6
  9. package/dist/devtools/devtools-messages-leader.d.ts +28 -28
  10. package/dist/devtools/devtools-messages-leader.d.ts.map +1 -1
  11. package/dist/devtools/devtools-messages-leader.js.map +1 -1
  12. package/dist/leader-thread/LeaderSyncProcessor.js +3 -1
  13. package/dist/leader-thread/LeaderSyncProcessor.js.map +1 -1
  14. package/dist/leader-thread/make-leader-thread-layer.d.ts.map +1 -1
  15. package/dist/leader-thread/make-leader-thread-layer.js +21 -4
  16. package/dist/leader-thread/make-leader-thread-layer.js.map +1 -1
  17. package/dist/leader-thread/shutdown-channel.d.ts +2 -2
  18. package/dist/leader-thread/shutdown-channel.d.ts.map +1 -1
  19. package/dist/leader-thread/shutdown-channel.js +2 -2
  20. package/dist/leader-thread/shutdown-channel.js.map +1 -1
  21. package/dist/leader-thread/types.d.ts +1 -1
  22. package/dist/leader-thread/types.d.ts.map +1 -1
  23. package/dist/materializer-helper.d.ts +3 -3
  24. package/dist/materializer-helper.d.ts.map +1 -1
  25. package/dist/materializer-helper.js +2 -2
  26. package/dist/materializer-helper.js.map +1 -1
  27. package/dist/rematerialize-from-eventlog.js +1 -1
  28. package/dist/rematerialize-from-eventlog.js.map +1 -1
  29. package/dist/schema/EventDef.d.ts +104 -178
  30. package/dist/schema/EventSequenceNumber.d.ts +5 -0
  31. package/dist/schema/EventSequenceNumber.d.ts.map +1 -1
  32. package/dist/schema/EventSequenceNumber.js +7 -2
  33. package/dist/schema/EventSequenceNumber.js.map +1 -1
  34. package/dist/schema/EventSequenceNumber.test.js +2 -2
  35. package/dist/schema/LiveStoreEvent.d.ts +6 -5
  36. package/dist/schema/LiveStoreEvent.d.ts.map +1 -1
  37. package/dist/schema/LiveStoreEvent.js +5 -0
  38. package/dist/schema/LiveStoreEvent.js.map +1 -1
  39. package/dist/schema/schema.d.ts +3 -0
  40. package/dist/schema/schema.d.ts.map +1 -1
  41. package/dist/schema/schema.js.map +1 -1
  42. package/dist/schema/state/sqlite/client-document-def.d.ts +3 -2
  43. package/dist/schema/state/sqlite/client-document-def.d.ts.map +1 -1
  44. package/dist/schema/state/sqlite/client-document-def.js +6 -4
  45. package/dist/schema/state/sqlite/client-document-def.js.map +1 -1
  46. package/dist/schema/state/sqlite/client-document-def.test.js +76 -1
  47. package/dist/schema/state/sqlite/client-document-def.test.js.map +1 -1
  48. package/dist/schema/state/sqlite/column-annotations.d.ts +34 -0
  49. package/dist/schema/state/sqlite/column-annotations.d.ts.map +1 -0
  50. package/dist/schema/state/sqlite/column-annotations.js +50 -0
  51. package/dist/schema/state/sqlite/column-annotations.js.map +1 -0
  52. package/dist/schema/state/sqlite/column-annotations.test.d.ts +2 -0
  53. package/dist/schema/state/sqlite/column-annotations.test.d.ts.map +1 -0
  54. package/dist/schema/state/sqlite/column-annotations.test.js +179 -0
  55. package/dist/schema/state/sqlite/column-annotations.test.js.map +1 -0
  56. package/dist/schema/state/sqlite/column-spec.d.ts +11 -0
  57. package/dist/schema/state/sqlite/column-spec.d.ts.map +1 -0
  58. package/dist/schema/state/sqlite/column-spec.js +39 -0
  59. package/dist/schema/state/sqlite/column-spec.js.map +1 -0
  60. package/dist/schema/state/sqlite/column-spec.test.d.ts +2 -0
  61. package/dist/schema/state/sqlite/column-spec.test.d.ts.map +1 -0
  62. package/dist/schema/state/sqlite/column-spec.test.js +146 -0
  63. package/dist/schema/state/sqlite/column-spec.test.js.map +1 -0
  64. package/dist/schema/state/sqlite/db-schema/ast/sqlite.d.ts +1 -0
  65. package/dist/schema/state/sqlite/db-schema/ast/sqlite.d.ts.map +1 -1
  66. package/dist/schema/state/sqlite/db-schema/ast/sqlite.js +1 -0
  67. package/dist/schema/state/sqlite/db-schema/ast/sqlite.js.map +1 -1
  68. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.d.ts +17 -4
  69. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.d.ts.map +1 -1
  70. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.js +2 -0
  71. package/dist/schema/state/sqlite/db-schema/dsl/field-defs.js.map +1 -1
  72. package/dist/schema/state/sqlite/db-schema/dsl/mod.d.ts +65 -165
  73. package/dist/schema/state/sqlite/db-schema/dsl/mod.d.ts.map +1 -1
  74. package/dist/schema/state/sqlite/db-schema/dsl/mod.js +1 -0
  75. package/dist/schema/state/sqlite/db-schema/dsl/mod.js.map +1 -1
  76. package/dist/schema/state/sqlite/mod.d.ts +2 -0
  77. package/dist/schema/state/sqlite/mod.d.ts.map +1 -1
  78. package/dist/schema/state/sqlite/mod.js +2 -0
  79. package/dist/schema/state/sqlite/mod.js.map +1 -1
  80. package/dist/schema/state/sqlite/query-builder/api.d.ts +309 -560
  81. package/dist/schema/state/sqlite/query-builder/api.d.ts.map +1 -1
  82. package/dist/schema/state/sqlite/query-builder/astToSql.d.ts +1 -0
  83. package/dist/schema/state/sqlite/query-builder/astToSql.d.ts.map +1 -1
  84. package/dist/schema/state/sqlite/query-builder/astToSql.js +8 -6
  85. package/dist/schema/state/sqlite/query-builder/astToSql.js.map +1 -1
  86. package/dist/schema/state/sqlite/system-tables.d.ts +464 -46
  87. package/dist/schema/state/sqlite/system-tables.d.ts.map +1 -1
  88. package/dist/schema/state/sqlite/table-def.d.ts +161 -152
  89. package/dist/schema/state/sqlite/table-def.d.ts.map +1 -1
  90. package/dist/schema/state/sqlite/table-def.js +251 -5
  91. package/dist/schema/state/sqlite/table-def.js.map +1 -1
  92. package/dist/schema/state/sqlite/table-def.test.d.ts +2 -0
  93. package/dist/schema/state/sqlite/table-def.test.d.ts.map +1 -0
  94. package/dist/schema/state/sqlite/table-def.test.js +635 -0
  95. package/dist/schema/state/sqlite/table-def.test.js.map +1 -0
  96. package/dist/schema-management/common.d.ts +1 -1
  97. package/dist/schema-management/common.d.ts.map +1 -1
  98. package/dist/schema-management/common.js +11 -2
  99. package/dist/schema-management/common.js.map +1 -1
  100. package/dist/schema-management/migrations.d.ts +0 -1
  101. package/dist/schema-management/migrations.d.ts.map +1 -1
  102. package/dist/schema-management/migrations.js +4 -30
  103. package/dist/schema-management/migrations.js.map +1 -1
  104. package/dist/schema-management/migrations.test.d.ts +2 -0
  105. package/dist/schema-management/migrations.test.d.ts.map +1 -0
  106. package/dist/schema-management/migrations.test.js +52 -0
  107. package/dist/schema-management/migrations.test.js.map +1 -0
  108. package/dist/sql-queries/types.d.ts +37 -133
  109. package/dist/sqlite-db-helper.d.ts +3 -1
  110. package/dist/sqlite-db-helper.d.ts.map +1 -1
  111. package/dist/sqlite-db-helper.js +16 -0
  112. package/dist/sqlite-db-helper.js.map +1 -1
  113. package/dist/sqlite-types.d.ts +4 -4
  114. package/dist/sqlite-types.d.ts.map +1 -1
  115. package/dist/sync/ClientSessionSyncProcessor.d.ts +2 -2
  116. package/dist/sync/ClientSessionSyncProcessor.d.ts.map +1 -1
  117. package/dist/sync/ClientSessionSyncProcessor.js +8 -7
  118. package/dist/sync/ClientSessionSyncProcessor.js.map +1 -1
  119. package/dist/sync/sync.d.ts.map +1 -1
  120. package/dist/sync/sync.js.map +1 -1
  121. package/dist/util.d.ts +3 -3
  122. package/dist/util.d.ts.map +1 -1
  123. package/dist/util.js.map +1 -1
  124. package/dist/version.d.ts +1 -1
  125. package/dist/version.js +1 -1
  126. package/package.json +4 -4
  127. package/src/ClientSessionLeaderThreadProxy.ts +2 -2
  128. package/src/adapter-types.ts +6 -4
  129. package/src/devtools/devtools-messages-leader.ts +3 -3
  130. package/src/leader-thread/LeaderSyncProcessor.ts +3 -1
  131. package/src/leader-thread/make-leader-thread-layer.ts +26 -7
  132. package/src/leader-thread/shutdown-channel.ts +2 -2
  133. package/src/leader-thread/types.ts +1 -1
  134. package/src/materializer-helper.ts +5 -11
  135. package/src/rematerialize-from-eventlog.ts +2 -2
  136. package/src/schema/EventSequenceNumber.test.ts +2 -2
  137. package/src/schema/EventSequenceNumber.ts +8 -2
  138. package/src/schema/LiveStoreEvent.ts +7 -1
  139. package/src/schema/schema.ts +4 -0
  140. package/src/schema/state/sqlite/client-document-def.test.ts +89 -1
  141. package/src/schema/state/sqlite/client-document-def.ts +7 -4
  142. package/src/schema/state/sqlite/column-annotations.test.ts +212 -0
  143. package/src/schema/state/sqlite/column-annotations.ts +77 -0
  144. package/src/schema/state/sqlite/column-spec.test.ts +223 -0
  145. package/src/schema/state/sqlite/column-spec.ts +42 -0
  146. package/src/schema/state/sqlite/db-schema/ast/sqlite.ts +2 -0
  147. package/src/schema/state/sqlite/db-schema/dsl/__snapshots__/field-defs.test.ts.snap +15 -0
  148. package/src/schema/state/sqlite/db-schema/dsl/field-defs.ts +20 -2
  149. package/src/schema/state/sqlite/db-schema/dsl/mod.ts +1 -0
  150. package/src/schema/state/sqlite/mod.ts +2 -0
  151. package/src/schema/state/sqlite/query-builder/api.ts +4 -3
  152. package/src/schema/state/sqlite/query-builder/astToSql.ts +9 -7
  153. package/src/schema/state/sqlite/table-def.test.ts +798 -0
  154. package/src/schema/state/sqlite/table-def.ts +472 -16
  155. package/src/schema-management/common.ts +10 -3
  156. package/src/schema-management/migrations.ts +4 -33
  157. package/src/sqlite-db-helper.ts +19 -1
  158. package/src/sqlite-types.ts +4 -4
  159. package/src/sync/ClientSessionSyncProcessor.ts +13 -8
  160. package/src/sync/sync.ts +2 -0
  161. package/src/util.ts +7 -2
  162. package/src/version.ts +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"table-def.js","sourceRoot":"","sources":["../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,CAAC;YACrC,OAAO,uBAAuB,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,CAAA;QAClD,CAAC;aAAM,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;YACtC,OAAO,uBAAuB,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,CAAA;QACrD,CAAC;IACH,CAAC;IAED,2CAA2C;IAC3C,OAAO,uBAAuB,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAA;AAC5D,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=table-def.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table-def.test.d.ts","sourceRoot":"","sources":["../../../../src/schema/state/sqlite/table-def.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,635 @@
1
+ import { Schema } from '@livestore/utils/effect';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { State } from "../../mod.js";
4
+ import { withAutoIncrement, withColumnType, withDefault, withPrimaryKey, withUnique } from "./column-annotations.js";
5
+ describe('table function overloads', () => {
6
+ it('should extract table name from title annotation', () => {
7
+ const TodoSchema = Schema.Struct({
8
+ id: Schema.String,
9
+ text: Schema.String,
10
+ completed: Schema.Boolean,
11
+ }).annotations({ title: 'todos' });
12
+ const todosTable = State.SQLite.table({
13
+ schema: TodoSchema,
14
+ });
15
+ expect(todosTable.sqliteDef.name).toBe('todos');
16
+ });
17
+ it('should extract table name from identifier annotation', () => {
18
+ const TodoSchema = Schema.Struct({
19
+ id: Schema.String,
20
+ text: Schema.String,
21
+ completed: Schema.Boolean,
22
+ }).annotations({ identifier: 'TodoItem' });
23
+ const todosTable = State.SQLite.table({ schema: TodoSchema });
24
+ expect(todosTable.sqliteDef.name).toBe('TodoItem');
25
+ });
26
+ it('should prefer title over identifier annotation', () => {
27
+ const TodoSchema = Schema.Struct({
28
+ id: Schema.String,
29
+ text: Schema.String,
30
+ completed: Schema.Boolean,
31
+ }).annotations({
32
+ title: 'todos',
33
+ identifier: 'TodoItem',
34
+ });
35
+ const todosTable = State.SQLite.table({ schema: TodoSchema });
36
+ expect(todosTable.sqliteDef.name).toBe('todos');
37
+ });
38
+ it('should throw when schema has no name, title, or identifier', () => {
39
+ const TodoSchema = Schema.Struct({
40
+ id: Schema.String,
41
+ text: Schema.String,
42
+ completed: Schema.Boolean,
43
+ });
44
+ expect(() => State.SQLite.table({ schema: TodoSchema })).toThrow('When using schema without explicit name, the schema must have a title or identifier annotation');
45
+ });
46
+ it('should work with columns parameter', () => {
47
+ const todosTable = State.SQLite.table({
48
+ name: 'todos',
49
+ columns: {
50
+ id: State.SQLite.text({ primaryKey: true }),
51
+ text: State.SQLite.text({ default: '' }),
52
+ completed: State.SQLite.boolean({ default: false }),
53
+ },
54
+ });
55
+ expect(todosTable.sqliteDef.name).toBe('todos');
56
+ expect(todosTable.sqliteDef.columns).toHaveProperty('id');
57
+ expect(todosTable.sqliteDef.columns).toHaveProperty('text');
58
+ expect(todosTable.sqliteDef.columns).toHaveProperty('completed');
59
+ });
60
+ it('should work with schema parameter', () => {
61
+ const TodoSchema = Schema.Struct({
62
+ id: Schema.String,
63
+ text: Schema.String,
64
+ completed: Schema.Boolean,
65
+ });
66
+ const todosTable = State.SQLite.table({
67
+ name: 'todos',
68
+ schema: TodoSchema,
69
+ });
70
+ expect(todosTable.sqliteDef.name).toBe('todos');
71
+ expect(todosTable.sqliteDef.columns).toHaveProperty('id');
72
+ expect(todosTable.sqliteDef.columns).toHaveProperty('text');
73
+ expect(todosTable.sqliteDef.columns).toHaveProperty('completed');
74
+ });
75
+ it('should work with single column', () => {
76
+ const simpleTable = State.SQLite.table({
77
+ name: 'simple',
78
+ columns: State.SQLite.text({ primaryKey: true }),
79
+ });
80
+ expect(simpleTable.sqliteDef.name).toBe('simple');
81
+ expect(simpleTable.sqliteDef.columns).toHaveProperty('value');
82
+ expect(simpleTable.sqliteDef.columns.value.primaryKey).toBe(true);
83
+ });
84
+ it('should handle optional fields in schema', () => {
85
+ const UserSchema = Schema.Struct({
86
+ id: Schema.String,
87
+ name: Schema.String,
88
+ email: Schema.optional(Schema.String),
89
+ });
90
+ const userTable = State.SQLite.table({
91
+ name: 'users',
92
+ schema: UserSchema,
93
+ });
94
+ expect(userTable.sqliteDef.columns.id.nullable).toBe(false);
95
+ expect(userTable.sqliteDef.columns.name.nullable).toBe(false);
96
+ expect(userTable.sqliteDef.columns.email.nullable).toBe(true);
97
+ });
98
+ it('should handle Schema.Int as integer column', () => {
99
+ const CounterSchema = Schema.Struct({
100
+ id: Schema.String,
101
+ count: Schema.Int,
102
+ });
103
+ const counterTable = State.SQLite.table({
104
+ name: 'counters',
105
+ schema: CounterSchema,
106
+ });
107
+ expect(counterTable.sqliteDef.columns.count.columnType).toBe('integer');
108
+ });
109
+ it('should work with Schema.Class', () => {
110
+ class User extends Schema.Class('User')({
111
+ id: Schema.String,
112
+ name: Schema.String,
113
+ email: Schema.optional(Schema.String),
114
+ age: Schema.Int,
115
+ }) {
116
+ }
117
+ const userTable = State.SQLite.table({
118
+ name: 'users',
119
+ schema: User,
120
+ });
121
+ expect(userTable.sqliteDef.name).toBe('users');
122
+ expect(userTable.sqliteDef.columns).toHaveProperty('id');
123
+ expect(userTable.sqliteDef.columns).toHaveProperty('name');
124
+ expect(userTable.sqliteDef.columns).toHaveProperty('email');
125
+ expect(userTable.sqliteDef.columns).toHaveProperty('age');
126
+ // Check column types
127
+ expect(userTable.sqliteDef.columns.id.columnType).toBe('text');
128
+ expect(userTable.sqliteDef.columns.name.columnType).toBe('text');
129
+ expect(userTable.sqliteDef.columns.email.columnType).toBe('text');
130
+ expect(userTable.sqliteDef.columns.email.nullable).toBe(true);
131
+ expect(userTable.sqliteDef.columns.age.columnType).toBe('integer');
132
+ });
133
+ it('should extract table name from Schema.Class identifier', () => {
134
+ class TodoItem extends Schema.Class('TodoItem')({
135
+ id: Schema.String,
136
+ text: Schema.String,
137
+ completed: Schema.Boolean,
138
+ }) {
139
+ }
140
+ // Schema.Class doesn't set identifier/title annotations, so we need to provide an explicit name
141
+ const todosTable = State.SQLite.table({
142
+ name: 'TodoItem',
143
+ schema: TodoItem,
144
+ });
145
+ expect(todosTable.sqliteDef.name).toBe('TodoItem');
146
+ });
147
+ it('should properly infer types from schema', () => {
148
+ const UserSchema = Schema.Struct({
149
+ id: Schema.String,
150
+ name: Schema.String,
151
+ age: Schema.Int,
152
+ active: Schema.Boolean,
153
+ metadata: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Unknown })),
154
+ });
155
+ const userTable = State.SQLite.table({
156
+ name: 'users',
157
+ schema: UserSchema,
158
+ });
159
+ const _userTypeCheck = {
160
+ id: 'test-id',
161
+ name: 'John',
162
+ age: 30,
163
+ active: true,
164
+ metadata: { key1: 'value1', key2: 123 },
165
+ };
166
+ // Should derive proper column schema
167
+ expect(userTable.rowSchema.fields.age.toString()).toMatchInlineSnapshot(`"number"`);
168
+ expect(userTable.rowSchema.fields.active.toString()).toMatchInlineSnapshot(`"(number <-> boolean)"`);
169
+ expect(userTable.rowSchema.fields.metadata.toString()).toMatchInlineSnapshot(`"(parseJson <-> { readonly [x: string]: unknown })"`);
170
+ // These should compile without errors
171
+ const _idCheck = 'string';
172
+ const _nameCheck = 'string';
173
+ const _ageCheck = 123;
174
+ const _activeCheck = true;
175
+ const _metadataCheck = { foo: 'bar' };
176
+ // Verify column definitions
177
+ expect(userTable.sqliteDef.columns.id.columnType).toBe('text');
178
+ expect(userTable.sqliteDef.columns.name.columnType).toBe('text');
179
+ expect(userTable.sqliteDef.columns.age.columnType).toBe('integer');
180
+ expect(userTable.sqliteDef.columns.active.columnType).toBe('integer');
181
+ expect(userTable.sqliteDef.columns.metadata.columnType).toBe('text');
182
+ expect(userTable.sqliteDef.columns.metadata.nullable).toBe(true);
183
+ });
184
+ });
185
+ describe('getColumnDefForSchema', () => {
186
+ describe('basic types', () => {
187
+ it('should map Schema.String to text column', () => {
188
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.String);
189
+ expect(columnDef.columnType).toBe('text');
190
+ });
191
+ it('should map Schema.Number to real column', () => {
192
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Number);
193
+ expect(columnDef.columnType).toBe('real');
194
+ });
195
+ it('should map Schema.Boolean to integer column', () => {
196
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Boolean);
197
+ expect(columnDef.columnType).toBe('integer');
198
+ });
199
+ it('should map Schema.Date to text column', () => {
200
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Date);
201
+ expect(columnDef.columnType).toBe('text');
202
+ });
203
+ it('should map Schema.BigInt to text column', () => {
204
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.BigInt);
205
+ expect(columnDef.columnType).toBe('text');
206
+ });
207
+ });
208
+ describe('refinements', () => {
209
+ it('should map Schema.Int to integer column', () => {
210
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Int);
211
+ expect(columnDef.columnType).toBe('integer');
212
+ });
213
+ it('should map string refinements to text column', () => {
214
+ const refinements = [
215
+ { schema: Schema.NonEmptyString, name: 'NonEmptyString' },
216
+ { schema: Schema.Trim, name: 'Trim' },
217
+ { schema: Schema.UUID, name: 'UUID' },
218
+ { schema: Schema.ULID, name: 'ULID' },
219
+ { schema: Schema.String.pipe(Schema.minLength(5)), name: 'minLength' },
220
+ { schema: Schema.String.pipe(Schema.pattern(/^[A-Z]+$/)), name: 'pattern' },
221
+ ];
222
+ for (const { schema, name } of refinements) {
223
+ const columnDef = State.SQLite.getColumnDefForSchema(schema);
224
+ expect(columnDef.columnType, `${name} should map to text`).toBe('text');
225
+ }
226
+ });
227
+ it('should map number refinements to real column', () => {
228
+ const refinements = [
229
+ { schema: Schema.Finite, name: 'Finite' },
230
+ { schema: Schema.Number.pipe(Schema.positive()), name: 'positive' },
231
+ { schema: Schema.Number.pipe(Schema.between(0, 100)), name: 'between' },
232
+ ];
233
+ for (const { schema, name } of refinements) {
234
+ const columnDef = State.SQLite.getColumnDefForSchema(schema);
235
+ expect(columnDef.columnType, `${name} should map to real`).toBe('real');
236
+ }
237
+ });
238
+ });
239
+ describe('literal types', () => {
240
+ it('should map string literals to text column', () => {
241
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Literal('active'));
242
+ expect(columnDef.columnType).toBe('text');
243
+ });
244
+ it('should map number literals to real column', () => {
245
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Literal(42));
246
+ expect(columnDef.columnType).toBe('real');
247
+ });
248
+ it('should map boolean literals to integer column', () => {
249
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Literal(true));
250
+ expect(columnDef.columnType).toBe('integer');
251
+ });
252
+ });
253
+ describe('transformations', () => {
254
+ it('should map transformations based on target type', () => {
255
+ const StringToNumber = Schema.String.pipe(Schema.transform(Schema.Number, {
256
+ decode: Number.parseFloat,
257
+ encode: String,
258
+ }));
259
+ const columnDef = State.SQLite.getColumnDefForSchema(StringToNumber);
260
+ expect(columnDef.columnType).toBe('real'); // Based on the target type (Number)
261
+ });
262
+ it('should handle Date transformations', () => {
263
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Date);
264
+ expect(columnDef.columnType).toBe('text');
265
+ });
266
+ });
267
+ describe('complex types', () => {
268
+ it('should map structs to json column', () => {
269
+ const UserSchema = Schema.Struct({
270
+ name: Schema.String,
271
+ age: Schema.Number,
272
+ });
273
+ const columnDef = State.SQLite.getColumnDefForSchema(UserSchema);
274
+ expect(columnDef.columnType).toBe('text');
275
+ });
276
+ it('should map arrays to json column', () => {
277
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Array(Schema.String));
278
+ expect(columnDef.columnType).toBe('text');
279
+ });
280
+ it('should map records to json column', () => {
281
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Record({ key: Schema.String, value: Schema.Number }));
282
+ expect(columnDef.columnType).toBe('text');
283
+ });
284
+ it('should map tuples to json column', () => {
285
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Tuple(Schema.String, Schema.Number));
286
+ expect(columnDef.columnType).toBe('text');
287
+ });
288
+ it('should map tagged unions to json column', () => {
289
+ const ResultSchema = Schema.Union(Schema.Struct({ _tag: Schema.Literal('success'), value: Schema.String }), Schema.Struct({ _tag: Schema.Literal('error'), error: Schema.String }));
290
+ const columnDef = State.SQLite.getColumnDefForSchema(ResultSchema);
291
+ expect(columnDef.columnType).toBe('text');
292
+ });
293
+ });
294
+ describe('nested schemas', () => {
295
+ it('should handle deeply nested schemas', () => {
296
+ const NestedSchema = Schema.Struct({
297
+ level1: Schema.Struct({
298
+ level2: Schema.Struct({
299
+ value: Schema.String,
300
+ }),
301
+ }),
302
+ });
303
+ const columnDef = State.SQLite.getColumnDefForSchema(NestedSchema);
304
+ expect(columnDef.columnType).toBe('text');
305
+ });
306
+ it('should handle optional nested schemas', () => {
307
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Union(Schema.Struct({ name: Schema.String }), Schema.Undefined));
308
+ expect(columnDef.columnType).toBe('text');
309
+ });
310
+ });
311
+ describe('edge cases', () => {
312
+ it('should default to json column for unhandled types', () => {
313
+ // Test various edge cases that all result in JSON columns
314
+ const edgeCases = [
315
+ { schema: Schema.Unknown, name: 'Unknown' },
316
+ { schema: Schema.Any, name: 'Any' },
317
+ { schema: Schema.Null, name: 'Null' },
318
+ { schema: Schema.Undefined, name: 'Undefined' },
319
+ { schema: Schema.Void, name: 'Void' },
320
+ ];
321
+ for (const { schema, name } of edgeCases) {
322
+ const columnDef = State.SQLite.getColumnDefForSchema(schema);
323
+ expect(columnDef.columnType, `${name} should map to text (JSON storage)`).toBe('text');
324
+ }
325
+ });
326
+ it('should handle never schema', () => {
327
+ // Create a schema that should never validate
328
+ const neverSchema = Schema.String.pipe(Schema.filter(() => false, { message: () => 'Always fails' }));
329
+ const columnDef = State.SQLite.getColumnDefForSchema(neverSchema);
330
+ expect(columnDef.columnType).toBe('text');
331
+ });
332
+ it('should handle symbol schema', () => {
333
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Symbol);
334
+ expect(columnDef.columnType).toBe('text');
335
+ });
336
+ });
337
+ describe('custom schemas', () => {
338
+ it('should handle Schema.extend', () => {
339
+ const BaseSchema = Schema.Struct({
340
+ id: Schema.String,
341
+ createdAt: Schema.Date,
342
+ });
343
+ const ExtendedSchema = Schema.Struct({
344
+ ...BaseSchema.fields,
345
+ name: Schema.String,
346
+ updatedAt: Schema.Date,
347
+ });
348
+ const columnDef = State.SQLite.getColumnDefForSchema(ExtendedSchema);
349
+ expect(columnDef.columnType).toBe('text');
350
+ });
351
+ it('should handle Schema.pick', () => {
352
+ const UserSchema = Schema.Struct({
353
+ id: Schema.String,
354
+ name: Schema.String,
355
+ email: Schema.String,
356
+ });
357
+ const PickedSchema = UserSchema.pipe(Schema.pick('id', 'name'));
358
+ const columnDef = State.SQLite.getColumnDefForSchema(PickedSchema);
359
+ expect(columnDef.columnType).toBe('text');
360
+ });
361
+ it('should handle Schema.omit', () => {
362
+ const UserSchema = Schema.Struct({
363
+ id: Schema.String,
364
+ name: Schema.String,
365
+ password: Schema.String,
366
+ });
367
+ const PublicUserSchema = UserSchema.pipe(Schema.omit('password'));
368
+ const columnDef = State.SQLite.getColumnDefForSchema(PublicUserSchema);
369
+ expect(columnDef.columnType).toBe('text');
370
+ });
371
+ });
372
+ describe('annotations', () => {
373
+ it('should handle schemas with custom annotations', () => {
374
+ const AnnotatedString = Schema.String.annotations({ description: 'A special string' });
375
+ const AnnotatedNumber = Schema.Number.annotations({ min: 0, max: 100 });
376
+ expect(State.SQLite.getColumnDefForSchema(AnnotatedString).columnType).toBe('text');
377
+ expect(State.SQLite.getColumnDefForSchema(AnnotatedNumber).columnType).toBe('real');
378
+ });
379
+ });
380
+ describe('enums and literal unions', () => {
381
+ it('should handle enums and literal unions as text', () => {
382
+ const StatusEnum = Schema.Enums({
383
+ PENDING: 'pending',
384
+ ACTIVE: 'active',
385
+ INACTIVE: 'inactive',
386
+ });
387
+ const StatusUnion = Schema.Union(Schema.Literal('pending'), Schema.Literal('active'), Schema.Literal('inactive'));
388
+ expect(State.SQLite.getColumnDefForSchema(StatusEnum).columnType).toBe('text');
389
+ expect(State.SQLite.getColumnDefForSchema(StatusUnion).columnType).toBe('text');
390
+ });
391
+ });
392
+ describe('binary data', () => {
393
+ it('should handle Uint8Array as blob column', () => {
394
+ const columnDef = State.SQLite.getColumnDefForSchema(Schema.Uint8Array);
395
+ expect(columnDef.columnType).toBe('text'); // Stored as JSON
396
+ });
397
+ });
398
+ describe('recursive schemas', () => {
399
+ it('should handle recursive schemas as json', () => {
400
+ const TreeNode = Schema.Struct({
401
+ value: Schema.String,
402
+ children: Schema.Array(Schema.suspend(() => TreeNode)),
403
+ });
404
+ const columnDef = State.SQLite.getColumnDefForSchema(TreeNode);
405
+ expect(columnDef.columnType).toBe('text'); // Complex type stored as JSON
406
+ });
407
+ });
408
+ describe('annotations', () => {
409
+ describe('withColumnType', () => {
410
+ it('should respect column type annotation for text', () => {
411
+ const schema = Schema.Number.pipe(withColumnType('text'));
412
+ const columnDef = State.SQLite.getColumnDefForSchema(schema);
413
+ expect(columnDef.columnType).toBe('text');
414
+ });
415
+ it('should respect column type annotation for integer', () => {
416
+ const schema = Schema.String.pipe(withColumnType('integer'));
417
+ const columnDef = State.SQLite.getColumnDefForSchema(schema);
418
+ expect(columnDef.columnType).toBe('integer');
419
+ });
420
+ it('should respect column type annotation for real', () => {
421
+ const schema = Schema.Boolean.pipe(withColumnType('real'));
422
+ const columnDef = State.SQLite.getColumnDefForSchema(schema);
423
+ expect(columnDef.columnType).toBe('real');
424
+ });
425
+ it('should respect column type annotation for blob', () => {
426
+ const schema = Schema.String.pipe(withColumnType('blob'));
427
+ const columnDef = State.SQLite.getColumnDefForSchema(schema);
428
+ expect(columnDef.columnType).toBe('blob');
429
+ });
430
+ it('should override default type mapping', () => {
431
+ // Number normally maps to real, but we override to text
432
+ const schema = Schema.Number.pipe(withColumnType('text'));
433
+ const columnDef = State.SQLite.getColumnDefForSchema(schema);
434
+ expect(columnDef.columnType).toBe('text');
435
+ });
436
+ it('should work with dual API', () => {
437
+ // Test both forms of the dual API
438
+ const schema1 = withColumnType(Schema.String, 'integer');
439
+ const schema2 = Schema.String.pipe(withColumnType('integer'));
440
+ const columnDef1 = State.SQLite.getColumnDefForSchema(schema1);
441
+ const columnDef2 = State.SQLite.getColumnDefForSchema(schema2);
442
+ expect(columnDef1.columnType).toBe('integer');
443
+ expect(columnDef2.columnType).toBe('integer');
444
+ });
445
+ });
446
+ describe('withPrimaryKey', () => {
447
+ it('should add primary key annotation to schema', () => {
448
+ const UserSchema = Schema.Struct({
449
+ id: Schema.String.pipe(withPrimaryKey),
450
+ name: Schema.String,
451
+ email: Schema.optional(Schema.String),
452
+ nullable: Schema.NullOr(Schema.Int),
453
+ });
454
+ const userTable = State.SQLite.table({
455
+ name: 'users',
456
+ schema: UserSchema,
457
+ });
458
+ expect(userTable.sqliteDef.columns.id.primaryKey).toBe(true);
459
+ expect(userTable.sqliteDef.columns.id.nullable).toBe(false);
460
+ expect(userTable.sqliteDef.columns.name.primaryKey).toBe(false);
461
+ expect(userTable.sqliteDef.columns.email.primaryKey).toBe(false);
462
+ expect(userTable.sqliteDef.columns.email.nullable).toBe(true);
463
+ expect(userTable.sqliteDef.columns.nullable.primaryKey).toBe(false);
464
+ expect(userTable.sqliteDef.columns.nullable.nullable).toBe(true);
465
+ });
466
+ it('should throw when primary key is used with optional schema', () => {
467
+ // Note: Schema.optional returns a property signature, not a schema, so we can't pipe it
468
+ // Instead, we use Schema.Union to create an optional schema that can be piped
469
+ const optionalString = Schema.Union(Schema.String, Schema.Undefined);
470
+ const UserSchema = Schema.Struct({
471
+ id: optionalString.pipe(withPrimaryKey),
472
+ name: Schema.String,
473
+ });
474
+ expect(() => State.SQLite.table({
475
+ name: 'users',
476
+ schema: UserSchema,
477
+ })).toThrow('Primary key columns cannot be nullable');
478
+ });
479
+ it('should throw when primary key is used with NullOr schema', () => {
480
+ const UserSchema = Schema.Struct({
481
+ id: Schema.NullOr(Schema.String).pipe(withPrimaryKey),
482
+ name: Schema.String,
483
+ });
484
+ expect(() => State.SQLite.table({
485
+ name: 'users',
486
+ schema: UserSchema,
487
+ })).toThrow('Primary key columns cannot be nullable');
488
+ });
489
+ it('should work with column type annotation', () => {
490
+ const UserSchema = Schema.Struct({
491
+ id: Schema.Number.pipe(withColumnType('integer')).pipe(withPrimaryKey),
492
+ name: Schema.String,
493
+ });
494
+ const userTable = State.SQLite.table({
495
+ name: 'users',
496
+ schema: UserSchema,
497
+ });
498
+ expect(userTable.sqliteDef.columns.id.columnType).toBe('integer');
499
+ expect(userTable.sqliteDef.columns.id.primaryKey).toBe(true);
500
+ });
501
+ it('should work with Schema.Int and primary key', () => {
502
+ const UserSchema = Schema.Struct({
503
+ id: Schema.Int.pipe(withPrimaryKey),
504
+ name: Schema.String,
505
+ });
506
+ const userTable = State.SQLite.table({
507
+ name: 'users',
508
+ schema: UserSchema,
509
+ });
510
+ expect(userTable.sqliteDef.columns.id.columnType).toBe('integer');
511
+ expect(userTable.sqliteDef.columns.id.primaryKey).toBe(true);
512
+ });
513
+ });
514
+ describe('withAutoIncrement', () => {
515
+ it('should add autoIncrement annotation to schema', () => {
516
+ const UserSchema = Schema.Struct({
517
+ id: Schema.Int.pipe(withPrimaryKey).pipe(withAutoIncrement),
518
+ name: Schema.String,
519
+ });
520
+ const userTable = State.SQLite.table({
521
+ name: 'users',
522
+ schema: UserSchema,
523
+ });
524
+ expect(userTable.sqliteDef.columns.id.autoIncrement).toBe(true);
525
+ expect(userTable.sqliteDef.columns.id.primaryKey).toBe(true);
526
+ expect(userTable.sqliteDef.columns.id.columnType).toBe('integer');
527
+ });
528
+ });
529
+ describe('withDefault', () => {
530
+ it('should add default value annotation to schema', () => {
531
+ const UserSchema = Schema.Struct({
532
+ id: Schema.String,
533
+ active: Schema.Boolean.pipe(withDefault(true)),
534
+ createdAt: Schema.String.pipe(withDefault('CURRENT_TIMESTAMP')),
535
+ });
536
+ const userTable = State.SQLite.table({
537
+ name: 'users',
538
+ schema: UserSchema,
539
+ });
540
+ expect(userTable.sqliteDef.columns.active.default._tag).toBe('Some');
541
+ expect(userTable.sqliteDef.columns.active.default._tag === 'Some' &&
542
+ userTable.sqliteDef.columns.active.default.value).toBe(true);
543
+ expect(userTable.sqliteDef.columns.createdAt.default._tag).toBe('Some');
544
+ expect(userTable.sqliteDef.columns.createdAt.default._tag === 'Some' &&
545
+ userTable.sqliteDef.columns.createdAt.default.value).toBe('CURRENT_TIMESTAMP');
546
+ });
547
+ it('should work with dual API', () => {
548
+ const schema1 = withDefault(Schema.Int, 0);
549
+ const schema2 = Schema.Int.pipe(withDefault(0));
550
+ const UserSchema1 = Schema.Struct({ count: schema1 });
551
+ const UserSchema2 = Schema.Struct({ count: schema2 });
552
+ const table1 = State.SQLite.table({ name: 't1', schema: UserSchema1 });
553
+ const table2 = State.SQLite.table({ name: 't2', schema: UserSchema2 });
554
+ expect(table1.sqliteDef.columns.count.default._tag).toBe('Some');
555
+ expect(table1.sqliteDef.columns.count.default._tag === 'Some' && table1.sqliteDef.columns.count.default.value).toBe(0);
556
+ expect(table2.sqliteDef.columns.count.default._tag).toBe('Some');
557
+ expect(table2.sqliteDef.columns.count.default._tag === 'Some' && table2.sqliteDef.columns.count.default.value).toBe(0);
558
+ });
559
+ });
560
+ describe('withUnique', () => {
561
+ it('should create unique index for column with unique annotation', () => {
562
+ const UserSchema = Schema.Struct({
563
+ id: Schema.String,
564
+ email: Schema.String.pipe(withUnique),
565
+ username: Schema.String.pipe(withUnique),
566
+ });
567
+ const userTable = State.SQLite.table({
568
+ name: 'users',
569
+ schema: UserSchema,
570
+ });
571
+ // Check that unique indexes were created
572
+ const uniqueIndexes = userTable.sqliteDef.indexes?.filter((idx) => idx.isUnique) || [];
573
+ expect(uniqueIndexes).toHaveLength(2);
574
+ expect(uniqueIndexes.some((idx) => idx.name === 'idx_users_email_unique' && idx.columns.includes('email'))).toBe(true);
575
+ expect(uniqueIndexes.some((idx) => idx.name === 'idx_users_username_unique' && idx.columns.includes('username'))).toBe(true);
576
+ });
577
+ it('should combine unique indexes with user-provided indexes', () => {
578
+ const UserSchema = Schema.Struct({
579
+ id: Schema.String,
580
+ email: Schema.String.pipe(withUnique),
581
+ });
582
+ const userTable = State.SQLite.table({
583
+ name: 'users',
584
+ schema: UserSchema,
585
+ indexes: [{ name: 'idx_custom', columns: ['id', 'email'] }],
586
+ });
587
+ // Should have both custom index and unique index
588
+ expect(userTable.sqliteDef.indexes).toHaveLength(2);
589
+ expect(userTable.sqliteDef.indexes?.some((idx) => idx.name === 'idx_custom')).toBe(true);
590
+ expect(userTable.sqliteDef.indexes?.some((idx) => idx.name === 'idx_users_email_unique')).toBe(true);
591
+ });
592
+ });
593
+ describe('combined annotations', () => {
594
+ it('should work with multiple annotations', () => {
595
+ const schema = Schema.Uint8ArrayFromBase64.pipe(withColumnType('blob')).pipe(withPrimaryKey);
596
+ const UserSchema = Schema.Struct({
597
+ id: schema,
598
+ name: Schema.String,
599
+ });
600
+ const userTable = State.SQLite.table({
601
+ name: 'users',
602
+ schema: UserSchema,
603
+ });
604
+ expect(userTable.sqliteDef.columns.id.columnType).toBe('blob');
605
+ expect(userTable.sqliteDef.columns.id.primaryKey).toBe(true);
606
+ });
607
+ it('should combine all annotations', () => {
608
+ const UserSchema = Schema.Struct({
609
+ id: Schema.Int.pipe(withPrimaryKey).pipe(withAutoIncrement),
610
+ email: Schema.String.pipe(withUnique),
611
+ status: Schema.String.pipe(withDefault('active')),
612
+ metadata: Schema.Unknown.pipe(withColumnType('text')),
613
+ });
614
+ const userTable = State.SQLite.table({
615
+ name: 'users',
616
+ schema: UserSchema,
617
+ });
618
+ // Check id column
619
+ expect(userTable.sqliteDef.columns.id.primaryKey).toBe(true);
620
+ expect(userTable.sqliteDef.columns.id.autoIncrement).toBe(true);
621
+ expect(userTable.sqliteDef.columns.id.columnType).toBe('integer');
622
+ // Check email column and unique index
623
+ expect(userTable.sqliteDef.columns.email.columnType).toBe('text');
624
+ expect(userTable.sqliteDef.indexes?.some((idx) => idx.name === 'idx_users_email_unique' && idx.isUnique)).toBe(true);
625
+ // Check status column
626
+ expect(userTable.sqliteDef.columns.status.default._tag).toBe('Some');
627
+ expect(userTable.sqliteDef.columns.status.default._tag === 'Some' &&
628
+ userTable.sqliteDef.columns.status.default.value).toBe('active');
629
+ // Check metadata column
630
+ expect(userTable.sqliteDef.columns.metadata.columnType).toBe('text');
631
+ });
632
+ });
633
+ });
634
+ });
635
+ //# sourceMappingURL=table-def.test.js.map