From b53a7c350985cff0b49b97db81f6f1d8a12412fb Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sun, 8 Dec 2019 16:31:47 +0100 Subject: [PATCH] Prepare for mongodb storage backend --- src/utils/dataHolder.js | 21 +++++++++++++++++++++ src/utils/scanner.js | 3 ++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/utils/dataHolder.js b/src/utils/dataHolder.js index 7ef0fbd..40c4513 100644 --- a/src/utils/dataHolder.js +++ b/src/utils/dataHolder.js @@ -1,6 +1,7 @@ import fs from 'fs' import jsonfile from 'jsonfile' import path from 'path' +import mongoose from 'mongoose' export class DataHolder { constructor (config) { @@ -11,6 +12,25 @@ export class DataHolder { posts: [], other: {} } + } else if (this.config.storage === 'mongo') { + mongoose.connect(config.mongourl, { useNewUrlParser: true }) + this.db = mongoose.connection + this.db.on('error', (error) => console.error(`[DataHolder] ${error}`)) + this.db.once('open', () => console.log('[DataHolder] Connected to database')) + + 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 + }) } } @@ -52,6 +72,7 @@ export class DataHolder { } addPost (post) { + delete post.body this.data.posts.push(post) } diff --git a/src/utils/scanner.js b/src/utils/scanner.js index d85f508..c91d3ce 100644 --- a/src/utils/scanner.js +++ b/src/utils/scanner.js @@ -99,7 +99,8 @@ export class Scanner { filename: metadata.filename, title: frontMatter.attributes.title, summary: summary, - link: '/post/' + metadata.filename + link: '/post/' + metadata.filename, + body: frontMatter.body } this.dataHolder.addPost(post)