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

69 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-02-09 05:09:18 +00:00
const Model = require('flitter-orm/src/model/Model')
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 Model {
static get schema() {
// Return a flitter-orm schema here.
return {
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.save()
}
to_api_object() {
return {
name: this.Name,
uuid: this.UUID,
page_id: this.PageId,
column_ids: this.ColumnIds,
}
2020-02-19 07:11:28 +00:00
}
2020-02-09 05:09:18 +00:00
}
module.exports = exports = Database