0.1.2 • Published 6 years ago

manydb v0.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

manydb

npm version

Lightweight multi-idiom database API for Node.js.

Install

Install ManyDB:

yarn add manydb

// or

npm install manydb

Then install the package for your desired database (choose one):

yarn add pg     # PostgreSQL
yarn add mysql  # MySQL

It is possible to use multiple different database types, but in most cases only one should be used in order to minimize dependencies.

Usage

To use ManyDB, create a database with a config object and connect to it. You can then run queries on the database like normal.

ManyDB currently supports MySQL and PostgreSQL.

Example

import { createDb, POSTGRES } from manydb;

// Create and initialize database
const db = createDb({
  type: POSTGRES,
  user: 'myuser',
  password: 'pass', // optional
  host: 'localhost',
  database: 'my_db'
});

// Connect to database
db.connect()
  .then(() => {
    // ...
  })
  .catch(err => {
    // ...
  });

// Query
db.query('SELECT * FROM products')
  .then(result => {
    // ...
  })
  .catch(err => {
    // ...
  });

Creating a database

You can create a database using the provided createDb function.

Pass in a config object specifying the database details.

PropertyDescription
typeType of database to use. (It's recommended to use the provided type constants.)
userDatabase user.
passwordPassword for user.
hostDatabase host domain. (usually localhost for development)
databaseDatabase name.
// Create and initialize database
const db = createDb({
  type: POSTGRES,
  user: 'myuser',
  password: 'pass', // optional
  host: 'localhost',
  database: 'my_db'
});

Connection

You have to connect to the database before using it. Doing this is as simple as:

// Connect
db.connect()
  .then(() => {
    // ...
  })
  .catch(err => {
    // ...
  });

// Disconnect (only if needed)
db.disconnect()
  .then(() => {
    // ...
  })
  .catch(err => {
    // ...
  });

Query

Query the database.

// Query
db.query('SELECT * FROM products')
  .then(result => {
    // ...
  })
  .catch(err => {
    // ...
  });
0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago