Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to bookshelf #25

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3dfca41
Starting on bookshelf migration
elliotf Jun 7, 2014
0e36f41
Migrate to standard config solution
elliotf Jun 8, 2014
2545051
Placeholder model files
elliotf Jun 8, 2014
0054d5f
Namespace database config
elliotf Jun 8, 2014
41c50ec
Prepare configuration for knex
elliotf Jun 8, 2014
9ec0f78
Ditch shrinkwrap for now
elliotf Jun 8, 2014
e17a480
Use strict versioning for now
elliotf Jun 8, 2014
6fa967e
Updated dependencies
elliotf Apr 17, 2015
4abed2f
DB is migrated/cleared using knex
elliotf Apr 17, 2015
9eee55f
Flatten model tests
elliotf Apr 17, 2015
0c901fb
Try to get travis build passing
elliotf Apr 17, 2015
fa9cf06
Inform knex about the ci env
elliotf Apr 17, 2015
1bf100f
Start porting models to bookshelf
elliotf Apr 17, 2015
b211dd2
beginning of bookshelf version of Feed model
elliotf Apr 17, 2015
d2ac119
A poller is not really a model
elliotf Apr 18, 2015
0f1948d
A mailer is not a model, either
elliotf Apr 18, 2015
b4a6a5e
More inline with how I do code these days
elliotf Apr 18, 2015
e973cae
Beginning of agents.Fetcher, updated dep's
elliotf Apr 18, 2015
32600d7
More agents.Fetcher WIP
elliotf Apr 18, 2015
518f7c6
Make it easy to reset everything
elliotf Apr 18, 2015
04798fe
bookmodels.Article
elliotf Apr 18, 2015
9cefa00
Mysql, please pay attention to schema
elliotf Apr 18, 2015
7289305
Inject dependencies
elliotf Apr 18, 2015
236cbb2
Faster test display
elliotf Aug 15, 2015
074cf71
Merge branch 'migrate_to_bookshelf' of github.com:elliotf/rssmtp into…
elliotf Aug 15, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
coverage.html
*.sqlite
config/config.json
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# please wake up, travis
language: node_js
node_js:
- "0.10"
before_script:
- cp config/config.json.example config/config.json
- mysql -e 'create database rssmtp;'
- ./node_modules/.bin/knex migrate:latest
env: NODE_ENV=ci
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
clean:
rm -rf node_modules

coverage:
NODE_ENV=test \
DB_TOKEN="coverage" \
Expand Down Expand Up @@ -36,12 +39,12 @@ test:
APP_SMTP_SSL="true" \
APP_SMTP_FROM="[email protected]" \
APP_SMTP_PASS="dummy password" \
./node_modules/.bin/mocha --recursive test -R list
./node_modules/.bin/mocha --recursive test -R dot

testwatch:
DB_TOKEN="testwatch" ./node_modules/.bin/chicken -c 'clear; time make test' .

install:
npm install

.PHONY: cov coverage dev supper test testwatch
.PHONY: clean cov coverage dev supper test testwatch
30 changes: 30 additions & 0 deletions agents/fetcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

function Fetcher(getter) {
this._getter = getter;
}

Fetcher.prototype.fetchFeed = function fetchFeed(feed, done) {
var getter = this._getter;

feed.set('last_fetched', new Date());
feed
.save()
.exec(function(err) {
if (err) {
return done(err);
}

var url = feed.get('url');

getter(url, function(err, response, body) {
if (err) {
return done(err);
}

done();
});
});
};

module.exports = Fetcher;
17 changes: 17 additions & 0 deletions agents/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var _str = require('underscore.string');
var fs = require('fs');
var path = require('path');

var files = fs.readdirSync(__dirname);

files.forEach(function(file) {
if (file === path.basename(__filename)) {
return;
}

var class_name = _str.classify(path.basename(file, '.js'));

var model = require('./' + file);

module.exports[class_name] = model;
});
3 changes: 1 addition & 2 deletions models/mailer.js → agents/mailer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var nodemailer = require('nodemailer')
;
var nodemailer = require('nodemailer');

function Mailer() {
var settings = {
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions bookmodels/article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var db = require('../db');

var Article = db.BaseModel.extend({
tableName: 'articles',
defaults: function defaults() {
return {
link: null,
description: null,
title: 'untitled article'
}
}
}, {
});

module.exports = Article;
10 changes: 10 additions & 0 deletions bookmodels/feed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

var db = require('../db');

var Feed = db.BaseModel.extend({
tableName: 'feeds'
}, {
});

module.exports = Feed;
17 changes: 17 additions & 0 deletions bookmodels/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var _str = require('underscore.string');
var fs = require('fs');
var path = require('path');

var files = fs.readdirSync(__dirname);

files.forEach(function(file) {
if (file === path.basename(__filename)) {
return;
}

var class_name = _str.classify(path.basename(file, '.js'));

var model = require('./' + file);

module.exports[class_name] = model;
});
40 changes: 40 additions & 0 deletions bookmodels/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var db = require('../db');

var User = db.BaseModel.extend({
tableName: 'users'
}, {
findOrCreateFromOAUTH: function findOrCreateFromOAUTH(oauth_data, done) {
var attrs = {
oauth_provider: oauth_data.provider,
oauth_id: oauth_data.id
};

var created = false;
var user = User
.forge(attrs)

user
.fetch()
.then(function(result) {
if (result) {
return result;
}

created = true;
user.set('email', oauth_data.emails[0].value);

return user.save();
})
.exec(function(err, user) {
if (err) {
return done(err);
}

done(null, user, created);
});
}
});

module.exports = User;
17 changes: 17 additions & 0 deletions config/ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
database: {
// for sequelize
dialect: "mysql",
username: "travis",
password: null,
database: "rssmtp",
host: "127.0.0.1",

// for bookshelf
client: 'mysql2',
connection: {
database: 'rssmtp',
user: 'travis'
}
}
};
23 changes: 0 additions & 23 deletions config/config.json.example

This file was deleted.

19 changes: 19 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
database: {
// for sequelize
dialect: "mysql",
username: "root",
password: null,
database: "rssmtp",
host: "127.0.0.1",

//debug: true,

// for bookshelf
client: 'mysql2',
connection: {
database: 'rssmtp',
user: 'root'
}
}
};
2 changes: 2 additions & 0 deletions config/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = {
};
19 changes: 19 additions & 0 deletions config/prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
database: {
// for sequelize
dialect: "postgres",
username: "rssmtp",
password: "password_here",
database: "rssmtp_production",
host: "127.0.0.1",

// for bookshelf
client: 'pg',
connection: {
host: "127.0.0.1",
username: "rssmtp",
password: "password_here",
database: "rssmtp_production"
}
}
};
1 change: 1 addition & 0 deletions config/runtime.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 2 additions & 0 deletions config/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = {
};
62 changes: 62 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var _ = require('lodash');
var Bookshelf = require('bookshelf');
var config = require('config');
var Knex = require('knex');

var db_config = _.cloneDeep(config.database);
if (db_config.client.match(/^mysql/)) {
// make mysql pay attention to schema and not "just go with it"
db_config.pool = {
afterCreate: function(connection, callback) {
connection.query("SET sql_MODE='STRICT_ALL_TABLES';", function(err) {
callback(err, connection);
});
}
};
}

var knex = Knex(db_config);
var bookshelf = Bookshelf.initialize(knex);

bookshelf.plugin('registry');
var Parent = bookshelf.Model;
var BaseModel = Parent.extend({
initialize: function initialize(attrs, options) {
Parent.prototype.initialize.call(this, attrs, options);

this.on('saving', this.onSaving);
},
parse: function parse(input, options) {
var attrs = Parent.prototype.parse.call(this, input, options);

(this.dateColumns || []).forEach(function(column) {
if (null != attrs[column]) {
attrs[column] = new Date(attrs[column]);
}
});

return attrs;
},
onSaving: function onSaving(model, attrs, options) {
trim_milliseconds = this.trimMilliseconds;

(this.dateColumns || []).forEach(function(column) {
if (null != attrs[column]) {
attrs[column] = new Date(attrs[column]);

if (trim_milliseconds) {
attrs[column].setMilliseconds(0);
model.set(column, attrs[column]);
}
}
});
},
dateColumns: ['created_at', 'updated_at', 'last_fetched'],
trimMilliseconds: true,
hasTimestamps: true,
}, {
});

module.exports.knex = knex;
module.exports.bookshelf = bookshelf;
module.exports.BaseModel = BaseModel;
4 changes: 4 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';

module.exports.development = require('config').database;
module.exports.ci = require('config').database;
11 changes: 11 additions & 0 deletions lib/promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var BookPromise = require('bookshelf/lib/base/promise');
var KnexPromise = require('knex/lib/promise');

function unhandledRejectionHandler(err) {
throw err;
}

BookPromise.onPossiblyUnhandledRejection(unhandledRejectionHandler);
KnexPromise.onPossiblyUnhandledRejection(unhandledRejectionHandler);

module.exports = KnexPromise;
Loading