1
0
Fork 0
out-of-your-element-fork-th.../db/orm.js

185 lines
4 KiB
JavaScript
Raw Normal View History

2023-09-14 00:32:27 +00:00
// @ts-check
const {db} = require("../passthrough")
2023-10-05 23:31:10 +00:00
const U = require("./orm-defs")
2023-09-14 00:32:27 +00:00
/**
* @template {keyof U.Models} Table
* @template {keyof U.Models[Table]} Col
* @param {Table} table
* @param {Col[] | Col} cols
2023-10-05 23:31:10 +00:00
* @param {Partial<U.Models[Table]>} where
2023-09-14 00:32:27 +00:00
* @param {string} [e]
*/
2023-10-05 23:31:10 +00:00
function select(table, cols, where = {}, e = "") {
2023-09-14 00:32:27 +00:00
if (!Array.isArray(cols)) cols = [cols]
2023-10-05 23:31:10 +00:00
const parameters = []
const wheres = Object.entries(where).map(([col, value]) => {
parameters.push(value)
return `"${col}" = ?`
})
const whereString = wheres.length ? " WHERE " + wheres.join(" AND ") : ""
2023-09-14 00:32:27 +00:00
/** @type {U.Prepared<Pick<U.Models[Table], Col>>} */
2023-10-05 23:31:10 +00:00
const prepared = db.prepare(`SELECT ${cols.map(k => `"${String(k)}"`).join(", ")} FROM ${table} ${whereString} ${e}`)
prepared.get = prepared.get.bind(prepared, ...parameters)
prepared.all = prepared.all.bind(prepared, ...parameters)
2023-09-14 00:32:27 +00:00
return prepared
}
/**
* @template {keyof U.Models} Table
* @template {keyof U.Merge<U.Models[Table]>} Col
*/
class From {
/**
* @param {Table} table
*/
constructor(table) {
2023-10-05 23:31:10 +00:00
/** @private @type {Table[]} */
2023-09-14 00:32:27 +00:00
this.tables = [table]
2023-10-05 23:31:10 +00:00
/** @private */
2024-01-31 00:09:39 +00:00
this.directions = []
/** @private */
2023-09-14 00:32:27 +00:00
this.sql = ""
2023-10-05 23:31:10 +00:00
/** @private */
2023-09-14 00:32:27 +00:00
this.cols = []
2023-10-05 23:31:10 +00:00
/** @private */
this.makeColsSafe = true
/** @private */
2023-09-14 00:32:27 +00:00
this.using = []
2023-10-05 23:31:10 +00:00
/** @private */
2023-09-18 13:23:52 +00:00
this.isPluck = false
2023-10-05 23:31:10 +00:00
/** @private */
this.parameters = []
2023-09-14 00:32:27 +00:00
}
/**
* @template {keyof U.Models} Table2
* @param {Table2} table
* @param {Col & (keyof U.Models[Table2])} col
2024-01-31 00:09:39 +00:00
* @param {"inner" | "left"} [direction]
2023-09-14 00:32:27 +00:00
*/
2024-01-31 00:09:39 +00:00
join(table, col, direction = "inner") {
2023-09-14 00:32:27 +00:00
/** @type {From<Table | Table2, keyof U.Merge<U.Models[Table | Table2]>>} */
// @ts-ignore
const r = this
r.tables.push(table)
2024-01-31 00:09:39 +00:00
r.directions.push(direction.toUpperCase())
2023-09-14 00:32:27 +00:00
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
}
selectUnsafe(...cols) {
this.cols = cols
this.makeColsSafe = false
return this
}
2023-09-14 00:32:27 +00:00
/**
* @template {Col} Select
* @param {Select} col
*/
pluck(col) {
/** @type {Pluck<Table, Select>} */
// @ts-ignore
const r = this
r.cols = [col]
2023-09-18 13:23:52 +00:00
r.isPluck = true
2023-09-14 00:32:27 +00:00
return r
}
/**
* @param {string} sql
*/
and(sql) {
2023-10-05 23:31:10 +00:00
this.sql += " " + sql
return this
}
/**
* @param {Partial<U.Models[Table]>} conditions
*/
where(conditions) {
const wheres = Object.entries(conditions).map(([col, value]) => {
this.parameters.push(value)
return `"${col}" = ?`
})
this.sql += " WHERE " + wheres.join(" AND ")
2023-09-14 00:32:27 +00:00
return this
}
prepare() {
if (this.makeColsSafe) this.cols = this.cols.map(k => `"${k}"`)
let sql = `SELECT ${this.cols.join(", ")} FROM ${this.tables[0]} `
2023-09-14 00:32:27 +00:00
for (let i = 1; i < this.tables.length; i++) {
const table = this.tables[i]
const col = this.using[i-1]
2024-01-31 00:09:39 +00:00
const direction = this.directions[i-1]
sql += `${direction} JOIN ${table} USING (${col}) `
2023-09-14 00:32:27 +00:00
}
sql += this.sql
/** @type {U.Prepared<Pick<U.Merge<U.Models[Table]>, Col>>} */
2023-09-18 13:23:52 +00:00
let prepared = db.prepare(sql)
if (this.isPluck) prepared = prepared.pluck()
2023-09-14 00:32:27 +00:00
return prepared
}
get(..._) {
const prepared = this.prepare()
2023-10-05 23:31:10 +00:00
return prepared.get(...this.parameters, ..._)
2023-09-14 00:32:27 +00:00
}
all(..._) {
const prepared = this.prepare()
2023-10-05 23:31:10 +00:00
return prepared.all(...this.parameters, ..._)
2023-09-14 00:32:27 +00:00
}
}
2023-11-23 03:42:36 +00:00
/* c8 ignore start - this code is only used for types and does not actually execute */
2023-09-14 00:32:27 +00:00
/**
* @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(..._)
}
}
2023-11-23 03:42:36 +00:00
/* c8 ignore stop */
2023-09-14 00:32:27 +00:00
/**
* @template {keyof U.Models} Table
* @param {Table} table
*/
function from(table) {
return new From(table)
}
module.exports.from = from
module.exports.select = select