2023-02-28 16:51:38 +00:00
|
|
|
import { type Database, QueryBuilder } from 'wildebeest/backend/src/database'
|
2023-02-22 16:09:43 +00:00
|
|
|
import type { Env } from 'wildebeest/backend/src/types/env'
|
|
|
|
|
2023-02-28 16:51:38 +00:00
|
|
|
const qb: QueryBuilder = {
|
|
|
|
jsonExtract(obj: string, prop: string): string {
|
|
|
|
return `json_extract(${obj}, '$.${prop}')`
|
|
|
|
},
|
|
|
|
|
|
|
|
jsonExtractIsNull(obj: string, prop: string): string {
|
|
|
|
return `${qb.jsonExtract(obj, prop)} IS NULL`
|
|
|
|
},
|
|
|
|
|
|
|
|
set(array: string): string {
|
|
|
|
return `(SELECT value FROM json_each(${array}))`
|
|
|
|
},
|
|
|
|
|
|
|
|
epoch(): string {
|
|
|
|
return '00-00-00 00:00:00'
|
|
|
|
},
|
2023-03-01 09:20:59 +00:00
|
|
|
|
|
|
|
insertOrIgnore(q: string): string {
|
|
|
|
return `INSERT OR IGNORE ${q}`
|
|
|
|
},
|
2023-03-01 09:37:23 +00:00
|
|
|
|
|
|
|
psqlOnly(): string {
|
|
|
|
return ''
|
|
|
|
},
|
2023-03-08 15:56:48 +00:00
|
|
|
|
|
|
|
jsonArray(r: string): string {
|
|
|
|
return `json_array(${r})`
|
|
|
|
},
|
2023-02-28 16:51:38 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 10:19:25 +00:00
|
|
|
export default function make({ DATABASE }: Pick<Env, 'DATABASE'>): Database {
|
2023-02-28 16:51:38 +00:00
|
|
|
const db = DATABASE as any
|
|
|
|
db.qb = qb
|
2023-03-01 12:24:25 +00:00
|
|
|
db.client = 'd1'
|
2023-02-28 16:51:38 +00:00
|
|
|
|
|
|
|
return db as Database
|
2023-02-22 16:09:43 +00:00
|
|
|
}
|