Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"@std/assert": "jsr:@std/assert@^1.0.16"
},
"name": "@01edu/db",
"version": "0.1.5",
"version": "0.1.6",
"license": "MIT",
"exports": {
".": "./mod.ts",
Expand Down
35 changes: 22 additions & 13 deletions db/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,26 @@ export const initEntries = <
| RelationTable<string>[]
| Record<string, RelationTable<string>>,
const ET extends { [K: string]: EntryTypeDef<RelToTableProperties<R>> },
>(entryIds: ID, relations: R, entryTypes: ET): {
type: ID
insertListeners: Set<EntryListenerGeneric<RelToTableProperties<R>>>
view: { [K in (keyof ET & string)]: `entry_${Lowercase<K>}` }
archive: (id: number) => void
insert: {
[K in (keyof ET & string)]: (
params: InsertParams<RelToTableProperties<R>, ET[K]>,
) => number
}
} => {
>(entryIds: ID, relations: R, entryTypes: ET):
& Omit<
TableAPI<
'entryInternal',
& RelToTableProperties<R>
& typeof commonEntryProperties
>,
'insert'
>
& {
type: ID
insertListeners: Set<EntryListenerGeneric<RelToTableProperties<R>>>
view: { [K in (keyof ET & string)]: `entry_${Lowercase<K>}` }
archive: (id: number) => void
insert: {
[K in (keyof ET & string)]: (
params: InsertParams<RelToTableProperties<R>, ET[K]>,
) => number
}
} => {
type Relations = RelToTableProperties<R>
type EntryTrigger = EntryTriggerGeneric<Relations>
type EntryListener = EntryListenerGeneric<Relations>
Expand Down Expand Up @@ -222,7 +231,7 @@ export const initEntries = <

const fieldTables: Record<string, unknown> = {}
const entryNames = Object.keys(entryTypes) as EntryName[]
const insert = Object.fromEntries(
const insertEntry = Object.fromEntries(
entryNames.map((k) => {
const type = entryIds[k]
const trigger = (entryTypes[k] as { trigger?: EntryTrigger }).trigger
Expand Down Expand Up @@ -368,7 +377,7 @@ export const initEntries = <
...EntryInternal,
type: entryIds,
view,
insert,
insert: insertEntry,
archive,
insertListeners,
}
Expand Down
6 changes: 3 additions & 3 deletions db/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export type TableAPI<N extends string, P extends TableProperties> = {
* }
* ```
*/
require: (id: number) => Row<P, keyof FlattenProperties<P>>
require: (id: number | undefined) => Row<P, keyof FlattenProperties<P>>
/**
* Asserts that a row with the given ID exists, throwing an error if not.
* @param id - The ID to check.
Expand Down Expand Up @@ -366,8 +366,8 @@ export const createTable = <N extends string, P extends TableProperties>(
const get = (id: number): Row<P, keyof FlatProps> | undefined =>
getByIdStmt.get(id)

const require = (id: number) => {
const match = getByIdStmt.get(id)
const require = (id: number | undefined) => {
const match = id && getByIdStmt.get(id)
if (!match) throw new respond.NotFoundError(notFound)
return match as Row<P, keyof FlatProps>
}
Expand Down