1.0.0 • Published 4 years ago

sql-type v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

SQL Type

NPM version NPM downloads Build status Test coverage

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