Files
epg/scripts/core/db.js

77 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-01-09 18:15:38 +03:00
const nedb = require('nedb-promises')
2022-01-06 12:59:37 +03:00
const file = require('./file')
2023-05-15 19:11:07 +03:00
const DB_DIR = process.env.DB_DIR || './scripts/tmp/database'
2022-01-09 18:15:38 +03:00
class Database {
constructor(filepath) {
this.filepath = filepath
2022-01-15 18:27:43 +03:00
}
load() {
2022-01-09 18:15:38 +03:00
this.db = nedb.create({
2022-01-15 18:27:43 +03:00
filename: file.resolve(this.filepath),
2022-01-09 18:15:38 +03:00
autoload: true,
onload: err => {
if (err) console.error(err)
},
compareStrings: (a, b) => {
a = a.replace(/\s/g, '_')
b = b.replace(/\s/g, '_')
return a.localeCompare(b, undefined, {
sensitivity: 'accent',
numeric: true
})
}
2022-01-06 12:59:37 +03:00
})
}
2022-01-09 18:15:38 +03:00
removeIndex(field) {
return this.db.removeIndex(field)
}
2022-01-06 12:59:37 +03:00
2022-01-09 18:15:38 +03:00
addIndex(options) {
return this.db.ensureIndex(options)
}
2022-01-06 12:59:37 +03:00
2022-01-09 18:15:38 +03:00
compact() {
return this.db.persistence.compactDatafile()
}
2022-01-06 12:59:37 +03:00
2022-01-15 18:27:43 +03:00
stopAutocompact() {
return this.db.persistence.stopAutocompaction()
}
2022-01-09 18:15:38 +03:00
reset() {
return file.clear(this.filepath)
}
2022-01-06 12:59:37 +03:00
2022-01-09 18:15:38 +03:00
count(query) {
return this.db.count(query)
}
2022-01-06 12:59:37 +03:00
2022-01-09 18:15:38 +03:00
insert(doc) {
return this.db.insert(doc)
}
2022-01-06 12:59:37 +03:00
2022-01-09 18:15:38 +03:00
update(query, update) {
return this.db.update(query, update)
}
2022-01-06 12:59:37 +03:00
2022-01-09 18:15:38 +03:00
find(query) {
return this.db.find(query)
}
2022-01-06 12:59:37 +03:00
2022-01-09 18:15:38 +03:00
remove(query, options) {
return this.db.remove(query, options)
}
2022-01-06 12:59:37 +03:00
}
2022-01-09 18:15:38 +03:00
const db = {}
2022-01-30 02:41:43 +03:00
db.queue = new Database(`${DB_DIR}/queue.db`)
2022-01-09 18:15:38 +03:00
db.programs = new Database(`${DB_DIR}/programs.db`)
2022-01-06 12:59:37 +03:00
module.exports = db