@evolu/common 5.4.7 → 6.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 (242) hide show
  1. package/README.md +30 -34
  2. package/dist/src/Array.d.ts +17 -0
  3. package/dist/src/Array.d.ts.map +1 -0
  4. package/dist/src/Array.js +12 -0
  5. package/dist/src/Assert.d.ts +68 -0
  6. package/dist/src/Assert.d.ts.map +1 -0
  7. package/dist/src/Assert.js +77 -0
  8. package/dist/src/BigInt.d.ts +20 -0
  9. package/dist/src/BigInt.d.ts.map +1 -0
  10. package/dist/src/BigInt.js +18 -0
  11. package/dist/src/Buffer.d.ts +92 -0
  12. package/dist/src/Buffer.d.ts.map +1 -0
  13. package/dist/src/Buffer.js +62 -0
  14. package/dist/src/Callbacks.d.ts +20 -0
  15. package/dist/src/Callbacks.d.ts.map +1 -0
  16. package/dist/src/Callbacks.js +18 -0
  17. package/dist/src/Console.d.ts +78 -0
  18. package/dist/src/Console.d.ts.map +1 -0
  19. package/dist/src/Console.js +103 -0
  20. package/dist/src/Crypto.d.ts +72 -39
  21. package/dist/src/Crypto.d.ts.map +1 -1
  22. package/dist/src/Crypto.js +89 -54
  23. package/dist/src/Eq.d.ts +97 -0
  24. package/dist/src/Eq.d.ts.map +1 -0
  25. package/dist/src/Eq.js +167 -0
  26. package/dist/src/Error.d.ts +14 -10
  27. package/dist/src/Error.d.ts.map +1 -1
  28. package/dist/src/Error.js +43 -11
  29. package/dist/src/Evolu/Config.d.ts +69 -0
  30. package/dist/src/Evolu/Config.d.ts.map +1 -0
  31. package/dist/src/Evolu/Config.js +9 -0
  32. package/dist/src/Evolu/Db.d.ts +126 -0
  33. package/dist/src/Evolu/Db.d.ts.map +1 -0
  34. package/dist/src/Evolu/Db.js +774 -0
  35. package/dist/src/Evolu/Diff.d.ts +43 -0
  36. package/dist/src/Evolu/Diff.d.ts.map +1 -0
  37. package/dist/src/Evolu/Diff.js +95 -0
  38. package/dist/src/Evolu/Evolu.d.ts +334 -0
  39. package/dist/src/Evolu/Evolu.d.ts.map +1 -0
  40. package/dist/src/Evolu/Evolu.js +434 -0
  41. package/dist/src/Evolu/Internal.d.ts +26 -0
  42. package/dist/src/Evolu/Internal.d.ts.map +1 -0
  43. package/dist/src/Evolu/Internal.js +25 -0
  44. package/dist/src/Evolu/Kysely.d.ts +6 -0
  45. package/dist/src/Evolu/Kysely.d.ts.map +1 -0
  46. package/dist/src/Evolu/Kysely.js +21 -0
  47. package/dist/src/Evolu/Owner.d.ts +155 -0
  48. package/dist/src/Evolu/Owner.d.ts.map +1 -0
  49. package/dist/src/Evolu/Owner.js +126 -0
  50. package/dist/src/Evolu/Platform.d.ts +23 -0
  51. package/dist/src/Evolu/Platform.d.ts.map +1 -0
  52. package/dist/src/Evolu/Platform.js +1 -0
  53. package/dist/src/Evolu/Protocol.d.ts +401 -0
  54. package/dist/src/Evolu/Protocol.d.ts.map +1 -0
  55. package/dist/src/Evolu/Protocol.js +1151 -0
  56. package/dist/src/Evolu/Public.d.ts +18 -0
  57. package/dist/src/Evolu/Public.d.ts.map +1 -0
  58. package/dist/src/Evolu/Public.js +11 -0
  59. package/dist/src/Evolu/PublicKysely.d.ts +148 -0
  60. package/dist/src/Evolu/PublicKysely.d.ts.map +1 -0
  61. package/dist/src/Evolu/PublicKysely.js +185 -0
  62. package/dist/src/Evolu/Query.d.ts +63 -0
  63. package/dist/src/Evolu/Query.d.ts.map +1 -0
  64. package/dist/src/Evolu/Query.js +61 -0
  65. package/dist/src/Evolu/Relay.d.ts +13 -0
  66. package/dist/src/Evolu/Relay.d.ts.map +1 -0
  67. package/dist/src/Evolu/Relay.js +109 -0
  68. package/dist/src/Evolu/Schema.d.ts +201 -0
  69. package/dist/src/Evolu/Schema.d.ts.map +1 -0
  70. package/dist/src/Evolu/Schema.js +150 -0
  71. package/dist/src/Evolu/Storage.d.ts +49 -0
  72. package/dist/src/Evolu/Storage.d.ts.map +1 -0
  73. package/dist/src/Evolu/Storage.js +1111 -0
  74. package/dist/src/Evolu/Sync.d.ts +59 -0
  75. package/dist/src/Evolu/Sync.d.ts.map +1 -0
  76. package/dist/src/Evolu/Sync.js +29 -0
  77. package/dist/src/Evolu/Timestamp.d.ts +106 -0
  78. package/dist/src/Evolu/Timestamp.d.ts.map +1 -0
  79. package/dist/src/Evolu/Timestamp.js +179 -0
  80. package/dist/src/Function.d.ts +54 -0
  81. package/dist/src/Function.d.ts.map +1 -0
  82. package/dist/src/Function.js +38 -0
  83. package/dist/src/ManyToManyMap.d.ts +26 -0
  84. package/dist/src/ManyToManyMap.d.ts.map +1 -0
  85. package/dist/src/ManyToManyMap.js +92 -0
  86. package/dist/src/NanoId.d.ts +27 -0
  87. package/dist/src/NanoId.d.ts.map +1 -0
  88. package/dist/src/NanoId.js +6 -0
  89. package/dist/src/Number.d.ts +42 -0
  90. package/dist/src/Number.d.ts.map +1 -0
  91. package/dist/src/Number.js +55 -0
  92. package/dist/src/Object.d.ts +35 -0
  93. package/dist/src/Object.d.ts.map +1 -0
  94. package/dist/src/Object.js +36 -0
  95. package/dist/src/Order.d.ts +90 -0
  96. package/dist/src/Order.d.ts.map +1 -0
  97. package/dist/src/Order.js +85 -0
  98. package/dist/src/Promise.d.ts +180 -0
  99. package/dist/src/Promise.d.ts.map +1 -0
  100. package/dist/src/Promise.js +176 -0
  101. package/dist/src/Random.d.ts +52 -0
  102. package/dist/src/Random.d.ts.map +1 -0
  103. package/dist/src/Random.js +29 -0
  104. package/dist/src/Ref.d.ts +40 -0
  105. package/dist/src/Ref.d.ts.map +1 -0
  106. package/dist/src/Ref.js +13 -0
  107. package/dist/src/Result.d.ts +421 -0
  108. package/dist/src/Result.d.ts.map +1 -0
  109. package/dist/src/Result.js +357 -0
  110. package/dist/src/Skiplist.d.ts +23 -0
  111. package/dist/src/Skiplist.d.ts.map +1 -0
  112. package/dist/src/Skiplist.js +58 -0
  113. package/dist/src/Sqlite.d.ts +116 -52
  114. package/dist/src/Sqlite.d.ts.map +1 -1
  115. package/dist/src/Sqlite.js +183 -67
  116. package/dist/src/Store.d.ts +45 -8
  117. package/dist/src/Store.d.ts.map +1 -1
  118. package/dist/src/Store.js +33 -17
  119. package/dist/src/String.d.ts +2 -0
  120. package/dist/src/String.d.ts.map +1 -0
  121. package/dist/src/String.js +14 -0
  122. package/dist/src/Time.d.ts +20 -0
  123. package/dist/src/Time.d.ts.map +1 -0
  124. package/dist/src/Time.js +25 -0
  125. package/dist/src/Type.d.ts +1937 -0
  126. package/dist/src/Type.d.ts.map +1 -0
  127. package/dist/src/Type.js +2002 -0
  128. package/dist/src/Types.d.ts +188 -0
  129. package/dist/src/Types.d.ts.map +1 -0
  130. package/dist/src/Types.js +6 -0
  131. package/dist/src/WebSocket.d.ts +112 -0
  132. package/dist/src/WebSocket.d.ts.map +1 -0
  133. package/dist/src/WebSocket.js +139 -0
  134. package/dist/src/Worker.d.ts +44 -0
  135. package/dist/src/Worker.d.ts.map +1 -0
  136. package/dist/src/Worker.js +66 -0
  137. package/dist/src/index.d.ts +24 -11
  138. package/dist/src/index.d.ts.map +1 -1
  139. package/dist/src/index.js +24 -11
  140. package/package.json +29 -38
  141. package/src/Array.ts +39 -0
  142. package/src/Assert.ts +116 -0
  143. package/src/BigInt.ts +29 -0
  144. package/src/Buffer.ts +175 -0
  145. package/src/Callbacks.ts +43 -0
  146. package/src/Console.ts +159 -0
  147. package/src/Crypto.ts +169 -115
  148. package/src/Eq.ts +204 -0
  149. package/src/Error.ts +57 -20
  150. package/src/Evolu/Config.ts +83 -0
  151. package/src/Evolu/Db.ts +1275 -0
  152. package/src/Evolu/Diff.ts +142 -0
  153. package/src/Evolu/Evolu.ts +947 -0
  154. package/src/Evolu/Internal.ts +26 -0
  155. package/src/Evolu/Kysely.ts +38 -0
  156. package/src/Evolu/Owner.ts +296 -0
  157. package/src/Evolu/Platform.ts +27 -0
  158. package/src/Evolu/Protocol.ts +1857 -0
  159. package/src/Evolu/Public.ts +43 -0
  160. package/src/Evolu/PublicKysely.ts +240 -0
  161. package/src/Evolu/Query.ts +167 -0
  162. package/src/Evolu/Relay.ts +142 -0
  163. package/src/Evolu/Schema.ts +417 -0
  164. package/src/Evolu/Storage.ts +1281 -0
  165. package/src/Evolu/Sync.ts +105 -0
  166. package/src/Evolu/Timestamp.ts +311 -0
  167. package/src/Function.ts +58 -0
  168. package/src/ManyToManyMap.ts +140 -0
  169. package/src/NanoId.ts +39 -0
  170. package/src/Number.ts +90 -0
  171. package/src/Object.ts +64 -0
  172. package/src/Order.ts +113 -0
  173. package/src/Promise.ts +295 -0
  174. package/src/Random.ts +68 -0
  175. package/src/Ref.ts +63 -0
  176. package/src/Result.ts +453 -0
  177. package/src/Skiplist.ts +102 -0
  178. package/src/Sqlite.ts +366 -153
  179. package/src/Store.ts +79 -36
  180. package/src/String.ts +10 -0
  181. package/src/Time.ts +36 -0
  182. package/src/Type.ts +3978 -0
  183. package/src/Types.ts +209 -0
  184. package/src/WebSocket.ts +273 -0
  185. package/src/Worker.ts +129 -0
  186. package/src/index.ts +24 -11
  187. package/dist/src/Config.d.ts +0 -56
  188. package/dist/src/Config.d.ts.map +0 -1
  189. package/dist/src/Config.js +0 -39
  190. package/dist/src/Crdt.d.ts +0 -89
  191. package/dist/src/Crdt.d.ts.map +0 -1
  192. package/dist/src/Crdt.js +0 -181
  193. package/dist/src/Db.d.ts +0 -107
  194. package/dist/src/Db.d.ts.map +0 -1
  195. package/dist/src/Db.js +0 -443
  196. package/dist/src/Diff.d.ts +0 -27
  197. package/dist/src/Diff.d.ts.map +0 -1
  198. package/dist/src/Diff.js +0 -84
  199. package/dist/src/Evolu.d.ts +0 -426
  200. package/dist/src/Evolu.d.ts.map +0 -1
  201. package/dist/src/Evolu.js +0 -333
  202. package/dist/src/Model.d.ts +0 -141
  203. package/dist/src/Model.d.ts.map +0 -1
  204. package/dist/src/Model.js +0 -125
  205. package/dist/src/Murmurhash.d.ts +0 -2
  206. package/dist/src/Murmurhash.d.ts.map +0 -1
  207. package/dist/src/Murmurhash.js +0 -60
  208. package/dist/src/Owner.d.ts +0 -33
  209. package/dist/src/Owner.d.ts.map +0 -1
  210. package/dist/src/Owner.js +0 -26
  211. package/dist/src/Platform.d.ts +0 -37
  212. package/dist/src/Platform.d.ts.map +0 -1
  213. package/dist/src/Platform.js +0 -11
  214. package/dist/src/Protobuf.d.ts +0 -81
  215. package/dist/src/Protobuf.d.ts.map +0 -1
  216. package/dist/src/Protobuf.js +0 -92
  217. package/dist/src/Public.d.ts +0 -13
  218. package/dist/src/Public.d.ts.map +0 -1
  219. package/dist/src/Public.js +0 -6
  220. package/dist/src/Socket.d.ts +0 -8
  221. package/dist/src/Socket.d.ts.map +0 -1
  222. package/dist/src/Socket.js +0 -51
  223. package/dist/src/Sql.d.ts +0 -12
  224. package/dist/src/Sql.d.ts.map +0 -1
  225. package/dist/src/Sql.js +0 -30
  226. package/dist/src/Sync.d.ts +0 -70
  227. package/dist/src/Sync.d.ts.map +0 -1
  228. package/dist/src/Sync.js +0 -127
  229. package/src/Config.ts +0 -119
  230. package/src/Crdt.ts +0 -361
  231. package/src/Db.ts +0 -955
  232. package/src/Diff.ts +0 -114
  233. package/src/Evolu.ts +0 -1016
  234. package/src/Model.ts +0 -233
  235. package/src/Murmurhash.ts +0 -70
  236. package/src/Owner.ts +0 -69
  237. package/src/Platform.ts +0 -47
  238. package/src/Protobuf.ts +0 -155
  239. package/src/Public.ts +0 -12
  240. package/src/Socket.ts +0 -83
  241. package/src/Sql.ts +0 -41
  242. package/src/Sync.ts +0 -315
@@ -1,426 +0,0 @@
1
- import * as S from "@effect/schema/Schema";
2
- import * as Context from "effect/Context";
3
- import * as Effect from "effect/Effect";
4
- import * as Layer from "effect/Layer";
5
- import * as Record from "effect/Record";
6
- import * as Kysely from "kysely";
7
- import { Config } from "./Config.js";
8
- import { TimestampString } from "./Crdt.js";
9
- import { Mnemonic, NanoIdGenerator } from "./Crypto.js";
10
- import { DbFactory, DbSchema, Index, Queries, Query, QueryResult, QueryResultsPromisesFromQueries, Row } from "./Db.js";
11
- import { EvoluError } from "./Error.js";
12
- import { Id, SqliteBoolean, SqliteDate } from "./Model.js";
13
- import { Owner } from "./Owner.js";
14
- import { AppState } from "./Platform.js";
15
- import { SqliteQueryOptions, Value } from "./Sqlite.js";
16
- import { Listener, Unsubscribe } from "./Store.js";
17
- import { SyncState } from "./Sync.js";
18
- /**
19
- * The Evolu interface provides a type-safe SQL query building and state
20
- * management defined by a database schema. It leverages Kysely for creating SQL
21
- * queries in TypeScript, enabling operations such as data querying, loading,
22
- * subscription to data changes, and mutations (create, update, createOrUpdate).
23
- * It also includes functionalities for error handling, syncing state
24
- * management, and owner data manipulation. Specifically, Evolu allows:
25
- *
26
- * - Subscribing to and getting errors via subscribeError and getError.
27
- * - Creating type-safe SQL queries with createQuery, leveraging Kysely's
28
- * capabilities.
29
- * - Loading queries and subscribing to query result changes using loadQuery,
30
- * loadQueries, subscribeQuery, and getQuery.
31
- * - Subscribing to and getting the owner's information and sync state changes.
32
- * - Performing mutations on the database with create, update, and createOrUpdate
33
- * methods, which include automatic management of common columns like
34
- * createdAt, updatedAt, and isDeleted.
35
- * - Managing owner data with resetOwner and restoreOwner.
36
- * - Ensuring the database schema's integrity with ensureSchema.
37
- */
38
- export interface Evolu<T extends EvoluSchema = EvoluSchema> {
39
- readonly socket: Promise<WebSocket | undefined>;
40
- /**
41
- * Subscribe to {@link EvoluError} changes.
42
- *
43
- * @example
44
- * const unsubscribe = evolu.subscribeError(() => {
45
- * const error = evolu.getError();
46
- * console.log(error);
47
- * });
48
- */
49
- readonly subscribeError: (listener: Listener) => Unsubscribe;
50
- /** Get {@link EvoluError}. */
51
- readonly getError: () => EvoluError | null;
52
- /**
53
- * Create type-safe SQL {@link Query}.
54
- *
55
- * Evolu uses Kysely - the type-safe SQL query builder for TypeScript. See
56
- * https://kysely.dev.
57
- *
58
- * For mutations, use {@link create} and {@link update}.
59
- *
60
- * @example
61
- * const allTodos = evolu.createQuery((db) =>
62
- * db.selectFrom("todo").selectAll(),
63
- * );
64
- *
65
- * const todoById = (id: TodoId) =>
66
- * evolu.createQuery((db) =>
67
- * db.selectFrom("todo").selectAll().where("id", "=", id),
68
- * );
69
- */
70
- readonly createQuery: <R extends Row>(queryCallback: (db: Pick<Kysely.Kysely<{
71
- [Table in keyof T]: NullableExceptIdCreatedAtUpdatedAt<{
72
- [Column in keyof T[Table]]: T[Table][Column];
73
- }>;
74
- } & {
75
- readonly evolu_message: {
76
- readonly timestamp: TimestampString;
77
- readonly table: keyof T;
78
- readonly row: Id;
79
- readonly column: string;
80
- readonly value: Value;
81
- };
82
- }>, "selectFrom" | "fn" | "with" | "withRecursive">) => Kysely.SelectQueryBuilder<any, any, R>, options?: SqliteQueryOptions) => Query<R>;
83
- /**
84
- * Load {@link Query} and return a promise with {@link QueryResult}.
85
- *
86
- * A returned promise always resolves successfully because there is no reason
87
- * why loading should fail. All data are local, and the query is typed. A
88
- * serious unexpected Evolu error shall be handled with
89
- * {@link subscribeError}.
90
- *
91
- * Loading is batched, and returned promises are cached, so there is no need
92
- * for an additional cache. Evolu's internal cache is invalidated on
93
- * mutation.
94
- *
95
- * The returned promise is enriched with special status and value properties
96
- * for the upcoming React `use` Hook, but other UI libraries can also leverage
97
- * them. Speaking of React, there are two essential React Suspense-related
98
- * patterns that every developer should be aware of—passing promises to
99
- * children and caching over mutations.
100
- *
101
- * With promises passed to children, we can load a query as soon as possible,
102
- * but we don't have to use the returned promise immediately. That's useful
103
- * for prefetching, which is generally not necessary for local-first apps but
104
- * can be if a query takes a long time to load.
105
- *
106
- * Caching over mutation is a pattern that every developer should know. As we
107
- * said, Evolu caches promise until a mutation happens. A query loaded after
108
- * that will return a new pending promise. That's okay for general usage but
109
- * not for UI with React Suspense because a mutation would suspend rerendered
110
- * queries on a page, and that's not a good UX.
111
- *
112
- * We call this pattern "caching over mutation" because it has no globally
113
- * accepted name yet. React RFC for React Cache does not exist yet.
114
- *
115
- * For better UX, a query must be subscribed for updates. This way, instead of
116
- * Suspense flashes, the user sees new data immediately because Evolu replaces
117
- * cached promises with fresh, already resolved new ones.
118
- *
119
- * If you are curious why Evolu does not do that for all queries by default,
120
- * the answer is simple: performance. Tracking changes is costly and
121
- * meaningful only for visible (hence subscribed) queries anyway. To subscribe
122
- * to a query, use {@link subscribeQuery}.
123
- *
124
- * @example
125
- * const allTodos = evolu.createQuery((db) =>
126
- * db.selectFrom("todo").selectAll(),
127
- * );
128
- * evolu.loadQuery(allTodos).then(({ rows }) => {
129
- * console.log(rows);
130
- * });
131
- */
132
- readonly loadQuery: <R extends Row>(query: Query<R>) => Promise<QueryResult<R>>;
133
- /**
134
- * Load an array of {@link Query} queries and return an array of
135
- * {@link QueryResult} promises. It's like `queries.map(loadQuery)` but with
136
- * proper types for returned promises.
137
- *
138
- * @example
139
- * evolu.loadQueries([allTodos, todoById(1)]);
140
- */
141
- readonly loadQueries: <R extends Row, Q extends Queries<R>>(queries: [...Q]) => [...QueryResultsPromisesFromQueries<Q>];
142
- /**
143
- * Subscribe to {@link Query} {@link QueryResult} changes.
144
- *
145
- * @example
146
- * const unsubscribe = evolu.subscribeQuery(allTodos)(() => {
147
- * const { rows } = evolu.getQuery(allTodos);
148
- * });
149
- */
150
- readonly subscribeQuery: (query: Query) => (listener: Listener) => Unsubscribe;
151
- /**
152
- * Get {@link Query} {@link QueryResult}.
153
- *
154
- * @example
155
- * const unsubscribe = evolu.subscribeQuery(allTodos)(() => {
156
- * const { rows } = evolu.getQuery(allTodos);
157
- * });
158
- */
159
- readonly getQuery: <R extends Row>(query: Query<R>) => QueryResult<R>;
160
- /**
161
- * Subscribe to {@link Owner} changes.
162
- *
163
- * @example
164
- * const unsubscribe = evolu.subscribeOwner(() => {
165
- * const owner = evolu.getOwner();
166
- * });
167
- */
168
- readonly subscribeOwner: (listener: Listener) => Unsubscribe;
169
- /**
170
- * Get {@link Owner}.
171
- *
172
- * @example
173
- * const unsubscribe = evolu.subscribeOwner(() => {
174
- * const owner = evolu.getOwner();
175
- * });
176
- */
177
- readonly getOwner: () => Owner | null;
178
- /**
179
- * Subscribe to {@link SyncState} changes.
180
- *
181
- * @example
182
- * const unsubscribe = evolu.subscribeSyncState(() => {
183
- * const syncState = evolu.getSyncState();
184
- * });
185
- */
186
- readonly subscribeSyncState: (listener: Listener) => Unsubscribe;
187
- /**
188
- * Get {@link SyncState}.
189
- *
190
- * @example
191
- * const unsubscribe = evolu.subscribeSyncState(() => {
192
- * const syncState = evolu.getSyncState();
193
- * });
194
- */
195
- readonly getSyncState: () => SyncState;
196
- /**
197
- * Create a row in the database and returns a new ID. The first argument is
198
- * the table name, and the second is an object.
199
- *
200
- * The third optional argument, the onComplete callback, is generally
201
- * unnecessary because creating a row cannot fail. Still, UI libraries can use
202
- * it to ensure the DOM is updated if we want to manipulate it, for example,
203
- * to focus an element.
204
- *
205
- * Evolu does not use SQL for mutations to ensure data can be safely and
206
- * predictably merged without conflicts.
207
- *
208
- * Explicit mutations also allow Evolu to automatically add and update a few
209
- * useful columns common to all tables. Those columns are: `createdAt`,
210
- * `updatedAt`, and `isDeleted`.
211
- *
212
- * @example
213
- * import * as S from "@effect/schema/Schema";
214
- *
215
- * // Evolu uses the Schema to enforce domain model.
216
- * const title = S.decodeSync(Evolu.NonEmptyString1000)("A title");
217
- *
218
- * const { id } = evolu.create("todo", { title }, () => {
219
- * // onComplete callback
220
- * });
221
- */
222
- create: Mutate<T, "create">;
223
- /**
224
- * Update a row in the database and return the existing ID. The first argument
225
- * is the table name, and the second is an object.
226
- *
227
- * The third optional argument, the onComplete callback, is generally
228
- * unnecessary because updating a row cannot fail. Still, UI libraries can use
229
- * it to ensure the DOM is updated if we want to manipulate it, for example,
230
- * to focus an element.
231
- *
232
- * Evolu does not use SQL for mutations to ensure data can be safely and
233
- * predictably merged without conflicts.
234
- *
235
- * Explicit mutations also allow Evolu to automatically add and update a few
236
- * useful columns common to all tables. Those columns are: `createdAt`,
237
- * `updatedAt`, and `isDeleted`.
238
- *
239
- * @example
240
- * import * as S from "@effect/schema/Schema";
241
- *
242
- * // Evolu uses the Schema to enforce domain model.
243
- * const title = S.decodeSync(Evolu.NonEmptyString1000)("A title");
244
- * evolu.update("todo", { id, title });
245
- *
246
- * // To delete a row, set `isDeleted` to true.
247
- * evolu.update("todo", { id, isDeleted: true });
248
- */
249
- update: Mutate<T, "update">;
250
- /**
251
- * Create or update a row in the database and return the existing ID. The
252
- * first argument is the table name, and the second is an object.
253
- *
254
- * This function is useful when we already have an `id` and want to create a
255
- * new row or update an existing one.
256
- *
257
- * The third optional argument, the onComplete callback, is generally
258
- * unnecessary because updating a row cannot fail. Still, UI libraries can use
259
- * it to ensure the DOM is updated if we want to manipulate it, for example,
260
- * to focus an element.
261
- *
262
- * Evolu does not use SQL for mutations to ensure data can be safely and
263
- * predictably merged without conflicts.
264
- *
265
- * Explicit mutations also allow Evolu to automatically add and update a few
266
- * useful columns common to all tables. Those columns are: `createdAt`,
267
- * `updatedAt`, and `isDeleted`.
268
- *
269
- * @example
270
- * import * as S from "@effect/schema/Schema";
271
- * import { Id } from "@evolu/react";
272
- *
273
- * // Id can be stable.
274
- * // 2024-02-0800000000000
275
- * const id = S.decodeSync(Id)(date.toString().padEnd(21, "0")) as TodoId;
276
- *
277
- * evolu.createOrUpdate("todo", { id, title });
278
- */
279
- createOrUpdate: Mutate<T, "createOrUpdate">;
280
- /**
281
- * Delete {@link Owner} and all their data from the current device. After the
282
- * deletion, Evolu will purge the application state. For browsers, this will
283
- * reload all tabs using Evolu. For native apps, it will restart the app.
284
- *
285
- * Reloading can be turned off via options if you want to provide a different
286
- * UX.
287
- */
288
- readonly resetOwner: (options?: {
289
- readonly reload: boolean;
290
- }) => Promise<void>;
291
- /**
292
- * Restore {@link Owner} with all their synced data. It uses {@link resetOwner},
293
- * so be careful.
294
- */
295
- readonly restoreOwner: (mnemonic: Mnemonic, options?: {
296
- readonly reload: boolean;
297
- }) => Promise<void>;
298
- /**
299
- * Reload the app in a platform-specific way. For browsers, this will reload
300
- * all tabs using Evolu. For native apps, it will restart the app.
301
- */
302
- readonly reloadApp: () => void;
303
- /**
304
- * Ensure tables and columns defined in {@link EvoluSchema} exist in the
305
- * database.
306
- *
307
- * This function is for hot/live reloading.
308
- */
309
- readonly ensureSchema: (schema: DbSchema) => void;
310
- /** Export SQLite database as Uint8Array. */
311
- readonly exportDatabase: () => Promise<Uint8Array>;
312
- }
313
- /** A type to define tables, columns, and column types. */
314
- export type EvoluSchema = Record.ReadonlyRecord<string, Record.ReadonlyRecord<string, Value> & {
315
- readonly id: Id;
316
- }>;
317
- type NullableExceptIdCreatedAtUpdatedAt<T> = {
318
- readonly [K in keyof T]: K extends "id" | "createdAt" | "updatedAt" ? T[K] : T[K] | null;
319
- };
320
- type Mutate<T extends EvoluSchema = EvoluSchema, Mode extends "create" | "update" | "createOrUpdate" = "update"> = <K extends keyof T>(table: K, values: Kysely.Simplify<Mode extends "create" ? PartialForNullable<Castable<Omit<T[K], "id" | "createdAt" | "updatedAt" | "isDeleted">>> : Mode extends "update" ? Partial<Castable<Omit<T[K], "id" | "createdAt" | "updatedAt">>> & {
321
- readonly id: T[K]["id"];
322
- } : PartialForNullable<Castable<Omit<T[K], "createdAt" | "updatedAt" | "isDeleted">>>>, onComplete?: MutateOnComplete) => {
323
- readonly id: T[K]["id"];
324
- };
325
- type MutateOnComplete = () => void;
326
- type PartialForNullable<T, NK extends keyof T = {
327
- [K in keyof T]: null extends T[K] ? K : never;
328
- }[keyof T], NP = Pick<T, Exclude<keyof T, NK>> & Partial<Pick<T, NK>>> = {
329
- [K in keyof NP]: NP[K];
330
- };
331
- /**
332
- * SQLite doesn't support Date nor Boolean types, so Evolu emulates them with
333
- * {@link SqliteBoolean} and {@link SqliteDate}.
334
- */
335
- type Castable<T> = {
336
- readonly [K in keyof T]: T[K] extends SqliteBoolean ? boolean | SqliteBoolean : T[K] extends null | SqliteBoolean ? null | boolean | SqliteBoolean : T[K] extends SqliteDate ? Date | SqliteDate : T[K] extends null | SqliteDate ? null | Date | SqliteDate : T[K];
337
- };
338
- declare const EvoluFactory_base: Context.TagClass<EvoluFactory, "EvoluFactory", {
339
- /**
340
- * Create Evolu from the database schema.
341
- *
342
- * Tables with a name prefixed with `_` are local-only, which means they are
343
- * never synced. It's useful for device-specific or temporal data.
344
- *
345
- * @example
346
- * import * as S from "@effect/schema/Schema";
347
- * import * as E from "@evolu/react";
348
- * // The same API for different platforms
349
- * // import * as E from "@evolu/react-native";
350
- * // import * as E from "@evolu/common-web";
351
- *
352
- * const TodoId = E.id("Todo");
353
- * type TodoId = typeof TodoId.Type;
354
- *
355
- * const TodoTable = E.table({
356
- * id: TodoId,
357
- * title: E.NonEmptyString1000,
358
- * });
359
- * type TodoTable = typeof TodoTable.Type;
360
- *
361
- * const Database = E.database({
362
- * todo: TodoTable,
363
- *
364
- * // Prefix `_` makes the table local-only (it will not sync)
365
- * _todo: TodoTable,
366
- * });
367
- * type Database = typeof Database.Type;
368
- *
369
- * const evolu = E.createEvolu(Database);
370
- */
371
- readonly createEvolu: <T extends EvoluSchema, I>(schema: S.Schema<T, I>, config?: Partial<EvoluConfig<T>>) => Evolu<T>;
372
- }>;
373
- export declare class EvoluFactory extends EvoluFactory_base {
374
- static Common: Layer.Layer<EvoluFactory, never, NanoIdGenerator | AppState | DbFactory>;
375
- }
376
- export interface EvoluConfig<T extends EvoluSchema = EvoluSchema> extends Config {
377
- /**
378
- * Use the `indexes` option to define SQLite indexes.
379
- *
380
- * Table and column names are not typed because Kysely doesn't support it.
381
- *
382
- * https://medium.com/@JasonWyatt/squeezing-performance-from-sqlite-indexes-indexes-c4e175f3c346
383
- *
384
- * @example
385
- * const indexes = [
386
- * createIndex("indexTodoCreatedAt").on("todo").column("createdAt"),
387
- *
388
- * createIndex("indexTodoCategoryCreatedAt")
389
- * .on("todoCategory")
390
- * .column("createdAt"),
391
- * ];
392
- */
393
- indexes: ReadonlyArray<Index>;
394
- /** Use this option to create initial data (fixtures). */
395
- initialData: (evolu: EvoluForInitialData<T>) => void;
396
- /**
397
- * Use this option to create Evolu with the specified mnemonic. If omitted,
398
- * the mnemonic will be autogenerated. That should be the default behavior
399
- * until special UX requirements are needed (e.g., multitenancy).
400
- */
401
- mnemonic: Mnemonic;
402
- }
403
- interface EvoluForInitialData<T extends EvoluSchema = EvoluSchema> {
404
- create: Mutate<T, "create">;
405
- createOrUpdate: Mutate<T, "createOrUpdate">;
406
- }
407
- declare const createIndex: (indexName: string) => Kysely.CreateIndexBuilder;
408
- type CreateIndex = typeof createIndex;
409
- /**
410
- * Create SQLite indexes.
411
- *
412
- * See https://www.evolu.dev/docs/indexes
413
- *
414
- * @example
415
- * const indexes = createIndexes((create) => [
416
- * create("indexTodoCreatedAt").on("todo").column("createdAt"),
417
- * create("indexTodoCategoryCreatedAt")
418
- * .on("todoCategory")
419
- * .column("createdAt"),
420
- * ]);
421
- */
422
- export declare const createIndexes: (callback: (create: CreateIndex) => ReadonlyArray<Kysely.CreateIndexBuilder<any>>) => ReadonlyArray<Index>;
423
- /** Create a namespaced lock name. */
424
- export declare const getLockName: (name: string) => Effect.Effect<string, never, Config>;
425
- export {};
426
- //# sourceMappingURL=Evolu.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Evolu.d.ts","sourceRoot":"","sources":["../../src/Evolu.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,uBAAuB,CAAC;AAG3C,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AAExC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAKtC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,EAAE,MAAM,EAAgC,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAExD,OAAO,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