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-generate

v1.5.0

Published

Automatic generation of SQL definitions for use by the sql module

Downloads

325

Readme

node-sql-generate

Build Status NPM version

This library is a command line tool/API for automatically generating definitions usable by the node-sql library. It inspects your database and generates JavaScript.

Currently there is support for MySQL, Postgres and MSSQL.

Installation

Install via NPM: npm install -g sql-generate

This will put the binary node-sql-generate in your path.

Note: you will need to install the appropriate client drivers separately (mysql, pg, mssql). This module does not install those dependencies for you (this was changed as of v1.0.0).

Usage

Command line

In its simplest form, you do something like this: node-sql-generate --dsn "mysql://user:password@server/database" which will spit out the generated code to stdout.

// autogenerated by node-sql-generate v0.1.2 on Tue May 21 2013 01:04:12 GMT-0700 (PDT)
var sql = require('sql');

/**
 * SQL definition for database.bar
 */
exports.bar = sql.define({
	name: 'bar',
	columns: [
		'id',
		'foo_id'
	]
});

/**
 * SQL definition for database.foo
 */
exports.foo = sql.define({
	name: 'foo',
	columns: [
		'id',
		'field_1',
		'foo_bar_baz'
	]
});

If your DSN isn't as specific (i.e. it just points to a socket file), then you'll need to specify the database and dialect explicitly: node-sql-generate --dsn "/var/run/mysql.sock" --database "test" --dialect "mysql"

Postgres users will need to specify a schema: node-sql-generate --dsn "postgres://user:password@server/database" --schema "test".

Omitting it will assume a schema of "public".

--help

  Usage: node-sql-generate [options]

  Options:

    -h, --help                     output usage information
    -V, --version                  output the version number
    --dsn <dsn>                    Connection string
    -d, --dialect <dialect>        Specify the SQL dialect: "mysql", "pg" or "mssql"
    -o, --output-file <file>       Output to this file; defaults to stdout
    -i, --indent <token>           Indentation token; defaults to a TAB character
    -D, --database <name>          Name of database to extract from
    -s, --schema <name>            Name of schema to extract from (Postgres/MSSQL only)
    -r, --exclude-regex <regexes>  Comma-delimited list of regular expressions for tables to exclude
    --camelize                     Convert underscored names to camel case, requires sql >= 0.18.0
    --eol <token>                  Line terminator token; defaults to "\n"
    --mode <mode>                  The permission mode of the generated file; defaults to 0644
    --encoding <encoding>          The encoding to use for writing; defaults to "utf8"
    --prepend <text>               Prepend text to the beginning of the file
    --append <text>                Append text to the end of the file
    --modularize                   Omit "require('sql')" and wrap generated code in "module.exports = function(sql) {...}"
    --omit-comments                Omit autogenerated comments
    --include-schema               Include schema in definition
    --include-meta                 Include metadata for columns (purely information, not used by sql lib)
    -v, --verbose                  Print debugging information

Example DSN:
  PostgreSQL: "postgres://user:password@host:5432/database"
       MySQL: "mysql://user:password@host:3306/database"
       MSSQL: "mssql://server=server[\instance];database=database;user=user;password=password;"

API

You can also generate these files programmatically.

var generateSqlDefinition = require('sql-generate'),
	options = {
		dsn: 'mysql://user:password@host/database',
		omitComments: true,
		prepend: '// (c) 2013 the raddest dude alive',
		excludeRegex: [ /raster_/, /geography_/, /geometry_/ ]
	};

generateSqlDefinition(options, function(err, stats) {
	if (err) {
		console.error(err);
		return;
	}

	console.log(stats.buffer);
});

Development

To run the tests, you'll need to install Vagrant.

git clone [email protected]:tmont/node-sql-generate.git
cd node-sql-generate
npm install
vagrant up linux
npm test

Windows Testing

The Vagrant script will install a Windows VM which takes up about 14GB on your hard drive. Install at your own risk :)

Run vagrant up windows to provision the windows VM.

Also, some users experienced segfaults while running windows tests (probably due to unrelated environment issues) on Linux. This issue only manifested in node 0.12 (0.10.x was okay). Other users reported no issues (on OS X and Linux).

Travis does not yet support Windows, so the MSSQL tests are disabled when run by continuous integration.