Removed knex, bookshelf and sqlite

dev
Jonatan Nilsson 2020-04-06 23:05:03 +00:00
parent 27871c9ed4
commit 68024783b9
16 changed files with 643 additions and 2339 deletions

View File

@ -1,88 +0,0 @@
import knex from 'knex'
import bookshelf from 'bookshelf'
import defaults from './defaults.mjs'
import config from './config.mjs'
import log from './log.mjs'
let host = config.get('knex:connection')
log.info(host, 'Connecting to DB')
const client = knex(config.get('knex'))
// Check if we're running tests while connected to
// potential production environment.
/* istanbul ignore if */
if (config.get('NODE_ENV') === 'test' &&
config.get('knex:connection:database') !== 'test' ||
config.get('knex:connection:connection')) {
// There is an offchance that we're running tests on
// production database. Exit NOW!
log.error('Critical: potentially running test on production enviroment. Shutting down.')
process.exit(1)
}
let shelf = bookshelf(client)
// Helper method to create models
shelf.createModel = (attr, opts) => {
// Create default attributes to all models
let attributes = defaults(attr, {
/**
* Initialize a new instance of model. This does not get called when
* relations to this model is being fetched though.
*/
initialize() {
this.on('fetching', this.checkFetching)
},
/**
* Event handler when fetch() is called. This gets called for both
* when getSingle() or just manual fetch() is called as well as
* when relation models through belongsTo() resources get fetched.
*
* @param {Model} model - The model instance if fetch() was used. For
* belongsTo this is the relation model thingy.
* @param {Array} columns - Array of columns to select if fetch() was used.
* Otherwise this is null.
* @param {Object} options - Options for the fetch. Includes the query
* builder object.
*/
checkFetching(model, columns, options) {
options.query.where({ is_deleted: false })
},
})
// Create default options for all models
let options = defaults(opts, {
/**
* Create new model object in database.
*
* @param {Object} data - The values the new model should have
* @return {Model} The resulted model
*/
create(data) {
return this.forge(data).save()
},
getSingle(id, withRelated = [], require = true) {
let where = { id: Number(id) || 0 }
return this.query({ where })
.fetch({ require, withRelated })
},
getAll(where = {}, withRelated = [], orderBy = 'id') {
where.is_deleted = false
return this.query({ where })
.orderBy(orderBy, 'ASC')
.fetchAll({ withRelated })
},
})
return shelf.Model.extend(attributes, options)
}
export default shelf

View File

@ -1,41 +0,0 @@
import bookshelf from '../bookshelf.mjs'
/* Content model:
{
id,
name,
graphic,
html,
css,
data,
}
*/
const Content = bookshelf.createModel({
tableName: 'content',
format(attributes) {
attributes.graphic = JSON.stringify(attributes.graphic)
attributes.data = JSON.stringify(attributes.data)
return attributes
},
parse(attributes) {
if (attributes.graphic) {
attributes.graphic = JSON.parse(attributes.graphic)
}
if (attributes.data) {
attributes.data = JSON.parse(attributes.data)
}
return attributes
},
}, {
getSingle(name, withRelated = [], require = false) {
let where = { name }
return this.query({ where })
.fetch({ require, withRelated })
},
})
export default Content

View File

@ -1,30 +0,0 @@
import bookshelf from '../bookshelf.mjs'
/* Graphic model:
{
id,
name,
engine,
settings,
is_deleted,
}
*/
const Graphic = bookshelf.createModel({
tableName: 'graphics',
format(attributes) {
attributes.settings = JSON.stringify(attributes.settings)
return attributes
},
parse(attributes) {
if (attributes.settings) {
attributes.settings = JSON.parse(attributes.settings)
}
return attributes
},
}, {
})
export default Graphic

View File

@ -1,30 +0,0 @@
import bookshelf from '../bookshelf.mjs'
/* Preset model:
{
id,
graphic_id,
values,
sort,
is_deleted,
}
*/
const Preset = bookshelf.createModel({
tableName: 'presets',
format(attributes) {
attributes.values = JSON.stringify(attributes.values)
return attributes
},
parse(attributes) {
if (attributes.values) {
attributes.values = JSON.parse(attributes.values)
}
return attributes
},
}, {
})
export default Preset

View File

@ -1,35 +0,0 @@
import bookshelf from '../bookshelf.mjs'
import Graphic from '../graphic/model.mjs'
/* Schedule model:
{
id,
graphic_id,
values,
sort,
is_deleted,
}
*/
const Schedule = bookshelf.createModel({
tableName: 'schedule',
graphic() {
return this.belongsTo(Graphic, 'graphic_id')
},
format(attributes) {
attributes.values = JSON.stringify(attributes.values)
return attributes
},
parse(attributes) {
if (attributes.values) {
attributes.values = JSON.parse(attributes.values)
}
return attributes
},
}, {
})
export default Schedule

View File

@ -1,11 +1,8 @@
import Schedule from './model.mjs'
export async function all(ctx) {
let graphics = ctx.db.get('graphics')
let data = ctx.db.get('schedule').forEach(function(s) {
s.graphic = graphics.getById(s.graphic_id).value()
}).sortBy('sort').value()
// let data = await Schedule.getAll({ }, ['graphic'], 'sort')
ctx.io.emit('schedule.all', data)
total(ctx)
@ -34,21 +31,26 @@ export async function add(ctx, payload) {
}
export async function patch(ctx, payload) {
await Promise.all(payload.map(async item => {
let scheduleItem = await Schedule.getSingle(item.id)
let schedule = ctx.db.get('schedule')
scheduleItem.set({ sort: item.sort })
schedule.forEach(function(item) {
schedule.updateById(Number(item.id), { sort: item.sort })
})
await scheduleItem.save()
}))
await schedule.write()
await all(ctx)
}
export async function remove(ctx, payload) {
let scheduleItem = await Schedule.getSingle(payload.id)
let schedule = ctx.db.get('schedule')
let schedItem = schedule.removeById(Number(payload.id)).value()
await schedule.write()
await scheduleItem.destroy()
schedItem.deleted_at = new Date().getTime()
schedItem.type = 'schedule'
await ctx.db.get('trash').insert(schedItem).write()
await all(ctx)
}

View File

@ -1,27 +0,0 @@
import knex from 'knex'
import defaults from './defaults.mjs'
import config from './config.mjs'
import log from './log.mjs'
// This is important for setup to run cleanly.
let knexConfig = defaults(config.get('knex'), null) // Clone
knexConfig.pool = { min: 1, max: 1 }
let knexSetup = knex(knexConfig)
export default function setup() {
log.info(knexConfig, 'Running database integrity scan.')
return knexSetup.migrate.latest({
directory: './migrations',
})
.then((result) => {
if (result[1].length === 0) {
return log.info('Database is up to date')
}
for (let i = 0; i < result[1].length; i++) {
log.info('Applied migration from', result[1][i].substr(result[1][i].lastIndexOf('\\') + 1))
}
return knexSetup.destroy()
})
}

View File

@ -1,12 +1,9 @@
import log from './api/log.mjs'
import setup from './api/setup.mjs'
// Run the database script automatically.
log.info('Running database integrity scan.')
log.info('Starting server.')
setup().then(() => {
import('./api/server.mjs')
}).catch((error) => {
log.error(error, 'Error while preparing database')
import('./api/server.mjs').catch((error) => {
log.error(error, 'Error while starting server')
process.exit(1)
})

View File

@ -1,12 +0,0 @@
const _ = require('lodash')
const config = require('./config')
let out = {}
// This is important for setup to run cleanly.
let knexConfig = _.cloneDeep(config.get('knex'))
knexConfig.pool = { min: 1, max: 1 }
out[config.get('NODE_ENV')] = knexConfig
module.exports = out

View File

@ -1,23 +0,0 @@
/* eslint-disable */
'use strict';
exports.up = function(knex, Promise) {
return Promise.all([
knex.schema.createTable('store', function(table) {
table.increments()
table.text('name')
table.text('value')
}).then(() => {
return knex('store').insert({
name: 'content',
value: '{}'
})
}),
]);
};
exports.down = function(knex, Promise) {
return Promise.all([
knex.schema.dropTable('store'),
]);
};

View File

@ -1,29 +0,0 @@
/* eslint-disable */
'use strict';
exports.up = function(knex, Promise) {
return Promise.all([
knex.schema.dropTable('store'),
knex.schema.createTable('graphics', function(table) {
table.increments()
table.text('name')
table.text('engine')
table.text('settings')
table.boolean('is_deleted')
}),
knex.schema.createTable('presets', function(table) {
table.increments()
table.integer('graphic_id').references('graphics.id')
table.text('values')
table.integer('sort')
table.boolean('is_deleted')
}),
]);
};
exports.down = function(knex, Promise) {
return Promise.all([
knex.schema.dropTable('graphics'),
knex.schema.dropTable('presets'),
]);
};

View File

@ -1,24 +0,0 @@
/* eslint-disable */
'use strict';
exports.up = function(knex, Promise) {
return Promise.all([
knex.schema.createTable('settings', function(table) {
table.increments()
table.text('name')
table.text('value')
table.boolean('is_deleted')
}).then(() => {
return knex('settings').insert({
name: 'casparcg',
value: ''
})
}),
]);
};
exports.down = function(knex, Promise) {
return Promise.all([
knex.schema.dropTable('settings'),
]);
};

View File

@ -1,22 +0,0 @@
/* eslint-disable */
'use strict';
exports.up = function(knex, Promise) {
return Promise.all([
knex.schema.createTable('content', function(table) {
table.increments()
table.text('graphic')
table.text('name')
table.text('html')
table.text('css')
table.text('data')
table.boolean('is_deleted')
}),
]);
};
exports.down = function(knex, Promise) {
return Promise.all([
knex.schema.dropTable('content'),
]);
};

View File

@ -1,20 +0,0 @@
/* eslint-disable */
'use strict';
exports.up = function(knex, Promise) {
return Promise.all([
knex.schema.createTable('schedule', function(table) {
table.increments()
table.integer('graphic_id').references('graphics.id')
table.text('values')
table.integer('sort')
table.boolean('is_deleted')
}),
]);
};
exports.down = function(knex, Promise) {
return Promise.all([
knex.schema.dropTable('schedule'),
]);
};

2566
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -34,11 +34,8 @@
},
"homepage": "https://github.com/nfp-projects/caspar-sup#readme",
"dependencies": {
"bookshelf": "^0.11.1",
"bunyan": "^1.8.12",
"casparcg-connection": "4.9.0",
"ip": "^1.1.5",
"knex": "^0.12.9",
"koa": "^2.4.1",
"koa-better-serve": "^2.0.7",
"koa-socket": "^4.4.0",
@ -46,7 +43,6 @@
"lowdb": "^1.0.0",
"nconf": "^0.9.1",
"socket.io": "^2.3.0",
"sqlite3": "^4.1.1",
"tslib": "^1.11.1"
},
"devDependencies": {