New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

async-db-adapter

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-db-adapter

Async database adapter for Javascript(& Typescript).

  • 0.5.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
decreased by-28.57%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 03 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc