mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-29 15:44:13 +00:00
b003f48d7b
Also includes various improvements and bugfixes to services and the migration from legacy /data to volumes. The switch ti migrations involves a dirty hack for webpack to properly resolve the paths to the migrations js files - it uses an expression that webpack can't resolve, so we hardcode it to a value and use the ContextReplacementPlugin to make that value resolve to the migrations folder. The downsides to this approach are: - a change in knex code would break this - the migration code is added twice to the supervisor image: once in the migrations folder (because knex needs to loop through the directory to find the files), and once inside app.js (because I can't make webpack treat them as external) Signed-off-by: Pablo Carranza Velez <pablo@resin.io>
31 lines
696 B
CoffeeScript
31 lines
696 B
CoffeeScript
Knex = require 'knex'
|
|
path = require 'path'
|
|
constants = require './lib/constants'
|
|
|
|
module.exports = class DB
|
|
constructor: ({ @databasePath } = {}) ->
|
|
@databasePath ?= constants.databasePath
|
|
@knex = Knex(
|
|
client: 'sqlite3'
|
|
connection:
|
|
filename: @databasePath
|
|
useNullAsDefault: true
|
|
)
|
|
|
|
init: =>
|
|
@knex.migrate.latest(directory: path.join(__dirname, 'migrations'))
|
|
|
|
# Returns a knex object for one of the models (tables)
|
|
models: (modelName) =>
|
|
@knex(modelName)
|
|
|
|
upsertModel: (modelName, obj, id, trx) =>
|
|
knex = trx ? @knex
|
|
knex(modelName).update(obj).where(id)
|
|
.then (n) ->
|
|
if n == 0
|
|
knex(modelName).insert(obj)
|
|
|
|
transaction: (cb) =>
|
|
@knex.transaction(cb)
|