diff options
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, +}; |