Restructure data holder

This commit is contained in:
LordMathis 2019-12-26 21:22:18 +01:00
parent 41870d90d9
commit 7e152520c2
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
5 changed files with 124 additions and 79 deletions

View File

@ -8,6 +8,8 @@ import jsonfile from 'jsonfile'
import { ServerRenderer } from './utils/serverRender'
import { Scanner } from './utils/scanner'
import { DataHolder } from './utils/dataHolder'
import { FileStorage } from './utils/storage/file'
import { MongoStorage } from './utils/storage/mongo'
const port = process.env.PORT || 3000
const app = express()
@ -45,7 +47,14 @@ if (head == null) {
}
}
const dataHolder = new DataHolder(config)
let storage
if (config.storage === 'file') {
storage = new FileStorage()
} else if (config.storage === 'mongo') {
storage = new MongoStorage()
}
const dataHolder = new DataHolder(storage)
const scanner = new Scanner(config, dataHolder)
const serverRenderer = new ServerRenderer(head, config, dataHolder)

View File

@ -1,85 +1,22 @@
import fs from 'fs'
import jsonfile from 'jsonfile'
import path from 'path'
import mongoose from 'mongoose'
export class DataHolder {
constructor (config) {
this.config = config
if (this.config.storage === 'file') {
this.data = {
posts: [],
other: {}
}
} else if (this.config.storage === 'mongo') {
this.Post = mongoose.model('Post', {
filename: String,
published: String,
title: String,
summary: String,
link: String,
body: String
})
this.Other = mongoose.model('Other', {
filename: String,
body: String
})
}
}
getData (reqPath) {
if (this.config.storage === 'file') {
return this.getDataFromFile(reqPath)
}
}
getDataFromFile (reqPath) {
if (reqPath === '') {
return Promise.resolve(this.data)
} else if (reqPath === 'resume') {
const fileName = path.join(process.cwd(), 'content', reqPath + '.md')
return this.readFile(fileName, 'resume', 'utf8')
} else {
const fileName = path.join(process.cwd(), 'content', reqPath + '.md')
return this.readFile(fileName, 'post', 'utf8')
}
}
readFile (fileName, type, options) {
return new Promise(function (resolve, reject) {
fs.readFile(fileName, options, (err, data) => {
err ? reject(err) : resolve({
type: type,
data: data
})
})
})
}
readJson (dataPath) {
return new Promise(function (resolve, reject) {
jsonfile.readFile(dataPath, (err, data) => {
err ? reject(err) : resolve(data)
})
})
constructor (storage) {
this.storage = storage
}
addPost (post) {
if (this.config.storage === 'file') {
delete post.body
this.data.posts.push(post)
}
this.storage.addPost(post)
}
addOther (filename, data) {
this.data.other[filename] = data
this.storage.addOther(filename, data)
}
deleteFile (filepath) {
this.data.posts = this.data.posts.filter((post) =>
post.filename !== filepath
)
this.storage.deleteFile(filepath)
}
getData (reqPath) {
return this.storage.getData(reqPath)
}
}

View File

@ -24,7 +24,7 @@ export class Scanner {
})
.on('change', filepath => {
console.log(`[Scanner] File ${filepath} has been changed`)
this.updateFile(filepath)
this.addFile(filepath)
})
.on('unlink', filepath => {
console.log(`[Scanner] File ${filepath} has been removed`)
@ -42,11 +42,6 @@ export class Scanner {
}
}
updateFile (filepath) {
this.deleteFile(filepath)
this.addFile(filepath)
}
deleteFile (filepath) {
this.dataHolder.deleteFile(filepath)
}

69
src/utils/storage/file.js Normal file
View File

@ -0,0 +1,69 @@
import fs from 'fs'
import path from 'path'
export class FileStorage {
constructor () {
this.data = {
posts: [],
other: {}
}
}
addPost (post) {
delete post.body
const postIndex = this._findWithAttr(this.data.posts, 'filename', post.filename)
if (postIndex === -1) {
this.data.posts.push(post)
} else {
this.data.posts[postIndex] = post
}
}
addOther (filename, data) {
this.data.other[filename] = data
}
deleteFile (filepath) {
this.data.posts = this.data.posts.filter((post) =>
post.filename !== filepath
)
}
getData (reqPath) {
return this._getDataFromFile(reqPath)
}
_getDataFromFile (reqPath) {
if (reqPath === '') {
return Promise.resolve(this.data)
} else if (reqPath === 'resume') {
const fileName = path.join(process.cwd(), 'content', reqPath + '.md')
return this._readFile(fileName, 'resume', 'utf8')
} else {
const fileName = path.join(process.cwd(), 'content', reqPath + '.md')
return this._readFile(fileName, 'post', 'utf8')
}
}
_readFile (fileName, type, options) {
return new Promise(function (resolve, reject) {
fs.readFile(fileName, options, (err, data) => {
err ? reject(err) : resolve({
type: type,
data: data
})
})
})
}
_findWithAttr (array, attr, value) {
for (let i = 0; i < array.length; i += 1) {
if (array[i][attr] === value) {
return i
}
}
return -1
}
}

View File

@ -0,0 +1,35 @@
import mongoose from 'mongoose'
export class MongoStorage {
constructor () {
this.Post = mongoose.model('Post', {
filename: String,
published: String,
title: String,
summary: String,
link: String,
body: String
})
this.Other = mongoose.model('Other', {
filename: String,
body: String
})
}
addPost (post) {
}
addOther (filename, data) {
}
deleteFile (filepath) {
}
getData (reqPath) {
}
}