Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including sql-type with all npm packages installed. Try it out:

var sqlType = require("sql-type")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

sql-type v1.0.0

Type-safe SQL builder using ES2015 template tags

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

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free