service-core/core/db.mjs
Jonatan Nilsson f008af597d
Some checks failed
continuous-integration/appveyor/branch AppVeyor build failed
Application almost done
2022-01-27 18:25:33 +00:00

103 lines
2.3 KiB
JavaScript

import { Low, JSONFile, Memory } from 'lowdb'
import { type } from 'os'
import { defaults, isObject } from './defaults.mjs'
export default function GetDB(config, log, orgFilename = 'db.json') {
let adapter = new Memory()
let fullpath = 'in-memory'
if (orgFilename) {
fullpath = orgFilename
adapter = new JSONFile(fullpath)
}
const db = new Low(adapter)
db.id = 'id'
db.config = config
db.createId = function(collection) {
if (collection.length) {
return (collection[collection.length - 1].id || 0) + 1
}
return 1
}
db.get = function(collection, id, returnIndex = false) {
let col = db.getCollection(collection)
for (let i = col.length - 1; i >= 0; i--) {
if (col[i][db.id] === id) {
if (returnIndex) return i
return col[i]
}
}
return null
}
db.getCollection = function(collection) {
if (typeof(collection) === 'string') {
return db.data[collection]
}
return collection
}
db.upsert = function(collection, item) {
let col = db.getCollection(collection)
if (item[db.id]) {
let i = db.get(col, item[db.id], true)
if (i !== null) {
col[i] = item
return
}
} else {
item[db.id] = db.createId(col)
}
col.push(item)
}
db.remove = function(collection, itemOrId) {
let col = db.getCollection(collection)
let id = itemOrId
if (typeof(id) === 'object') {
id = id[db.id]
}
for (let i = col.length - 1; i >= 0; i--) {
if (col[i][db.id] === id) {
col.splice(i, 1)
return true
}
}
return false
}
db.addApplication = function(name) {
db.data.core[name] ||= {}
defaults(db.data.core[name], {
active: null,
latestInstalled: null,
latestVersion: null,
updater: '',
versions: [],
})
}
db.log = log
return db.read()
.then(function() {
if (!isObject(db.data)) {
db.log.warn(`File ${fullpath} was empty or not a json object, clearing it.`)
db.data = {}
}
defaults(db.data, { core: { version: 1 } })
return db.write()
})
.then(
function() { return db },
function(err) {
let wrapped = new Error(`Error writing to ${fullpath}: ${err.message} (${err.code})`)
wrapped.code = err.code
throw wrapped
}
)
}