-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (53 loc) · 1.72 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const dbUtils = require('./lib/db-utils')
const createSchema = require('./lib/schema')
/**
* @typedef SchemaDefinition
* @type {Object}
* @property {string} tableName Name of the table were to save the documents
* @property {string[]} indexes possible keys you will be able to find documents
* @property {string} [primaryKey='id']
* @property {string} [primaryKeyType='STRING']
*/
/**
* Create a connection to a sqlite db using the given schema JSON dbs
* @param {string} location Route to the db file, could be ':memory:', or a path
* @param {SchemaDefinition[]} schemaDefinitions
*/
const createDB = (location, schemaDefinitions) => {
let db = null
const schemas = schemaDefinitions.map(createSchema)
const self = {
conn: db,
connect: async () => {
if (db) throw new Error('Cannot connect when already connected.')
db = await dbUtils.open(location)
self.conn = db
await Promise.all(schemas.map((schema) => schema.init(db)))
return self
},
disconnect: () => new Promise((resolve, reject) => {
const conn = db
db = null
self.conn = null
if (conn && conn.open) {
conn.close((err) => {
if (err) return reject(err)
resolve()
})
} else {
resolve()
}
})
}
schemas.forEach((schema) => {
const { schemaDefinition } = schema
// do not allow JS reserved object words like __proto__ nor current
// self used key, this will also check for repeated tableNames
if (schemaDefinition.tableName in self) {
throw new Error(`The tableName "${schemaDefinition.tableName}" is not a valid name`)
}
self[schemaDefinition.tableName] = schema
})
return self
}
module.exports = createDB