diff options
author | terminaldweller <thabogre@gmail.com> | 2022-07-05 15:49:16 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2022-07-05 15:49:16 +0000 |
commit | 4eaa437c0a285c617e30a533d1ef369eaa8644a6 (patch) | |
tree | dfc5d498039284e4ea30880b08197e5f24b3a2c6 /model.js | |
parent | was using node image for self-signed certificates (diff) | |
download | blog-4eaa437c0a285c617e30a533d1ef369eaa8644a6.tar.gz blog-4eaa437c0a285c617e30a533d1ef369eaa8644a6.zip |
WIP, adding rss to the blog
Diffstat (limited to 'model.js')
-rw-r--r-- | model.js | 55 |
1 files changed, 42 insertions, 13 deletions
@@ -1,22 +1,30 @@ "use strict"; +const fs = require("fs"); +const path = require("path"); const mongoose = require("mongoose"); +const Schema = mongoose.Schema; mongoose.Promise = global.Promise; const db = {}; db.mongoose = mongoose; -db.url = "mongo:27017"; +const db_pass = fs.readFileSync("/run/secrets/mongo_pass").toString(); +const db_user = fs.readFileSync("/run/secrets/mongo_user").toString(); +db.url = "mongodb://" + db_user + ":" + db_pass + "@mongo:27017"; -const blogPostModel = mongoose.model( - "blogPost", - mongoose.Schema( - { - title: String, - description: String, - published: Boolean, - }, - { timestamps: true } - ) -); +const BlogPostSchema = new Schema({ + title: { type: String, required: true, trim: true }, + slug: { type: String, required: true, lowercase: true, trim: true }, + body: { type: String, required: true }, + teaser: { type: String, required: true }, + keywords: { type: Array, required: true }, + lastUpdatedAt: { type: Number }, +}); + +// a simple hook to update the timestamp(lastUpdatedAt) on update +BlogPostSchema.pre("save", function (next) { + this.lastUpdatedAt = Date.now(); + next(); +}); function dbInit() { db.mongoose @@ -29,4 +37,25 @@ function dbInit() { process.exit(1); }); } -module.exports = dbInit; + +function populateDB(model) { + let filePaths = fs.readdirSync(path.join(__dirname, "mds")); + filePaths.forEach((fileName) => { + let fileContent = fs + .readFileSync(path.join(__dirname, "mds", fileName), "utf-8") + .toString(); + let newBlogPost = new model({ + title: fileName, + slug: "slug", + body: fileContent, + teaser: "teaser", + keywords: ["kw1", "kw2"], + }); + newBlogPost.save(); + }); +} +module.exports = { + blogPost: mongoose.model("BlogPost", BlogPostSchema), + dbInit: dbInit, + populateDB: populateDB, +}; |