npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sql-gen

v0.1.1

Published

A SQL query builder, inspired by SQLAlchemy

Downloads

5

Readme

node-sql-gen

A SQL query builder, inspired by SQLAlchemy.

Installation

npm install sql-gen

Example

We can define tables using sql.table() and sql.column():

import sql from "sql-gen";

const Author = sql.table("author", {
    id: sql.column({name: "id", type: sql.types.int, primaryKey: true}),
    name: sql.column({name: "name", type: sql.types.string})
});
const Book = sql.table("book", {
    id: sql.column({name: "id", type: sql.types.int, primaryKey: true}),
    authorId: sql.column({name: "author_id", type: sql.types.int}),
    title: sql.column({name: "title", type: sql.types.string}),
    genre: sql.column({name: "genre", type: sql.types.string})
});

We can then define a query:

const query = sql.from(Book)
    .join(Author, sql.eq(Book.c.authorId, Author.c.id))
    .where(sql.eq(Book.c.genre, "comedy"))
    .select(Author.c.name, Book.c.title);

We can use compile() to turn a query into a string and parameters, ready to pass into a database connection:

sql.compile(query)
//  {
//      "text": "SELECT author.name, book.title FROM book JOIN author ON book.author_id = author.id WHERE book.genre = ?",
//      "params": ["comedy"]
//  }

If using node-sqlite3:

const {text, params} = sql.compile(query);
const database = new sqlite3.Database("path/to/database");
database.all(text, ...params, (error, rows) => {
    if (error) {
        console.error(error);
    } else {
        console.log(rows);
    }
});

API

table(name, columns)

Represent a table in a database. Takes the following arguments:

  • name: the name of the table in the database.

  • columns: the columns in the table. The keys should be the name by which you want to refer to the columns. The values should be the result of calling column().

Returns an instance of Table, which has the following properties:

  • c: the columns of the table, which can then be used in generating queries. For instance:

    const Author = table("author", {
        id: sql.column({name: "id", type: sql.types.int, primaryKey: true}),
        name: sql.column({name: "name", type: sql.types.string})
    });
    sql.from(Author).select(Author.c.name)
  • as(alias): create a from clause for this table with an alternate name, as specified by alias. For instance:

    const Author = table("author", {
        id: sql.column({name: "id", type: sql.types.int, primaryKey: true}),
        name: sql.column({name: "name", type: sql.types.string})
    });
    const authorAlias = Author.as("favourite_author");
    sql.from(authorAlias).select(authorAlias.c.name).where(sql.eq(authorAlias.c.id, 42))
  • primaryKey: if the table has no columns defined as a primary key, this is null. Otherwise, this is an object with a columns property, which is an array of all of the columns in the primary key.

column(options)

Represent a column in a table.

Options should be an object with the following properties:

  • name: the name of the column in the database.

  • type: the type of the column. Use a value from types.

  • primaryKey (optional): set to true to mark this column as part of the table's primary key. Defaults to false.

  • nullable (optional): set to false to mark this column as NOT NULL. Defaults to true.

types

  • types.int: SQL integer type.
  • types.string: SQL string type.

from(selectable)

Create an instance of Query using selectable as the primary from clause.

Query

Query is used to generate SQL queries. It has the following properties:

  • join(selectable, condition): creates a JOIN clause onto the given selectable.

  • select(...columns): specify the columns to select.

  • distinct(): add a DISTINCT qualifier to this query.

  • where(condition): add a WHERE clause. If there's already a WHERE clause, condition is added using AND. condition should be a SQL expression.

  • subquery(): turn this query into a subquery that can then be selected from, similarly to a table.

createTable(table)

Represents a CREATE TABLE statement. Use compile() to compile it.

compile(query)

Turn a query or statement into a query that can be executed. Returns an object with two properties:

  • text: the text of the query
  • params: any parameters that have been generated during compilation of the query