Add ability to store column order
This commit is contained in:
parent
f03b4efb7e
commit
5bdcf59dbd
@ -73,12 +73,24 @@ class FormDatabase extends Controller {
|
|||||||
const DatabaseId = req.params.DatabaseId
|
const DatabaseId = req.params.DatabaseId
|
||||||
const db = await Database.findOne({UUID: DatabaseId})
|
const db = await Database.findOne({UUID: DatabaseId})
|
||||||
if ( !db ) return res.status(404).message('Database not found with that ID.').api({})
|
if ( !db ) return res.status(404).message('Database not found with that ID.').api({})
|
||||||
// if ( !db.accessible_by(req.user) ) return req.security.deny()
|
|
||||||
|
|
||||||
const columns = (await ColumnDef.find({ DatabaseId: db.UUID })).map(x => {
|
const columns = []
|
||||||
x.additionalData = x.data()
|
for ( const col_id of db.ColumnIds ) {
|
||||||
return x
|
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 ) {
|
||||||
|
return (await ColumnDef.find({DatabaseId: db.UUID})).map(x => {
|
||||||
|
x.additionalData = x.data()
|
||||||
|
return x
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return res.api(columns)
|
return res.api(columns)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,6 +145,9 @@ class FormDatabase extends Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.ColumnIds = update_columns.map(x => x.UUID)
|
||||||
|
await db.save()
|
||||||
|
|
||||||
return res.api(update_columns)
|
return res.api(update_columns)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +205,8 @@ class FormDatabase extends Controller {
|
|||||||
dbe.RowData.UUID = dbe.UUID
|
dbe.RowData.UUID = dbe.UUID
|
||||||
new_recs.push(dbe)
|
new_recs.push(dbe)
|
||||||
}
|
}
|
||||||
return res.api(new_recs)
|
|
||||||
|
return res.api(await this._set_indices(db, new_recs))
|
||||||
}
|
}
|
||||||
|
|
||||||
async drop_database(req, res) {
|
async drop_database(req, res) {
|
||||||
@ -215,6 +231,28 @@ class FormDatabase extends Controller {
|
|||||||
return res.api({})
|
return res.api({})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = exports = FormDatabase
|
module.exports = exports = FormDatabase
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
const Model = require('flitter-orm/src/model/Model')
|
const Model = require('flitter-orm/src/model/Model')
|
||||||
const uuid = require('uuid/v4')
|
const uuid = require('uuid/v4')
|
||||||
|
const ColumnDef = require('./ColumnDef.model')
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Database Model
|
* Database Model
|
||||||
@ -21,6 +22,10 @@ class Database extends Model {
|
|||||||
accessible_by(user, mode = 'view') {
|
accessible_by(user, mode = 'view') {
|
||||||
return user.can(`database:${this.UUID}:${mode}`)
|
return user.can(`database:${this.UUID}:${mode}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async get_columns() {
|
||||||
|
return ColumnDef.find({DatabaseId: this.UUID});
|
||||||
|
}
|
||||||
|
|
||||||
// Static and instance methods can go here
|
// Static and instance methods can go here
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user