@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 146 lines 4.58 KB
Raw
1 /**
2 * Typed IndexedDB schema version definitions.
3 *
4 * This module represents one logical IndexedDB database version as a non-empty set of `IndexedDbTable` definitions.
5 * Versions are consumed by `IndexedDbDatabase.make` and `.add` to type query builders and migration transactions, so
6 * applications can describe the tables available after initialization or after each schema upgrade.
7 *
8 * Use an `IndexedDbVersion` when defining the initial stores for a browser database, adding or removing object stores,
9 * changing indexes, or moving data between differently shaped table schemas. The version value is a typed description of
10 * the target schema; creating and deleting object stores or indexes still happens explicitly inside the corresponding
11 * `IndexedDbDatabase` migration callback.
12 *
13 * IndexedDB versioning is ordered by the migration chain rather than by a number stored here. Each `.add` step becomes
14 * the next browser database version, and only migrations after the browser's current version are run. Include every table
15 * that should be queryable in each target version, avoid duplicate table names, and remember that key-path or
16 * auto-increment changes usually require creating a new object store and copying data during the upgrade transaction.
17 *
18 * @stability unstable
19 * @since 4.0.0
20 */
21 import type { NonEmptyReadonlyArray } from "effect/Array"
22 import type { Pipeable } from "effect/Pipeable"
23 import { pipeArguments } from "effect/Pipeable"
24 import type * as IndexedDbTable from "./IndexedDbTable.ts"
25
26 const TypeId = "~@effect/platform-browser/IndexedDbVersion"
27
28 /**
29 * Typed IndexedDB version definition containing the tables available in that schema version.
30 *
31 * @stability unstable
32 * @category models
33 * @since 4.0.0
34 */
35 export interface IndexedDbVersion<
36 out Tables extends IndexedDbTable.AnyWithProps
37 > extends Pipeable {
38 new(_: never): {}
39 readonly [TypeId]: typeof TypeId
40 readonly tables: ReadonlyMap<string, Tables>
41 }
42
43 /**
44 * Type-erased shape of an `IndexedDbVersion`.
45 *
46 * @stability unstable
47 * @category models
48 * @since 4.0.0
49 */
50 export interface Any {
51 readonly [TypeId]: typeof TypeId
52 }
53
54 /**
55 * Type-erased `IndexedDbVersion` retaining version properties with broad table types.
56 *
57 * @stability unstable
58 * @category models
59 * @since 4.0.0
60 */
61 export type AnyWithProps = IndexedDbVersion<IndexedDbTable.AnyWithProps>
62
63 /**
64 * Extracts the table union from an `IndexedDbVersion`.
65 *
66 * @stability unstable
67 * @category utility types
68 * @since 4.0.0
69 */
70 export type Tables<Db extends Any> = Db extends IndexedDbVersion<infer _Tables> ? _Tables : never
71
72 /**
73 * Selects a table by name from an `IndexedDbVersion`.
74 *
75 * @stability unstable
76 * @category utility types
77 * @since 4.0.0
78 */
79 export type TableWithName<
80 Db extends Any,
81 TableName extends string
82 > = IndexedDbTable.WithName<Tables<Db>, TableName>
83
84 /**
85 * Extracts the schema for a named table within an `IndexedDbVersion`.
86 *
87 * @stability unstable
88 * @category utility types
89 * @since 4.0.0
90 */
91 export type SchemaWithName<
92 Db extends Any,
93 TableName extends string
94 > = IndexedDbTable.TableSchema<IndexedDbTable.WithName<Tables<Db>, TableName>>
95
96 const Proto = {
97 [TypeId]: TypeId,
98 pipe() {
99 return pipeArguments(this, arguments)
100 }
101 }
102
103 const makeProto = <Tables extends IndexedDbTable.AnyWithProps>(options: {
104 readonly tables: ReadonlyMap<string, Tables>
105 }): IndexedDbVersion<Tables> => {
106 // oxlint-disable-next-line typescript/no-extraneous-class
107 class Version {}
108 Object.assign(Version, Proto)
109 ;(Version as any).tables = options.tables
110 return Version as any
111 }
112
113 /**
114 * Creates an `IndexedDbVersion` from one or more table definitions.
115 *
116 * **When to use**
117 *
118 * Use when you need a typed description of the target IndexedDB schema that a
119 * database migration will materialize.
120 *
121 * **Details**
122 *
123 * The returned version exposes a `tables` map keyed by each table's
124 * `tableName`, and its type is the union of the supplied table definitions.
125 *
126 * **Gotchas**
127 *
128 * This constructor only describes the target schema; object stores and indexes
129 * still need to be created in the corresponding `IndexedDbDatabase` migration.
130 * Duplicate table names are not rejected, and the runtime map keeps the later
131 * table for a repeated key.
132 *
133 * @see {@link IndexedDbTable.make} for creating table definitions consumed by this constructor
134 *
135 * @stability unstable
136 * @category constructors
137 * @since 4.0.0
138 */
139 export const make = <
140 const Tables extends NonEmptyReadonlyArray<IndexedDbTable.AnyWithProps>
141 >(
142 ...tables: Tables
143 ): IndexedDbVersion<Tables[number]> =>
144 makeProto({
145 tables: new Map(tables.map((table) => [table.tableName, table]))
146 })