Created
August 8, 2014 13:07
-
-
Save adammcarth/d09a4106b48af09ac3af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ......................// | |
// 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