db-util-lite
a db utility for node apps
Last updated 2 years ago by isaacmeals .
ISC · Original npm · Tarball · package.json
$ cnpm install db-util-lite 
SYNC missed versions from official npm registry.

Db Util Lite

A lite database set of utilities for node apps. Currently only supports Postgres.

Current functionality:

  • migrations pertaining to database schemas

In order to use any of the functionality, the databases has to already have been created. This code will not create nor drop databases.

Migrations

When creating a migration, a file is generated. That file has SQL that is used when moving forward and SQL that is used when going backwards.

Here is the template for this migration file:

import { SQL, SQLStatement } from "sql-template-strings";

export const name = "";

export const up = (): SQLStatement => {
    const query = SQL`
        SELECT 'up'
    `;

    return query;
};

export const down = (): SQLStatement => {
    const query = SQL`
        SELECT 'down'
    `;

    return query;
};

The name constant should match a name in the db-util.json file.

the migration file is in TypeScript so before running migrations run a transpiler.

Bash Commands

Setup (creates a table used for managing migrations)

node node_modules/db-util-lite setup --src /some/path/db-util.json

Remove (only removes the one table from the setup)

node node_modules/db-util-lite remove --src /some/path/db-util.json --env localhost

Create a migration:

node node_modules/db-util-lite createMigration --name first --src /some/path/db-util.json  --db pg --dir /Users/Isaac/projects/db-util/migrations

Run migrations backwards:

node node_modules/db-util-lite runMigrations --src /some/path/db-util.json --go down

Run migrations foward:

node node_modules/db-util-lite runMigrations --src /some/path/db-util.json --go up

File:db-util.json

{
  "version": "1.0.0",
  "appDir": "/path/to/app/",
  "environment": "localhost",
  "seedDir": "seeds",
  "migrationDir": "src/migrations",
  "databases": [
    {
      "name": "one-db",
      "type": "postgres",
      "connStrs" :
        [{
          "env": "localhost",
          "user": "postgres",
          "host": "localhost",
          "database": "this-new-app",
          "password": "postgres",
          "port": "5433"
        },
        {
          "env": "development",
          "user": "postgres",
          "host": "localhost",
          "database": "this-new-app",
          "password": "postgres",
          "port": "5433"
        }]
    },
    {
      "name": "another-db",
      "type": "postgres",
      "connStrs" :
        [{
          "env": "localhost",
          "user": "postgres",
          "host": "localhost",
          "database": "this-new-app",
          "password": "postgres",
          "port": "5433"
        },
        {
          "env": "development",
          "user": "postgres",
          "host": "localhost",
          "database": "this-new-app",
          "password": "postgres",
          "port": "5433"
        }]
    }
  ]
}

Query

This feature exposes the query module that was built for migrations and allows for that module to be used anywhere in the app.

Features:

  • allows for the app to talk to many different dabases
  • runs all queries in UTC time
import { db as dbUtil } from "db-util-lite";
import { SQL, SQLStatement } from "sql-template-strings";

dbUtil.db.conf.setup("/some/path/db-util.json");
dbUtil.db.conn.setup(dbUtil.db.conf.allDatabases(), "localhost");

const query: SQLStatement = SQL`SELECT count(*) FROM persons`;

const dbOpts: dbUtil.db.conn.QueryOptions  = {
    name: "value-from-file",
};

await dbUtil.db.conn.rows(query, dbOpts);

Current Tags

  • 2.0.48                                ...           latest (2 years ago)

69 Versions

  • 2.0.48                                ...           2 years ago
  • 2.0.47                                ...           2 years ago
  • 2.0.46                                ...           2 years ago
  • 2.0.45                                ...           2 years ago
  • 2.0.44                                ...           2 years ago
  • 2.0.43                                ...           2 years ago
  • 2.0.42                                ...           2 years ago
  • 2.0.41                                ...           2 years ago
  • 2.0.40                                ...           2 years ago
  • 2.0.39                                ...           2 years ago
  • 2.0.38                                ...           2 years ago
  • 2.0.37                                ...           2 years ago
  • 2.0.36                                ...           2 years ago
  • 2.0.35                                ...           2 years ago
  • 2.0.34                                ...           2 years ago
  • 2.0.33                                ...           2 years ago
  • 2.0.32                                ...           2 years ago
  • 2.0.31                                ...           2 years ago
  • 2.0.30                                ...           2 years ago
  • 2.0.29                                ...           2 years ago
  • 2.0.28                                ...           2 years ago
  • 2.0.27                                ...           2 years ago
  • 2.0.26                                ...           2 years ago
  • 2.0.25                                ...           2 years ago
  • 2.0.24                                ...           2 years ago
  • 2.0.23                                ...           2 years ago
  • 2.0.22                                ...           2 years ago
  • 2.0.21                                ...           3 years ago
  • 2.0.20                                ...           3 years ago
  • 2.0.19                                ...           3 years ago
  • 2.0.18                                ...           3 years ago
  • 2.0.17                                ...           3 years ago
  • 2.0.16                                ...           3 years ago
  • 2.0.15                                ...           3 years ago
  • 2.0.14                                ...           3 years ago
  • 2.0.13                                ...           3 years ago
  • 2.0.12                                ...           3 years ago
  • 2.0.11                                ...           3 years ago
  • 2.0.10                                ...           3 years ago
  • 2.0.9                                ...           3 years ago
  • 2.0.8                                ...           3 years ago
  • 2.0.7                                ...           3 years ago
  • 2.0.6                                ...           3 years ago
  • 2.0.5                                ...           3 years ago
  • 2.0.4                                ...           3 years ago
  • 2.0.3                                ...           3 years ago
  • 2.0.2                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.1.18                                ...           3 years ago
  • 1.1.17                                ...           3 years ago
  • 1.1.16                                ...           3 years ago
  • 1.1.15                                ...           3 years ago
  • 1.1.14                                ...           3 years ago
  • 1.1.13                                ...           3 years ago
  • 1.1.12                                ...           3 years ago
  • 1.1.11                                ...           3 years ago
  • 1.1.10                                ...           3 years ago
  • 1.1.9                                ...           3 years ago
  • 1.1.8                                ...           3 years ago
  • 1.1.7                                ...           3 years ago
  • 1.1.6                                ...           3 years ago
  • 1.1.5                                ...           3 years ago
  • 1.1.4                                ...           3 years ago
  • 1.1.3                                ...           3 years ago
  • 1.1.2                                ...           3 years ago
  • 1.1.1                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 0
Last Month 0
Dependencies (9)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |