@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.
- package/README.md +2 -2
- package/dist/src/Array.d.ts +256 -12
- package/dist/src/Array.d.ts.map +1 -1
- package/dist/src/Array.js +215 -9
- package/dist/src/Assert.d.ts +0 -13
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +0 -15
- package/dist/src/Brand.d.ts +75 -0
- package/dist/src/Brand.d.ts.map +1 -0
- package/dist/src/Brand.js +1 -0
- package/dist/src/Buffer.d.ts +1 -1
- package/dist/src/Buffer.d.ts.map +1 -1
- package/dist/src/Buffer.js +8 -7
- package/dist/src/Cache.d.ts +44 -0
- package/dist/src/Cache.d.ts.map +1 -0
- package/dist/src/Cache.js +52 -0
- package/dist/src/Callbacks.d.ts +45 -12
- package/dist/src/Callbacks.d.ts.map +1 -1
- package/dist/src/Callbacks.js +14 -7
- package/dist/src/Console.d.ts +31 -6
- package/dist/src/Console.d.ts.map +1 -1
- package/dist/src/Console.js +72 -9
- package/dist/src/Crypto.d.ts +56 -42
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +40 -53
- package/dist/src/Evolu/Db.d.ts +162 -74
- package/dist/src/Evolu/Db.d.ts.map +1 -1
- package/dist/src/Evolu/Db.js +284 -702
- package/dist/src/Evolu/Diff.d.ts +3 -3
- package/dist/src/Evolu/Diff.d.ts.map +1 -1
- package/dist/src/Evolu/Diff.js +7 -5
- package/dist/src/Evolu/Evolu.d.ts +214 -134
- package/dist/src/Evolu/Evolu.d.ts.map +1 -1
- package/dist/src/Evolu/Evolu.js +189 -180
- package/dist/src/Evolu/Internal.d.ts +0 -2
- package/dist/src/Evolu/Internal.d.ts.map +1 -1
- package/dist/src/Evolu/Internal.js +0 -2
- package/dist/src/Evolu/LocalAuth.d.ts +150 -0
- package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
- package/dist/src/Evolu/LocalAuth.js +174 -0
- package/dist/src/Evolu/Owner.d.ts +273 -120
- package/dist/src/Evolu/Owner.d.ts.map +1 -1
- package/dist/src/Evolu/Owner.js +130 -104
- package/dist/src/Evolu/Platform.d.ts +9 -7
- package/dist/src/Evolu/Platform.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.d.ts +268 -240
- package/dist/src/Evolu/Protocol.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.js +600 -454
- package/dist/src/Evolu/Public.d.ts +6 -8
- package/dist/src/Evolu/Public.d.ts.map +1 -1
- package/dist/src/Evolu/Public.js +2 -3
- package/dist/src/Evolu/PublicKysely.d.ts.map +1 -1
- package/dist/src/Evolu/PublicKysely.js +3 -4
- package/dist/src/Evolu/Query.d.ts +2 -1
- package/dist/src/Evolu/Query.d.ts.map +1 -1
- package/dist/src/Evolu/Query.js +1 -1
- package/dist/src/Evolu/Relay.d.ts +91 -8
- package/dist/src/Evolu/Relay.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.js +214 -88
- package/dist/src/Evolu/Schema.d.ts +125 -47
- package/dist/src/Evolu/Schema.d.ts.map +1 -1
- package/dist/src/Evolu/Schema.js +175 -31
- package/dist/src/Evolu/Storage.d.ts +249 -27
- package/dist/src/Evolu/Storage.d.ts.map +1 -1
- package/dist/src/Evolu/Storage.js +198 -92
- package/dist/src/Evolu/Sync.d.ts +68 -13
- package/dist/src/Evolu/Sync.d.ts.map +1 -1
- package/dist/src/Evolu/Sync.js +469 -20
- package/dist/src/Evolu/Timestamp.d.ts +83 -30
- package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
- package/dist/src/Evolu/Timestamp.js +79 -34
- package/dist/src/Identicon.d.ts +35 -0
- package/dist/src/Identicon.d.ts.map +1 -0
- package/dist/src/Identicon.js +143 -0
- package/dist/src/Instances.d.ts +34 -0
- package/dist/src/Instances.d.ts.map +1 -0
- package/dist/src/Instances.js +44 -0
- package/dist/src/Number.d.ts +4 -3
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Number.js +5 -4
- package/dist/src/Object.d.ts +10 -4
- package/dist/src/Object.d.ts.map +1 -1
- package/dist/src/Object.js +9 -3
- package/dist/src/Platform.d.ts +20 -0
- package/dist/src/Platform.d.ts.map +1 -0
- package/dist/src/Platform.js +22 -0
- package/dist/src/Random.d.ts +3 -2
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/Relation.d.ts +79 -0
- package/dist/src/Relation.d.ts.map +1 -0
- package/dist/src/Relation.js +127 -0
- package/dist/src/Resources.d.ts +118 -0
- package/dist/src/Resources.d.ts.map +1 -0
- package/dist/src/Resources.js +197 -0
- package/dist/src/Result.d.ts +184 -52
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +30 -241
- package/dist/src/Skiplist.js +2 -1
- package/dist/src/Sqlite.d.ts +89 -5
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +136 -9
- package/dist/src/Task.d.ts +586 -0
- package/dist/src/Task.d.ts.map +1 -0
- package/dist/src/Task.js +469 -0
- package/dist/src/Time.d.ts +66 -1
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +99 -5
- package/dist/src/Type.d.ts +676 -343
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +718 -467
- package/dist/src/Types.d.ts +1 -75
- package/dist/src/Types.d.ts.map +1 -1
- package/dist/src/WebSocket.d.ts +5 -2
- package/dist/src/WebSocket.d.ts.map +1 -1
- package/dist/src/WebSocket.js +12 -18
- package/dist/src/Worker.d.ts +39 -11
- package/dist/src/Worker.d.ts.map +1 -1
- package/dist/src/Worker.js +22 -4
- package/dist/src/index.d.ts +8 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +8 -3
- package/package.json +15 -14
- package/src/Array.ts +301 -19
- package/src/Assert.ts +0 -21
- package/src/Brand.ts +75 -0
- package/src/Buffer.ts +7 -7
- package/src/Cache.ts +85 -0
- package/src/Callbacks.ts +62 -22
- package/src/Console.ts +91 -11
- package/src/Crypto.ts +94 -90
- package/src/Evolu/Db.ts +519 -1026
- package/src/Evolu/Diff.ts +7 -5
- package/src/Evolu/Evolu.ts +477 -361
- package/src/Evolu/Internal.ts +0 -2
- package/src/Evolu/LocalAuth.ts +463 -0
- package/src/Evolu/Owner.ts +355 -228
- package/src/Evolu/Platform.ts +9 -9
- package/src/Evolu/Protocol.ts +842 -742
- package/src/Evolu/Public.ts +7 -14
- package/src/Evolu/PublicKysely.ts +4 -5
- package/src/Evolu/Query.ts +3 -2
- package/src/Evolu/Relay.ts +406 -103
- package/src/Evolu/Schema.ts +323 -91
- package/src/Evolu/Storage.ts +559 -137
- package/src/Evolu/Sync.ts +819 -36
- package/src/Evolu/Timestamp.ts +90 -58
- package/src/Identicon.ts +197 -0
- package/src/Instances.ts +90 -0
- package/src/Number.ts +6 -10
- package/src/Object.ts +13 -5
- package/src/Platform.ts +26 -0
- package/src/Random.ts +3 -2
- package/src/Relation.ts +234 -0
- package/src/Resources.ts +367 -0
- package/src/Result.ts +191 -54
- package/src/Skiplist.ts +1 -1
- package/src/Sqlite.ts +152 -17
- package/src/Task.ts +901 -0
- package/src/Time.ts +180 -5
- package/src/Type.ts +1135 -730
- package/src/Types.ts +1 -77
- package/src/WebSocket.ts +27 -25
- package/src/Worker.ts +72 -23
- package/src/index.ts +8 -3
- package/dist/src/Evolu/Config.d.ts +0 -69
- package/dist/src/Evolu/Config.d.ts.map +0 -1
- package/dist/src/Evolu/Config.js +0 -9
- package/dist/src/Evolu/Kysely.d.ts +0 -6
- package/dist/src/Evolu/Kysely.d.ts.map +0 -1
- package/dist/src/Evolu/Kysely.js +0 -21
- package/dist/src/ManyToManyMap.d.ts +0 -26
- package/dist/src/ManyToManyMap.d.ts.map +0 -1
- package/dist/src/ManyToManyMap.js +0 -92
- package/dist/src/NanoId.d.ts +0 -27
- package/dist/src/NanoId.d.ts.map +0 -1
- package/dist/src/NanoId.js +0 -6
- package/dist/src/Promise.d.ts +0 -180
- package/dist/src/Promise.d.ts.map +0 -1
- package/dist/src/Promise.js +0 -176
- package/src/Evolu/Config.ts +0 -83
- package/src/Evolu/Kysely.ts +0 -38
- package/src/ManyToManyMap.ts +0 -140
- package/src/NanoId.ts +0 -39
- package/src/Promise.ts +0 -295
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import
|
|
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,
|
|
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 {
|
|
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 {
|
|
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
|
|
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 ?
|
|
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
|
|
64
|
-
export type
|
|
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
|
|
69
|
-
export
|
|
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"
|
|
72
|
-
} &
|
|
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:
|
|
74
|
+
readonly timestamp: TimestampBytes;
|
|
76
75
|
readonly table: keyof S;
|
|
77
|
-
readonly id:
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
|
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
|
-
|
|
111
|
-
|
|
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
|
|
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
|
|
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
|
|
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({
|
|
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,
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Schema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,EAA8B,cAAc,EAAE,MAAM,cAAc,CAAC;AAC1E,OAAO,EAAM,MAAM,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAGL,aAAa,EACb,SAAS,EACT,WAAW,EAEX,kBAAkB,EAClB,WAAW,EACZ,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,OAAO,EAI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,IACpB;KACD,MAAM,IAAI,MAAM,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,WAAW,CAClD,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAC9B;CACF,CAAC,MAAM,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAE/B,eAAO,MAAM,OAAO;;;EAGlB,CAAC;AACH,MAAM,MAAM,OAAO,GAAG,OAAO,OAAO,CAAC,IAAI,CAAC;AAE1C,eAAO,MAAM,OAAO;;;EAAwC,CAAC;AAC7D,MAAM,MAAM,OAAO,GAAG,OAAO,OAAO,CAAC,IAAI,CAAC;AAE1C,eAAO,MAAM,QAAQ;;;;;;;;;EAGnB,CAAC;AACH,MAAM,MAAM,QAAQ,GAAG,OAAO,QAAQ,CAAC,IAAI,CAAC;AAE5C,kEAAkE;AAClE,eAAO,MAAM,WAAW,GACrB,MAAM,SAAS,MACf,iBAAwB;IAAE,UAAU,CAAC,EAAE,OAAO,CAAA;CAAO,KAAG,MAAM,CAC7D,QAAQ,EACR,WAAW,CA4DZ,CAAC;AAKJ,eAAO,MAAM,cAAc,GACxB,MAAM,SAAS,MAEd,WAAW,QAAQ,EACnB,eAAe,QAAQ,EACvB,UAAU;IAAE,aAAa,EAAE,OAAO,CAAA;CAAE,KACnC,MAAM,CAAC,IAAI,EAAE,WAAW,CAoD1B,CAAC;AAqBJ,eAAO,MAAM,MAAM,wBASjB,CAAC"}
|
package/dist/src/Evolu/Schema.js
CHANGED
|
@@ -1,41 +1,44 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { assert } from "../Assert.js";
|
|
1
|
+
import * as Kysely from "kysely";
|
|
3
2
|
import { mapObject, objectToEntries } from "../Object.js";
|
|
4
|
-
import {
|
|
5
|
-
import { SqliteBoolean } from "../Sqlite.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
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
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
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
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
:
|
|
35
|
-
|
|
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
|
|
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
|
|
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
|
|
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({
|
|
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) =>
|
|
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);
|