2020-02-09 05:09:18 +00:00
|
|
|
const Controller = require('libflitter/controller/Controller')
|
|
|
|
const Database = require('../../../models/api/db/Database.model')
|
|
|
|
const ColumnDef = require('../../../models/api/db/ColumnDef.model')
|
|
|
|
const DBEntry = require('../../../models/api/db/DBEntry.model')
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Database Controller
|
|
|
|
* -------------------------------------------------------------
|
|
|
|
* Put some description here!
|
|
|
|
*/
|
|
|
|
class FormDatabase extends Controller {
|
|
|
|
|
|
|
|
async create_new(req, res) {
|
2020-11-11 19:54:20 +00:00
|
|
|
const { page, node } = req.form
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const db = new Database({
|
2020-10-31 02:59:42 +00:00
|
|
|
Name: req.body.name || req.body.Name || 'New Database',
|
2020-02-09 05:09:18 +00:00
|
|
|
NodeId: node.UUID,
|
|
|
|
PageId: page.UUID,
|
|
|
|
})
|
|
|
|
|
2020-10-31 02:59:42 +00:00
|
|
|
if ( req.body.UUID ) {
|
|
|
|
const existingUUID = await Database.findOne({ UUID: req.body.UUID })
|
|
|
|
if ( !existingUUID ) {
|
|
|
|
db.UUID = req.body.UUID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 21:53:39 +00:00
|
|
|
await db.version_save(`Added to page "${page.Name}"`, req.user.id)
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
req.user.allow(`database:${db.UUID}`);
|
|
|
|
await req.user.save()
|
|
|
|
|
|
|
|
return res.api(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
async get_config(req, res) {
|
2020-11-11 19:54:20 +00:00
|
|
|
const { page, node, database } = req.form
|
2020-02-09 05:09:18 +00:00
|
|
|
|
2021-03-16 15:35:57 +00:00
|
|
|
if ( req.query.version ) {
|
|
|
|
console.log('db version', req.query.version)
|
|
|
|
return res.api(await database.as_version(req.query.version))
|
|
|
|
}
|
|
|
|
|
2020-11-11 19:54:20 +00:00
|
|
|
return res.api(database)
|
2020-02-09 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async get_columns(req, res) {
|
2021-03-16 15:35:57 +00:00
|
|
|
let { page, node, database } = req.form
|
|
|
|
if ( req.query.database_version ) {
|
|
|
|
database = await database.as_version(req.query.database_version)
|
|
|
|
if ( !database )
|
|
|
|
return res.status(400)
|
|
|
|
.message('Version not found.')
|
|
|
|
.api()
|
|
|
|
}
|
2020-02-09 05:09:18 +00:00
|
|
|
|
2020-02-19 07:11:28 +00:00
|
|
|
const columns = []
|
2020-11-11 19:54:20 +00:00
|
|
|
for ( const col_id of database.ColumnIds ) {
|
2020-02-19 07:11:28 +00:00
|
|
|
const rec = await ColumnDef.findOne({UUID: col_id})
|
|
|
|
if ( rec ) {
|
|
|
|
rec.additionalData = rec.data()
|
|
|
|
columns.push(rec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback for backwards compat
|
|
|
|
if ( columns.length < 1 ) {
|
2020-11-11 19:54:20 +00:00
|
|
|
return res.api((await ColumnDef.find({DatabaseId: database.UUID})).map(x => {
|
2020-02-19 07:11:28 +00:00
|
|
|
x.additionalData = x.data()
|
|
|
|
return x
|
2020-02-19 07:18:45 +00:00
|
|
|
}))
|
2020-02-19 07:11:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 05:09:18 +00:00
|
|
|
return res.api(columns)
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:09:23 +00:00
|
|
|
async set_name(req, res) {
|
|
|
|
if ( !req.body.Name ) {
|
|
|
|
return res.status(400)
|
|
|
|
.message('Missing required field: Name')
|
|
|
|
.api()
|
|
|
|
}
|
|
|
|
|
2020-11-11 19:54:20 +00:00
|
|
|
const { page, node, database } = req.form
|
2020-10-13 15:09:23 +00:00
|
|
|
|
2020-11-11 19:54:20 +00:00
|
|
|
if ( req.body.Name !== database.Name ) {
|
|
|
|
database.Name = req.body.Name
|
|
|
|
await database.version_save(`Changed database name to "${req.body.Name}"`, req.user.id)
|
2020-11-02 21:53:39 +00:00
|
|
|
} else {
|
2020-11-11 19:54:20 +00:00
|
|
|
await database.save()
|
2020-11-02 21:53:39 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 19:54:20 +00:00
|
|
|
return res.api(database)
|
2020-10-13 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 05:09:18 +00:00
|
|
|
async set_columns(req, res) {
|
2020-11-11 19:54:20 +00:00
|
|
|
const { page, node, database } = req.form
|
2020-02-09 05:09:18 +00:00
|
|
|
|
2020-11-11 19:54:20 +00:00
|
|
|
const existing_columns = await ColumnDef.find({ DatabaseId: database.UUID })
|
2020-02-09 05:09:18 +00:00
|
|
|
const assoc_columns = []
|
|
|
|
existing_columns.forEach(col => assoc_columns[col.UUID] = col)
|
|
|
|
|
|
|
|
const update_columns = []
|
|
|
|
for ( const col of req.body.columns ) {
|
|
|
|
if ( col.UUID && assoc_columns[col.UUID] ) {
|
|
|
|
assoc_columns[col.UUID].headerName = col.headerName
|
|
|
|
assoc_columns[col.UUID].field = col.field
|
2020-11-11 19:54:20 +00:00
|
|
|
assoc_columns[col.UUID].DatabaseId = database.UUID
|
2020-02-09 05:09:18 +00:00
|
|
|
assoc_columns[col.UUID].Type = col.Type
|
2020-02-18 17:17:49 +00:00
|
|
|
assoc_columns[col.UUID].additionalData = JSON.stringify(col.additionalData)
|
2020-11-02 22:04:45 +00:00
|
|
|
await assoc_columns[col.UUID].version_save(`Updated in page "${page.Name}"`, req.user.id)
|
2020-02-09 05:09:18 +00:00
|
|
|
update_columns.push(assoc_columns[col.UUID])
|
|
|
|
} else {
|
|
|
|
const new_col = new ColumnDef({
|
|
|
|
headerName: col.headerName,
|
|
|
|
field: col.field,
|
2020-11-11 19:54:20 +00:00
|
|
|
DatabaseId: database.UUID,
|
2020-02-09 05:09:18 +00:00
|
|
|
Type: col.Type,
|
2020-02-18 17:17:49 +00:00
|
|
|
additionalData: JSON.stringify(col.additionalData),
|
2020-02-09 05:09:18 +00:00
|
|
|
})
|
|
|
|
|
2020-10-31 02:59:42 +00:00
|
|
|
if ( col.UUID ) {
|
|
|
|
const existingUUID = await ColumnDef.findOne({ UUID: col.UUID })
|
|
|
|
if ( !existingUUID ) {
|
|
|
|
new_col.UUID = col.UUID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 22:04:45 +00:00
|
|
|
await new_col.version_save(`Added to database`, req.user.id)
|
2020-02-09 05:09:18 +00:00
|
|
|
update_columns.push(new_col)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const col of existing_columns ) {
|
|
|
|
if ( !update_columns.includes(col) ) {
|
|
|
|
await col.delete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 21:53:39 +00:00
|
|
|
const new_cols = update_columns.map(x => x.UUID)
|
2020-11-11 19:54:20 +00:00
|
|
|
const no_updates = (new_cols.length === database.ColumnIds.length) && (new_cols.every(val => database.ColumnIds.includes(val)));
|
2020-11-02 21:53:39 +00:00
|
|
|
if ( !no_updates ) {
|
2020-11-11 19:54:20 +00:00
|
|
|
database.ColumnIds = new_cols
|
|
|
|
await database.version_save('Updated columns', req.user.id)
|
2020-11-02 21:53:39 +00:00
|
|
|
} else {
|
2020-11-11 19:54:20 +00:00
|
|
|
await database.save()
|
2020-11-02 21:53:39 +00:00
|
|
|
}
|
2020-02-19 07:11:28 +00:00
|
|
|
|
2020-02-09 05:09:18 +00:00
|
|
|
return res.api(update_columns)
|
|
|
|
}
|
|
|
|
|
|
|
|
async get_data(req, res) {
|
2020-11-11 19:54:20 +00:00
|
|
|
const { page, node, database } = req.form
|
2020-02-09 05:09:18 +00:00
|
|
|
|
2020-11-11 19:54:20 +00:00
|
|
|
const entries = await DBEntry.find({DatabaseId: database.UUID})
|
2020-02-09 05:09:18 +00:00
|
|
|
entries.forEach(entry => entry.RowData.UUID = entry.UUID)
|
|
|
|
|
|
|
|
return res.api(entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
async set_data(req, res) {
|
2020-11-11 19:54:20 +00:00
|
|
|
const { page, node, database } = req.form
|
2020-02-09 05:09:18 +00:00
|
|
|
|
2020-11-11 19:54:20 +00:00
|
|
|
await DBEntry.deleteMany({DatabaseId: database.UUID})
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const new_recs = []
|
|
|
|
for ( const rec of req.body ) {
|
2020-11-11 19:54:20 +00:00
|
|
|
const data = {DatabaseId: database.UUID}
|
2020-02-09 05:09:18 +00:00
|
|
|
if ( rec.UUID ) data.UUID = rec.UUID
|
|
|
|
delete rec.UUID
|
|
|
|
data.RowData = rec
|
|
|
|
|
|
|
|
const dbe = new DBEntry(data)
|
|
|
|
await dbe.save()
|
|
|
|
dbe.RowData.UUID = dbe.UUID
|
|
|
|
new_recs.push(dbe)
|
|
|
|
}
|
2020-02-19 07:11:28 +00:00
|
|
|
|
2020-11-11 19:54:20 +00:00
|
|
|
await database.version_save('Updated data', req.user.id)
|
|
|
|
return res.api(await this._set_indices(database, new_recs))
|
2020-02-09 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async drop_database(req, res) {
|
2020-11-11 19:54:20 +00:00
|
|
|
const { page, node, database } = req.form
|
2020-02-09 05:09:18 +00:00
|
|
|
|
2020-11-11 19:54:20 +00:00
|
|
|
await DBEntry.deleteMany({DatabaseId: database.UUID})
|
|
|
|
await database.version_save('Deleted', req.user.id)
|
|
|
|
await database.delete()
|
2020-02-09 07:58:29 +00:00
|
|
|
return res.api({})
|
2020-02-09 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 07:11:28 +00:00
|
|
|
async _set_indices(db, data) {
|
|
|
|
const index_columns = await ColumnDef.find({DatabaseId: db.UUID, Type: 'index'})
|
|
|
|
|
|
|
|
for ( const col of index_columns ) {
|
|
|
|
let max_val = 0
|
|
|
|
data.forEach(row => {
|
|
|
|
const val = row.RowData[col.field]
|
|
|
|
if ( val && val > max_val ) max_val = val
|
|
|
|
});
|
|
|
|
|
|
|
|
let next_val = max_val + 1
|
|
|
|
for ( const row of data ) {
|
|
|
|
if ( !row.RowData[col.field] ) {
|
|
|
|
row.RowData[col.field] = next_val
|
|
|
|
next_val += 1
|
|
|
|
await row.save()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
2020-02-09 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = FormDatabase
|