0.5.1 • Published 6 years ago

async-db-adapter v0.5.1

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

Async DB Adapter

Downloads Version License

NPM

Async database adapter for Javascript(& Typescript).

Installation

npm install async-db-adapter --save

Usage

You can create as follows:

(Please refer to the Config section for config.)

const connection = require("async-db-adapter").create({
  adapter: "mysql"
  /* config */
})
// or import {create} from "async-db-adapter"
// create(/* ... */)

Support Database Connection

  • mysql (require npm install mysql --save)
  • mysql2 (require npm install mysql2 --save)
  • pg (require npm install pg --save)
  • sqlite3 (require npm install sqlite3 --save)

Create Connection

Use adapter, pool parameter of create function`s config

mysql

const connection = create({
  adapter: "mysql",
  ...mysqlConfig,
}) // return instanceof MysqlConnection
const connection = create({
  adapter: "mysql",
  pool: true,
  ...mysqlConfig,
}) // return instanceof MysqlPool

mysql2

const connection = create({
  adapter: "mysql2",
  ...mysqlConfig,
}) // return instanceof Mysql2Connection
const connection = create({
  adapter: "mysql2",
  pool: true,
  ...mysqlConfig,
}) // return instanceof Mysql2Pool

pg

const connection = create({
  adapter: "pg",
  ...pgConfig,
}) // return instanceof PgConnection
const connection = create({
  adapter: "pg",
  pool: true,
  ...pgConfig,
}) // return instanceof PgPool

sqlite3

const connection = create({
  adapter: "sqlite3",
  filename: ":memory:",
}) // return instanceof Sqlite3Connection

Multiple Connection (Array)

const connections = create([
  {
    adapter: "mysql2",
    pool: true,
    ...mysqlConfig,
  },
  {
    adapter: "pg",
    pool: true,
    ...pgConfig,
  },
  {
    adapter: "sqlite3",
    filename: ":memory:",
  },
]) // return instanceof [MysqlPool, PgPool, Sqlite3Connection]

Multiple Connection (Object)

const connections = create({
  default: {
    adapter: "mysql2",
    pool: true,
    ...mysqlConfig,
  },
  pg: {
    adapter: "pg",
    pool: true,
    ...pgConfig,
  },
  sqlite: {
    adapter: "sqlite3",
    filename: ":memory:",
  },
}) // return instanceof {default: MysqlPool, pg: PgPool, sqlite: Sqlite3Connection}

Methods

All adapter objects inherit the following interfaces:

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

// mysql-pool, mysql2-pool, pg-pool
interface Pool extends Connection {
  getConnection(): Promise<Connection>
}

// mysql, mysql2, pg, sqlite3
interface Connection {
  close(): Promise<void>
  query(query: string, values?: any): Promise<any>
  select(query: string, values?: any): Promise<Row[]>
  first(query: string, values?: any): Promise<Row|undefined>
  transaction<P>(handler: TransactionHandler<P>): Promise<P>
}

Config

Config can be defined as follows:

Mysql / Mysql2

Use the connection option of the mysql or mysql2.

interface MysqlConnectionConfig extends MysqlBaseConfig {
  readonly adapter: "mysql" | "mysql2"
  readonly pool?: false
}

interface MysqlPoolConfig extends MysqlBaseConfig {
  readonly adapter: "mysql" | "mysql2"
  readonly pool: true

  acquireTimeout?: number
  waitForConnections?: boolean
  connectionLimit?: number
  queueLimit?: number
}

interface MysqlBaseConfig {
  host?: string
  port?: number
  user?: string
  password?: string
  database?: string
  charset?: string
  timeout?: number
  localAddress?: string
  socketPath?: string
  timezone?: string
  connectTimeout?: number
  stringifyObjects?: boolean
  insecureAuth?: boolean
  supportBigNumbers?: boolean
  bigNumberStrings?: boolean
  dateStrings?: boolean
  trace?: boolean
  multipleStatements?: boolean
  flags?: string[]
  queryFormat?(query: string, values: any): void
}

Postgres

Use the connection option of the pg.

interface PgConnectionConfig extends PgBaseConfig {
  readonly adapter: "pg"
  readonly pool?: false
}

interface PgPoolConfig extends PgBaseConfig {
  readonly adapter: "pg"
  readonly pool: true

  max?: number
  min?: number
  connectionTimeoutMillis?: number
  idleTimeoutMillis?: number

  application_name?: string
  Promise?: PromiseConstructorLike
}

interface PgBaseConfig {
  ssl?: boolean | tls.TlsOptions

  user?: string
  database?: string
  password?: string
  port?: number
  host?: string
  connectionString?: string
  keepAlive?: boolean
  stream?: stream.Duplex
}

Sqlite

Use the connection option of the sqlite3.

interface Sqlite3ConnectionConfig {
  readonly adapter: "sqlite3"
  readonly pool?: false
  filename: string
  mode?: number
}

License

MIT

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago