Add additionalData field to ColumnDef

This commit is contained in:
garrettmills 2020-02-18 11:17:49 -06:00
parent 46f60a671a
commit f03b4efb7e
2 changed files with 20 additions and 1 deletions

View File

@ -75,7 +75,10 @@ class FormDatabase extends Controller {
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 })
const columns = (await ColumnDef.find({ DatabaseId: db.UUID })).map(x => {
x.additionalData = x.data()
return x
})
return res.api(columns)
}
@ -107,6 +110,7 @@ class FormDatabase extends Controller {
assoc_columns[col.UUID].field = col.field
assoc_columns[col.UUID].DatabaseId = db.UUID
assoc_columns[col.UUID].Type = col.Type
assoc_columns[col.UUID].additionalData = JSON.stringify(col.additionalData)
await assoc_columns[col.UUID].save()
update_columns.push(assoc_columns[col.UUID])
} else {
@ -115,6 +119,7 @@ class FormDatabase extends Controller {
field: col.field,
DatabaseId: db.UUID,
Type: col.Type,
additionalData: JSON.stringify(col.additionalData),
})
await new_col.save()

View File

@ -15,10 +15,24 @@ class ColumnDef extends Model {
DatabaseId: String,
UUID: { type: String, default: () => uuid() },
Type: { type: String, default: 'text' }, // text, number for now
additionalData: { type: String, default: '{}' }
}
}
// Static and instance methods can go here
data_get(key) {
return JSON.parse(this.additionalData ? this.additionalData : '{}')[key]
}
data_set(key, value) {
const json = JSON.parse(this.additionalData ? this.additionalData : '{}')
json[key] = value
this.additionalData = JSON.stringify(json)
}
data() {
return JSON.parse(this.additionalData ? this.additionalData : '{}')
}
}
module.exports = exports = ColumnDef