TL;DR - How do I use my Waterline models from within a grunt task, in a sails project?
I am attempting to create some grunt tasks within a Sails project to process a CSV file and insert data into a Postgres database. I was hoping to use Waterline for this, though was unsuccessful in connecting to the database.
I was wondering, how would I use my Waterline models from within a grunt task?
I am not overly sure of what Sails does behind the scenes to make my models accessible, though I would be very interested in understanding.
I will update in the morning with example code of what I have tried.
I was importing things wrong. The following works properly :
var CWD = process.cwd();
var path = require('path');
var Waterline = require('waterline');
var User = Waterline.Collection.extend(require(path.join(CWD, 'api/models/User')));
module.exports = function (grunt) {
grunt.registerTask('seedDb', 'Given a list of addresses, assign long and lat.', function (clinicCsv, outputCsv) {
// tell grunt this task is async
var done = this.async();
// create ORM
var orm = new Waterline();
orm.loadCollection(User);
// initialize ORM
orm.initialize({
adapters: {
'sails-postgresql': require('sails-postgresql')
},
connections: require(path.join(CWD, 'config/connections')).connections,
defaults: require(path.join(CWD, 'config/models')).models
}, function (err, ontology) {
if (err) throw err;
console.log(ontology.collections);
done();
});
});
};
Where api/models/User is :
module.exports = {
identity: 'User',
connection: 'localPostgresqlServer',
attributes: {
firstName: {
type: 'string'
},
lastName: {
type: 'sting'
},
email: {
type: 'email'
},
password: {
type: 'string'
}
}
};
And config/connections and config/models are in the standard Sails format. The key was specifying the connection within the model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With