@evolu/common 6.0.1-preview.2 → 6.0.1-preview.21
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/Assert.d.ts +6 -3
- package/dist/src/Assert.d.ts.map +1 -1
- package/dist/src/Assert.js +7 -4
- 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 +1 -1
- package/dist/src/CallbackRegistry.d.ts +53 -0
- package/dist/src/CallbackRegistry.d.ts.map +1 -0
- package/dist/src/CallbackRegistry.js +25 -0
- 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 +61 -34
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +29 -42
- package/dist/src/Evolu/Db.d.ts +159 -66
- package/dist/src/Evolu/Db.d.ts.map +1 -1
- package/dist/src/Evolu/Db.js +300 -705
- 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 +200 -115
- package/dist/src/Evolu/Evolu.d.ts.map +1 -1
- package/dist/src/Evolu/Evolu.js +287 -157
- 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 +144 -0
- package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
- package/dist/src/Evolu/LocalAuth.js +171 -0
- package/dist/src/Evolu/Owner.d.ts +209 -124
- package/dist/src/Evolu/Owner.d.ts.map +1 -1
- package/dist/src/Evolu/Owner.js +118 -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 +256 -211
- package/dist/src/Evolu/Protocol.d.ts.map +1 -1
- package/dist/src/Evolu/Protocol.js +558 -359
- 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.js +3 -3
- package/dist/src/Evolu/Query.d.ts +2 -1
- package/dist/src/Evolu/Query.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.d.ts +72 -4
- package/dist/src/Evolu/Relay.d.ts.map +1 -1
- package/dist/src/Evolu/Relay.js +124 -13
- package/dist/src/Evolu/Schema.d.ts +129 -73
- package/dist/src/Evolu/Schema.d.ts.map +1 -1
- package/dist/src/Evolu/Schema.js +169 -89
- package/dist/src/Evolu/Storage.d.ts +159 -14
- package/dist/src/Evolu/Storage.d.ts.map +1 -1
- package/dist/src/Evolu/Storage.js +42 -32
- 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 +414 -20
- package/dist/src/Evolu/Timestamp.d.ts +35 -27
- package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
- package/dist/src/Evolu/Timestamp.js +27 -18
- package/dist/src/ManyToManyMap.d.ts +74 -10
- package/dist/src/ManyToManyMap.d.ts.map +1 -1
- package/dist/src/ManyToManyMap.js +41 -6
- package/dist/src/Number.d.ts +2 -1
- package/dist/src/Number.d.ts.map +1 -1
- package/dist/src/Random.d.ts +3 -2
- package/dist/src/Random.d.ts.map +1 -1
- package/dist/src/RefCountedResourceManager.d.ts +119 -0
- package/dist/src/RefCountedResourceManager.d.ts.map +1 -0
- package/dist/src/RefCountedResourceManager.js +197 -0
- package/dist/src/Result.d.ts +176 -51
- package/dist/src/Result.d.ts.map +1 -1
- package/dist/src/Result.js +30 -241
- package/dist/src/Sqlite.d.ts +21 -4
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +50 -8
- package/dist/src/Task.d.ts +511 -0
- package/dist/src/Task.d.ts.map +1 -0
- package/dist/src/Task.js +410 -0
- package/dist/src/Time.d.ts +59 -0
- package/dist/src/Time.d.ts.map +1 -1
- package/dist/src/Time.js +87 -4
- package/dist/src/Type.d.ts +474 -327
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +502 -436
- package/dist/src/Types.d.ts +0 -74
- 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 -13
- 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 +3 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -3
- package/package.json +14 -13
- package/src/Assert.ts +8 -7
- package/src/Brand.ts +75 -0
- package/src/Buffer.ts +1 -1
- package/src/CallbackRegistry.ts +84 -0
- package/src/Console.ts +91 -11
- package/src/Crypto.ts +94 -79
- package/src/Evolu/Db.ts +528 -1027
- package/src/Evolu/Diff.ts +7 -5
- package/src/Evolu/Evolu.ts +682 -329
- package/src/Evolu/Internal.ts +0 -2
- package/src/Evolu/LocalAuth.ts +422 -0
- package/src/Evolu/Owner.ts +282 -227
- package/src/Evolu/Platform.ts +9 -9
- package/src/Evolu/Protocol.ts +788 -640
- package/src/Evolu/Public.ts +7 -14
- package/src/Evolu/PublicKysely.ts +3 -3
- package/src/Evolu/Query.ts +2 -1
- package/src/Evolu/Relay.ts +238 -18
- package/src/Evolu/Schema.ts +391 -191
- package/src/Evolu/Storage.ts +275 -55
- package/src/Evolu/Sync.ts +708 -37
- package/src/Evolu/Timestamp.ts +38 -35
- package/src/ManyToManyMap.ts +127 -24
- package/src/Number.ts +2 -6
- package/src/Random.ts +3 -2
- package/src/RefCountedResourceManager.ts +368 -0
- package/src/Result.ts +183 -53
- package/src/Sqlite.ts +59 -23
- package/src/Task.ts +779 -0
- package/src/Time.ts +168 -4
- package/src/Type.ts +708 -660
- package/src/Types.ts +0 -76
- package/src/WebSocket.ts +23 -17
- package/src/Worker.ts +72 -23
- package/src/index.ts +3 -3
- package/dist/src/Callbacks.d.ts +0 -20
- package/dist/src/Callbacks.d.ts.map +0 -1
- package/dist/src/Callbacks.js +0 -18
- 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/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/Callbacks.ts +0 -43
- package/src/Evolu/Config.ts +0 -83
- package/src/Evolu/Kysely.ts +0 -38
- package/src/NanoId.ts +0 -39
- package/src/Promise.ts +0 -295
|
@@ -1,24 +1,16 @@
|
|
|
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, DateIso, 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
|
*
|
|
16
|
-
* - Each top-level key represents a table name.
|
|
17
|
-
* - The value for each table name is a record of column names mapped to their
|
|
18
|
-
* respective data types, defined by {@link Type}.
|
|
19
|
-
* - Each table must include a mandatory `id` column of type {@link Id}.
|
|
20
|
-
* - No table may contain {@link DefaultColumns}.
|
|
21
|
-
*
|
|
22
14
|
* Table schema defines columns that are required for table rows. For not
|
|
23
15
|
* required columns, use {@link nullOr}.
|
|
24
16
|
*
|
|
@@ -50,64 +42,104 @@ import { BinaryTimestamp } from "./Timestamp.js";
|
|
|
50
42
|
* };
|
|
51
43
|
* ```
|
|
52
44
|
*/
|
|
53
|
-
export type EvoluSchema = ReadonlyRecord<string, ReadonlyRecord<string, Type<any, any, any, any, any
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
45
|
+
export type EvoluSchema = ReadonlyRecord<string, ReadonlyRecord<string, Type<any, any, any, any, any, any>>>;
|
|
46
|
+
/**
|
|
47
|
+
* Validates an {@link EvoluSchema} at compile time, returning the first error
|
|
48
|
+
* found as a readable string literal type. This approach provides much clearer
|
|
49
|
+
* and more actionable TypeScript errors than the default, which are often hard
|
|
50
|
+
* to read.
|
|
51
|
+
*
|
|
52
|
+
* Validates the following schema requirements:
|
|
53
|
+
*
|
|
54
|
+
* 1. All tables must have an 'id' column
|
|
55
|
+
* 2. The 'id' column must be a branded ID type (created with id() function)
|
|
56
|
+
* 3. Tables cannot use default column names (createdAt, updatedAt, isDeleted)
|
|
57
|
+
* 4. All column types must be compatible with SQLite (extend SqliteValue)
|
|
58
|
+
*/
|
|
59
|
+
export type ValidateSchema<S extends EvoluSchema> = ValidateSchemaHasId<S> extends never ? ValidateIdColumnType<S> extends never ? ValidateNoDefaultColumns<S> extends never ? ValidateColumnTypes<S> extends never ? S : ValidateColumnTypes<S> : ValidateNoDefaultColumns<S> : ValidateIdColumnType<S> : ValidateSchemaHasId<S>;
|
|
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;
|
|
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 ValidateNoDefaultColumns<S extends EvoluSchema> = keyof S extends infer TableName ? TableName extends keyof S ? keyof S[TableName] extends infer ColumnName ? ColumnName extends keyof S[TableName] ? ColumnName extends "createdAt" | "updatedAt" | "isDeleted" ? SchemaValidationError<`Table "${TableName & string}" uses default column name "${ColumnName & string}". Default columns (createdAt, updatedAt, isDeleted) are added automatically.`> : never : never : never : never : never;
|
|
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;
|
|
64
|
+
/** Schema validation error that shows clear, readable messages */
|
|
65
|
+
export type SchemaValidationError<Message extends string> = `❌ Schema Error: ${Message}`;
|
|
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<{
|
|
57
69
|
[Table in keyof S]: {
|
|
58
70
|
readonly [Column in keyof S[Table]]: Column extends "id" | "createdAt" | "updatedAt" ? InferType<S[Table][Column]> : InferType<S[Table][Column]> | null;
|
|
59
71
|
} & DefaultColumns;
|
|
60
72
|
} & {
|
|
61
73
|
readonly evolu_history: {
|
|
62
|
-
readonly timestamp:
|
|
74
|
+
readonly timestamp: TimestampBytes;
|
|
63
75
|
readonly table: keyof S;
|
|
64
|
-
readonly
|
|
76
|
+
readonly id: IdBytes;
|
|
65
77
|
readonly column: string;
|
|
66
78
|
readonly value: SqliteValue;
|
|
67
79
|
};
|
|
68
|
-
}>, "selectFrom" | "fn" | "with" | "withRecursive">) => SelectQueryBuilder<any, any, R>, options?: SqliteQueryOptions) => Query<Simplify<R>>;
|
|
69
|
-
export declare const DefaultColumns: ObjectType<{
|
|
70
|
-
createdAt: BrandType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>, "DateIso", import("../Type.js").DateIsoStringError, import("../Type.js").StringError>;
|
|
71
|
-
updatedAt: BrandType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>, "DateIso", import("../Type.js").DateIsoStringError, import("../Type.js").StringError>;
|
|
72
|
-
isDeleted: import("../Type.js").UnionType<[Type<"Null", null, null, import("../Type.js").NullError, null, import("../Type.js").NullError>, import("../Type.js").TransformType<Type<"Boolean", boolean, boolean, import("../Type.js").BooleanError, boolean, import("../Type.js").BooleanError>, import("../Type.js").UnionType<[import("../Type.js").LiteralType<0>, import("../Type.js").LiteralType<1>]>, never>]>;
|
|
73
|
-
}>;
|
|
74
|
-
export type DefaultColumns = typeof DefaultColumns.Type;
|
|
80
|
+
}>, "selectFrom" | "fn" | "with" | "withRecursive">) => Kysely.SelectQueryBuilder<any, any, R>, options?: SqliteQueryOptions) => Query<Simplify<R>>;
|
|
75
81
|
/**
|
|
76
|
-
*
|
|
82
|
+
* Default columns automatically added to all tables.
|
|
77
83
|
*
|
|
78
|
-
* -
|
|
79
|
-
*
|
|
80
|
-
* -
|
|
84
|
+
* - `createdAt`: Set by Evolu when `insert` is called, or can be custom with
|
|
85
|
+
* `upsert`.
|
|
86
|
+
* - `updatedAt`: Always set by Evolu, derived from {@link CrdtMessage} timestamp.
|
|
87
|
+
* If you defer sync to avoid leaking time activity, use a custom column to
|
|
88
|
+
* preserve real update time.
|
|
89
|
+
* - `isDeleted`: Soft delete flag.
|
|
81
90
|
*/
|
|
82
|
-
export declare const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
}, import("../Type.js").StringError | import("../Type.js").RegexError<"Base64Url">, never>>>;
|
|
89
|
-
export type ValidEvoluSchema = typeof ValidEvoluSchema.Type;
|
|
90
|
-
export interface ValidEvoluSchemaError extends TypeError<"ValidEvoluSchema"> {
|
|
91
|
-
readonly reason: {
|
|
92
|
-
kind: "DefaultColumnError";
|
|
93
|
-
tableName: string;
|
|
94
|
-
columnName: string;
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Asserts that the given value is {@link ValidEvoluSchema}.
|
|
99
|
-
*
|
|
100
|
-
* Throws an error if the value is not a valid Evolu schema.
|
|
101
|
-
*/
|
|
102
|
-
export declare const assertValidEvoluSchema: (value: unknown) => ValidEvoluSchema;
|
|
103
|
-
export declare const validEvoluSchemaToDbSchema: (validEvoluSchema: ValidEvoluSchema, indexes?: DbIndexesBuilder) => DbSchema;
|
|
91
|
+
export declare const DefaultColumns: ObjectType<{
|
|
92
|
+
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>;
|
|
93
|
+
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>;
|
|
94
|
+
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>]>]>;
|
|
95
|
+
}>;
|
|
96
|
+
export type DefaultColumns = typeof DefaultColumns.Type;
|
|
104
97
|
export type MutationKind = "insert" | "update" | "upsert";
|
|
105
98
|
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<{
|
|
106
99
|
readonly id: S[TableName]["id"]["Type"];
|
|
107
100
|
}, ValidMutationSizeError | MergeObjectTypeErrors<ObjectType<MutationMapping<S[TableName], Kind>>>>;
|
|
108
101
|
export type MutationMapping<P extends Record<string, AnyType>, M extends MutationKind> = M extends "insert" ? InsertableProps<P> : M extends "update" ? UpdateableProps<P> : UpsertableProps<P>;
|
|
109
102
|
export interface MutationOptions {
|
|
103
|
+
/**
|
|
104
|
+
* Called after the mutation is completed and the local state is updated.
|
|
105
|
+
* Useful for triggering side effects (e.g., notifications, UI updates) after
|
|
106
|
+
* insert, update, or upsert.
|
|
107
|
+
*/
|
|
110
108
|
readonly onComplete?: () => void;
|
|
109
|
+
/**
|
|
110
|
+
* Specifies the owner ID for this mutation. If omitted, the default
|
|
111
|
+
* {@link AppOwner} is used.
|
|
112
|
+
*
|
|
113
|
+
* The owner must be used with `evolu.useOwner()` to enable sync. Mutations
|
|
114
|
+
* with unused owners are stored locally but not synced until the owner is
|
|
115
|
+
* used.
|
|
116
|
+
*
|
|
117
|
+
* ### Example
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* // Partition your own data by project (derived from your AppOwner)
|
|
121
|
+
* const projectOwner = deriveShardOwner(appOwner, [
|
|
122
|
+
* "project",
|
|
123
|
+
* projectId,
|
|
124
|
+
* ]);
|
|
125
|
+
* evolu.insert(
|
|
126
|
+
* "task",
|
|
127
|
+
* { title: "Task 1" },
|
|
128
|
+
* { ownerId: projectOwner.id },
|
|
129
|
+
* );
|
|
130
|
+
*
|
|
131
|
+
* // Collaborative data (independent owner shared with others)
|
|
132
|
+
* const sharedOwner = createSharedOwner(sharedSecret);
|
|
133
|
+
* evolu.insert(
|
|
134
|
+
* "comment",
|
|
135
|
+
* { text: "Hello" },
|
|
136
|
+
* { ownerId: sharedOwner.id },
|
|
137
|
+
* );
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @experimental
|
|
141
|
+
*/
|
|
142
|
+
readonly ownerId?: OwnerId;
|
|
111
143
|
/**
|
|
112
144
|
* Only validate, don't mutate.
|
|
113
145
|
*
|
|
@@ -115,24 +147,11 @@ export interface MutationOptions {
|
|
|
115
147
|
* `onlyValidate: true`.
|
|
116
148
|
*/
|
|
117
149
|
readonly onlyValidate?: boolean;
|
|
118
|
-
/**
|
|
119
|
-
* The owner to use for this mutation. Can be a {@link ShardOwner} for sharding
|
|
120
|
-
* app data or a {@link SharedOwner} for collaborative write access. If
|
|
121
|
-
* omitted, defaults to the app's {@link AppOwner}.
|
|
122
|
-
*/
|
|
123
|
-
readonly owner?: ShardOwner | SharedOwner;
|
|
124
150
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
* measured via MessagePack. Evolu Protocol DbChange will be smaller thanks to
|
|
129
|
-
* various optimizations.
|
|
130
|
-
*/
|
|
131
|
-
export declare const maxMutationSize = 655360;
|
|
132
|
-
export interface ValidMutationSizeError extends TypeError<"ValidMutationSize"> {
|
|
151
|
+
export interface MutationChange extends DbChange {
|
|
152
|
+
/** Owner of the change. If undefined, the change belongs to the AppOwner. */
|
|
153
|
+
readonly ownerId?: OwnerId | undefined;
|
|
133
154
|
}
|
|
134
|
-
export declare const formatValidMutationSizeError: import("../Type.js").TypeErrorFormatter<ValidMutationSizeError>;
|
|
135
|
-
export type ValidMutationSize<Props extends Record<string, AnyType>> = BrandType<ObjectType<Props>, "ValidMutationSize", ValidMutationSizeError, InferErrors<ObjectType<Props>>>;
|
|
136
155
|
/**
|
|
137
156
|
* Type Factory to create insertable {@link Type}. It makes nullable Types
|
|
138
157
|
* optional, omits Id, and ensures the {@link maxMutationSize}.
|
|
@@ -176,20 +195,28 @@ export type UpdateableProps<Props extends Record<string, AnyType>> = {
|
|
|
176
195
|
};
|
|
177
196
|
export type Updateable<Props extends Record<string, AnyType>> = InferInput<ObjectType<UpdateableProps<Props>>>;
|
|
178
197
|
/**
|
|
179
|
-
* Type Factory to create upsertable Type. It makes nullable Types optional
|
|
180
|
-
* ensures the
|
|
198
|
+
* Type Factory to create upsertable Type. It makes nullable Types optional,
|
|
199
|
+
* includes optional default columns (createdAt, isDeleted), and ensures the
|
|
200
|
+
* {@link maxMutationSize}.
|
|
181
201
|
*
|
|
182
202
|
* ### Example
|
|
183
203
|
*
|
|
184
204
|
* ```ts
|
|
185
205
|
* const UpsertableTodo = upsertable(Schema.todo);
|
|
186
206
|
* type UpsertableTodo = typeof UpsertableTodo.Type;
|
|
187
|
-
* const todo = UpsertableTodo.from({
|
|
207
|
+
* const todo = UpsertableTodo.from({
|
|
208
|
+
* id,
|
|
209
|
+
* title,
|
|
210
|
+
* createdAt: "2023-01-01T00:00:00.000Z",
|
|
211
|
+
* });
|
|
188
212
|
* if (!todo.ok) return; // handle errors
|
|
189
213
|
* ```
|
|
190
214
|
*/
|
|
191
215
|
export declare const upsertable: <Props extends Record<string, AnyType>>(props: Props) => ValidMutationSize<UpsertableProps<Props>>;
|
|
192
|
-
export type UpsertableProps<Props extends Record<string, AnyType>> = NullableToOptionalProps<Props
|
|
216
|
+
export type UpsertableProps<Props extends Record<string, AnyType>> = NullableToOptionalProps<Props & {
|
|
217
|
+
createdAt: OptionalType<typeof DateIso>;
|
|
218
|
+
isDeleted: OptionalType<typeof SqliteBoolean>;
|
|
219
|
+
}>;
|
|
193
220
|
export type Upsertable<Props extends Record<string, AnyType>> = InferInput<ObjectType<UpsertableProps<Props>>>;
|
|
194
221
|
export type InferEvoluSchemaError<S extends EvoluSchema> = {
|
|
195
222
|
[Table in keyof S]: InferMutationTypeErrors<S[Table]>;
|
|
@@ -198,4 +225,33 @@ export type InferMutationTypeErrors<T extends Record<string, AnyType>> = InferCo
|
|
|
198
225
|
export type InferColumnErrors<T extends Record<string, AnyType>, M extends MutationKind> = {
|
|
199
226
|
[Column in keyof MutationMapping<T, M>]: InferErrors<MutationMapping<T, M>[Column]>;
|
|
200
227
|
}[keyof MutationMapping<T, M>];
|
|
228
|
+
export declare const DbTable: ObjectType<{
|
|
229
|
+
name: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
|
|
230
|
+
columns: import("../Type.js").ArrayType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>>;
|
|
231
|
+
}>;
|
|
232
|
+
export type DbTable = typeof DbTable.Type;
|
|
233
|
+
export declare const DbIndex: ObjectType<{
|
|
234
|
+
name: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
|
|
235
|
+
sql: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
|
|
236
|
+
}>;
|
|
237
|
+
export type DbIndex = typeof DbIndex.Type;
|
|
238
|
+
export declare const DbSchema: ObjectType<{
|
|
239
|
+
tables: import("../Type.js").ArrayType<ObjectType<{
|
|
240
|
+
name: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
|
|
241
|
+
columns: import("../Type.js").ArrayType<Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>>;
|
|
242
|
+
}>>;
|
|
243
|
+
indexes: import("../Type.js").ArrayType<ObjectType<{
|
|
244
|
+
name: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
|
|
245
|
+
sql: Type<"String", string, string, import("../Type.js").StringError, string, import("../Type.js").StringError>;
|
|
246
|
+
}>>;
|
|
247
|
+
}>;
|
|
248
|
+
export type DbSchema = typeof DbSchema.Type;
|
|
249
|
+
/** Get the current database schema by reading SQLite metadata. */
|
|
250
|
+
export declare const getDbSchema: (deps: SqliteDep) => ({ allIndexes }?: {
|
|
251
|
+
allIndexes?: boolean;
|
|
252
|
+
}) => Result<DbSchema, SqliteError>;
|
|
253
|
+
export declare const ensureDbSchema: (deps: SqliteDep) => (newSchema: DbSchema, currentSchema: DbSchema, options?: {
|
|
254
|
+
ignoreIndexes: boolean;
|
|
255
|
+
}) => Result<void, SqliteError>;
|
|
256
|
+
export declare const kysely: Kysely.Kysely<unknown>;
|
|
201
257
|
//# 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,EAEP,OAAO,EACP,OAAO,EACP,WAAW,EACX,UAAU,EACV,SAAS,EAET,qBAAqB,EAErB,uBAAuB,EAGvB,UAAU,EAGV,YAAY,EAEZ,OAAO,EACP,IAAI,EACJ,iBAAiB,EAEjB,sBAAsB,EACvB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,E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/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,CAuD1B,CAAC;AAsBJ,eAAO,MAAM,MAAM,wBASjB,CAAC"}
|
package/dist/src/Evolu/Schema.js
CHANGED
|
@@ -1,97 +1,38 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as Kysely from "kysely";
|
|
2
2
|
import { mapObject, objectToEntries } from "../Object.js";
|
|
3
|
-
import {
|
|
4
|
-
import { SqliteBoolean } from "../Sqlite.js";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export const DefaultColumns = object({
|
|
9
|
-
createdAt: DateIsoString,
|
|
10
|
-
updatedAt: DateIsoString,
|
|
11
|
-
isDeleted: nullOr(SqliteBoolean),
|
|
12
|
-
});
|
|
13
|
-
const isDefaultColumnName = (value) => value === "createdAt" || value === "updatedAt" || value === "isDeleted";
|
|
14
|
-
/**
|
|
15
|
-
* Valid {@link EvoluSchema}.
|
|
16
|
-
*
|
|
17
|
-
* - Table and column names must be Base64Url strings.
|
|
18
|
-
* - Each table must include an `id` column of type {@link Id}.
|
|
19
|
-
* - Default column names (`createdAt`, `updatedAt`, `isDeleted`) are not allowed.
|
|
20
|
-
*/
|
|
21
|
-
export const ValidEvoluSchema = brand("ValidEvoluSchema", record(Base64Url256, object({ id: EvoluType }, record(Base64Url256, Unknown))), (value) => {
|
|
22
|
-
for (const tableName in value) {
|
|
23
|
-
for (const columnName in value[tableName]) {
|
|
24
|
-
if (isDefaultColumnName(columnName)) {
|
|
25
|
-
return err({
|
|
26
|
-
type: "ValidEvoluSchema",
|
|
27
|
-
value,
|
|
28
|
-
reason: {
|
|
29
|
-
kind: "DefaultColumnError",
|
|
30
|
-
tableName,
|
|
31
|
-
columnName,
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return ok(value);
|
|
38
|
-
});
|
|
39
|
-
/**
|
|
40
|
-
* Asserts that the given value is {@link ValidEvoluSchema}.
|
|
41
|
-
*
|
|
42
|
-
* Throws an error if the value is not a valid Evolu schema.
|
|
43
|
-
*/
|
|
44
|
-
export const assertValidEvoluSchema = (value) => {
|
|
45
|
-
const validEvoluSchema = ValidEvoluSchema.fromUnknown(value);
|
|
46
|
-
if (!validEvoluSchema.ok) {
|
|
47
|
-
const message = formatValidEvoluSchemaError(validEvoluSchema.error);
|
|
48
|
-
throw new Error(`Invalid Evolu schema: ${message}`);
|
|
49
|
-
}
|
|
50
|
-
return validEvoluSchema.value;
|
|
51
|
-
};
|
|
52
|
-
const formatValidEvoluSchemaError = (error) => {
|
|
53
|
-
if (error.type === "Record") {
|
|
54
|
-
if (error.reason.kind === "Key") {
|
|
55
|
-
return `The table "${error.reason.key}" has invalid name. A table name must be Base64Url256 string (A-Z, a-z, 0-9, -, _).`;
|
|
56
|
-
}
|
|
57
|
-
if (error.reason.kind === "Value" &&
|
|
58
|
-
error.reason.error.reason.kind === "Props" &&
|
|
59
|
-
error.reason.error.reason.errors.id?.type === "EvoluType") {
|
|
60
|
-
return `The table "${error.reason.key}" has invalid ID column. Check examples.`;
|
|
61
|
-
}
|
|
62
|
-
if (error.reason.kind === "Value" &&
|
|
63
|
-
error.reason.error.reason.kind === "IndexKey") {
|
|
64
|
-
return `The table "${error.reason.key}" has invalid column name "${error.reason.error.reason.key}". A column name must be Base64Url256 string (A-Z, a-z, 0-9, -, _).`;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
if (error.type === "ValidEvoluSchema") {
|
|
68
|
-
return `The table "${error.reason.tableName}" uses reserved column name "${error.reason.columnName}". Reserved column names are: createdAt, updatedAt, isDeleted.`;
|
|
69
|
-
}
|
|
70
|
-
return JSON.stringify(error, null, 2);
|
|
71
|
-
};
|
|
72
|
-
export const validEvoluSchemaToDbSchema = (validEvoluSchema, indexes) => {
|
|
73
|
-
const tables = objectToEntries(validEvoluSchema).map(([tableName, table]) => ({
|
|
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
|
+
export const evoluSchemaToDbSchema = (schema, indexesConfig) => {
|
|
7
|
+
const tables = objectToEntries(schema).map(([tableName, table]) => ({
|
|
74
8
|
name: tableName,
|
|
75
9
|
columns: objectToEntries(table)
|
|
76
10
|
.filter(([k]) => k !== "id")
|
|
77
11
|
.map(([k]) => k),
|
|
78
12
|
}));
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
13
|
+
const indexes = indexesConfig
|
|
14
|
+
? indexesConfig(createIndex).map((index) => ({
|
|
15
|
+
name: index.toOperationNode().name.name,
|
|
16
|
+
sql: index.compile().sql,
|
|
17
|
+
}))
|
|
18
|
+
: [];
|
|
19
|
+
return { tables, indexes };
|
|
83
20
|
};
|
|
84
21
|
/**
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
22
|
+
* Default columns automatically added to all tables.
|
|
23
|
+
*
|
|
24
|
+
* - `createdAt`: Set by Evolu when `insert` is called, or can be custom with
|
|
25
|
+
* `upsert`.
|
|
26
|
+
* - `updatedAt`: Always set by Evolu, derived from {@link CrdtMessage} timestamp.
|
|
27
|
+
* If you defer sync to avoid leaking time activity, use a custom column to
|
|
28
|
+
* preserve real update time.
|
|
29
|
+
* - `isDeleted`: Soft delete flag.
|
|
89
30
|
*/
|
|
90
|
-
export const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
:
|
|
94
|
-
|
|
31
|
+
export const DefaultColumns = object({
|
|
32
|
+
createdAt: DateIso,
|
|
33
|
+
updatedAt: DateIso,
|
|
34
|
+
isDeleted: nullOr(SqliteBoolean),
|
|
35
|
+
});
|
|
95
36
|
/**
|
|
96
37
|
* Type Factory to create insertable {@link Type}. It makes nullable Types
|
|
97
38
|
* optional, omits Id, and ensures the {@link maxMutationSize}.
|
|
@@ -135,16 +76,155 @@ export const updateable = (props) => {
|
|
|
135
76
|
return validMutationSize(object(updateableProps));
|
|
136
77
|
};
|
|
137
78
|
/**
|
|
138
|
-
* Type Factory to create upsertable Type. It makes nullable Types optional
|
|
139
|
-
* ensures the
|
|
79
|
+
* Type Factory to create upsertable Type. It makes nullable Types optional,
|
|
80
|
+
* includes optional default columns (createdAt, isDeleted), and ensures the
|
|
81
|
+
* {@link maxMutationSize}.
|
|
140
82
|
*
|
|
141
83
|
* ### Example
|
|
142
84
|
*
|
|
143
85
|
* ```ts
|
|
144
86
|
* const UpsertableTodo = upsertable(Schema.todo);
|
|
145
87
|
* type UpsertableTodo = typeof UpsertableTodo.Type;
|
|
146
|
-
* const todo = UpsertableTodo.from({
|
|
88
|
+
* const todo = UpsertableTodo.from({
|
|
89
|
+
* id,
|
|
90
|
+
* title,
|
|
91
|
+
* createdAt: "2023-01-01T00:00:00.000Z",
|
|
92
|
+
* });
|
|
147
93
|
* if (!todo.ok) return; // handle errors
|
|
148
94
|
* ```
|
|
149
95
|
*/
|
|
150
|
-
export const upsertable = (props) =>
|
|
96
|
+
export const upsertable = (props) => {
|
|
97
|
+
const propsWithDefaults = {
|
|
98
|
+
...props,
|
|
99
|
+
createdAt: optional(DateIso),
|
|
100
|
+
isDeleted: optional(SqliteBoolean),
|
|
101
|
+
};
|
|
102
|
+
return validMutationSize(nullableToOptional(propsWithDefaults));
|
|
103
|
+
};
|
|
104
|
+
export const DbTable = object({
|
|
105
|
+
name: String,
|
|
106
|
+
columns: array(String),
|
|
107
|
+
});
|
|
108
|
+
export const DbIndex = object({ name: String, sql: String });
|
|
109
|
+
export const DbSchema = object({
|
|
110
|
+
tables: array(DbTable),
|
|
111
|
+
indexes: array(DbIndex),
|
|
112
|
+
});
|
|
113
|
+
/** Get the current database schema by reading SQLite metadata. */
|
|
114
|
+
export const getDbSchema = (deps) => ({ allIndexes = false } = {}) => {
|
|
115
|
+
const map = new Map();
|
|
116
|
+
const tableAndColumnInfoRows = deps.sqlite.exec(sql `
|
|
117
|
+
select
|
|
118
|
+
sqlite_master.name as tableName,
|
|
119
|
+
table_info.name as columnName
|
|
120
|
+
from
|
|
121
|
+
sqlite_master
|
|
122
|
+
join pragma_table_info(sqlite_master.name) as table_info;
|
|
123
|
+
`);
|
|
124
|
+
if (!tableAndColumnInfoRows.ok)
|
|
125
|
+
return tableAndColumnInfoRows;
|
|
126
|
+
tableAndColumnInfoRows.value.rows.forEach((row) => {
|
|
127
|
+
const { tableName, columnName } = row;
|
|
128
|
+
if (!map.has(tableName))
|
|
129
|
+
map.set(tableName, []);
|
|
130
|
+
map.get(tableName)?.push(columnName);
|
|
131
|
+
});
|
|
132
|
+
const tables = Array.from(map, ([name, columns]) => ({ name, columns }));
|
|
133
|
+
const indexesRows = deps.sqlite.exec(allIndexes
|
|
134
|
+
? sql `
|
|
135
|
+
select name, sql
|
|
136
|
+
from sqlite_master
|
|
137
|
+
where type = 'index' and name not like 'sqlite_%';
|
|
138
|
+
`
|
|
139
|
+
: sql `
|
|
140
|
+
select name, sql
|
|
141
|
+
from sqlite_master
|
|
142
|
+
where
|
|
143
|
+
type = 'index'
|
|
144
|
+
and name not like 'sqlite_%'
|
|
145
|
+
and name not like 'evolu_%';
|
|
146
|
+
`);
|
|
147
|
+
if (!indexesRows.ok)
|
|
148
|
+
return indexesRows;
|
|
149
|
+
const indexes = indexesRows.value.rows.map((row) => ({
|
|
150
|
+
name: row.name,
|
|
151
|
+
/**
|
|
152
|
+
* SQLite returns "CREATE INDEX" for "create index" for some reason.
|
|
153
|
+
* Other keywords remain unchanged. We have to normalize the casing for
|
|
154
|
+
* {@link indexesAreEqual} manually.
|
|
155
|
+
*/
|
|
156
|
+
sql: row.sql
|
|
157
|
+
.replace("CREATE INDEX", "create index")
|
|
158
|
+
.replace("CREATE UNIQUE INDEX", "create unique index"),
|
|
159
|
+
}));
|
|
160
|
+
return ok({ tables, indexes });
|
|
161
|
+
};
|
|
162
|
+
const indexesAreEqual = (self, that) => self.name === that.name && self.sql === that.sql;
|
|
163
|
+
export const ensureDbSchema = (deps) => (newSchema, currentSchema, options) => {
|
|
164
|
+
const queries = [];
|
|
165
|
+
newSchema.tables.forEach((newTable) => {
|
|
166
|
+
const currentTable = currentSchema.tables.find((t) => t.name === newTable.name);
|
|
167
|
+
if (!currentTable) {
|
|
168
|
+
queries.push({
|
|
169
|
+
sql: createTableWithDefaultColumns(newTable.name, newTable.columns),
|
|
170
|
+
parameters: [],
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
newTable.columns
|
|
175
|
+
.filter((newColumn) => !currentTable.columns.includes(newColumn))
|
|
176
|
+
.forEach((newColumn) => {
|
|
177
|
+
queries.push(sql `
|
|
178
|
+
alter table ${sql.identifier(newTable.name)}
|
|
179
|
+
add column ${sql.identifier(newColumn)} blob;
|
|
180
|
+
`);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
if (options?.ignoreIndexes !== true) {
|
|
185
|
+
// Remove current indexes that are not in the newSchema.
|
|
186
|
+
currentSchema.indexes
|
|
187
|
+
.filter((currentIndex) => !newSchema.indexes.some((newIndex) => indexesAreEqual(newIndex, currentIndex)))
|
|
188
|
+
.forEach((index) => {
|
|
189
|
+
queries.push(sql `drop index ${sql.identifier(index.name)};`);
|
|
190
|
+
});
|
|
191
|
+
// Add new indexes that are not in the currentSchema.
|
|
192
|
+
newSchema.indexes
|
|
193
|
+
.filter((newIndex) => !currentSchema.indexes.some((currentIndex) => indexesAreEqual(newIndex, currentIndex)))
|
|
194
|
+
.forEach((newIndex) => {
|
|
195
|
+
queries.push({ sql: `${newIndex.sql};`, parameters: [] });
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
for (const query of queries) {
|
|
199
|
+
const result = deps.sqlite.exec(query);
|
|
200
|
+
if (!result.ok)
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
return ok();
|
|
204
|
+
};
|
|
205
|
+
const createTableWithDefaultColumns = (tableName, columns) => `
|
|
206
|
+
create table ${sql.identifier(tableName).sql} (
|
|
207
|
+
"id" text primary key,
|
|
208
|
+
${columns
|
|
209
|
+
// Add default columns.
|
|
210
|
+
.concat(["createdAt", "updatedAt", "isDeleted"])
|
|
211
|
+
.filter((c) => c !== "id")
|
|
212
|
+
// "A column with affinity BLOB does not prefer one storage class over another
|
|
213
|
+
// and no attempt is made to coerce data from one storage class into another."
|
|
214
|
+
// https://www.sqlite.org/datatype3.html
|
|
215
|
+
.map((name) => `${sql.identifier(name).sql} blob`)
|
|
216
|
+
.join(", ")}
|
|
217
|
+
);
|
|
218
|
+
`;
|
|
219
|
+
// https://kysely.dev/docs/recipes/splitting-query-building-and-execution
|
|
220
|
+
export const kysely = new Kysely.Kysely({
|
|
221
|
+
dialect: {
|
|
222
|
+
createAdapter: () => new Kysely.SqliteAdapter(),
|
|
223
|
+
createDriver: () => new Kysely.DummyDriver(),
|
|
224
|
+
createIntrospector() {
|
|
225
|
+
throw new Error("Not implemeneted");
|
|
226
|
+
},
|
|
227
|
+
createQueryCompiler: () => new Kysely.SqliteQueryCompiler(),
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
const createIndex = kysely.schema.createIndex.bind(kysely.schema);
|