Async DB Adapter


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"
})
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,
})
const connection = create({
adapter: "mysql",
pool: true,
...mysqlConfig,
})
mysql2
const connection = create({
adapter: "mysql2",
...mysqlConfig,
})
const connection = create({
adapter: "mysql2",
pool: true,
...mysqlConfig,
})
pg
const connection = create({
adapter: "pg",
...pgConfig,
})
const connection = create({
adapter: "pg",
pool: true,
...pgConfig,
})
sqlite3
const connection = create({
adapter: "sqlite3",
filename: ":memory:",
})
Multiple Connection (Array)
const connections = create([
{
adapter: "mysql2",
pool: true,
...mysqlConfig,
},
{
adapter: "pg",
pool: true,
...pgConfig,
},
{
adapter: "sqlite3",
filename: ":memory:",
},
])
Multiple Connection (Object)
const connections = create({
default: {
adapter: "mysql2",
pool: true,
...mysqlConfig,
},
pg: {
adapter: "pg",
pool: true,
...pgConfig,
},
sqlite: {
adapter: "sqlite3",
filename: ":memory:",
},
})
Methods
All adapter objects inherit the following interfaces:
type TransactionHandler<P> = (connection: Connection) => Promise<P>|P
interface Pool extends Connection {
getConnection(): Promise<Connection>
}
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