diff --git a/app/controllers/api/v1/FormDatabase.controller.js b/app/controllers/api/v1/FormDatabase.controller.js index f244b6a..a9c57fd 100644 --- a/app/controllers/api/v1/FormDatabase.controller.js +++ b/app/controllers/api/v1/FormDatabase.controller.js @@ -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() diff --git a/app/models/api/db/ColumnDef.model.js b/app/models/api/db/ColumnDef.model.js index 89ae54f..c137628 100644 --- a/app/models/api/db/ColumnDef.model.js +++ b/app/models/api/db/ColumnDef.model.js @@ -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