@evolu/common 6.0.1-preview.8 → 7.0.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 (184) hide show
  1. package/README.md +2 -2
  2. package/dist/src/Array.d.ts +256 -12
  3. package/dist/src/Array.d.ts.map +1 -1
  4. package/dist/src/Array.js +215 -9
  5. package/dist/src/Assert.d.ts +0 -13
  6. package/dist/src/Assert.d.ts.map +1 -1
  7. package/dist/src/Assert.js +0 -15
  8. package/dist/src/Brand.d.ts +75 -0
  9. package/dist/src/Brand.d.ts.map +1 -0
  10. package/dist/src/Brand.js +1 -0
  11. package/dist/src/Buffer.d.ts +1 -1
  12. package/dist/src/Buffer.d.ts.map +1 -1
  13. package/dist/src/Buffer.js +8 -7
  14. package/dist/src/Cache.d.ts +44 -0
  15. package/dist/src/Cache.d.ts.map +1 -0
  16. package/dist/src/Cache.js +52 -0
  17. package/dist/src/Callbacks.d.ts +45 -12
  18. package/dist/src/Callbacks.d.ts.map +1 -1
  19. package/dist/src/Callbacks.js +14 -7
  20. package/dist/src/Console.d.ts +31 -6
  21. package/dist/src/Console.d.ts.map +1 -1
  22. package/dist/src/Console.js +72 -9
  23. package/dist/src/Crypto.d.ts +56 -42
  24. package/dist/src/Crypto.d.ts.map +1 -1
  25. package/dist/src/Crypto.js +40 -53
  26. package/dist/src/Evolu/Db.d.ts +162 -74
  27. package/dist/src/Evolu/Db.d.ts.map +1 -1
  28. package/dist/src/Evolu/Db.js +284 -702
  29. package/dist/src/Evolu/Diff.d.ts +3 -3
  30. package/dist/src/Evolu/Diff.d.ts.map +1 -1
  31. package/dist/src/Evolu/Diff.js +7 -5
  32. package/dist/src/Evolu/Evolu.d.ts +214 -134
  33. package/dist/src/Evolu/Evolu.d.ts.map +1 -1
  34. package/dist/src/Evolu/Evolu.js +189 -180
  35. package/dist/src/Evolu/Internal.d.ts +0 -2
  36. package/dist/src/Evolu/Internal.d.ts.map +1 -1
  37. package/dist/src/Evolu/Internal.js +0 -2
  38. package/dist/src/Evolu/LocalAuth.d.ts +150 -0
  39. package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
  40. package/dist/src/Evolu/LocalAuth.js +174 -0
  41. package/dist/src/Evolu/Owner.d.ts +273 -120
  42. package/dist/src/Evolu/Owner.d.ts.map +1 -1
  43. package/dist/src/Evolu/Owner.js +130 -104
  44. package/dist/src/Evolu/Platform.d.ts +9 -7
  45. package/dist/src/Evolu/Platform.d.ts.map +1 -1
  46. package/dist/src/Evolu/Protocol.d.ts +268 -240
  47. package/dist/src/Evolu/Protocol.d.ts.map +1 -1
  48. package/dist/src/Evolu/Protocol.js +600 -454
  49. package/dist/src/Evolu/Public.d.ts +6 -8
  50. package/dist/src/Evolu/Public.d.ts.map +1 -1
  51. package/dist/src/Evolu/Public.js +2 -3
  52. package/dist/src/Evolu/PublicKysely.d.ts.map +1 -1
  53. package/dist/src/Evolu/PublicKysely.js +3 -4
  54. package/dist/src/Evolu/Query.d.ts +2 -1
  55. package/dist/src/Evolu/Query.d.ts.map +1 -1
  56. package/dist/src/Evolu/Query.js +1 -1
  57. package/dist/src/Evolu/Relay.d.ts +91 -8
  58. package/dist/src/Evolu/Relay.d.ts.map +1 -1
  59. package/dist/src/Evolu/Relay.js +214 -88
  60. package/dist/src/Evolu/Schema.d.ts +125 -47
  61. package/dist/src/Evolu/Schema.d.ts.map +1 -1
  62. package/dist/src/Evolu/Schema.js +175 -31
  63. package/dist/src/Evolu/Storage.d.ts +249 -27
  64. package/dist/src/Evolu/Storage.d.ts.map +1 -1
  65. package/dist/src/Evolu/Storage.js +198 -92
  66. package/dist/src/Evolu/Sync.d.ts +68 -13
  67. package/dist/src/Evolu/Sync.d.ts.map +1 -1
  68. package/dist/src/Evolu/Sync.js +469 -20
  69. package/dist/src/Evolu/Timestamp.d.ts +83 -30
  70. package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
  71. package/dist/src/Evolu/Timestamp.js +79 -34
  72. package/dist/src/Identicon.d.ts +35 -0
  73. package/dist/src/Identicon.d.ts.map +1 -0
  74. package/dist/src/Identicon.js +143 -0
  75. package/dist/src/Instances.d.ts +34 -0
  76. package/dist/src/Instances.d.ts.map +1 -0
  77. package/dist/src/Instances.js +44 -0
  78. package/dist/src/Number.d.ts +4 -3
  79. package/dist/src/Number.d.ts.map +1 -1
  80. package/dist/src/Number.js +5 -4
  81. package/dist/src/Object.d.ts +10 -4
  82. package/dist/src/Object.d.ts.map +1 -1
  83. package/dist/src/Object.js +9 -3
  84. package/dist/src/Platform.d.ts +20 -0
  85. package/dist/src/Platform.d.ts.map +1 -0
  86. package/dist/src/Platform.js +22 -0
  87. package/dist/src/Random.d.ts +3 -2
  88. package/dist/src/Random.d.ts.map +1 -1
  89. package/dist/src/Relation.d.ts +79 -0
  90. package/dist/src/Relation.d.ts.map +1 -0
  91. package/dist/src/Relation.js +127 -0
  92. package/dist/src/Resources.d.ts +118 -0
  93. package/dist/src/Resources.d.ts.map +1 -0
  94. package/dist/src/Resources.js +197 -0
  95. package/dist/src/Result.d.ts +184 -52
  96. package/dist/src/Result.d.ts.map +1 -1
  97. package/dist/src/Result.js +30 -241
  98. package/dist/src/Skiplist.js +2 -1
  99. package/dist/src/Sqlite.d.ts +89 -5
  100. package/dist/src/Sqlite.d.ts.map +1 -1
  101. package/dist/src/Sqlite.js +136 -9
  102. package/dist/src/Task.d.ts +586 -0
  103. package/dist/src/Task.d.ts.map +1 -0
  104. package/dist/src/Task.js +469 -0
  105. package/dist/src/Time.d.ts +66 -1
  106. package/dist/src/Time.d.ts.map +1 -1
  107. package/dist/src/Time.js +99 -5
  108. package/dist/src/Type.d.ts +676 -343
  109. package/dist/src/Type.d.ts.map +1 -1
  110. package/dist/src/Type.js +718 -467
  111. package/dist/src/Types.d.ts +1 -75
  112. package/dist/src/Types.d.ts.map +1 -1
  113. package/dist/src/WebSocket.d.ts +5 -2
  114. package/dist/src/WebSocket.d.ts.map +1 -1
  115. package/dist/src/WebSocket.js +12 -18
  116. package/dist/src/Worker.d.ts +39 -11
  117. package/dist/src/Worker.d.ts.map +1 -1
  118. package/dist/src/Worker.js +22 -4
  119. package/dist/src/index.d.ts +8 -3
  120. package/dist/src/index.d.ts.map +1 -1
  121. package/dist/src/index.js +8 -3
  122. package/package.json +15 -14
  123. package/src/Array.ts +301 -19
  124. package/src/Assert.ts +0 -21
  125. package/src/Brand.ts +75 -0
  126. package/src/Buffer.ts +7 -7
  127. package/src/Cache.ts +85 -0
  128. package/src/Callbacks.ts +62 -22
  129. package/src/Console.ts +91 -11
  130. package/src/Crypto.ts +94 -90
  131. package/src/Evolu/Db.ts +519 -1026
  132. package/src/Evolu/Diff.ts +7 -5
  133. package/src/Evolu/Evolu.ts +477 -361
  134. package/src/Evolu/Internal.ts +0 -2
  135. package/src/Evolu/LocalAuth.ts +463 -0
  136. package/src/Evolu/Owner.ts +355 -228
  137. package/src/Evolu/Platform.ts +9 -9
  138. package/src/Evolu/Protocol.ts +842 -742
  139. package/src/Evolu/Public.ts +7 -14
  140. package/src/Evolu/PublicKysely.ts +4 -5
  141. package/src/Evolu/Query.ts +3 -2
  142. package/src/Evolu/Relay.ts +406 -103
  143. package/src/Evolu/Schema.ts +323 -91
  144. package/src/Evolu/Storage.ts +559 -137
  145. package/src/Evolu/Sync.ts +819 -36
  146. package/src/Evolu/Timestamp.ts +90 -58
  147. package/src/Identicon.ts +197 -0
  148. package/src/Instances.ts +90 -0
  149. package/src/Number.ts +6 -10
  150. package/src/Object.ts +13 -5
  151. package/src/Platform.ts +26 -0
  152. package/src/Random.ts +3 -2
  153. package/src/Relation.ts +234 -0
  154. package/src/Resources.ts +367 -0
  155. package/src/Result.ts +191 -54
  156. package/src/Skiplist.ts +1 -1
  157. package/src/Sqlite.ts +152 -17
  158. package/src/Task.ts +901 -0
  159. package/src/Time.ts +180 -5
  160. package/src/Type.ts +1135 -730
  161. package/src/Types.ts +1 -77
  162. package/src/WebSocket.ts +27 -25
  163. package/src/Worker.ts +72 -23
  164. package/src/index.ts +8 -3
  165. package/dist/src/Evolu/Config.d.ts +0 -69
  166. package/dist/src/Evolu/Config.d.ts.map +0 -1
  167. package/dist/src/Evolu/Config.js +0 -9
  168. package/dist/src/Evolu/Kysely.d.ts +0 -6
  169. package/dist/src/Evolu/Kysely.d.ts.map +0 -1
  170. package/dist/src/Evolu/Kysely.js +0 -21
  171. package/dist/src/ManyToManyMap.d.ts +0 -26
  172. package/dist/src/ManyToManyMap.d.ts.map +0 -1
  173. package/dist/src/ManyToManyMap.js +0 -92
  174. package/dist/src/NanoId.d.ts +0 -27
  175. package/dist/src/NanoId.d.ts.map +0 -1
  176. package/dist/src/NanoId.js +0 -6
  177. package/dist/src/Promise.d.ts +0 -180
  178. package/dist/src/Promise.d.ts.map +0 -1
  179. package/dist/src/Promise.js +0 -176
  180. package/src/Evolu/Config.ts +0 -83
  181. package/src/Evolu/Kysely.ts +0 -38
  182. package/src/ManyToManyMap.ts +0 -140
  183. package/src/NanoId.ts +0 -39
  184. package/src/Promise.ts +0 -295
@@ -1,15 +1,13 @@
1
- import { Kysely, SelectQueryBuilder } from "kysely";
1
+ import * as Kysely from "kysely";
2
2
  import { ReadonlyRecord } from "../Object.js";
3
3
  import { Result } from "../Result.js";
4
- import { SqliteBoolean, SqliteQueryOptions, SqliteValue } from "../Sqlite.js";
5
- import { AnyType, BrandType, IdType, InferErrors, InferInput, InferType, MergeObjectTypeErrors, NullableToOptionalProps, ObjectType, OptionalType, Type, TypeError } from "../Type.js";
4
+ import { SqliteBoolean, SqliteDep, SqliteError, SqliteQueryOptions, SqliteValue } from "../Sqlite.js";
5
+ import { AnyType, IdBytes, InferErrors, InferInput, InferType, MergeObjectTypeErrors, NullableToOptionalProps, ObjectType, OptionalType, TableId, Type, ValidMutationSize, ValidMutationSizeError } from "../Type.js";
6
6
  import { Simplify } from "../Types.js";
7
- import { DbSchema } from "./Db.js";
8
- import { DbIndexesBuilder } from "./Kysely.js";
9
- import { ShardOwner, SharedOwner } from "./Owner.js";
10
- import { BinaryId } from "./Protocol.js";
7
+ import { OwnerId } from "./Owner.js";
11
8
  import { Query, Row } from "./Query.js";
12
- import { BinaryTimestamp } from "./Timestamp.js";
9
+ import { DbChange } from "./Storage.js";
10
+ import { TimestampBytes } from "./Timestamp.js";
13
11
  /**
14
12
  * Defines the schema of an Evolu database.
15
13
  *
@@ -44,7 +42,7 @@ import { BinaryTimestamp } from "./Timestamp.js";
44
42
  * };
45
43
  * ```
46
44
  */
47
- export type EvoluSchema = ReadonlyRecord<string, ReadonlyRecord<string, Type<any, any, any, any, any>>>;
45
+ export type EvoluSchema = ReadonlyRecord<string, ReadonlyRecord<string, Type<any, any, any, any, any, any>>>;
48
46
  /**
49
47
  * Validates an {@link EvoluSchema} at compile time, returning the first error
50
48
  * found as a readable string literal type. This approach provides much clearer
@@ -55,43 +53,94 @@ export type EvoluSchema = ReadonlyRecord<string, ReadonlyRecord<string, Type<any
55
53
  *
56
54
  * 1. All tables must have an 'id' column
57
55
  * 2. The 'id' column must be a branded ID type (created with id() function)
58
- * 3. Tables cannot use default column names (createdAt, updatedAt, isDeleted)
56
+ * 3. Tables cannot use system column names (createdAt, updatedAt, isDeleted)
59
57
  * 4. All column types must be compatible with SQLite (extend SqliteValue)
60
58
  */
61
- export type ValidateSchema<S extends EvoluSchema> = ValidateSchemaHasId<S> extends never ? ValidateIdColumnType<S> extends never ? ValidateNoDefaultColumns<S> extends never ? ValidateColumnTypes<S> extends never ? S : ValidateColumnTypes<S> : ValidateNoDefaultColumns<S> : ValidateIdColumnType<S> : ValidateSchemaHasId<S>;
59
+ export type ValidateSchema<S extends EvoluSchema> = ValidateSchemaHasId<S> extends never ? ValidateIdColumnType<S> extends never ? ValidateNoSystemColumns<S> extends never ? ValidateColumnTypes<S> extends never ? S : ValidateColumnTypes<S> : ValidateNoSystemColumns<S> : ValidateIdColumnType<S> : ValidateSchemaHasId<S>;
62
60
  export type ValidateSchemaHasId<S extends EvoluSchema> = keyof S extends infer TableName ? TableName extends keyof S ? "id" extends keyof S[TableName] ? never : SchemaValidationError<`Table "${TableName & string}" is missing required id column.`> : never : never;
63
- export type ValidateIdColumnType<S extends EvoluSchema> = keyof S extends infer TableName ? TableName extends keyof S ? "id" extends keyof S[TableName] ? S[TableName]["id"] extends IdType<any> ? never : SchemaValidationError<`Table "${TableName & string}" id column must be a branded ID type (created with id("${TableName & string}")).`> : never : never : never;
64
- export type ValidateNoDefaultColumns<S extends EvoluSchema> = keyof S extends infer TableName ? TableName extends keyof S ? keyof S[TableName] extends infer ColumnName ? ColumnName extends keyof S[TableName] ? ColumnName extends "createdAt" | "updatedAt" | "isDeleted" ? SchemaValidationError<`Table "${TableName & string}" uses default column name "${ColumnName & string}". Default columns (createdAt, updatedAt, isDeleted) are added automatically.`> : never : never : never : never : never;
61
+ export type ValidateIdColumnType<S extends EvoluSchema> = keyof S extends infer TableName ? TableName extends keyof S ? "id" extends keyof S[TableName] ? S[TableName]["id"] extends TableId<any> ? never : SchemaValidationError<`Table "${TableName & string}" id column must be a branded ID type (created with id("${TableName & string}")).`> : never : never : never;
62
+ export type ValidateNoSystemColumns<S extends EvoluSchema> = keyof S extends infer TableName ? TableName extends keyof S ? keyof S[TableName] extends infer ColumnName ? ColumnName extends keyof S[TableName] ? ColumnName extends "createdAt" | "updatedAt" | "isDeleted" | "ownerId" ? SchemaValidationError<`Table "${TableName & string}" uses system column name "${ColumnName & string}". System columns (createdAt, updatedAt, isDeleted, ownerId) are added automatically.`> : never : never : never : never : never;
65
63
  export type ValidateColumnTypes<S extends EvoluSchema> = keyof S extends infer TableName ? TableName extends keyof S ? keyof S[TableName] extends infer ColumnName ? ColumnName extends keyof S[TableName] ? InferType<S[TableName][ColumnName]> extends SqliteValue ? never : SchemaValidationError<`Table "${TableName & string}" column "${ColumnName & string}" type is not compatible with SQLite. Column types must extend SqliteValue (string, number, Uint8Array, or null).`> : never : never : never : never;
66
64
  /** Schema validation error that shows clear, readable messages */
67
65
  export type SchemaValidationError<Message extends string> = `❌ Schema Error: ${Message}`;
68
- export declare const evoluSchemaToDbSchema: (schema: EvoluSchema, indexes?: DbIndexesBuilder) => DbSchema;
69
- export type CreateQuery<S extends EvoluSchema> = <R extends Row>(queryCallback: (db: Pick<Kysely<{
66
+ export type IndexesConfig = (create: (indexName: string) => Kysely.CreateIndexBuilder) => ReadonlyArray<Kysely.CreateIndexBuilder<any>>;
67
+ export declare const evoluSchemaToDbSchema: (schema: EvoluSchema, indexesConfig?: IndexesConfig) => DbSchema;
68
+ export type CreateQuery<S extends EvoluSchema> = <R extends Row>(queryCallback: (db: Pick<Kysely.Kysely<{
70
69
  [Table in keyof S]: {
71
- readonly [Column in keyof S[Table]]: Column extends "id" | "createdAt" | "updatedAt" ? InferType<S[Table][Column]> : InferType<S[Table][Column]> | null;
72
- } & DefaultColumns;
70
+ readonly [Column in keyof S[Table]]: Column extends "id" ? InferType<S[Table][Column]> : InferType<S[Table][Column]> | null;
71
+ } & SystemColumns;
73
72
  } & {
74
73
  readonly evolu_history: {
75
- readonly timestamp: BinaryTimestamp;
74
+ readonly timestamp: TimestampBytes;
76
75
  readonly table: keyof S;
77
- readonly id: BinaryId;
76
+ readonly id: IdBytes;
78
77
  readonly column: string;
79
78
  readonly value: SqliteValue;
80
79
  };
81
- }>, "selectFrom" | "fn" | "with" | "withRecursive">) => SelectQueryBuilder<any, any, R>, options?: SqliteQueryOptions) => Query<Simplify<R>>;
82
- export declare const DefaultColumns: ObjectType<{
83
- createdAt: BrandType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>, "DateIso", import("../Type.js").DateIsoStringError, import("../Type.js").StringError>;
84
- updatedAt: BrandType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>, "DateIso", import("../Type.js").DateIsoStringError, import("../Type.js").StringError>;
85
- isDeleted: import("../Type.js").UnionType<[Type<"Null", null, null, import("../Type.js").NullError, null, import("../Type.js").NullError>, import("../Type.js").TransformType<Type<"Boolean", boolean, boolean, import("../Type.js").BooleanError, boolean, import("../Type.js").BooleanError>, import("../Type.js").UnionType<[import("../Type.js").LiteralType<0>, import("../Type.js").LiteralType<1>]>, never>]>;
80
+ }>, "selectFrom" | "fn" | "with" | "withRecursive">) => Kysely.SelectQueryBuilder<any, any, R>, options?: SqliteQueryOptions) => Query<Simplify<R>>;
81
+ /**
82
+ * System columns that are implicitly defined by Evolu.
83
+ *
84
+ * - `createdAt`: Set by Evolu on row creation, derived from {@link Timestamp}.
85
+ * - `updatedAt`: Set by Evolu on every row change, derived from {@link Timestamp}.
86
+ * - `isDeleted`: Soft delete flag created by Evolu and used by the developer to
87
+ * mark rows as deleted.
88
+ * - `ownerId`: Represents ownership and logically partitions the database.
89
+ */
90
+ export declare const SystemColumns: ObjectType<{
91
+ createdAt: import("../Type.js").BrandType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>, "DateIso", import("../Type.js").DateIsoError, import("../Type.js").StringError>;
92
+ updatedAt: import("../Type.js").BrandType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>, "DateIso", import("../Type.js").DateIsoError, import("../Type.js").StringError>;
93
+ isDeleted: import("../Type.js").UnionType<[Type<"Null", null, null, import("../Type.js").NullError, null, import("../Type.js").NullError>, import("../Type.js").UnionType<[import("../Type.js").LiteralType<0>, import("../Type.js").LiteralType<1>]>]>;
94
+ ownerId: import("../Type.js").BrandType<import("../Type.js").BrandType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>, "Id", import("../Type.js").IdError, import("../Type.js").StringError>, "OwnerId", import("../Type.js").BrandWithoutRefineError<"OwnerId", import("../Type.js").StringError | import("../Type.js").IdError>, never>;
86
95
  }>;
87
- export type DefaultColumns = typeof DefaultColumns.Type;
96
+ export type SystemColumns = typeof SystemColumns.Type;
97
+ export declare const systemColumns: string[];
88
98
  export type MutationKind = "insert" | "update" | "upsert";
89
99
  export type Mutation<S extends EvoluSchema, Kind extends MutationKind> = <TableName extends keyof S>(table: TableName, props: InferInput<ObjectType<MutationMapping<S[TableName], Kind>>>, options?: MutationOptions) => Result<{
90
100
  readonly id: S[TableName]["id"]["Type"];
91
101
  }, ValidMutationSizeError | MergeObjectTypeErrors<ObjectType<MutationMapping<S[TableName], Kind>>>>;
92
102
  export type MutationMapping<P extends Record<string, AnyType>, M extends MutationKind> = M extends "insert" ? InsertableProps<P> : M extends "update" ? UpdateableProps<P> : UpsertableProps<P>;
93
103
  export interface MutationOptions {
104
+ /**
105
+ * Called after the mutation is completed and the local state is updated.
106
+ * Useful for triggering side effects (e.g., notifications, UI updates) after
107
+ * insert, update, or upsert.
108
+ */
94
109
  readonly onComplete?: () => void;
110
+ /**
111
+ * Specifies the owner ID for this mutation. If omitted, the default
112
+ * {@link AppOwner} is used.
113
+ *
114
+ * The owner must be used with `evolu.useOwner()` to enable sync. Mutations
115
+ * with unused owners are stored locally but not synced until the owner is
116
+ * used.
117
+ *
118
+ * ### Example
119
+ *
120
+ * ```ts
121
+ * // Partition your own data by project (derived from your AppOwner)
122
+ * const projectOwner = deriveShardOwner(appOwner, [
123
+ * "project",
124
+ * projectId,
125
+ * ]);
126
+ * evolu.insert(
127
+ * "task",
128
+ * { title: "Task 1" },
129
+ * { ownerId: projectOwner.id },
130
+ * );
131
+ *
132
+ * // Collaborative data (independent owner shared with others)
133
+ * const sharedOwner = createSharedOwner(sharedSecret);
134
+ * evolu.insert(
135
+ * "comment",
136
+ * { text: "Hello" },
137
+ * { ownerId: sharedOwner.id },
138
+ * );
139
+ * ```
140
+ *
141
+ * @experimental
142
+ */
143
+ readonly ownerId?: OwnerId;
95
144
  /**
96
145
  * Only validate, don't mutate.
97
146
  *
@@ -99,27 +148,15 @@ export interface MutationOptions {
99
148
  * `onlyValidate: true`.
100
149
  */
101
150
  readonly onlyValidate?: boolean;
102
- /**
103
- * The owner to use for this mutation. Can be a {@link ShardOwner} for sharding
104
- * app data or a {@link SharedOwner} for collaborative write access. If
105
- * omitted, defaults to the app's {@link AppOwner}.
106
- */
107
- readonly owner?: ShardOwner | SharedOwner;
108
151
  }
109
- /**
110
- * Evolu has to limit the maximum mutation size. Otherwise, sync couldn't use
111
- * the {@link maxProtocolMessageRangesSize}. The max size is 640KB in bytes,
112
- * measured via MessagePack. Evolu Protocol DbChange will be smaller thanks to
113
- * various optimizations.
114
- */
115
- export declare const maxMutationSize = 655360;
116
- export interface ValidMutationSizeError extends TypeError<"ValidMutationSize"> {
152
+ export interface MutationChange extends DbChange {
153
+ /** Owner of the change. If undefined, the change belongs to the AppOwner. */
154
+ readonly ownerId?: OwnerId | undefined;
117
155
  }
118
- export declare const formatValidMutationSizeError: import("../Type.js").TypeErrorFormatter<ValidMutationSizeError>;
119
- export type ValidMutationSize<Props extends Record<string, AnyType>> = BrandType<ObjectType<Props>, "ValidMutationSize", ValidMutationSizeError, InferErrors<ObjectType<Props>>>;
120
156
  /**
121
157
  * Type Factory to create insertable {@link Type}. It makes nullable Types
122
- * optional, omits Id, and ensures the {@link maxMutationSize}.
158
+ * optional (so they are not required), omits Id, and ensures the
159
+ * {@link maxMutationSize}.
123
160
  *
124
161
  * ### Example
125
162
  *
@@ -135,7 +172,7 @@ export type InsertableProps<Props extends Record<string, AnyType>> = Omit<Nullab
135
172
  export type Insertable<Props extends Record<string, AnyType>> = InferInput<ObjectType<InsertableProps<Props>>>;
136
173
  /**
137
174
  * Type Factory to create updateable {@link Type}. It makes everything except for
138
- * the `id` column partial (i.e. optional) and ensures the
175
+ * the `id` column optional (so they are not required) and ensures the
139
176
  * {@link maxMutationSize}.
140
177
  *
141
178
  * ### Example
@@ -160,20 +197,32 @@ export type UpdateableProps<Props extends Record<string, AnyType>> = {
160
197
  };
161
198
  export type Updateable<Props extends Record<string, AnyType>> = InferInput<ObjectType<UpdateableProps<Props>>>;
162
199
  /**
163
- * Type Factory to create upsertable Type. It makes nullable Types optional and
164
- * ensures the {@link maxMutationSize}.
200
+ * Type Factory to create an upsertable Type. It makes nullable Types optional
201
+ * (so they are not required) and ensures the {@link maxMutationSize}.
202
+ *
203
+ * Upsert is like insert, except it requires an ID. It's useful for inserting
204
+ * rows with external ID via {@link createIdFromString}.
205
+ *
206
+ * Note that it's not possible to upsert a row with `createdAt` nor `updatedAt`,
207
+ * because they are derived from {@link CrdtMessage} timestamp. For external
208
+ * createdAt, use a different column.
165
209
  *
166
210
  * ### Example
167
211
  *
168
212
  * ```ts
169
213
  * const UpsertableTodo = upsertable(Schema.todo);
170
214
  * type UpsertableTodo = typeof UpsertableTodo.Type;
171
- * const todo = UpsertableTodo.from({ id, title });
215
+ * const todo = UpsertableTodo.from({
216
+ * id,
217
+ * title,
218
+ * });
172
219
  * if (!todo.ok) return; // handle errors
173
220
  * ```
174
221
  */
175
222
  export declare const upsertable: <Props extends Record<string, AnyType>>(props: Props) => ValidMutationSize<UpsertableProps<Props>>;
176
- export type UpsertableProps<Props extends Record<string, AnyType>> = NullableToOptionalProps<Props>;
223
+ export type UpsertableProps<Props extends Record<string, AnyType>> = NullableToOptionalProps<Props & {
224
+ isDeleted: OptionalType<typeof SqliteBoolean>;
225
+ }>;
177
226
  export type Upsertable<Props extends Record<string, AnyType>> = InferInput<ObjectType<UpsertableProps<Props>>>;
178
227
  export type InferEvoluSchemaError<S extends EvoluSchema> = {
179
228
  [Table in keyof S]: InferMutationTypeErrors<S[Table]>;
@@ -182,4 +231,33 @@ export type InferMutationTypeErrors<T extends Record<string, AnyType>> = InferCo
182
231
  export type InferColumnErrors<T extends Record<string, AnyType>, M extends MutationKind> = {
183
232
  [Column in keyof MutationMapping<T, M>]: InferErrors<MutationMapping<T, M>[Column]>;
184
233
  }[keyof MutationMapping<T, M>];
234
+ export declare const DbTable: ObjectType<{
235
+ name: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
236
+ columns: import("../Type.js").ArrayType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>>;
237
+ }>;
238
+ export type DbTable = typeof DbTable.Type;
239
+ export declare const DbIndex: ObjectType<{
240
+ name: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
241
+ sql: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
242
+ }>;
243
+ export type DbIndex = typeof DbIndex.Type;
244
+ export declare const DbSchema: ObjectType<{
245
+ tables: import("../Type.js").ArrayType<ObjectType<{
246
+ name: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
247
+ columns: import("../Type.js").ArrayType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>>;
248
+ }>>;
249
+ indexes: import("../Type.js").ArrayType<ObjectType<{
250
+ name: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
251
+ sql: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
252
+ }>>;
253
+ }>;
254
+ export type DbSchema = typeof DbSchema.Type;
255
+ /** Get the current database schema by reading SQLite metadata. */
256
+ export declare const getDbSchema: (deps: SqliteDep) => ({ allIndexes }?: {
257
+ allIndexes?: boolean;
258
+ }) => Result<DbSchema, SqliteError>;
259
+ export declare const ensureDbSchema: (deps: SqliteDep) => (newSchema: DbSchema, currentSchema: DbSchema, options?: {
260
+ ignoreIndexes: boolean;
261
+ }) => Result<void, SqliteError>;
262
+ export declare const kysely: Kysely.Kysely<unknown>;
185
263
  //# sourceMappingURL=Schema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;AAGpD,OAAO,EAA8B,cAAc,EAAE,MAAM,cAAc,CAAC;AAC1E,OAAO,EAAW,MAAM,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,EACL,OAAO,EAEP,SAAS,EAGT,MAAM,EACN,WAAW,EACX,UAAU,EACV,SAAS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ACnB,eAAe,QAAQ,EACvB,UAAU;IAAE,aAAa,EAAE,OAAO,CAAA;CAAE,KACnC,MAAM,CAAC,IAAI,EAAE,WAAW,CAoD1B,CAAC;AAqBJ,eAAO,MAAM,MAAM,wBASjB,CAAC"}
@@ -1,41 +1,44 @@
1
- import { pack } from "msgpackr";
2
- import { assert } from "../Assert.js";
1
+ import * as Kysely from "kysely";
3
2
  import { mapObject, objectToEntries } from "../Object.js";
4
- import { err, ok } from "../Result.js";
5
- import { SqliteBoolean } from "../Sqlite.js";
6
- import { brand, createTypeErrorFormatter, DateIsoString, nullableToOptional, nullOr, object, omit, optional, } from "../Type.js";
7
- import { DbSchema } from "./Db.js";
8
- import { createIndexes } from "./Kysely.js";
9
- export const evoluSchemaToDbSchema = (schema, indexes) => {
3
+ import { ok } from "../Result.js";
4
+ import { sql, SqliteBoolean, } from "../Sqlite.js";
5
+ import { array, DateIso, nullableToOptional, nullOr, object, omit, optional, String, validMutationSize, } from "../Type.js";
6
+ import { OwnerId } from "./Owner.js";
7
+ export const evoluSchemaToDbSchema = (schema, indexesConfig) => {
10
8
  const tables = objectToEntries(schema).map(([tableName, table]) => ({
11
9
  name: tableName,
12
10
  columns: objectToEntries(table)
13
11
  .filter(([k]) => k !== "id")
14
12
  .map(([k]) => k),
15
13
  }));
16
- const dbSchema = { tables, indexes: createIndexes(indexes) };
17
- assert(DbSchema.is(dbSchema), "Invalid EvoluSchema: Table and column names must use only characters A-Za-z0-9_- and be at most 256 characters long.");
18
- return dbSchema;
14
+ const indexes = indexesConfig
15
+ ? indexesConfig(createIndex).map((index) => ({
16
+ name: index.toOperationNode().name.name,
17
+ sql: index.compile().sql,
18
+ }))
19
+ : [];
20
+ return { tables, indexes };
19
21
  };
20
- export const DefaultColumns = object({
21
- createdAt: DateIsoString,
22
- updatedAt: DateIsoString,
23
- isDeleted: nullOr(SqliteBoolean),
24
- });
25
22
  /**
26
- * Evolu has to limit the maximum mutation size. Otherwise, sync couldn't use
27
- * the {@link maxProtocolMessageRangesSize}. The max size is 640KB in bytes,
28
- * measured via MessagePack. Evolu Protocol DbChange will be smaller thanks to
29
- * various optimizations.
23
+ * System columns that are implicitly defined by Evolu.
24
+ *
25
+ * - `createdAt`: Set by Evolu on row creation, derived from {@link Timestamp}.
26
+ * - `updatedAt`: Set by Evolu on every row change, derived from {@link Timestamp}.
27
+ * - `isDeleted`: Soft delete flag created by Evolu and used by the developer to
28
+ * mark rows as deleted.
29
+ * - `ownerId`: Represents ownership and logically partitions the database.
30
30
  */
31
- export const maxMutationSize = 655360;
32
- const validMutationSize = (type) => brand("ValidMutationSize", type, (value) => pack(value).byteLength <= maxMutationSize
33
- ? ok(value)
34
- : err({ type: "ValidMutationSize", value }));
35
- export const formatValidMutationSizeError = createTypeErrorFormatter((error) => `The mutation size exceeds the maximum limit of ${maxMutationSize} bytes. The provided mutation has a size of ${pack(error.value).byteLength} bytes.`);
31
+ export const SystemColumns = object({
32
+ createdAt: DateIso,
33
+ updatedAt: DateIso,
34
+ isDeleted: nullOr(SqliteBoolean),
35
+ ownerId: OwnerId,
36
+ });
37
+ export const systemColumns = Object.keys(SystemColumns.props);
36
38
  /**
37
39
  * Type Factory to create insertable {@link Type}. It makes nullable Types
38
- * optional, omits Id, and ensures the {@link maxMutationSize}.
40
+ * optional (so they are not required), omits Id, and ensures the
41
+ * {@link maxMutationSize}.
39
42
  *
40
43
  * ### Example
41
44
  *
@@ -53,7 +56,7 @@ export const insertable = (props) => {
53
56
  };
54
57
  /**
55
58
  * Type Factory to create updateable {@link Type}. It makes everything except for
56
- * the `id` column partial (i.e. optional) and ensures the
59
+ * the `id` column optional (so they are not required) and ensures the
57
60
  * {@link maxMutationSize}.
58
61
  *
59
62
  * ### Example
@@ -76,16 +79,157 @@ export const updateable = (props) => {
76
79
  return validMutationSize(object(updateableProps));
77
80
  };
78
81
  /**
79
- * Type Factory to create upsertable Type. It makes nullable Types optional and
80
- * ensures the {@link maxMutationSize}.
82
+ * Type Factory to create an upsertable Type. It makes nullable Types optional
83
+ * (so they are not required) and ensures the {@link maxMutationSize}.
84
+ *
85
+ * Upsert is like insert, except it requires an ID. It's useful for inserting
86
+ * rows with external ID via {@link createIdFromString}.
87
+ *
88
+ * Note that it's not possible to upsert a row with `createdAt` nor `updatedAt`,
89
+ * because they are derived from {@link CrdtMessage} timestamp. For external
90
+ * createdAt, use a different column.
81
91
  *
82
92
  * ### Example
83
93
  *
84
94
  * ```ts
85
95
  * const UpsertableTodo = upsertable(Schema.todo);
86
96
  * type UpsertableTodo = typeof UpsertableTodo.Type;
87
- * const todo = UpsertableTodo.from({ id, title });
97
+ * const todo = UpsertableTodo.from({
98
+ * id,
99
+ * title,
100
+ * });
88
101
  * if (!todo.ok) return; // handle errors
89
102
  * ```
90
103
  */
91
- export const upsertable = (props) => validMutationSize(nullableToOptional(props));
104
+ export const upsertable = (props) => {
105
+ const propsWithDefaults = {
106
+ ...props,
107
+ isDeleted: optional(SqliteBoolean),
108
+ };
109
+ return validMutationSize(nullableToOptional(propsWithDefaults));
110
+ };
111
+ export const DbTable = object({
112
+ name: String,
113
+ columns: array(String),
114
+ });
115
+ export const DbIndex = object({ name: String, sql: String });
116
+ export const DbSchema = object({
117
+ tables: array(DbTable),
118
+ indexes: array(DbIndex),
119
+ });
120
+ /** Get the current database schema by reading SQLite metadata. */
121
+ export const getDbSchema = (deps) => ({ allIndexes = false } = {}) => {
122
+ const map = new Map();
123
+ const tableAndColumnInfoRows = deps.sqlite.exec(sql `
124
+ select
125
+ sqlite_master.name as tableName,
126
+ table_info.name as columnName
127
+ from
128
+ sqlite_master
129
+ join pragma_table_info(sqlite_master.name) as table_info;
130
+ `);
131
+ if (!tableAndColumnInfoRows.ok)
132
+ return tableAndColumnInfoRows;
133
+ tableAndColumnInfoRows.value.rows.forEach((row) => {
134
+ const { tableName, columnName } = row;
135
+ if (!map.has(tableName))
136
+ map.set(tableName, []);
137
+ map.get(tableName)?.push(columnName);
138
+ });
139
+ const tables = Array.from(map, ([name, columns]) => ({ name, columns }));
140
+ const indexesRows = deps.sqlite.exec(allIndexes
141
+ ? sql `
142
+ select name, sql
143
+ from sqlite_master
144
+ where type = 'index' and name not like 'sqlite_%';
145
+ `
146
+ : sql `
147
+ select name, sql
148
+ from sqlite_master
149
+ where
150
+ type = 'index'
151
+ and name not like 'sqlite_%'
152
+ and name not like 'evolu_%';
153
+ `);
154
+ if (!indexesRows.ok)
155
+ return indexesRows;
156
+ const indexes = indexesRows.value.rows.map((row) => ({
157
+ name: row.name,
158
+ /**
159
+ * SQLite returns "CREATE INDEX" for "create index" for some reason.
160
+ * Other keywords remain unchanged. We have to normalize the casing for
161
+ * {@link indexesAreEqual} manually.
162
+ */
163
+ sql: row.sql
164
+ .replace("CREATE INDEX", "create index")
165
+ .replace("CREATE UNIQUE INDEX", "create unique index"),
166
+ }));
167
+ return ok({ tables, indexes });
168
+ };
169
+ const indexesAreEqual = (self, that) => self.name === that.name && self.sql === that.sql;
170
+ export const ensureDbSchema = (deps) => (newSchema, currentSchema, options) => {
171
+ const queries = [];
172
+ newSchema.tables.forEach((newTable) => {
173
+ const currentTable = currentSchema.tables.find((t) => t.name === newTable.name);
174
+ if (!currentTable) {
175
+ queries.push(createAppTable(newTable));
176
+ }
177
+ else {
178
+ newTable.columns
179
+ .filter((newColumn) => !currentTable.columns.includes(newColumn))
180
+ .forEach((newColumn) => {
181
+ queries.push(sql `
182
+ alter table ${sql.identifier(newTable.name)}
183
+ add column ${sql.identifier(newColumn)} blob;
184
+ `);
185
+ });
186
+ }
187
+ });
188
+ if (options?.ignoreIndexes !== true) {
189
+ // Remove current indexes that are not in the newSchema.
190
+ currentSchema.indexes
191
+ .filter((currentIndex) => !newSchema.indexes.some((newIndex) => indexesAreEqual(newIndex, currentIndex)))
192
+ .forEach((index) => {
193
+ queries.push(sql `drop index ${sql.identifier(index.name)};`);
194
+ });
195
+ // Add new indexes that are not in the currentSchema.
196
+ newSchema.indexes
197
+ .filter((newIndex) => !currentSchema.indexes.some((currentIndex) => indexesAreEqual(newIndex, currentIndex)))
198
+ .forEach((newIndex) => {
199
+ queries.push({ sql: `${newIndex.sql};`, parameters: [] });
200
+ });
201
+ }
202
+ for (const query of queries) {
203
+ const result = deps.sqlite.exec(query);
204
+ if (!result.ok)
205
+ return result;
206
+ }
207
+ return ok();
208
+ };
209
+ const createAppTable = (table) => sql `
210
+ create table ${sql.identifier(table.name)} (
211
+ "id" text,
212
+ ${sql.raw(`${systemColumns
213
+ .concat(table.columns)
214
+ .filter((c) => c !== "id")
215
+ // With strict tables and any type, data is preserved exactly as received
216
+ // without any type affinity coercion. This allows storing any data type
217
+ // while maintaining strict null enforcement for primary key columns.
218
+ .map((name) => `${sql.identifier(name).sql} any`)
219
+ .join(", ")}`)},
220
+ primary key ("ownerId", "id")
221
+ )
222
+ without rowid, strict;
223
+ `;
224
+ // https://kysely.dev/docs/recipes/splitting-query-building-and-execution
225
+ export const kysely = new Kysely.Kysely({
226
+ dialect: {
227
+ createAdapter: () => new Kysely.SqliteAdapter(),
228
+ createDriver: () => new Kysely.DummyDriver(),
229
+ createIntrospector() {
230
+ throw new Error("Not implemeneted");
231
+ },
232
+ createQueryCompiler: () => new Kysely.SqliteQueryCompiler(),
233
+ },
234
+ });
235
+ const createIndex = kysely.schema.createIndex.bind(kysely.schema);