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

sqldef

v0.0.1

Published

Simple node wrapper around sqldef for easy use with node

Downloads

10

Readme

node-sqldef

Simple node wrapper around sqldef for easy use with node.

This is a super-simple wrapper around sqldef that will download the appropriate (for your platform/arch) CLI from sqldef releases and run it. It works with mysql & postgres.

If it's unclear how this might be usful in your workflow, just checkout this live demo.

usage

Install it in your project as a dev-dependency, or use it standalone in npx:

npm i -D sqldef

# or

npx sqldef --help

Code

You can use it in your own code (like for migration scripts or whatever)

import sqldef from 'sqldef'

const main = async () => {
  const output = await (sqldef({
    type,         // the type of your database ("mysql" or "postgres")
    database,     // the name of your database
    user,         // the username of your database
    password,     // the password of your database
    host,         // the hostname of your database
    port,         // the port of your database
    socket,       // the unix socket (for mysql)
    file,         // the schema file to read/write
    dry: true,    // dry run - don't do anything to the database
    get: true     // get the current definition from database
  }))
}
main()

CLI

Use it in your npm scripts.

Usage: sqldef <command> [options]

Commands:
  sqldef export  Export your database to a file
  sqldef import  Import your database from a file

Options:
  -?, --help        Show help                                          [boolean]
  -f, --file        The schema file to import/export     [default: "schema.sql"]
  -t, --type        The type of the database                  [default: "mysql"]
  -h, --host        The host of the database              [default: "localhost"]
  -d, --database    The name of the database                   [default: "test"]
  -u, --user        The user of the database                   [default: "root"]
  -P, --password    The password for the database                  [default: ""]
  -p, --port        The port of the database
  -s, --socket      The socket of the database (only for mysql)
  -x, --no-confirm  Don't confirm before running the import/export     [boolean]
  -v, --version     Show version number                                [boolean]

Examples:
  sqldef export --user=cool                 Save your current schema, from your
  --password=mysecret                       mysql database, in schema.sql
  --database=mydatabase
  sqldef import --user=cool                 Save the structure from your currnet
  --password=mysecret                       database in schema.sql
  --database=mydatabase

configuration

This uses cosmiconfig, which gives us a lot of options for configuration, and your settings will be merged from all the things you setup.

  • a sqldef property in package.json
  • a .sqldefrc file in JSON or YAML format
  • a .sqldefrc.json file
  • a .sqldefrc.yaml, .sqldefrc.yml, or .sqldefrc.js file
  • a sqldef.config.js file exporting a JS object

ideas

Here are some cool ways to use this tool in your project.

development

Make a file called schema.sql, and make a .sqldefrc file with your settings. Make sure .sqldefrc is in your .gitignore, so you don't accidentally check-in your database secrets.

.sqldefrc

type: mysql
host: localhost
database: test
user: root
password: root
file: schema.sql

Maybe also make a .sqldefrc.example to help your users set stuff up.

Now you can run npx sqldef import to apply the schema in schema.sql, and npx sqldef export to put the current structure in the database from your schema.sql.

CI

Let's say you want to run it in CI with the schema in schema.sql. I recommend you use environment-variables to keep all the config in your CI settings:

DB_TYPE=mysql
DB_HOST=mysqlhost.com
DB_PWD=mysecretpassword
DB_USER=root

Now, install as a dev-dependency in your project with npm i -D sqldef, and make a script in package.json for doing schema migration:

{
  "scripts": {
    "migrate": "sqldef import --file=schema.sql --type=$DB_TYPE --host=$DB_HOST --password=$DB_PWD --user=$DB_USER --no-confirm",
    "schema": "sqldef export --file=schema.sql --type=$DB_TYPE --host=$DB_HOST --password=$DB_PWD --user=$DB_USER"
  }
}

Now, configure your CI to execute npm run migrate when code gets checked in. You might want to keep the above .sqldefrc method (from above) for development. The configuration you setup there will override the DB_ environment vars, so it should work pretty well together. If you are using environment variables for other things, you could also just use your environment, and set it all in once place (like .env using dotenv.)