backend/app/models/api/db/Database.model.js
garrettmills 14babd5b8b
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing
Track database column versions and include in database version data
2020-11-02 16:04:45 -06:00

86 lines
2.3 KiB
JavaScript

const VersionedModel = require('../../VersionedModel')
const uuid = require('uuid/v4')
const ColumnDef = require('./ColumnDef.model')
const Page = require('../Page.model')
const Node = require('../Node.model')
const ActiveScope = require('../../scopes/Active.scope')
/*
* Database Model
* -------------------------------------------------------------
* Put some description here!
*/
class Database extends VersionedModel {
static get schema() {
// Return a flitter-orm schema here.
return {
...super.schema,
Name: String,
NodeId: String,
PageId: String,
ColumnIds: [String],
UUID: { type: String, default: () => uuid() },
Active: { type: Boolean, default: true },
}
}
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)
}
async get_columns() {
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`)
}
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,
}
}
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
}
}
module.exports = exports = Database