2021-02-04 15:19:04 +00:00
|
|
|
const { Model } = require('flitter-orm')
|
|
|
|
const uuid = require('uuid').v4
|
|
|
|
const { ObjectId } = require('mongodb')
|
|
|
|
|
|
|
|
class FileBoxModel extends Model {
|
2021-02-04 18:57:11 +00:00
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models']
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:19:04 +00:00
|
|
|
static get schema() {
|
|
|
|
return {
|
|
|
|
UUID: { type: String, default: uuid },
|
|
|
|
name: String,
|
|
|
|
pageId: String,
|
|
|
|
parentUUID: String,
|
|
|
|
rootUUID: String,
|
|
|
|
fileIds: [String],
|
|
|
|
active: { type: Boolean, default: true },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async parent() {
|
|
|
|
return this.constructor.findOne({
|
|
|
|
UUID: this.parentUUID,
|
|
|
|
active: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async root() {
|
|
|
|
return this.constructor.findOne({
|
|
|
|
UUID: this.rootUUID,
|
|
|
|
active: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-04 18:57:11 +00:00
|
|
|
async children() {
|
|
|
|
return this.constructor.find({
|
|
|
|
parentUUID: this.UUID,
|
|
|
|
active: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:19:04 +00:00
|
|
|
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 {
|
2021-02-04 18:57:11 +00:00
|
|
|
type: 'folder',
|
2021-02-04 15:19:04 +00:00
|
|
|
UUID: this.UUID,
|
|
|
|
name: this.name,
|
2021-02-04 18:57:11 +00:00
|
|
|
title: this.name,
|
2021-02-04 15:19:04 +00:00
|
|
|
pageId: this.pageId,
|
|
|
|
parentUUID: this.parentUUID,
|
|
|
|
rootUUID: this.rootUUID,
|
|
|
|
fileIds: this.fileIds || [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = FileBoxModel
|