| 193 | * @since 4.0.0 |
| 194 | */ |
| 195 | export const make = < |
| 196 | const Name extends string, |
| 197 | TableSchema extends AnySchemaStruct, |
| 198 | const Indexes extends Record< |
| 199 | string, |
| 200 | IndexedDbQueryBuilder.KeyPath<TableSchema> |
| 201 | >, |
| 202 | const KeyPath extends |
| 203 | | (AutoIncrement extends true ? IndexedDbQueryBuilder.KeyPathNumber<NoInfer<TableSchema>> |
| 204 | : IndexedDbQueryBuilder.KeyPath<NoInfer<TableSchema>>) |
| 205 | | undefined = undefined, |
| 206 | const AutoIncrement extends boolean = false |
| 207 | >(options: { |
| 208 | readonly name: Name |
| 209 | readonly schema: [KeyPath] extends [undefined] |
| 210 | ? "key" extends keyof TableSchema["fields"] ? "Cannot have a 'key' field when keyPath is undefined" |
| 211 | : TableSchema |
| 212 | : TableSchema |
| 213 | readonly keyPath?: KeyPath |
| 214 | readonly indexes?: Indexes | undefined |
| 215 | readonly autoIncrement?: IsValidAutoIncrementKeyPath< |
| 216 | TableSchema, |
| 217 | KeyPath |
| 218 | > extends true ? AutoIncrement | undefined |
| 219 | : never |
| 220 | readonly durability?: IDBTransactionDurability | undefined |
| 221 | }): IndexedDbTable< |
| 222 | Name, |
| 223 | TableSchema, |
| 224 | Indexes, |
| 225 | Extract<KeyPath, Readonly<IDBValidKey | undefined>>, |
| 226 | AutoIncrement |
| 227 | > => { |
| 228 | // oxlint-disable-next-line typescript/no-extraneous-class |
| 229 | class Table {} |
| 230 | Object.assign(Table, Proto) |
| 231 | const readSchema = options.keyPath === undefined |
| 232 | ? Schema.Struct({ |
| 233 | ...(options.schema as Schema.Struct<{}>).fields, |
| 234 | key: IndexedDb.IDBValidKey |
| 235 | }) |
| 236 | : options.schema |
| 237 | ;(Table as any).tableName = options.name |
| 238 | ;(Table as any).tableSchema = options.schema |
| 239 | ;(Table as any).readSchema = readSchema |
| 240 | ;(Table as any).arraySchema = Schema.Array(readSchema as any) |
| 241 | ;(Table as any).autoincrementSchema = options.autoIncrement |
| 242 | ? Schema.Struct(Struct.omit((options.schema as Schema.Struct<{}>).fields, [options.keyPath!] as any)) |
| 243 | : options.schema |
| 244 | ;(Table as any).keyPath = options.keyPath |
| 245 | ;(Table as any).indexes = options.indexes |
| 246 | ;(Table as any).autoIncrement = options.autoIncrement === true |
| 247 | ;(Table as any).durability = options.durability ?? "relaxed" |
| 248 | return Table as any |
| 249 | } |
| 250 | |
| 251 | // ----------------------------------------------------------------------------- |
| 252 | // internal |