Skip to content

Instantly share code, notes, and snippets.

View andrewjmead's full-sized avatar

Andrew Mead andrewjmead

View GitHub Profile
@andrewjmead
andrewjmead / app.js
Last active January 5, 2020 20:54
Sequelize One-To-One Relationship
var Sequelize = require('sequelize');
var sequelize = new Sequelize(undefined, undefined, undefined, {
'dialect': 'sqlite',
'storage': __dirname + '/basic-sqlite-database.sqlite'
});
var Profile = sequelize.define('profile', {
someData: Sequelize.STRING
});
@andrewjmead
andrewjmead / server.js
Last active April 21, 2016 00:41
Rajesh - Validate description is string
// ...
// POST /todos
app.post('/todos', middleware.requireAuthentication, function(req, res) {
var body = _.pick(req.body, 'description', 'completed');
db.todo.create(body).then(function (todo) {
todo.setUser(req.user).then(function () {
res.json(todo);
@andrewjmead
andrewjmead / many-to-many.js
Created February 1, 2016 14:05
Sequelize Many-to-many Example
var Sequelize = require('sequelize');
var sequelize = new Sequelize(undefined, undefined, undefined, {
'dialect': 'sqlite',
'storage': __dirname + '/basic-sqlite-database.sqlite'
});
var Todo = sequelize.define('todo', {
description: {
type: Sequelize.STRING,
allowNull: false,
@andrewjmead
andrewjmead / nested-prop.js
Last active February 1, 2016 14:39
Nested props this deafult
var a = {
prop: {
nested: function (){
console.log(this);
}
}
}
a.prop.nested() // return { nested: [Function] }, not the top level object
@andrewjmead
andrewjmead / script.js
Last active February 2, 2016 13:25
Cron Job Midnight
var CronJob = require('cron').CronJob;
var job = new CronJob({
cronTime: '00 00 00 * * *',
onTick: function() {
/*
* Run a 12:00am every night
*/
},
start: false
});
@andrewjmead
andrewjmead / api-doc.js
Last active February 3, 2016 13:14
Shena - Company Tickets
// GET /companies/:companyId/tickets
{
company: {
name: 'Some LLC'
// Other company fields
},
tickets: [{
id: 1,
userId: 23
// Other ticket fields
@andrewjmead
andrewjmead / app.js
Last active February 3, 2016 20:47
Shena - Query Eager Load
// PART ONE - Query todos with user info included
Todo.findAll({include: [User]}).then(function(todos) {
todos.forEach(function (todo) {
console.log(todo.toJSON());
});
}, function() {});
// Printed to screen for each todo
// You can get the username by calling todo.user.userName
// {
@andrewjmead
andrewjmead / app.js
Created February 5, 2016 18:57
Raistlin - Password Manager Fixed
var storage = require('node-persist');
var crypto = require('crypto-js');
storage.initSync();
var accounts = [];
var argv = require('yargs').command('create', 'Create a new entry in the password manager', function(yargs) {
yargs.options({
accountName: {
demand: true,
type: 'string',
@andrewjmead
andrewjmead / app.js
Created February 15, 2016 22:26
John - getAccountIndex()
function getAccountIndex(acctNumber) {
for (var i = 0; i < accounts.length; i++) {
var account = accounts[i];
if (acctNumber === account.accountNumber) {
var index = accounts.indexOf(account);
console.log("Found it!");
console.log("Index: " + accounts.indexOf(account));
@andrewjmead
andrewjmead / app.js
Created February 15, 2016 22:30
ES6 Method Declaration
var obj = {
age: 25,
getAge () {
console.log(this.age);
}
}
obj.getAge();