Last active
December 20, 2015 02:49
-
-
Save SinisterMinister/6059618 to your computer and use it in GitHub Desktop.
WIP of figuring out the interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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({firstName: 'Will', lastName: 'Smith'}).find(), | |
smithUsers = ORM.factory('user').where({lastName: 'Smith'}).findAll(); | |
/** | |
* 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(), | |
last50Users = ORM.factory('user').orderBy('id', /* descending */ true).limit(50).findAll(), | |
otherUsers = ORM.factory('user').find(options); | |
/** | |
* Pull relationships with model. This just pulls the data with the object to | |
* help limit the number of calls to the service. Might be non performant | |
*/ | |
var user = ORM.factory('user').with('roles').with('profile.photo').find(1); | |
/** | |
* Joins for later | |
*/ | |
var usersWithNewPhotos = ORM.factory('user').has('photos').where('photo.dateCreated').greaterThan(yesterday).findAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment