core Document format

This commit is contained in:
tmshv committed 2019-08-02 16:08:13 +03:00
1 parent d04c61aef5
commit 862118fe1c
1 file changed
+36 -36
+36 -36
View File
@@ -1,56 +1,56 @@
const Data = require('./Data') const Data = require('./Data')
const Image = require('./Image') const Image = require('./Image')
const {find, findOne} = require('../lib/store') const { find, findOne } = require('../lib/store')
const store = () => Data.getStore(Document) const store = () => Data.getStore(Document)
class Document { class Document {
static async find(query, options = {}) { static async find(query, options = {}) {
const items = await find(store(), query, options) const items = await find(store(), query, options)
return Promise.all(items.map(processDocument)) return Promise.all(items.map(processDocument))
} }
static async findById(id) { static async findById(id) {
const data = await findOne(store(), {id}) const data = await findOne(store(), { id })
if (!data) return null if (!data) return null
return processDocument(data) return processDocument(data)
} }
constructor(data) { constructor(data) {
this.id = data.id this.id = data.id
this.hash = data.hash this.hash = data.hash
this.file = data.file this.file = data.file
// In the future: use fileInfo only // In the future: use fileInfo only
this.fileInfo = data.fileInfo this.fileInfo = data.fileInfo
? new DocumentFile(data.fileInfo) ? new DocumentFile(data.fileInfo)
: new DocumentFile(data.file) : new DocumentFile(data.file)
this.type = data.type this.type = data.type
this.title = data.title this.title = data.title
this.category = data.category this.category = data.category
this.url = data.url this.url = data.url
this.preview = data.preview this.preview = data.preview
this.viewUrl = `/document/${this.id}` this.viewUrl = `/document/${this.id}`
} }
} }
class DocumentFile { class DocumentFile {
constructor(data) { constructor(data) {
this.name = data.name this.name = data.name
this.size = data.size this.size = data.size
} }
} }
const processDocument = async data => { const processDocument = async data => {
const imageId = data.preview const imageId = data.preview
const preview = await Image.findById(imageId) const preview = await Image.findById(imageId)
return new Document({ return new Document({
...data, ...data,
preview, preview,
}) })
} }
module.exports = Document module.exports = Document