-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
33 lines (31 loc) · 921 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const TableProxy = require('./proxy')
/**
* Create new connection to database.
*
* By accessing an attribute an existing table will be returned
* or a new one will be created.
*
* See table class about how to do CRUD operations
*
* @example
* // Memory database
* const dataSet = await connect()
* const pk = await dataSet.myNewTableName.insert({'myNewColumnName': 'data'})
*
* @example
* // File database
* const dataSet = await connect('mydb.sqlite')
* const pk = await dataSet.myNewTableName.insert({'myNewColumnName': 'data'})
*
* @param {string} [name=":memory:"]
* @returns {Object} new Alopex instance.
*/
async function connect(name) {
const dbName = name || ':memory:'
const sqlite3Async = await import('sqlite-async');
return sqlite3Async.Database.open(dbName).then((db) => {
db.dbName = dbName
return new Proxy(db, TableProxy)
})
}
module.exports = connect