DailyJS

Node Roundup: Sequelize 1.0, node-shell, Formaline

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

cli node modules database express

Node Roundup: Sequelize 1.0, node-shell, Formaline

Posted by Alex R. Young on .
Featured

cli node modules database express

Node Roundup: Sequelize 1.0, node-shell, Formaline

Posted by Alex R. Young on .

Sequelize 1.0.0

Sequelize (GitHub: sdepold / sequelize,
License, npm: sequelize) by Sascha Depold has just been updated to version
1.0.0. Sascha has written about the release in Changes in Sequelize
1.0.0
.

There's a nice new API for chained queries:

new Sequelize.Utils.QueryChainer
  .add(person.save())
  .add(pet.save())
  .run()
  .on('success', function() { });

And changes to the way models are instantiated:

// Before
new Model({attr1: 1, attr2: 2}).save()

// Now
Model.build({}).save()
// to first create the instance and save it afterwards or…

Model.create({})
// to create it and directly write it to the database

Other changes include:

  • An event-based architecture instead of callbacks
  • More convenient syncing
  • A count method for models
  • New model schema options: unique, primaryKey, autoIncrement

And plenty more!

If you're interested in Sequelize but haven't tried it before,
www.sequelizejs.com has great documentation to get you started.

node-shell

node-shell (npm: shell) by David Worms is a module for building shell scripts. It's interesting
because it lifts a lot of concepts straight from Express, particular
"pluggable" modules. It's very similar to working with Express.

This is a cut-down example from the author's documentation:

var shell = require('shell');
var app = new shell.Shell();

app.configure(function() {
    app.use(shell.history({
        shell: app
    }));

    // ... snip
});

// Command registration
app.cmd('redis keys :pattern', 'Find keys', function(req, res, next){
    if(!app.client){
        app.client = require('redis').createClient();
    }
    app.client.keys(req.params.pattern, function(err, keys){
        if(err){ return res.styles.red(err.message), next(); }
        res.cyan(keys.join('\n')||'no keys');
        res.prompt();
    });
});

// Event notification
app.on('redis quit', function(){
    if(app.client){
        app.client.quit();
    }
});

If you're a web app developer who hasn't worked with the console before,
node-shell looks like it might be a good way to leapfrog a lot of
tediousness.

Formaline

Formaline (License: MIT, npm: formaline) by Guglielmo Ferri helps handle form requests and file uploads, complete with connect middleware. It has several interesting
features:

  • Progress tracking
  • HTML5 multiple file upload support
  • Speaks JSON
  • Tested against malicious and incorrect HTTP headers
  • Handles filename collisions
  • The SHA1 data checksum of received files can be returned

Basic usage looks like this:

var formaline = require('formaline'),
    form = new formaline({});

form.on('filereceived', function(json) {

});

The resulting json object will contain these properties:

json = { 
  hashname: '..',           // <-- 40 HEX SHA1 STRING ( IT IS THE (SHA1) RESULTING HASH OF FILENAME )
  name: '..',               // <-- FILE ORIGINAL NAME  
  path: '..',               // <-- FILE PATH       
  type: '..',               // <-- MIME TYPE
  size: 217,                // <-- BYTES 
  fieldname: '..',          // <-- FILE FIELD NAME 
  datasha1sum: '..',        // <-- 40 HEX SHA1 STRING  ( IT IS THE (SHA1) RESULTING HASH OF THE FILE'S DATA )
  lastModifiedDate: '..'    // <-- FILE MTIME       
}