@stdjs/database
    Overview
    Discussions
    Alternatives
    Reviews
    Code
    Tutorials
    Playground
    Chat
    Versions

@stdjs/database

Database Adapter with Async/Promise for Javascript(& Typescript).

2.0.0-alpha.2  •  Updated 6 months ago  •  by Cris  •  MIT License

STDJS - Database

Downloads Version License

NPM

Database Adapter with Async/Promise for Javascript(& Typescript).

Installation

npm install @stdjs/database --save

Support Database Connection

  • mysql
    • npm install mysql --save (in typescript npm install @types/mysql -D)
  • mysql2
    • npm install mysql2 --save (in typescript npm install types/mysql2 -D)
  • pg
    • npm install pg --save (in typescript npm install @types/pg -D)
  • sqlite3
    • npm install sqlite3 --save (in typescript npm install @types/sqlite3 -D)

Interfaces

All adapter objects inherit the following interfaces:


export type TransactionHandler<TResult> = (connection: Connection) => Promise<TResult> | TResult

export interface Connector {
  dialect: string
  connect(): Connection
}

export interface Pool extends Connection {
  getConnection(): Promise<PoolConnection>
}

export interface PoolConnection extends Connection {
  release(): Promise<void>
}

export interface Connection {
  close(): Promise<void>
  query(query: string, values?: any[]): Promise<QueryResult>
  first<TRow>(query: string, values?: any[]): Promise<TRow> // throw RowNotFoundError
  select<TRow extends Row>(query: string, values?: any[]): Promise<TRow[]>
  transaction<TResult>(handler: TransactionHandler<TResult>): Promise<TResult>
}

export interface QueryResult {
  insertId?: number|string // insert query only
  changes: number
  raw: any
}

export interface Row {
  [key: string]: any
}

Usage

You can create as follows:

(Please refer to the Config section for config.)

const database = require("@stdjs/database")
const connection = database.createConnection(/* Connector */)

// or
import { create } from "@stdjs/database"
const connection = createConnection(/* Connector */)

Create Connection

mysql

Options

import { createConnection } from "@stdjs/database" 
import { MysqlConnector } from '@stdjs/database/lib/driver/mysql'

const connection = createConnection(new MysqlConnector({
  /* mysql connection options */
}))

const pool = createConnection(new MysqlConnector({
  pool: true,
  /* mysql pool options */
}))

mysql2

Options

import { createConnection } from "@stdjs/database" 
import { Mysql2Connector } from '@stdjs/database/lib/driver/mysql2'

const connection = createConnection(new Mysql2Connector({
  /* mysql2 connection options */
}))

const pool = createConnection(new Mysql2Connector({
  pool: true,
  /* mysql2 pool options */
}))

pg

Options

import { createConnection } from "@stdjs/database" 
import { PgConnector } from '@stdjs/database/lib/driver/pg'

const connection = createConnection(new PgConnector({
  /* pg connection options */
}))

const pool = createConnection(new PgConnector({
  pool: true,
  /* pg pool options */
}))

sqlite3

Options

interface Sqlite3ConnectorOptions {
  filename: string
  mode?: number
}
import { createConnection } from "@stdjs/database" 
import { Sqlite3Connector } from '@stdjs/database/lib/driver/sqlite3'

const connection = createConnection(new Sqlite3Connector({
  /* sqlite3 connection options (Sqlite3ConnectorOptions) */
}))

Cluster Connection

If you are using the cluster as AWS Aurora DB, you can do the following:

import { createCluster } from "@stdjs/database" 
import { Mysql2Connector } from '@stdjs/database/lib/driver/mysql2'

const connection = createCluster({
  write: new Mysql2Connector({
    pool: true,
    host: "stdjs.com",
  }),
  read: new Mysql2Connector({
    pool: true,
    host: "readonly.stdjs.com",
  }),
})

The select and first methods use read connection and the query method uses write connection.

License

MIT

Popularity

Maintenance

Development

Last ver 6 months ago
Created 1 year ago
Last commit 1 second ago
1 second between commits

Technology

Node version: 8.16.0
139.5K unpacked

Compliance

MIT License
OSI Approved
0 vulnerabilities

Contributors

0 contributors

Dependencies

Tags

database-adapter
db-adapter
database
db
adapter
async
mysql
mysql2
pg
postgre
sqlite3
sqlite
aurora
auroradb
rds
aws
typescript
Ready for the next level?
Join Openbase's founding team to help us build the ultimate open-source app store, work with the latest technologies, and enjoy great culture, impact and autonomy
Openbase helps developers choose among and use millions of open-source packages, so they can build amazing products faster.
FacebookLinkedIn
© 2020 Devstore, Inc.