Skip to content

Commit 2f0d6bc

Browse files
committed
query local db file
1 parent 19a5183 commit 2f0d6bc

28 files changed

Lines changed: 640 additions & 473 deletions

File tree

netlify.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[build]
22
command = "pnpm run build"
3-
publish = "build"
3+
publish = "build"
4+
5+
[functions]
6+
included_files = ["databases/catdat/catdat.db"]

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
"@sveltejs/adapter-netlify": "^6.0.4",
2929
"@sveltejs/kit": "^2.57.1",
3030
"@sveltejs/vite-plugin-svelte": "^7.0.0",
31+
"@types/better-sqlite3": "^7.6.13",
3132
"@types/katex": "^0.16.8",
3233
"@types/markdown-it": "^14.1.2",
3334
"@types/node": "^25.5.0",
3435
"@types/nodemailer": "^8.0.0",
36+
"better-sqlite3": "^12.9.0",
3537
"chokidar": "^5.0.0",
3638
"dotenv": "^17.3.1",
3739
"husky": "^9.1.7",

pnpm-lock.yaml

Lines changed: 568 additions & 413 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
onlyBuiltDependencies:
2+
- better-sqlite3
13
overrides:
24
cookie@<0.7.0: '>=0.7.0'
35
vite@>=8.0.0 <=8.0.4: '>=8.0.5'

src/lib/server/consistency.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { query } from '$lib/server/db.catdat'
33

44
type AtomicImplication = { assumptions: string[]; conclusion: string }
55

6-
export async function check_consistency(
6+
export function check_consistency(
77
satisfied_properties: Set<string>,
88
unsatisfied_properties: Set<string>,
9-
): Promise<{ consistent: boolean } | null> {
9+
): { consistent: boolean } | null {
1010
for (const p of satisfied_properties) {
1111
if (unsatisfied_properties.has(p)) return { consistent: false }
1212
}
1313

14-
const implications = await get_atomic_implications()
14+
const implications = get_atomic_implications()
1515
if (!implications) return null
1616

1717
return check_consistency_worker(
@@ -50,8 +50,8 @@ function check_consistency_worker(
5050
return { consistent: true }
5151
}
5252

53-
async function get_atomic_implications(): Promise<AtomicImplication[] | null> {
54-
const { rows: implications, err } = await query<{
53+
function get_atomic_implications(): AtomicImplication[] | null {
54+
const { rows: implications, err } = query<{
5555
assumptions: string
5656
conclusions: string
5757
is_equivalence: number
@@ -88,18 +88,18 @@ async function get_atomic_implications(): Promise<AtomicImplication[] | null> {
8888
return atomic_implications
8989
}
9090

91-
export async function get_missing_combinations() {
92-
const implications = await get_atomic_implications()
91+
export function get_missing_combinations() {
92+
const implications = get_atomic_implications()
9393
if (!implications) return null
9494

95-
const { rows: properties, err } = await query<{
95+
const { rows: properties, err } = query<{
9696
id: string
9797
dual_property_id: string | null
9898
}>(sql`SELECT id, dual_property_id FROM category_properties ORDER BY lower(id)`)
9999

100100
if (err) return null
101101

102-
const { rows: existing, err: err_existing } = await query<{
102+
const { rows: existing, err: err_existing } = query<{
103103
p: string
104104
q: string
105105
}>(sql`

src/lib/server/db.catdat.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
1-
import { createClient, type LibsqlError } from '@libsql/client'
21
import type { Arrayed } from '$lib/commons/types'
3-
import { CATDAT_DB_AUTH_TOKEN, CATDAT_DB_URL } from '$env/static/private'
2+
import Database, { SqliteError } from 'better-sqlite3'
3+
import { join } from 'node:path'
44

5-
const db = createClient({
6-
url: CATDAT_DB_URL,
7-
authToken: CATDAT_DB_AUTH_TOKEN,
8-
})
5+
const db_path = join(process.cwd(), 'databases', 'catdat', 'catdat.db')
96

10-
db.execute('PRAGMA foreign_keys = ON')
7+
const db = new Database(db_path, { readonly: true })
8+
9+
db.exec('PRAGMA foreign_keys = ON')
1110

1211
/**
13-
* Small wrapper around db.execute to handle errors,
12+
* Small wrapper around db.prepare.all to handle errors,
1413
* use sql templates, and specify the type of the result.
1514
*/
16-
export async function query<T>(stmt: { sql: string; values: any[] }) {
15+
export function query<T>(stmt: { sql: string; values: any[] }) {
1716
try {
18-
const { rows } = await db.execute(stmt.sql, stmt.values)
17+
const rows = db.prepare(stmt.sql).all(...stmt.values)
1918
return { rows: rows as T[], err: null }
2019
} catch (err) {
2120
console.error(err)
22-
return { rows: null, err: err as LibsqlError }
21+
return { rows: null, err: err as SqliteError }
2322
}
2423
}
2524

2625
/**
27-
* Small wrapper around db.batch to handle errors
26+
* Small wrapper around db.transaction to handle errors
2827
* use sql templates, and specify the type of the result.
2928
*/
30-
export async function batch<T extends any[]>(queries: { sql: string; values: any[] }[]) {
29+
export function batch<T extends any[]>(queries: { sql: string; values: any[] }[]) {
3130
try {
32-
const results = await db.batch(
33-
queries.map((query) => ({
34-
sql: query.sql,
35-
args: query.values,
36-
})),
37-
)
38-
return { results: results.map(({ rows }) => rows) as Arrayed<T>, err: null }
31+
const run_batch = db.transaction(() => {
32+
const results = []
33+
34+
for (const query of queries) {
35+
const result = db.prepare(query.sql).all(...query.values)
36+
results.push(result)
37+
}
38+
39+
return results
40+
})
41+
42+
const results = run_batch() as Arrayed<T>
43+
return { results, err: null }
3944
} catch (err) {
4045
console.error(err)
41-
return { results: null, err: err as LibsqlError }
46+
return { rows: null, err: err as SqliteError }
4247
}
4348
}

src/lib/server/search.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ type NamedObject = {
1111
name: string
1212
}
1313

14-
export async function search_handler(event: RequestEvent, type: 'category' | 'functor') {
14+
export function search_handler(event: RequestEvent, type: 'category' | 'functor') {
1515
const satisfied_query = event.url.searchParams.get('satisfied')
1616
const unsatisfied_query = event.url.searchParams.get('unsatisfied')
1717

1818
if (!satisfied_query && !unsatisfied_query) {
1919
error(400, 'No properties selected')
2020
}
2121

22-
const { rows: all_properties_objects, err: err_all } = await query<{
22+
const { rows: all_properties_objects, err: err_all } = query<{
2323
id: string
2424
dual_property_id: string | null
2525
}>(get_property_query(type))
@@ -71,7 +71,7 @@ export async function search_handler(event: RequestEvent, type: 'category' | 'fu
7171

7272
// TODO: implement this for functors as well
7373
if (type === 'category') {
74-
const check = await check_consistency(
74+
const check = check_consistency(
7575
new Set(satisfied_properties),
7676
new Set(unsatisfied_properties),
7777
)
@@ -106,7 +106,7 @@ export async function search_handler(event: RequestEvent, type: 'category' | 'fu
106106
type,
107107
)
108108

109-
const { rows: found_objects, err } = await query<NamedObject>({
109+
const { rows: found_objects, err } = query<NamedObject>({
110110
sql: search_query,
111111
values: [
112112
...all_selected_properties,

src/routes/categories/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { error } from '@sveltejs/kit'
44
import sql from 'sql-template-tag'
55

66
export const load = async () => {
7-
const { results, err } = await batch<[CategoryShort, TagObject]>([
7+
const { results, err } = batch<[CategoryShort, TagObject]>([
88
sql`SELECT id, name FROM categories ORDER BY lower(name)`,
99
sql`SELECT tag FROM tags ORDER BY position`,
1010
])

src/routes/categories/[tag]/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import sql from 'sql-template-tag'
55
import type { EntryGenerator } from './$types'
66

77
export const entries: EntryGenerator = async () => {
8-
const { rows, err } = await query<TagObject>(sql`SELECT tag FROM tags`)
8+
const { rows, err } = query<TagObject>(sql`SELECT tag FROM tags`)
99
if (err) throw new Error('Database error: Failed to load tags')
1010
return rows
1111
}
1212

1313
export const load = async (event) => {
1414
const tag = event.params.tag
1515

16-
const { rows: categories, err } = await query<CategoryShort>(sql`
16+
const { rows: categories, err } = query<CategoryShort>(sql`
1717
SELECT c.id, c.name FROM categories c
1818
LEFT JOIN category_tag_assignments t ON c.id = t.category_id
1919
WHERE t.tag = ${tag}

src/routes/category-comparison/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { query } from '$lib/server/db.catdat'
44
import { error } from '@sveltejs/kit'
55

66
export const load = async () => {
7-
const { rows: categories, err } = await query<CategoryShort>(sql`
7+
const { rows: categories, err } = query<CategoryShort>(sql`
88
SELECT id, name FROM categories ORDER BY lower(name)
99
`)
1010

0 commit comments

Comments
 (0)