Skip to content

Commit c4af6df

Browse files
committed
build: import assets + authors tables
1 parent ed0f884 commit c4af6df

File tree

4 files changed

+67
-19
lines changed

4 files changed

+67
-19
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"@biomejs/biome": "^1.9.4",
5151
"@commitlint/cli": "^19.7.1",
5252
"@commitlint/config-conventional": "^19.7.1",
53-
"@libsql/client": "^0.15.1",
5453
"@types/bun": "^1.2.8",
5554
"ajv": "^8.17.1",
5655
"consola": "^3.4.2",

scripts/build-database.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import { basename } from 'node:path'
55
import { Glob } from 'bun'
66
import { consola } from 'consola'
77
import { drizzle } from 'drizzle-orm/bun-sqlite'
8+
import type { SQLiteTable, TableConfig } from 'drizzle-orm/sqlite-core'
89
import { parse } from 'smol-toml'
910
import * as schema from '#~/schema'
11+
12+
import type { Asset } from '../collections/$schemas/.types/assets'
13+
import type { Author } from '../collections/$schemas/.types/authors'
14+
1015
const require = createRequire(import.meta.url)
1116

1217
type DrizzleKitApi = typeof import('drizzle-kit/api')
@@ -41,27 +46,43 @@ for (const stmt of createStatements) {
4146

4247
consola.success('Database initialized\n')
4348

44-
consola.start('Importing assets')
49+
const scan = (path: string) => new Glob(path).scan()
4550

46-
import type { Asset } from '../collections/$schemas/.types/assets'
51+
const importCollection = async <CollectionSchema, DatabaseSchema extends SQLiteTable<TableConfig>>(
52+
name: string,
53+
schema: DatabaseSchema,
54+
mapper: (schema: CollectionSchema, id: string) => DatabaseSchema['$inferInsert'],
55+
) => {
56+
consola.start(`Importing ${name}`)
4757

48-
const scan = (path: string) => new Glob(path).scan()
58+
const statements = []
4959

50-
const statements = []
60+
for await (const filePath of scan(`./collections/${name}/**/*.toml`)) {
61+
const id = basename(filePath, '.toml')
62+
const data = parse(await readFile(filePath, 'utf-8')) as unknown as CollectionSchema
5163

52-
for await (const filePath of scan('./collections/assets/**/*.toml')) {
53-
const id = basename(filePath, '.toml')
54-
const data = parse(await readFile(filePath, 'utf-8')) as unknown as Asset
64+
statements.push(db.insert(schema).values(mapper(data, id)))
65+
}
5566

56-
statements.push(
57-
db.insert(schema.assets).values({
58-
id,
59-
name: data.name,
60-
reference: data.reference,
61-
}),
62-
)
67+
return Promise.all(statements).then(() => consola.success(`Imported ${name}\n`))
6368
}
6469

65-
await Promise.all(statements)
70+
await importCollection<Asset, typeof schema.assets>(
71+
'assets',
72+
schema.assets,
73+
({ name, reference }, id) => ({
74+
id,
75+
name,
76+
reference,
77+
}),
78+
)
6679

67-
consola.success('Imported assets\n')
80+
await importCollection<Author, typeof schema.authors>(
81+
'authors',
82+
schema.authors,
83+
({ name, otherNames }, id) => ({
84+
id,
85+
name,
86+
otherNames,
87+
}),
88+
)

src/relations.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,25 @@ export default defineRelations(schema, (r) => ({
4343
from: r.lines.lineGroupId,
4444
to: r.lineGroups.id,
4545
}),
46+
assetContent: r.many.assetLines({
47+
from: r.lines.id,
48+
to: r.assetLines.lineId,
49+
}),
50+
},
51+
assetLines: {
52+
line: r.one.lines({
53+
from: r.assetLines.lineId,
54+
to: r.lines.id,
55+
}),
56+
asset: r.one.assets({
57+
from: r.assetLines.assetId,
58+
to: r.assets.id,
59+
}),
60+
},
61+
assets: {
62+
lines: r.many.assetLines({
63+
from: r.assets.id,
64+
to: r.assetLines.assetId,
65+
}),
4666
},
4767
}))

src/schema.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ export const lineGroups = sqliteTable('line_groups', {
3131
id: text().primaryKey(),
3232
sectionId: text(),
3333
authorId: text(),
34-
index: integer(),
34+
order: integer(),
3535
})
3636

3737
export const lines = sqliteTable('lines', {
3838
id: text().primaryKey(),
3939
lineGroupId: text(),
4040
content: json(),
41-
index: integer(),
41+
order: integer(),
42+
})
43+
44+
export const assetLines = sqliteTable('asset_lines', {
45+
assetId: text(),
46+
lineId: text(),
47+
type: text(),
48+
language: text(),
49+
data: text(),
4250
})

0 commit comments

Comments
 (0)