DB API Reads
This commit is contained in:
@@ -33,6 +33,16 @@ class ColumnDef extends Model {
|
||||
data() {
|
||||
return JSON.parse(this.additionalData ? this.additionalData : '{}')
|
||||
}
|
||||
|
||||
to_api_object() {
|
||||
return {
|
||||
name: this.headerName,
|
||||
uuid: this.UUID,
|
||||
database_id: this.DatabaseId,
|
||||
type: this.Type,
|
||||
metadata: this.data()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = ColumnDef
|
||||
|
||||
@@ -17,6 +17,22 @@ class DBEntry extends Model {
|
||||
}
|
||||
|
||||
// Static and instance methods can go here
|
||||
to_api_object() {
|
||||
return {
|
||||
uuid: this.UUID,
|
||||
database_id: this.DatabaseId,
|
||||
data: this.RowData,
|
||||
}
|
||||
}
|
||||
|
||||
static async from_cursor(cursor) {
|
||||
const arr = await cursor.toArray()
|
||||
const collection = []
|
||||
for ( const rec of arr ) {
|
||||
collection.push(new this(rec))
|
||||
}
|
||||
return collection
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = DBEntry
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
const Model = require('flitter-orm/src/model/Model')
|
||||
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
|
||||
@@ -16,18 +19,50 @@ class Database extends Model {
|
||||
PageId: String,
|
||||
ColumnIds: [String],
|
||||
UUID: { type: String, default: () => uuid() },
|
||||
Active: { type: Boolean, default: true },
|
||||
}
|
||||
}
|
||||
|
||||
accessible_by(user, mode = 'view') {
|
||||
return user.can(`database:${this.UUID}:${mode}`)
|
||||
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() {
|
||||
return ColumnDef.find({DatabaseId: this.UUID});
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
// Static and instance methods can go here
|
||||
}
|
||||
|
||||
module.exports = exports = Database
|
||||
|
||||
Reference in New Issue
Block a user