backend/app/models/api/db/Database.model.js

86 lines
2.3 KiB
JavaScript
Raw Normal View History

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!
*/
class Database extends VersionedModel {
2020-02-09 05:09:18 +00:00
static get schema() {
// Return a flitter-orm schema here.
return {
...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
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,
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
}
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