Skip to content

Instantly share code, notes, and snippets.

@adammcarth
Created August 8, 2014 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adammcarth/d09a4106b48af09ac3af to your computer and use it in GitHub Desktop.
Save adammcarth/d09a4106b48af09ac3af to your computer and use it in GitHub Desktop.
// ......................//
// Database configuration
// ......................//
// Establish connection to the MySQLite database file...
var db_connection = require("knex")({
client: "sqlite3",
connection: {
filename: "../blog_database.db" // path to sqlite database we just created
}
});
// Load the bookshelf library with the database settings
var bookshelf = require("bookshelf")(db_connection);
// ......................//
// Define the Post model
// ......................//
var Post = bookshelf.Model.extend({
tableName: "Posts",
initialize: function() {
this.on("saving", this.setTime);
this.on("saving", this.validatePost);
},
// SQLite doesn't handle current timestamps for us, so set one manually
// when a new post is saved.
setTime: function() {
this.set({
created_at: new Date()
});
},
validatePost: function() {
// Return false if someone tries to submit an empty post.
if (this.attributes.title == undefined || this.attributes.content == undefined) {
console.log("Oops. Your post must have both a title and content.");
return false;
}
}
});
module.exports = Post;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment