-
Notifications
You must be signed in to change notification settings - Fork 3
Argon, Active Record for JavaScript
Argon is an asynchronous JavaScript implementation of the Active Record pattern, sounds complex I know, but is in fact quite simple once you get the hang of it, basically it just means that you will need a callback to manage the response of every method, just like the JQuery ajax method.
The name comes from the Argon element which symbol in the periodic table is ar (chosen to match Active Record initials).
We say that a method or function is asynchronous when it runs independent from the main program flow, this gives us performance improvements but forces us to think in a new way to solve a problem. We are used to program in a more structured way in which we call one method after the other and feeding one method with the response of the other, for instance:
file = open(file_name);
content = file.read();
content << " add some text";
file = open(file_name, callback);
callback(file) {
return file.read() << " add some text";
}
Argon provides a meta class with all the features required by the Active Record Model: getter methods, setter methods, object/DB relation, read methods, write methods and validations. Additionally we can save requests by caching the results.
Note: Requires Neon
Example:
Class("ExampleModel").includes(Argon.Model)({
storage : (new Argon.Storage.JsonRest({
url : {
post : '/path/to_post',
get : '/path/to_get',
put : '/path/to_put',
remove : '/path/to_remove'
}
})),
_cacheTimeToLive : {
all : 10000,
instance : 2000
}
prototype : {
name : null,
}
});
ExampleModel.findByName('example name', function(data) {
console.log(data.name); // example name
});
To ignore cache just feed the _cacheTimeToLive with 0 as time for both all and instance cache.