sql-type
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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

Package Sidebar

Install

npm i sql-type

Weekly Downloads

5

Version

1.0.0

License

MIT

Unpacked Size

48.5 kB

Total Files

9

Last publish

Collaborators

  • blakeembrey