Start moving from SQL to New Funny ORM
This commit is contained in:
parent
cab96cbc9e
commit
4e1e590c3a
8 changed files with 298 additions and 22 deletions
70
db/orm-utils.d.ts
vendored
Normal file
70
db/orm-utils.d.ts
vendored
Normal file
|
@ -0,0 +1,70 @@
|
|||
export type Models = {
|
||||
channel_room: {
|
||||
channel_id: string
|
||||
room_id: string
|
||||
name: string
|
||||
nick: string | null
|
||||
thread_parent: string | null
|
||||
custom_avatar: string | null
|
||||
}
|
||||
|
||||
event_message: {
|
||||
event_id: string
|
||||
message_id: string
|
||||
event_type: string | null
|
||||
event_subtype: string | null
|
||||
part: number
|
||||
source: number
|
||||
}
|
||||
|
||||
file: {
|
||||
discord_url: string
|
||||
mxc_url: string
|
||||
}
|
||||
|
||||
guild_space: {
|
||||
guild_id: string
|
||||
space_id: string
|
||||
}
|
||||
|
||||
member_cache: {
|
||||
room_id: string
|
||||
mxid: string
|
||||
displayname: string | null
|
||||
avatar_url: string | null
|
||||
}
|
||||
|
||||
message_channel: {
|
||||
message_id: string
|
||||
channel_id: string
|
||||
}
|
||||
|
||||
sim: {
|
||||
discord_id: string
|
||||
sim_name: string
|
||||
localpart: string
|
||||
mxid: string
|
||||
}
|
||||
|
||||
sim_member: {
|
||||
mxid: string
|
||||
room_id: string
|
||||
profile_event_content_hash: string
|
||||
}
|
||||
|
||||
webhook: {
|
||||
channel_id: string
|
||||
webhook_id: string
|
||||
webhook_token: string
|
||||
}
|
||||
}
|
||||
|
||||
export type Prepared<Row> = {
|
||||
pluck: () => Prepared<Row[keyof Row]>
|
||||
all: (..._: any[]) => Row[]
|
||||
get: (..._: any[]) => Row?
|
||||
}
|
||||
|
||||
export type AllKeys<U> = U extends any ? keyof U : never
|
||||
export type PickTypeOf<T, K extends AllKeys<T>> = T extends { [k in K]?: any } ? T[K] : never
|
||||
export type Merge<U> = {[x in AllKeys<U>]: PickTypeOf<U, x>}
|
1
db/orm-utils.js
Normal file
1
db/orm-utils.js
Normal file
|
@ -0,0 +1 @@
|
|||
module.exports = {}
|
140
db/orm.js
Normal file
140
db/orm.js
Normal file
|
@ -0,0 +1,140 @@
|
|||
// @ts-check
|
||||
|
||||
const {db} = require("../passthrough")
|
||||
const U = require("./orm-utils")
|
||||
|
||||
/**
|
||||
* @template {keyof U.Models} Table
|
||||
* @template {keyof U.Models[Table]} Col
|
||||
* @param {Table} table
|
||||
* @param {Col[] | Col} cols
|
||||
* @param {string} [e]
|
||||
*/
|
||||
function select(table, cols, e = "") {
|
||||
if (!Array.isArray(cols)) cols = [cols]
|
||||
/** @type {U.Prepared<Pick<U.Models[Table], Col>>} */
|
||||
const prepared = db.prepare(`SELECT ${cols.map(k => `"${String(k)}"`).join(", ")} FROM ${table} ${e}`)
|
||||
return prepared
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {keyof U.Models} Table
|
||||
* @template {keyof U.Merge<U.Models[Table]>} Col
|
||||
*/
|
||||
class From {
|
||||
/**
|
||||
* @param {Table} table
|
||||
*/
|
||||
constructor(table) {
|
||||
/** @type {Table[]} */
|
||||
this.tables = [table]
|
||||
|
||||
this.sql = ""
|
||||
this.cols = []
|
||||
this.using = []
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {keyof U.Models} Table2
|
||||
* @param {Table2} table
|
||||
* @param {Col & (keyof U.Models[Table2])} col
|
||||
*/
|
||||
join(table, col) {
|
||||
/** @type {From<Table | Table2, keyof U.Merge<U.Models[Table | Table2]>>} */
|
||||
// @ts-ignore
|
||||
const r = this
|
||||
r.tables.push(table)
|
||||
r.using.push(col)
|
||||
return r
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {Col} Select
|
||||
* @param {Col[] | Select[]} cols
|
||||
*/
|
||||
select(...cols) {
|
||||
/** @type {From<Table, Select>} */
|
||||
const r = this
|
||||
r.cols = cols
|
||||
return r
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {Col} Select
|
||||
* @param {Select} col
|
||||
*/
|
||||
pluck(col) {
|
||||
/** @type {Pluck<Table, Select>} */
|
||||
// @ts-ignore
|
||||
const r = this
|
||||
r.constructor = Pluck
|
||||
r.cols = [col]
|
||||
return r
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} sql
|
||||
*/
|
||||
and(sql) {
|
||||
this.sql = sql
|
||||
return this
|
||||
}
|
||||
|
||||
prepare() {
|
||||
let sql = `SELECT ${this.cols.map(k => `"${k}"`).join(", ")} FROM ${this.tables[0]} `
|
||||
for (let i = 1; i < this.tables.length; i++) {
|
||||
const table = this.tables[i]
|
||||
const col = this.using[i-1]
|
||||
sql += `INNER JOIN ${table} USING (${col}) `
|
||||
}
|
||||
sql += this.sql
|
||||
/** @type {U.Prepared<Pick<U.Merge<U.Models[Table]>, Col>>} */
|
||||
const prepared = db.prepare(sql)
|
||||
return prepared
|
||||
}
|
||||
|
||||
get(..._) {
|
||||
const prepared = this.prepare()
|
||||
return prepared.get(..._)
|
||||
}
|
||||
|
||||
all(..._) {
|
||||
const prepared = this.prepare()
|
||||
return prepared.all(..._)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {keyof U.Models} Table
|
||||
* @template {keyof U.Merge<U.Models[Table]>} Col
|
||||
*/
|
||||
class Pluck extends From {
|
||||
// @ts-ignore
|
||||
prepare() {
|
||||
/** @type {U.Prepared<U.Merge<U.Models[Table]>[Col]>} */
|
||||
// @ts-ignore
|
||||
const prepared = super.prepare()
|
||||
return prepared
|
||||
}
|
||||
|
||||
get(..._) {
|
||||
const prepared = this.prepare()
|
||||
return prepared.get(..._)
|
||||
}
|
||||
|
||||
all(..._) {
|
||||
const prepared = this.prepare()
|
||||
return prepared.all(..._)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {keyof U.Models} Table
|
||||
* @param {Table} table
|
||||
*/
|
||||
function from(table) {
|
||||
return new From(table)
|
||||
}
|
||||
|
||||
module.exports.from = from
|
||||
module.exports.select = select
|
Loading…
Add table
Add a link
Reference in a new issue