backend/app/models/api/files/FileBox.model.js
garrettmills f58fcd85b1
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing
Noded/frontend#75 - support searching file boxes and files from universal search
2021-02-04 20:41:03 -06:00

76 lines
1.7 KiB
JavaScript

const { Model } = require('flitter-orm')
const uuid = require('uuid').v4
const { ObjectId } = require('mongodb')
class FileBoxModel extends Model {
static get services() {
return [...super.services, 'models']
}
static get schema() {
return {
UUID: { type: String, default: uuid },
name: String,
pageId: String,
nodeId: String,
parentUUID: String,
rootUUID: String,
fileIds: [String],
active: { type: Boolean, default: true },
}
}
async page() {
const Page = this.models.get('api:Page')
return Page.findOne({
UUID: this.pageId,
})
}
async parent() {
return this.constructor.findOne({
UUID: this.parentUUID,
active: true,
})
}
async root() {
return this.constructor.findOne({
UUID: this.rootUUID,
active: true,
})
}
async children() {
return this.constructor.find({
parentUUID: this.UUID,
active: true,
})
}
async files() {
const File = this.models.get('upload::File')
return await File.find({
_id: {
$in: this.fileIds.map(x => ObjectId(x)),
}
})
}
async to_api() {
return {
type: 'folder',
UUID: this.UUID,
name: this.name,
title: this.name,
pageId: this.pageId,
nodeId: this.nodeId,
parentUUID: this.parentUUID,
rootUUID: this.rootUUID,
fileIds: this.fileIds || [],
}
}
}
module.exports = exports = FileBoxModel