Skip to content

Instantly share code, notes, and snippets.

@OpenSorceress
Forked from SinisterMinister/awesomeorm.js
Created July 23, 2013 07:05
Show Gist options
  • Save OpenSorceress/6060366 to your computer and use it in GitHub Desktop.
Save OpenSorceress/6060366 to your computer and use it in GitHub Desktop.
/**
* This is a WIP of potential usage of the ORM in a NodeJS implementation. Let's try implementing
* this as a module for other parts of the app to use. Less instantiation for later
*/
var ORM = require('awesomeorm-base'),
repository = require('awesomeorm-repository-rest')('http://rest.example.com/schema'),
dataParser = require('awesomeorm-data-jsonapi'),
modelBuilder = require('awesomeorm-model-encasulated');
module.exports = new ORM(repository, dataParser, modelBuilder);
/**
* Later we could call this module as such
*/
var ORM = require('./lib/rest-orm');
/**
* Now lets do some instantiation! All this should do is build an empty user model for use.
*/
var user = ORM.factory('user');
/**
* We could use it as a base object to create a new user with! Oh snap! Promises!
*/
user.set({firstName: "Testy", lastName: "McTesteroni"}).save().then(function (newUser) {
// We could do stuff here after the model has been saved
console.info(newUser.get('fullName')+"'s ID is", newUser.get('id'));
// Personally, I like accessors and mutators as we can do computed properties like the `fullName`
// one I used above. However, there is zero reason we couldn't have instantiated the ORM with
// a POJO model builder and used things like `newUser.id` instead. Though, I'm thinking there might
// be a clever way to do computed properties with POJO style objects as well
}, function (err) {
// Handle the error accordingly
});
/**
* Extending model's functionality
*/
ORM.extend('user', {
fullName: function (value) {
if (value !== undefined) {
// Act as the setter
var newVal = value.split(' ');
this.set('firstName', newVal[0]);
this.set('lastName', newVal[1]);
return;
}
// Act as the getter
return this.get('firstName')+' '+this.get('lastName');
}
});
/**
* Now lets do some loading. Both of these methods do the same thing, just with different signatures.
*/
var user1 = ORM.factory('user', 1),
user2 = ORM.factory('user').find(2);
/**
* They both also act as promises
*/
user1.then(function (user1) {
// POJO style implementation
console.info(user1.fullName);
});
/**
* Now lets add some querying. Find is singular, so if you want to find all users with the last
* name of Smith, you'd use findAll
*/
var user3 = ORM.factory('user').where({lastName: 'Smith'}).find(),
smithUsers = ORM.factory('user').where({lastName: 'Smith'}).findAll();
/**
* Range queries are HARD. Still working on the best way to handle them... Maybe something like this?
* Options, btw: { fields[field1, field2…], joins[], conditions[condition1, condition2...], … }... something like this
*/
var lastMonthsUsers = ORM.factory('user').where('id').between('100', '200').findAll(),
otherUsers = ORM.factory('user').find(options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment