Code Monkey home page Code Monkey logo

manydb's Introduction

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.

Property Description
type Type of database to use. (It's recommended to use the provided type constants.)
user Database user.
password Password for user.
host Database host domain. (usually localhost for development)
database Database 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 => {
    // ...
  });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.