Skip to content

Instantly share code, notes, and snippets.

@wess
Created February 2, 2023 14:36
Show Gist options
  • Save wess/edbfd483c5f475e57a9a187aa0087d4f to your computer and use it in GitHub Desktop.
Save wess/edbfd483c5f475e57a9a187aa0087d4f to your computer and use it in GitHub Desktop.
Knex based query generator
import YourKnexConnection from './connection';
import Schema from './schema';
import Provider from './provider';
interface User extends Schema {
email:string;
password:string;
}
const userProvider = Provider<User>(YourKnexConnection, 'users_table');
const register = async (email:string, password:string) => {
const hash = await argon2.hash(password);
const stmt = {
email,
password: hash,
};
const user = await provider.create(stmt);
if(!user) {
throw Error(`Unable to create account with email: ${email}`);
}
return user;
}
export default {
...userProvider,
register
}
import 'appendix';
import {Knex} from 'knex';
import Schema from './schema';
export type Statement = {[key:string]:string | number | boolean};
const Provider = <E extends Schema>(conn:Knex, table:string, returnFields:string[] = ['*']) => {
const create = async (stmt:Statement):Promise<E> => {
const createdAt = Object.keys(stmt).includes('createdAt') ? stmt.createdAt : Date.now();
const updatedAt = Date.now();
let updatedStmt = {
...stmt,
createdAt,
updatedAt,
};
try {
const result = await conn(table)
.insert(updatedStmt)
.returning(returnFields);
return <E>(result[0]);
} catch (err) {
throw err;
}
}
const update = async <E>(stmt:Statement, where:Statement):Promise<E> => {
const updatedAt = Date.now();
try {
const result = await conn(table)
.where(where)
.update({...stmt, updatedAt})
.returning(returnFields);
return <E>(result[0]);
} catch (err) {
throw err;
}
}
const upsert = async <E>(stmt:Statement):Promise<E> => {
const createdAt = Object.keys(stmt).includes('createdAt') ? stmt.createdAt : Date.now();
const updatedAt = Date.now();
const updatedStmt = {
...stmt,
createdAt,
updatedAt,
};
try {
const result = await conn(table)
.upsert(updatedStmt)
.returning(returnFields);
return <E>(result[0]);
} catch (err) {
throw err;
}
}
const get = async <E>(where:Statement):Promise<E> => {
try {
const result = await conn
.select(returnFields)
.from<E>(table)
.where(where)
.first();
return <E>result;
} catch (err) {
throw err;
}
}
const list = async <E>():Promise<E[]> => {
try {
return await conn
.select(returnFields)
.from<E>(table);
} catch (err) {
throw err;
}
}
const find = async <E>(where:Statement):Promise<E[]> => {
try {
return await conn
.select(returnFields)
.from<E>(table)
.where(where);
} catch (err) {
throw err;
}
}
const del = async <E>(where:Statement):Promise<number> => {
try {
return await conn(table)
.where(where)
.del();
} catch (err) {
throw err;
}
}
return {
create,
update,
upsert,
list,
get,
find,
del,
};
}
export default Provider;
interface Schema {
id: string | null;
updatedAt: number;
createdAt: number;
}
export default Schema;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment