sql-type
v1.0.0
Published
Type-safe SQL builder using ES2015 template tags
Downloads
9
Maintainers
Readme
SQL Type
Type-safe SQL builder using ES2015 template tags.
Installation
npm install sql-type --save
Usage
import {
query,
Table,
Column,
DefaultColumn,
CreateExpression,
ResultExpression
} from "sql-type";
export const table = new Table("users", {
id: new Column<string>("id"),
username: new Column<string>("username"),
createdAt: new DefaultColumn<Date>("created_at")
});
export type CreateData = Omit<CreateExpression<typeof table>, "id">;
export type Model = ResultExpression<typeof table>;
export async function one<T>(query: Query<T>) {
const result = await conn.query(query);
const { length } = result.rows;
if (length !== 1) throw new TypeError(`Expected 1 row, got ${length}`);
return query.decode(result.rows[0]);
}
export async function create(data: CreateData) {
return one(table.create(data).return(table.keys));
}
License
MIT