Add new WHERE feature to my funny orm
This commit is contained in:
		
							parent
							
								
									28abdac5b6
								
							
						
					
					
						commit
						475cd5b724
					
				
					 30 changed files with 149 additions and 105 deletions
				
			
		| 
						 | 
				
			
			@ -12,4 +12,8 @@ ALTER TABLE lottie RENAME COLUMN id TO sticker_id;
 | 
			
		|||
 | 
			
		||||
ALTER TABLE sim RENAME COLUMN discord_id TO user_id;
 | 
			
		||||
 | 
			
		||||
-- Rename id to emoji_id so joins make sense in the future
 | 
			
		||||
 | 
			
		||||
ALTER TABLE emoji RENAME COLUMN id TO emoji_id;
 | 
			
		||||
 | 
			
		||||
COMMIT;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										2
									
								
								db/orm-utils.d.ts → db/orm-defs.d.ts
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								db/orm-utils.d.ts → db/orm-defs.d.ts
									
										
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -64,7 +64,7 @@ export type Models = {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	emoji: {
 | 
			
		||||
		id: string
 | 
			
		||||
		emoji_id: string
 | 
			
		||||
		name: string
 | 
			
		||||
		animated: number
 | 
			
		||||
		mxc_url: string
 | 
			
		||||
							
								
								
									
										42
									
								
								db/orm.js
									
										
									
									
									
								
							
							
						
						
									
										42
									
								
								db/orm.js
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,19 +1,28 @@
 | 
			
		|||
// @ts-check
 | 
			
		||||
 | 
			
		||||
const {db} = require("../passthrough")
 | 
			
		||||
const U = require("./orm-utils")
 | 
			
		||||
const U = require("./orm-defs")
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @template {keyof U.Models} Table
 | 
			
		||||
 * @template {keyof U.Models[Table]} Col
 | 
			
		||||
 * @param {Table} table
 | 
			
		||||
 * @param {Col[] | Col} cols
 | 
			
		||||
 * @param {Partial<U.Models[Table]>} where
 | 
			
		||||
 * @param {string} [e]
 | 
			
		||||
 */
 | 
			
		||||
function select(table, cols, e = "") {
 | 
			
		||||
function select(table, cols, where = {}, e = "") {
 | 
			
		||||
	if (!Array.isArray(cols)) cols = [cols]
 | 
			
		||||
	const parameters = []
 | 
			
		||||
	const wheres = Object.entries(where).map(([col, value]) => {
 | 
			
		||||
		parameters.push(value)
 | 
			
		||||
		return `"${col}" = ?`
 | 
			
		||||
	})
 | 
			
		||||
	const whereString = wheres.length ? " WHERE " + wheres.join(" AND ") : ""
 | 
			
		||||
	/** @type {U.Prepared<Pick<U.Models[Table], Col>>} */
 | 
			
		||||
	const prepared = db.prepare(`SELECT ${cols.map(k => `"${String(k)}"`).join(", ")} FROM ${table} ${e}`)
 | 
			
		||||
	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)
 | 
			
		||||
	return prepared
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -26,13 +35,18 @@ class From {
 | 
			
		|||
	 * @param {Table} table
 | 
			
		||||
	 */
 | 
			
		||||
	constructor(table) {
 | 
			
		||||
		/** @type {Table[]} */
 | 
			
		||||
		/** @private @type {Table[]} */
 | 
			
		||||
		this.tables = [table]
 | 
			
		||||
 | 
			
		||||
		/** @private */
 | 
			
		||||
		this.sql = ""
 | 
			
		||||
		/** @private */
 | 
			
		||||
		this.cols = []
 | 
			
		||||
		/** @private */
 | 
			
		||||
		this.using = []
 | 
			
		||||
		/** @private */
 | 
			
		||||
		this.isPluck = false
 | 
			
		||||
		/** @private */
 | 
			
		||||
		this.parameters = []
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
| 
						 | 
				
			
			@ -78,7 +92,19 @@ class From {
 | 
			
		|||
	 * @param {string} sql
 | 
			
		||||
	 */
 | 
			
		||||
	and(sql) {
 | 
			
		||||
		this.sql = sql
 | 
			
		||||
		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 ")
 | 
			
		||||
		return this
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -98,12 +124,12 @@ class From {
 | 
			
		|||
 | 
			
		||||
	get(..._) {
 | 
			
		||||
		const prepared = this.prepare()
 | 
			
		||||
		return prepared.get(..._)
 | 
			
		||||
		return prepared.get(...this.parameters, ..._)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	all(..._) {
 | 
			
		||||
		const prepared = this.prepare()
 | 
			
		||||
		return prepared.all(..._)
 | 
			
		||||
		return prepared.all(...this.parameters, ..._)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,7 +6,7 @@ const data = require("../test/data")
 | 
			
		|||
const {db, select, from} = require("../passthrough")
 | 
			
		||||
 | 
			
		||||
test("orm: select: get works", t => {
 | 
			
		||||
	const row = select("guild_space", "guild_id", "WHERE space_id = ?").get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
 | 
			
		||||
	const row = select("guild_space", "guild_id", {}, "WHERE space_id = ?").get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
 | 
			
		||||
	t.equal(row?.guild_id, data.guild.general.id)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -16,10 +16,20 @@ test("orm: from: get works", t => {
 | 
			
		|||
})
 | 
			
		||||
 | 
			
		||||
test("orm: select: get pluck works", t => {
 | 
			
		||||
	const guildID = select("guild_space", "guild_id", "WHERE space_id = ?").pluck().get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
 | 
			
		||||
	const guildID = select("guild_space", "guild_id", {}, "WHERE space_id = ?").pluck().get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
 | 
			
		||||
	t.equal(guildID, data.guild.general.id)
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("orm: select: get, where and pluck works", t => {
 | 
			
		||||
	const channelID = select("message_channel", "channel_id", {message_id: "1128118177155526666"}).pluck().get()
 | 
			
		||||
	t.equal(channelID, "112760669178241024")
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("orm: select: all, where and pluck works on multiple columns", t => {
 | 
			
		||||
	const names = select("member_cache", "displayname", {room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe", mxid: "@cadence:cadence.moe"}).pluck().all()
 | 
			
		||||
	t.deepEqual(names, ["cadence [they]"])
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("orm: from: get pluck works", t => {
 | 
			
		||||
	const guildID = from("guild_space").pluck("guild_id").and("WHERE space_id = ?").get("!jjWAGMeQdNrVZSSfvz:cadence.moe")
 | 
			
		||||
	t.equal(guildID, data.guild.general.id)
 | 
			
		||||
| 
						 | 
				
			
			@ -29,3 +39,8 @@ test("orm: from: join and pluck works", t => {
 | 
			
		|||
	const mxid = from("sim").join("sim_member", "mxid").and("WHERE user_id = ? AND room_id = ?").pluck("mxid").get("771520384671416320", "!hYnGGlPHlbujVVfktC:cadence.moe")
 | 
			
		||||
	t.equal(mxid, "@_ooye_bojack_horseman:cadence.moe")
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
test("orm: from: where and pluck works", t => {
 | 
			
		||||
	const subtypes = from("event_message").where({message_id: "1141501302736695316"}).pluck("event_subtype").all()
 | 
			
		||||
	t.deepEqual(subtypes.sort(), ["m.image", "m.text"])
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue