@evolu/common 2.2.3 → 3.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.
- package/dist/src/Db.d.ts +62 -3
- package/dist/src/Db.d.ts.map +1 -1
- package/dist/src/Db.js +44 -2
- package/dist/src/ErrorStore.d.ts +3 -0
- package/dist/src/ErrorStore.d.ts.map +1 -1
- package/dist/src/Evolu.d.ts +22 -24
- package/dist/src/Evolu.d.ts.map +1 -1
- package/dist/src/Public.d.ts +1 -0
- package/dist/src/Public.d.ts.map +1 -1
- package/dist/src/Public.js +1 -0
- package/package.json +7 -7
- package/src/Db.ts +97 -8
- package/src/ErrorStore.ts +4 -0
- package/src/Evolu.ts +36 -33
- package/src/Public.ts +1 -0
package/dist/src/Db.d.ts
CHANGED
|
@@ -1,15 +1,74 @@
|
|
|
1
1
|
import * as S from "@effect/schema/Schema";
|
|
2
|
-
import { Brand, Context, Effect, Layer, ReadonlyArray, ReadonlyRecord } from "effect";
|
|
2
|
+
import { Brand, Context, Effect, Layer, ReadonlyArray, ReadonlyRecord, Types } from "effect";
|
|
3
3
|
import * as Kysely from "kysely";
|
|
4
4
|
import { Bip39, Mnemonic, NanoId } from "./Crypto.js";
|
|
5
5
|
import { Id } from "./Model.js";
|
|
6
6
|
import { Owner } from "./Owner.js";
|
|
7
7
|
import { Sqlite, SqliteQuery, Value } from "./Sqlite.js";
|
|
8
8
|
import { Store } from "./Store.js";
|
|
9
|
-
|
|
10
|
-
export type
|
|
9
|
+
import { EvoluTypeError } from "./ErrorStore.js";
|
|
10
|
+
export type DatabaseSchema = ReadonlyRecord.ReadonlyRecord<TableSchema>;
|
|
11
|
+
type TableSchema = ReadonlyRecord.ReadonlyRecord<Value> & {
|
|
11
12
|
readonly id: Id;
|
|
12
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Create table schema.
|
|
16
|
+
*
|
|
17
|
+
* Supported types are null, string, number, Uint8Array, JSON Object, and JSON
|
|
18
|
+
* Array. Use SqliteDate for dates and SqliteBoolean for booleans.
|
|
19
|
+
*
|
|
20
|
+
* Reserved columns are createdAt, updatedAt, isDeleted. Those columns are added
|
|
21
|
+
* by default.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const TodoId = id("Todo");
|
|
25
|
+
* type TodoId = S.Schema.To<typeof TodoId>;
|
|
26
|
+
*
|
|
27
|
+
* const TodoTable = table({
|
|
28
|
+
* id: TodoId,
|
|
29
|
+
* title: NonEmptyString1000,
|
|
30
|
+
* isCompleted: S.nullable(SqliteBoolean),
|
|
31
|
+
* });
|
|
32
|
+
* type TodoTable = S.Schema.To<typeof TodoTable>;
|
|
33
|
+
*/
|
|
34
|
+
export declare const table: <Fields extends TableFields>(fields: Fields) => ValidateFieldsTypes<Fields> extends true ? ValidateFieldsNames<Fields> extends true ? ValidateFieldsHasId<Fields> extends true ? S.Schema<Types.Simplify<{ readonly [K in Exclude<keyof Fields, S.FromOptionalKeys<Fields>>]: S.Schema.From<Fields[K]>; } & { readonly [K_1 in S.FromOptionalKeys<Fields>]?: S.Schema.From<Fields[K_1]>; } & {
|
|
35
|
+
readonly createdAt: string;
|
|
36
|
+
readonly updatedAt: string;
|
|
37
|
+
readonly isDeleted: number;
|
|
38
|
+
}>, Types.Simplify<{ readonly [K_2 in Exclude<keyof Fields, S.ToOptionalKeys<Fields>>]: S.Schema.To<Fields[K_2]>; } & { readonly [K_3 in S.ToOptionalKeys<Fields>]?: S.Schema.To<Fields[K_3]>; } & {
|
|
39
|
+
readonly createdAt: string & Brand.Brand<"SqliteDate">;
|
|
40
|
+
readonly updatedAt: string & Brand.Brand<"SqliteDate">;
|
|
41
|
+
readonly isDeleted: number & Brand.Brand<"SqliteBoolean">;
|
|
42
|
+
}>> : EvoluTypeError<"table() called without id column."> : EvoluTypeError<"table() called with a reserved column. Reserved columns are createdAt, updatedAt, isDeleted. Those columns are added by default."> : EvoluTypeError<"table() called with unsupported type. Supported types are null, string, number, Uint8Array, JSON Object, and JSON Array. Use SqliteDate for dates and SqliteBoolean for booleans.">;
|
|
43
|
+
declare const ReservedColumns: S.Schema<{
|
|
44
|
+
readonly createdAt: string;
|
|
45
|
+
readonly updatedAt: string;
|
|
46
|
+
readonly isDeleted: number;
|
|
47
|
+
}, {
|
|
48
|
+
readonly createdAt: string & Brand.Brand<"SqliteDate">;
|
|
49
|
+
readonly updatedAt: string & Brand.Brand<"SqliteDate">;
|
|
50
|
+
readonly isDeleted: number & Brand.Brand<"SqliteBoolean">;
|
|
51
|
+
}>;
|
|
52
|
+
type TableFields = Record<string, S.Schema<any, any>>;
|
|
53
|
+
type ValidateFieldsTypes<Fields extends TableFields> = keyof Fields extends infer K ? K extends keyof Fields ? Fields[K] extends TableFields ? ValidateFieldsTypes<Fields[K]> : Fields[K] extends S.Schema<infer _I, infer A> ? A extends Value ? true : false : never : never : never;
|
|
54
|
+
type ValidateFieldsNames<Fields extends TableFields> = keyof Fields extends infer K ? K extends keyof Fields ? K extends "createdAt" | "updatedAt" | "isDeleted" ? false : true : never : never;
|
|
55
|
+
type ValidateFieldsHasId<Fields extends TableFields> = "id" extends keyof Fields ? true : false;
|
|
56
|
+
/**
|
|
57
|
+
* Create database schema.
|
|
58
|
+
*
|
|
59
|
+
* Tables with a name prefixed with _ are local-only, which means they are not
|
|
60
|
+
* synced. Local-only tables are useful for device-specific or temporal data.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* const Database = database({
|
|
64
|
+
* // A local-only table.
|
|
65
|
+
* _todo: TodoTable,
|
|
66
|
+
* todo: TodoTable,
|
|
67
|
+
* todoCategory: TodoCategoryTable,
|
|
68
|
+
* });
|
|
69
|
+
* type Database = S.Schema.To<typeof Database>;
|
|
70
|
+
*/
|
|
71
|
+
export declare const database: <Fields extends S.StructFields>(fields: Fields) => S.Schema<Types.Simplify<S.FromStruct<Fields>>, Types.Simplify<S.ToStruct<Fields>>>;
|
|
13
72
|
declare const __queryBrand: unique symbol;
|
|
14
73
|
/**
|
|
15
74
|
* The query is an SQL query serialized as a string with a branded type
|
package/dist/src/Db.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Db.d.ts","sourceRoot":"","sources":["../../src/Db.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,uBAAuB,CAAC;AAG3C,OAAO,EACL,KAAK,EACL,OAAO,EACP,MAAM,EAEN,KAAK,EAGL,aAAa,EACb,cAAc,
|
|
1
|
+
{"version":3,"file":"Db.d.ts","sourceRoot":"","sources":["../../src/Db.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,uBAAuB,CAAC;AAG3C,OAAO,EACL,KAAK,EACL,OAAO,EACP,MAAM,EAEN,KAAK,EAGL,aAAa,EACb,cAAc,EAEd,KAAK,EAEN,MAAM,QAAQ,CAAC;AAChB,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAOjC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,EAAE,EAA6B,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,KAAK,EAAa,MAAM,YAAY,CAAC;AAO9C,OAAO,EAEL,MAAM,EACN,WAAW,EACX,KAAK,EAEN,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,KAAK,EAAa,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,MAAM,MAAM,cAAc,GAAG,cAAc,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;AAExE,KAAK,WAAW,GAAG,cAAc,CAAC,cAAc,CAAC,KAAK,CAAC,GAAG;IACxD,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC;CACjB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,KAAK;;;;;;;;oZAiByC,CAAC;AAE5D,QAAA,MAAM,eAAe;;;;;;;;EAInB,CAAC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EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;CACzC;AAgBD,eAAO,MAAM,cAAc,WAAY,EAAE,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,WAUtD,CAAC;AAEJ,eAAO,MAAM,WAAW,8EAUrB,CAAC;AAEJ,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,IAAI,EAAE,0BAA0B,CAAC;CAC3C;AAED,eAAO,MAAM,8BAA8B;;;;EAMzC,CAAC;AACH,MAAM,MAAM,8BAA8B,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,CACtD,OAAO,8BAA8B,CACtC,CAAC;AAEF,eAAO,MAAM,oCAAoC,8FAShD,CAAC;AAEF,eAAO,MAAM,QAAQ,cACR,QAAQ,KAClB,aAAa,CAAC,MAAM,GAAG,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,KAAK,CA4BlD,CAAC;AA6DL,eAAO,MAAM,YAAY,sBAEtB,aAAa,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,CAUjC,CAAC;AAEJ,MAAM,MAAM,SAAS,GAAG,KAAK,CAAC,cAAc,CAAC,CAAC;AAC9C,eAAO,MAAM,SAAS,mCAA2B,CAAC;AAElD,KAAK,cAAc,GAAG,WAAW,CAAC,KAAK,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC;AAE7D,eAAO,MAAM,aAAa,sCAGzB,CAAC"}
|
package/dist/src/Db.js
CHANGED
|
@@ -4,10 +4,53 @@ import { make } from "@effect/schema/Schema";
|
|
|
4
4
|
import { bytesToHex } from "@noble/ciphers/utils";
|
|
5
5
|
import { Context, Effect, Exit, Layer, Option, Predicate, ReadonlyArray, ReadonlyRecord, String, pipe, } from "effect";
|
|
6
6
|
import { initialMerkleTree, makeInitialTimestamp, merkleTreeToString, timestampToString, } from "./Crdt.js";
|
|
7
|
+
import { SqliteBoolean, SqliteDate } from "./Model.js";
|
|
7
8
|
import { makeOwner } from "./Owner.js";
|
|
8
9
|
import { createMessageTable, createMessageTableIndex, createOwnerTable, insertOwner, } from "./Sql.js";
|
|
9
10
|
import { Sqlite, isJsonObjectOrArray, } from "./Sqlite.js";
|
|
10
11
|
import { makeStore } from "./Store.js";
|
|
12
|
+
/**
|
|
13
|
+
* Create table schema.
|
|
14
|
+
*
|
|
15
|
+
* Supported types are null, string, number, Uint8Array, JSON Object, and JSON
|
|
16
|
+
* Array. Use SqliteDate for dates and SqliteBoolean for booleans.
|
|
17
|
+
*
|
|
18
|
+
* Reserved columns are createdAt, updatedAt, isDeleted. Those columns are added
|
|
19
|
+
* by default.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* const TodoId = id("Todo");
|
|
23
|
+
* type TodoId = S.Schema.To<typeof TodoId>;
|
|
24
|
+
*
|
|
25
|
+
* const TodoTable = table({
|
|
26
|
+
* id: TodoId,
|
|
27
|
+
* title: NonEmptyString1000,
|
|
28
|
+
* isCompleted: S.nullable(SqliteBoolean),
|
|
29
|
+
* });
|
|
30
|
+
* type TodoTable = S.Schema.To<typeof TodoTable>;
|
|
31
|
+
*/
|
|
32
|
+
export const table = (fields) => S.struct(fields).pipe(S.extend(ReservedColumns));
|
|
33
|
+
const ReservedColumns = S.struct({
|
|
34
|
+
createdAt: SqliteDate,
|
|
35
|
+
updatedAt: SqliteDate,
|
|
36
|
+
isDeleted: SqliteBoolean,
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* Create database schema.
|
|
40
|
+
*
|
|
41
|
+
* Tables with a name prefixed with _ are local-only, which means they are not
|
|
42
|
+
* synced. Local-only tables are useful for device-specific or temporal data.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const Database = database({
|
|
46
|
+
* // A local-only table.
|
|
47
|
+
* _todo: TodoTable,
|
|
48
|
+
* todo: TodoTable,
|
|
49
|
+
* todoCategory: TodoCategoryTable,
|
|
50
|
+
* });
|
|
51
|
+
* type Database = S.Schema.To<typeof Database>;
|
|
52
|
+
*/
|
|
53
|
+
export const database = S.struct;
|
|
11
54
|
// We use queries as keys, hence JSON.stringify.
|
|
12
55
|
export const serializeQuery = ({ sql, parameters, }) => {
|
|
13
56
|
const query = {
|
|
@@ -55,10 +98,9 @@ const getPropertySignatures = (schema) => {
|
|
|
55
98
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
56
99
|
return out;
|
|
57
100
|
};
|
|
58
|
-
const commonColumns = ["createdAt", "updatedAt", "isDeleted"];
|
|
59
101
|
export const schemaToTables = (schema) => pipe(getPropertySignatures(schema), ReadonlyRecord.toEntries, ReadonlyArray.map(([name, schema]) => ({
|
|
60
102
|
name,
|
|
61
|
-
columns: Object.keys(getPropertySignatures(schema))
|
|
103
|
+
columns: Object.keys(getPropertySignatures(schema)),
|
|
62
104
|
})));
|
|
63
105
|
export const transaction = (effect) => Effect.flatMap(Sqlite, (sqlite) => Effect.acquireUseRelease(sqlite.exec("BEGIN"), () => effect, (_, exit) => Exit.isFailure(exit) ? sqlite.exec("ROLLBACK") : sqlite.exec("END")));
|
|
64
106
|
export const SqliteNoSuchTableOrColumnError = S.struct({
|
package/dist/src/ErrorStore.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ErrorStore.d.ts","sourceRoot":"","sources":["../../src/ErrorStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3C,eAAO,MAAM,cAAc,4EAAqC,CAAC;AAEjE,+EAA+E;AAC/E,MAAM,MAAM,UAAU,GAAG,eAAe,GAAG,cAAc,CAAC;AAE1D;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC;IACjC,QAAQ,CAAC,KAAK,EAAE,iBAAiB,CAAC;CACnC;AAED,eAAO,MAAM,mBAAmB,UACvB,OAAO,KACb,aAAa,CAAC,KAAK,EAAE,eAAe,EAAE,KAAK,CAU7C,CAAC;AAEF;;;;GAIG;AACH,UAAU,iBAAiB;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC"}
|
|
1
|
+
{"version":3,"file":"ErrorStore.d.ts","sourceRoot":"","sources":["../../src/ErrorStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3C,eAAO,MAAM,cAAc,4EAAqC,CAAC;AAEjE,+EAA+E;AAC/E,MAAM,MAAM,UAAU,GAAG,eAAe,GAAG,cAAc,CAAC;AAE1D;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC;IACjC,QAAQ,CAAC,KAAK,EAAE,iBAAiB,CAAC;CACnC;AAED,eAAO,MAAM,mBAAmB,UACvB,OAAO,KACb,aAAa,CAAC,KAAK,EAAE,eAAe,EAAE,KAAK,CAU7C,CAAC;AAEF;;;;GAIG;AACH,UAAU,iBAAiB;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC;AAED,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,MAAM;IAC9C,QAAQ,CAAC,kBAAkB,EAAE,CAAC,CAAC;CAChC"}
|
package/dist/src/Evolu.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Context, Layer } from "effect";
|
|
|
3
3
|
import * as Kysely from "kysely";
|
|
4
4
|
import { Config } from "./Config.js";
|
|
5
5
|
import { Mnemonic } from "./Crypto.js";
|
|
6
|
-
import { Queries, Query, QueryResult, QueryResultsPromisesFromQueries, Row, RowsStore,
|
|
6
|
+
import { Queries, Query, QueryResult, QueryResultsPromisesFromQueries, Row, RowsStore, DatabaseSchema } from "./Db.js";
|
|
7
7
|
import { DbWorker } from "./DbWorker.js";
|
|
8
8
|
import { EvoluError } from "./ErrorStore.js";
|
|
9
9
|
import { SqliteBoolean, SqliteDate } from "./Model.js";
|
|
@@ -11,7 +11,7 @@ import { Owner } from "./Owner.js";
|
|
|
11
11
|
import { AppState, FlushSync } from "./Platform.js";
|
|
12
12
|
import { Store } from "./Store.js";
|
|
13
13
|
import { SyncState } from "./SyncWorker.js";
|
|
14
|
-
export interface Evolu<
|
|
14
|
+
export interface Evolu<S extends DatabaseSchema = DatabaseSchema> {
|
|
15
15
|
/**
|
|
16
16
|
* Subscribe to {@link EvoluError} changes.
|
|
17
17
|
*
|
|
@@ -42,7 +42,7 @@ export interface Evolu<T extends Schema = Schema> {
|
|
|
42
42
|
* db.selectFrom("todo").selectAll().where("id", "=", id),
|
|
43
43
|
* );
|
|
44
44
|
*/
|
|
45
|
-
readonly createQuery: CreateQuery<
|
|
45
|
+
readonly createQuery: CreateQuery<S>;
|
|
46
46
|
/**
|
|
47
47
|
* Load {@link Query} and return a promise with {@link QueryResult}.
|
|
48
48
|
*
|
|
@@ -182,7 +182,7 @@ export interface Evolu<T extends Schema = Schema> {
|
|
|
182
182
|
* // onComplete callback
|
|
183
183
|
* });
|
|
184
184
|
*/
|
|
185
|
-
create: Create<
|
|
185
|
+
create: Create<S>;
|
|
186
186
|
/**
|
|
187
187
|
* Update a row in the database and returns the existing ID. The first
|
|
188
188
|
* argument is the table name, and the second is an object.
|
|
@@ -209,7 +209,7 @@ export interface Evolu<T extends Schema = Schema> {
|
|
|
209
209
|
* // To delete a row, set `isDeleted` to true.
|
|
210
210
|
* evolu.update("todo", { id, isDeleted: true });
|
|
211
211
|
*/
|
|
212
|
-
update: Update<
|
|
212
|
+
update: Update<S>;
|
|
213
213
|
/**
|
|
214
214
|
* Delete {@link Owner} and all their data from the current device. After the
|
|
215
215
|
* deletion, Evolu will purge the application state. For browsers, this will
|
|
@@ -218,8 +218,11 @@ export interface Evolu<T extends Schema = Schema> {
|
|
|
218
218
|
readonly resetOwner: () => void;
|
|
219
219
|
/** Restore {@link Owner} with all their synced data. */
|
|
220
220
|
readonly restoreOwner: (mnemonic: Mnemonic) => void;
|
|
221
|
-
/**
|
|
222
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Ensure tables and columns defined in {@link DatabaseSchema} exist in the
|
|
223
|
+
* database.
|
|
224
|
+
*/
|
|
225
|
+
readonly ensureSchema: <From, To extends S>(schema: S.Schema<From, To>) => void;
|
|
223
226
|
/**
|
|
224
227
|
* Force sync with Evolu Server.
|
|
225
228
|
*
|
|
@@ -229,23 +232,18 @@ export interface Evolu<T extends Schema = Schema> {
|
|
|
229
232
|
*/
|
|
230
233
|
readonly sync: () => void;
|
|
231
234
|
}
|
|
232
|
-
export declare const Evolu: Context.Tag<Evolu<
|
|
233
|
-
type CreateQuery<S extends
|
|
234
|
-
type QueryCallback<S extends
|
|
235
|
-
type QuerySchema<S extends
|
|
235
|
+
export declare const Evolu: Context.Tag<Evolu<DatabaseSchema>, Evolu<DatabaseSchema>>;
|
|
236
|
+
type CreateQuery<S extends DatabaseSchema> = <R extends Row>(queryCallback: QueryCallback<S, R>) => Query<R>;
|
|
237
|
+
type QueryCallback<S extends DatabaseSchema, R extends Row> = (db: Pick<Kysely.Kysely<QuerySchema<S>>, "selectFrom" | "fn">) => Kysely.SelectQueryBuilder<any, any, R>;
|
|
238
|
+
type QuerySchema<S extends DatabaseSchema> = {
|
|
236
239
|
readonly [Table in keyof S]: NullableExceptId<{
|
|
237
240
|
readonly [Column in keyof S[Table]]: S[Table][Column];
|
|
238
|
-
}
|
|
241
|
+
}>;
|
|
239
242
|
};
|
|
240
243
|
type NullableExceptId<T> = {
|
|
241
244
|
readonly [K in keyof T]: K extends "id" ? T[K] : T[K] | null;
|
|
242
245
|
};
|
|
243
|
-
export
|
|
244
|
-
readonly createdAt: SqliteDate;
|
|
245
|
-
readonly updatedAt: SqliteDate;
|
|
246
|
-
readonly isDeleted: SqliteBoolean;
|
|
247
|
-
}
|
|
248
|
-
export declare const makeCreateQuery: <S extends Schema = Schema>() => CreateQuery<S>;
|
|
246
|
+
export declare const makeCreateQuery: <S extends DatabaseSchema = DatabaseSchema>() => CreateQuery<S>;
|
|
249
247
|
export interface LoadingPromises {
|
|
250
248
|
readonly get: <R extends Row>(query: Query<R>) => {
|
|
251
249
|
readonly promise: LoadingPromise<R>;
|
|
@@ -274,14 +272,14 @@ export interface SubscribedQueries {
|
|
|
274
272
|
}
|
|
275
273
|
export declare const SubscribedQueries: Context.Tag<SubscribedQueries, SubscribedQueries>;
|
|
276
274
|
export declare const SubscribedQueriesLive: Layer.Layer<RowsStore, never, SubscribedQueries>;
|
|
277
|
-
export type Create<S extends
|
|
278
|
-
export type Update<S extends
|
|
279
|
-
export type Mutate<S extends
|
|
275
|
+
export type Create<S extends DatabaseSchema = DatabaseSchema> = Mutate<S, "create">;
|
|
276
|
+
export type Update<S extends DatabaseSchema = DatabaseSchema> = Mutate<S, "update">;
|
|
277
|
+
export type Mutate<S extends DatabaseSchema = DatabaseSchema, Mode extends "create" | "update" = "update"> = <K extends keyof S>(table: K, values: Kysely.Simplify<Mode extends "create" ? PartialForNullable<Castable<Omit<S[K], "id" | "createdAt" | "updatedAt" | "isDeleted">>> : Partial<Castable<Omit<S[K], "id" | "createdAt" | "updatedAt">>> & {
|
|
280
278
|
readonly id: S[K]["id"];
|
|
281
279
|
}>, onComplete?: () => void) => {
|
|
282
280
|
readonly id: S[K]["id"];
|
|
283
281
|
};
|
|
284
|
-
export declare const Mutate: Context.Tag<Mutate<
|
|
282
|
+
export declare const Mutate: Context.Tag<Mutate<DatabaseSchema, "update">, Mutate<DatabaseSchema, "update">>;
|
|
285
283
|
type PartialForNullable<T, NK extends keyof T = {
|
|
286
284
|
[K in keyof T]: null extends T[K] ? K : never;
|
|
287
285
|
}[keyof T], NP = Pick<T, Exclude<keyof T, NK>> & Partial<Pick<T, NK>>> = {
|
|
@@ -298,7 +296,7 @@ type Castable<T> = {
|
|
|
298
296
|
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];
|
|
299
297
|
};
|
|
300
298
|
/** EvoluCommonLive has only platform independent side-effects. */
|
|
301
|
-
export declare const EvoluCommonLive: Layer.Layer<Config | FlushSync | AppState | DbWorker, never, Evolu<
|
|
302
|
-
export declare const makeCreateEvolu: (EvoluLive: Layer.Layer<Config, never, Evolu
|
|
299
|
+
export declare const EvoluCommonLive: Layer.Layer<Config | FlushSync | AppState | DbWorker, never, Evolu<DatabaseSchema>>;
|
|
300
|
+
export declare const makeCreateEvolu: (EvoluLive: Layer.Layer<Config, never, Evolu>) => <From, To extends DatabaseSchema>(schema: S.Schema<From, To>, config?: Partial<Config>) => Evolu<To>;
|
|
303
301
|
export {};
|
|
304
302
|
//# sourceMappingURL=Evolu.d.ts.map
|
package/dist/src/Evolu.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Evolu.d.ts","sourceRoot":"","sources":["../../src/Evolu.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,uBAAuB,CAAC;AAC3C,OAAO,EACL,OAAO,EAIP,KAAK,EAKN,MAAM,QAAQ,CAAC;AAChB,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,EAAE,MAAM,EAAc,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,QAAQ,EAAsB,MAAM,aAAa,CAAC;AAC3D,OAAO,EACL,OAAO,EACP,KAAK,EACL,WAAW,EACX,+BAA+B,EAC/B,GAAG,EACH,SAAS,EAET,
|
|
1
|
+
{"version":3,"file":"Evolu.d.ts","sourceRoot":"","sources":["../../src/Evolu.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,uBAAuB,CAAC;AAC3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EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IACvD;KAAG,CAAC,IAAI,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;CAAE,CAAC;AAE/B;;;;;;GAMG;AACH,KAAK,QAAQ,CAAC,CAAC,IAAI;IACjB,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,aAAa,GAC/C,OAAO,GAAG,aAAa,GACvB,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,aAAa,GAC/B,IAAI,GAAG,OAAO,GAAG,aAAa,GAC9B,CAAC,CAAC,CAAC,CAAC,SAAS,UAAU,GACrB,IAAI,GAAG,UAAU,GACjB,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,UAAU,GAC5B,IAAI,GAAG,IAAI,GAAG,UAAU,GACxB,CAAC,CAAC,CAAC,CAAC;CACf,CAAC;AA+IF,kEAAkE;AAClE,eAAO,MAAM,eAAe,qFAE3B,CAAC;AAEF,eAAO,MAAM,eAAe,cACd,WAAW,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,4EAGlC,QAAQ,MAAM,CAAC,cAYzB,CAAC"}
|
package/dist/src/Public.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type { Timestamp, TimestampError } from "./Crdt.js";
|
|
|
3
3
|
export type { Mnemonic, InvalidMnemonicError } from "./Crypto.js";
|
|
4
4
|
export type { EvoluError, UnexpectedError } from "./ErrorStore.js";
|
|
5
5
|
export * from "./Model.js";
|
|
6
|
+
export { table, database } from "./Db.js";
|
|
6
7
|
export type { Owner, OwnerId } from "./Owner.js";
|
|
7
8
|
export { canUseDom } from "./Platform.js";
|
|
8
9
|
export type { SyncState } from "./SyncWorker.js";
|
package/dist/src/Public.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Public.d.ts","sourceRoot":"","sources":["../../src/Public.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtE,YAAY,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3D,YAAY,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAClE,YAAY,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACnE,cAAc,YAAY,CAAC;AAC3B,YAAY,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,YAAY,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"Public.d.ts","sourceRoot":"","sources":["../../src/Public.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtE,YAAY,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3D,YAAY,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAClE,YAAY,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACnE,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC1C,YAAY,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,YAAY,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/src/Public.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evolu/common",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Local-first platform designed for privacy, ease of use, and no vendor lock-in to sync and backup people's lifetime data",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"evolu",
|
|
@@ -44,19 +44,19 @@
|
|
|
44
44
|
"README.md"
|
|
45
45
|
],
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@noble/ciphers": "^0.4.
|
|
48
|
-
"@noble/hashes": "^1.3.
|
|
47
|
+
"@noble/ciphers": "^0.4.1",
|
|
48
|
+
"@noble/hashes": "^1.3.3",
|
|
49
49
|
"@protobuf-ts/runtime": "^2.9.3",
|
|
50
50
|
"@scure/bip39": "^1.2.1",
|
|
51
51
|
"kysely": "^0.26.3",
|
|
52
52
|
"nanoid": "^5.0.4"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@effect/schema": "0.
|
|
55
|
+
"@effect/schema": "0.53.0",
|
|
56
56
|
"@protobuf-ts/plugin": "^2.9.3",
|
|
57
57
|
"@protobuf-ts/protoc": "^2.9.3",
|
|
58
58
|
"array-shuffle": "^3.0.0",
|
|
59
|
-
"effect": "2.0.0-next.
|
|
59
|
+
"effect": "2.0.0-next.60",
|
|
60
60
|
"eslint": "^8.54.0",
|
|
61
61
|
"typescript": "^5.3.3",
|
|
62
62
|
"vitest": "^1.0.4",
|
|
@@ -64,8 +64,8 @@
|
|
|
64
64
|
"eslint-config-evolu": "1.0.0"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"@effect/schema": "0.
|
|
68
|
-
"effect": "2.0.0-next.
|
|
67
|
+
"@effect/schema": "0.53.0",
|
|
68
|
+
"effect": "2.0.0-next.60"
|
|
69
69
|
},
|
|
70
70
|
"publishConfig": {
|
|
71
71
|
"access": "public"
|
package/src/Db.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
ReadonlyArray,
|
|
14
14
|
ReadonlyRecord,
|
|
15
15
|
String,
|
|
16
|
+
Types,
|
|
16
17
|
pipe,
|
|
17
18
|
} from "effect";
|
|
18
19
|
import * as Kysely from "kysely";
|
|
@@ -23,7 +24,7 @@ import {
|
|
|
23
24
|
timestampToString,
|
|
24
25
|
} from "./Crdt.js";
|
|
25
26
|
import { Bip39, Mnemonic, NanoId } from "./Crypto.js";
|
|
26
|
-
import { Id } from "./Model.js";
|
|
27
|
+
import { Id, SqliteBoolean, SqliteDate } from "./Model.js";
|
|
27
28
|
import { Owner, makeOwner } from "./Owner.js";
|
|
28
29
|
import {
|
|
29
30
|
createMessageTable,
|
|
@@ -39,13 +40,105 @@ import {
|
|
|
39
40
|
isJsonObjectOrArray,
|
|
40
41
|
} from "./Sqlite.js";
|
|
41
42
|
import { Store, makeStore } from "./Store.js";
|
|
43
|
+
import { EvoluTypeError } from "./ErrorStore.js";
|
|
42
44
|
|
|
43
|
-
export type
|
|
45
|
+
export type DatabaseSchema = ReadonlyRecord.ReadonlyRecord<TableSchema>;
|
|
44
46
|
|
|
45
|
-
|
|
47
|
+
type TableSchema = ReadonlyRecord.ReadonlyRecord<Value> & {
|
|
46
48
|
readonly id: Id;
|
|
47
49
|
};
|
|
48
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Create table schema.
|
|
53
|
+
*
|
|
54
|
+
* Supported types are null, string, number, Uint8Array, JSON Object, and JSON
|
|
55
|
+
* Array. Use SqliteDate for dates and SqliteBoolean for booleans.
|
|
56
|
+
*
|
|
57
|
+
* Reserved columns are createdAt, updatedAt, isDeleted. Those columns are added
|
|
58
|
+
* by default.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* const TodoId = id("Todo");
|
|
62
|
+
* type TodoId = S.Schema.To<typeof TodoId>;
|
|
63
|
+
*
|
|
64
|
+
* const TodoTable = table({
|
|
65
|
+
* id: TodoId,
|
|
66
|
+
* title: NonEmptyString1000,
|
|
67
|
+
* isCompleted: S.nullable(SqliteBoolean),
|
|
68
|
+
* });
|
|
69
|
+
* type TodoTable = S.Schema.To<typeof TodoTable>;
|
|
70
|
+
*/
|
|
71
|
+
export const table = <Fields extends TableFields>(
|
|
72
|
+
fields: Fields,
|
|
73
|
+
// Because Schema is invariant, we have to do validation like this.
|
|
74
|
+
): ValidateFieldsTypes<Fields> extends true
|
|
75
|
+
? ValidateFieldsNames<Fields> extends true
|
|
76
|
+
? ValidateFieldsHasId<Fields> extends true
|
|
77
|
+
? S.Schema<
|
|
78
|
+
Types.Simplify<
|
|
79
|
+
S.FromStruct<Fields> & S.Schema.From<typeof ReservedColumns>
|
|
80
|
+
>,
|
|
81
|
+
Types.Simplify<
|
|
82
|
+
S.ToStruct<Fields> & S.Schema.To<typeof ReservedColumns>
|
|
83
|
+
>
|
|
84
|
+
>
|
|
85
|
+
: EvoluTypeError<"table() called without id column.">
|
|
86
|
+
: EvoluTypeError<"table() called with a reserved column. Reserved columns are createdAt, updatedAt, isDeleted. Those columns are added by default.">
|
|
87
|
+
: EvoluTypeError<"table() called with unsupported type. Supported types are null, string, number, Uint8Array, JSON Object, and JSON Array. Use SqliteDate for dates and SqliteBoolean for booleans."> =>
|
|
88
|
+
S.struct(fields).pipe(S.extend(ReservedColumns)) as never;
|
|
89
|
+
|
|
90
|
+
const ReservedColumns = S.struct({
|
|
91
|
+
createdAt: SqliteDate,
|
|
92
|
+
updatedAt: SqliteDate,
|
|
93
|
+
isDeleted: SqliteBoolean,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
type TableFields = Record<string, S.Schema<any, any>>;
|
|
97
|
+
|
|
98
|
+
type ValidateFieldsTypes<Fields extends TableFields> =
|
|
99
|
+
keyof Fields extends infer K
|
|
100
|
+
? K extends keyof Fields
|
|
101
|
+
? Fields[K] extends TableFields
|
|
102
|
+
? ValidateFieldsTypes<Fields[K]>
|
|
103
|
+
: // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
104
|
+
Fields[K] extends S.Schema<infer _I, infer A>
|
|
105
|
+
? A extends Value
|
|
106
|
+
? true
|
|
107
|
+
: false
|
|
108
|
+
: never
|
|
109
|
+
: never
|
|
110
|
+
: never;
|
|
111
|
+
|
|
112
|
+
type ValidateFieldsNames<Fields extends TableFields> =
|
|
113
|
+
keyof Fields extends infer K
|
|
114
|
+
? K extends keyof Fields
|
|
115
|
+
? K extends "createdAt" | "updatedAt" | "isDeleted"
|
|
116
|
+
? false
|
|
117
|
+
: true
|
|
118
|
+
: never
|
|
119
|
+
: never;
|
|
120
|
+
|
|
121
|
+
type ValidateFieldsHasId<Fields extends TableFields> = "id" extends keyof Fields
|
|
122
|
+
? true
|
|
123
|
+
: false;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Create database schema.
|
|
127
|
+
*
|
|
128
|
+
* Tables with a name prefixed with _ are local-only, which means they are not
|
|
129
|
+
* synced. Local-only tables are useful for device-specific or temporal data.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* const Database = database({
|
|
133
|
+
* // A local-only table.
|
|
134
|
+
* _todo: TodoTable,
|
|
135
|
+
* todo: TodoTable,
|
|
136
|
+
* todoCategory: TodoCategoryTable,
|
|
137
|
+
* });
|
|
138
|
+
* type Database = S.Schema.To<typeof Database>;
|
|
139
|
+
*/
|
|
140
|
+
export const database = S.struct;
|
|
141
|
+
|
|
49
142
|
// https://blog.beraliv.dev/2021-05-07-opaque-type-in-typescript
|
|
50
143
|
declare const __queryBrand: unique symbol;
|
|
51
144
|
|
|
@@ -168,8 +261,6 @@ const getPropertySignatures = <I extends { [K in keyof A]: any }, A>(
|
|
|
168
261
|
return out as any;
|
|
169
262
|
};
|
|
170
263
|
|
|
171
|
-
const commonColumns = ["createdAt", "updatedAt", "isDeleted"];
|
|
172
|
-
|
|
173
264
|
export const schemaToTables = (schema: S.Schema<any, any>): Tables =>
|
|
174
265
|
pipe(
|
|
175
266
|
getPropertySignatures(schema),
|
|
@@ -177,9 +268,7 @@ export const schemaToTables = (schema: S.Schema<any, any>): Tables =>
|
|
|
177
268
|
ReadonlyArray.map(
|
|
178
269
|
([name, schema]): Table => ({
|
|
179
270
|
name,
|
|
180
|
-
columns: Object.keys(getPropertySignatures(schema))
|
|
181
|
-
commonColumns,
|
|
182
|
-
),
|
|
271
|
+
columns: Object.keys(getPropertySignatures(schema)),
|
|
183
272
|
}),
|
|
184
273
|
),
|
|
185
274
|
);
|
package/src/ErrorStore.ts
CHANGED
package/src/Evolu.ts
CHANGED
|
@@ -22,7 +22,7 @@ import {
|
|
|
22
22
|
Row,
|
|
23
23
|
RowsStore,
|
|
24
24
|
RowsStoreLive,
|
|
25
|
-
|
|
25
|
+
DatabaseSchema,
|
|
26
26
|
emptyRows,
|
|
27
27
|
queryResultFromRows,
|
|
28
28
|
schemaToTables,
|
|
@@ -39,7 +39,7 @@ import { SqliteQuery } from "./Sqlite.js";
|
|
|
39
39
|
import { Store, Unsubscribe, makeStore } from "./Store.js";
|
|
40
40
|
import { SyncState } from "./SyncWorker.js";
|
|
41
41
|
|
|
42
|
-
export interface Evolu<
|
|
42
|
+
export interface Evolu<S extends DatabaseSchema = DatabaseSchema> {
|
|
43
43
|
/**
|
|
44
44
|
* Subscribe to {@link EvoluError} changes.
|
|
45
45
|
*
|
|
@@ -72,7 +72,7 @@ export interface Evolu<T extends Schema = Schema> {
|
|
|
72
72
|
* db.selectFrom("todo").selectAll().where("id", "=", id),
|
|
73
73
|
* );
|
|
74
74
|
*/
|
|
75
|
-
readonly createQuery: CreateQuery<
|
|
75
|
+
readonly createQuery: CreateQuery<S>;
|
|
76
76
|
|
|
77
77
|
/**
|
|
78
78
|
* Load {@link Query} and return a promise with {@link QueryResult}.
|
|
@@ -223,7 +223,7 @@ export interface Evolu<T extends Schema = Schema> {
|
|
|
223
223
|
* // onComplete callback
|
|
224
224
|
* });
|
|
225
225
|
*/
|
|
226
|
-
create: Create<
|
|
226
|
+
create: Create<S>;
|
|
227
227
|
|
|
228
228
|
/**
|
|
229
229
|
* Update a row in the database and returns the existing ID. The first
|
|
@@ -251,7 +251,7 @@ export interface Evolu<T extends Schema = Schema> {
|
|
|
251
251
|
* // To delete a row, set `isDeleted` to true.
|
|
252
252
|
* evolu.update("todo", { id, isDeleted: true });
|
|
253
253
|
*/
|
|
254
|
-
update: Update<
|
|
254
|
+
update: Update<S>;
|
|
255
255
|
|
|
256
256
|
/**
|
|
257
257
|
* Delete {@link Owner} and all their data from the current device. After the
|
|
@@ -263,8 +263,11 @@ export interface Evolu<T extends Schema = Schema> {
|
|
|
263
263
|
/** Restore {@link Owner} with all their synced data. */
|
|
264
264
|
readonly restoreOwner: (mnemonic: Mnemonic) => void;
|
|
265
265
|
|
|
266
|
-
/**
|
|
267
|
-
|
|
266
|
+
/**
|
|
267
|
+
* Ensure tables and columns defined in {@link DatabaseSchema} exist in the
|
|
268
|
+
* database.
|
|
269
|
+
*/
|
|
270
|
+
readonly ensureSchema: <From, To extends S>(
|
|
268
271
|
schema: S.Schema<From, To>,
|
|
269
272
|
) => void;
|
|
270
273
|
|
|
@@ -280,11 +283,11 @@ export interface Evolu<T extends Schema = Schema> {
|
|
|
280
283
|
|
|
281
284
|
export const Evolu = Context.Tag<Evolu>();
|
|
282
285
|
|
|
283
|
-
type CreateQuery<S extends
|
|
286
|
+
type CreateQuery<S extends DatabaseSchema> = <R extends Row>(
|
|
284
287
|
queryCallback: QueryCallback<S, R>,
|
|
285
288
|
) => Query<R>;
|
|
286
289
|
|
|
287
|
-
type QueryCallback<S extends
|
|
290
|
+
type QueryCallback<S extends DatabaseSchema, R extends Row> = (
|
|
288
291
|
db: Pick<
|
|
289
292
|
Kysely.Kysely<QuerySchema<S>>,
|
|
290
293
|
| "selectFrom"
|
|
@@ -294,25 +297,17 @@ type QueryCallback<S extends Schema, R extends Row> = (
|
|
|
294
297
|
>,
|
|
295
298
|
) => Kysely.SelectQueryBuilder<any, any, R>;
|
|
296
299
|
|
|
297
|
-
type QuerySchema<S extends
|
|
298
|
-
readonly [Table in keyof S]: NullableExceptId<
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
} & CommonColumns
|
|
302
|
-
>;
|
|
300
|
+
type QuerySchema<S extends DatabaseSchema> = {
|
|
301
|
+
readonly [Table in keyof S]: NullableExceptId<{
|
|
302
|
+
readonly [Column in keyof S[Table]]: S[Table][Column];
|
|
303
|
+
}>;
|
|
303
304
|
};
|
|
304
305
|
|
|
305
306
|
type NullableExceptId<T> = {
|
|
306
307
|
readonly [K in keyof T]: K extends "id" ? T[K] : T[K] | null;
|
|
307
308
|
};
|
|
308
309
|
|
|
309
|
-
|
|
310
|
-
readonly createdAt: SqliteDate;
|
|
311
|
-
readonly updatedAt: SqliteDate;
|
|
312
|
-
readonly isDeleted: SqliteBoolean;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
const kysely = new Kysely.Kysely<QuerySchema<Schema>>({
|
|
310
|
+
const kysely = new Kysely.Kysely<QuerySchema<DatabaseSchema>>({
|
|
316
311
|
dialect: {
|
|
317
312
|
createAdapter: (): Kysely.DialectAdapter => new Kysely.SqliteAdapter(),
|
|
318
313
|
createDriver: (): Kysely.Driver => new Kysely.DummyDriver(),
|
|
@@ -325,7 +320,7 @@ const kysely = new Kysely.Kysely<QuerySchema<Schema>>({
|
|
|
325
320
|
});
|
|
326
321
|
|
|
327
322
|
export const makeCreateQuery =
|
|
328
|
-
<S extends
|
|
323
|
+
<S extends DatabaseSchema = DatabaseSchema>(): CreateQuery<S> =>
|
|
329
324
|
<R extends Row>(queryCallback: QueryCallback<S, R>) =>
|
|
330
325
|
pipe(
|
|
331
326
|
queryCallback(kysely as Kysely.Kysely<QuerySchema<S>>).compile(),
|
|
@@ -548,21 +543,29 @@ export const SubscribedQueriesLive = Layer.effect(
|
|
|
548
543
|
}),
|
|
549
544
|
);
|
|
550
545
|
|
|
551
|
-
export type Create<S extends
|
|
546
|
+
export type Create<S extends DatabaseSchema = DatabaseSchema> = Mutate<
|
|
547
|
+
S,
|
|
548
|
+
"create"
|
|
549
|
+
>;
|
|
552
550
|
|
|
553
|
-
export type Update<S extends
|
|
551
|
+
export type Update<S extends DatabaseSchema = DatabaseSchema> = Mutate<
|
|
552
|
+
S,
|
|
553
|
+
"update"
|
|
554
|
+
>;
|
|
554
555
|
|
|
555
556
|
export type Mutate<
|
|
556
|
-
S extends
|
|
557
|
+
S extends DatabaseSchema = DatabaseSchema,
|
|
557
558
|
Mode extends "create" | "update" = "update",
|
|
558
559
|
> = <K extends keyof S>(
|
|
559
560
|
table: K,
|
|
560
561
|
values: Kysely.Simplify<
|
|
561
562
|
Mode extends "create"
|
|
562
|
-
? PartialForNullable<
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
563
|
+
? PartialForNullable<
|
|
564
|
+
Castable<Omit<S[K], "id" | "createdAt" | "updatedAt" | "isDeleted">>
|
|
565
|
+
>
|
|
566
|
+
: Partial<Castable<Omit<S[K], "id" | "createdAt" | "updatedAt">>> & {
|
|
567
|
+
readonly id: S[K]["id"];
|
|
568
|
+
}
|
|
566
569
|
>,
|
|
567
570
|
onComplete?: () => void,
|
|
568
571
|
) => {
|
|
@@ -716,7 +719,7 @@ const EvoluCommon = Layer.effect(
|
|
|
716
719
|
subscribeSyncState: syncStateStore.subscribe,
|
|
717
720
|
getSyncState: syncStateStore.getState,
|
|
718
721
|
|
|
719
|
-
create: mutate as Mutate<
|
|
722
|
+
create: mutate as Mutate<DatabaseSchema, "create">,
|
|
720
723
|
update: mutate,
|
|
721
724
|
|
|
722
725
|
resetOwner: () => dbWorker.postMessage({ _tag: "reset" }),
|
|
@@ -746,8 +749,8 @@ export const EvoluCommonLive = EvoluCommon.pipe(
|
|
|
746
749
|
);
|
|
747
750
|
|
|
748
751
|
export const makeCreateEvolu =
|
|
749
|
-
(EvoluLive: Layer.Layer<Config, never, Evolu
|
|
750
|
-
<From, To extends
|
|
752
|
+
(EvoluLive: Layer.Layer<Config, never, Evolu>) =>
|
|
753
|
+
<From, To extends DatabaseSchema>(
|
|
751
754
|
schema: S.Schema<From, To>,
|
|
752
755
|
config?: Partial<Config>,
|
|
753
756
|
): Evolu<To> => {
|
package/src/Public.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type { Timestamp, TimestampError } from "./Crdt.js";
|
|
|
3
3
|
export type { Mnemonic, InvalidMnemonicError } from "./Crypto.js";
|
|
4
4
|
export type { EvoluError, UnexpectedError } from "./ErrorStore.js";
|
|
5
5
|
export * from "./Model.js";
|
|
6
|
+
export { table, database } from "./Db.js";
|
|
6
7
|
export type { Owner, OwnerId } from "./Owner.js";
|
|
7
8
|
export { canUseDom } from "./Platform.js";
|
|
8
9
|
export type { SyncState } from "./SyncWorker.js";
|