2020-11-02 21:53:39 +00:00
|
|
|
const VersionedModel = require('../../VersionedModel')
|
2020-02-09 05:09:18 +00:00
|
|
|
const uuid = require('uuid/v4')
|
2020-02-19 07:11:28 +00:00
|
|
|
const ColumnDef = require('./ColumnDef.model')
|
2020-03-01 21:37:52 +00:00
|
|
|
const Page = require('../Page.model')
|
|
|
|
const Node = require('../Node.model')
|
|
|
|
const ActiveScope = require('../../scopes/Active.scope')
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Database Model
|
|
|
|
* -------------------------------------------------------------
|
|
|
|
* Put some description here!
|
|
|
|
*/
|
2020-11-02 21:53:39 +00:00
|
|
|
class Database extends VersionedModel {
|
2020-02-09 05:09:18 +00:00
|
|
|
static get schema() {
|
|
|
|
// Return a flitter-orm schema here.
|
|
|
|
return {
|
2020-11-02 21:53:39 +00:00
|
|
|
...super.schema,
|
2020-02-09 05:09:18 +00:00
|
|
|
Name: String,
|
|
|
|
NodeId: String,
|
|
|
|
PageId: String,
|
|
|
|
ColumnIds: [String],
|
|
|
|
UUID: { type: String, default: () => uuid() },
|
2020-03-01 21:37:52 +00:00
|
|
|
Active: { type: Boolean, default: true },
|
2020-02-09 05:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 21:37:52 +00:00
|
|
|
static scopes = [new ActiveScope]
|
|
|
|
|
|
|
|
static async visible_by_user(user) {
|
|
|
|
const page_ids = (await Page.visible_by_user(user)).map(x => x.UUID)
|
|
|
|
return this.find({PageId: {$in: page_ids}})
|
|
|
|
}
|
|
|
|
|
|
|
|
async is_accessible_by(user, mode = 'view') {
|
|
|
|
const page = await this.page
|
|
|
|
return page.is_accessible_by(user, mode)
|
2020-02-09 05:09:18 +00:00
|
|
|
}
|
2020-02-19 07:11:28 +00:00
|
|
|
|
|
|
|
async get_columns() {
|
2020-03-01 21:37:52 +00:00
|
|
|
const cols = await ColumnDef.find({DatabaseId: this.UUID})
|
|
|
|
const assoc_cols = {}
|
|
|
|
cols.forEach(col => assoc_cols[col.UUID] = col)
|
|
|
|
return this.ColumnIds.map(x => assoc_cols[x])
|
|
|
|
}
|
|
|
|
|
|
|
|
get page() {
|
|
|
|
return this.belongs_to_one(Page, 'PageId', 'UUID')
|
|
|
|
}
|
|
|
|
|
|
|
|
get node() {
|
|
|
|
return this.belongs_to_one(Node, 'NodeId', 'UUID')
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete() {
|
|
|
|
this.Active = false
|
2020-11-02 21:53:39 +00:00
|
|
|
await this.version_save(`Deleted`)
|
2020-03-01 21:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
to_api_object() {
|
|
|
|
return {
|
|
|
|
name: this.Name,
|
|
|
|
uuid: this.UUID,
|
|
|
|
page_id: this.PageId,
|
|
|
|
column_ids: this.ColumnIds,
|
2020-11-02 21:53:39 +00:00
|
|
|
version_num: this.version_num,
|
|
|
|
version_message: this.version_message,
|
2020-03-01 21:37:52 +00:00
|
|
|
}
|
2020-02-19 07:11:28 +00:00
|
|
|
}
|
2020-11-02 22:04:45 +00:00
|
|
|
|
|
|
|
async cast_to_version_data() {
|
|
|
|
const data = await super.cast_to_version_data()
|
|
|
|
const cols = await this.get_columns()
|
|
|
|
|
|
|
|
data.column_version_nums = cols.map(x => {
|
|
|
|
return {
|
|
|
|
ColumnId: x.UUID,
|
|
|
|
version_num: x.version_num,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
2020-02-09 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = Database
|