Active Record for Javascript gives you persistence for your javascript applications with an Active Record twist.

From their docs...

Defining Your Model

ActiveRecord classes are created using the ActiveRecord.create method which takes three arguments: the name of the table that the class will reference, a field definition hash, and optionally a hash of instance methods that will be added to the class. If the table does not exist it will be automically created.

 
  1. var User = ActiveRecord.create('users',{  
  2.     username: '',  
  3.     password: '',  
  4.     post_count: 0,  
  5.     profile: {  
  6.         type: 'TEXT',  
  7.         value: ''  
  8.     }  
  9. },{  
  10.     getProfileWordCountfunction(){  
  11.         return this.get('profile').split(/\s+/).length;  
  12.     }  
  13. });  

Class & Instance Methods

JavaScript does not have true static methods or classes, but in this case any method of the User variable above is refered to as a class method, and any method of a particular user (that the User class would find) is refered to as an instance method. The most important class methods are create() and find():

 
  1. var jessica = User.create({  
  2.     username: 'Jessica',  
  3.     password: 'rabbit'  
  4. });  

Add new class or instance methods to all ActiveRecord models in the following way:

 
  1. ActiveRecord.ClassMethods.myClassMethod = function(){  
  2.     //this === model class  
  3. };  
  4. ActiveRecord.InstanceMethods.myInstanceMethod = function(){  
  5.     // this === model instance  
  6. };  

Getters & Setters

It is extremely important to note that all of the attributes/columns of the user are accessible directly for reading (for convenience), but cannot be written directly. You must use the set() method to set an attribute, you should use the get() method to access all attributes, but you must use the get() method if your attribute/column is a method of the object or a JavaScript reserved keyword ('save,'initialize','default', etc).

 
  1. jessica.username // 'Jessica'  
  2. jessica.get('username'); // 'Jessica'  
  3. jessica.username = 'new username';  
  4. jessica.get('username'); // 'Jessica'  
  5. jessica.set('username','new username');  
  6. jessica.get('username'); // 'new username'  

When Data is Persisted

Data is only persisted to the database in three cases: when you explicitly call save() on a record, when you call create() on a record, or create a child record through a relationship (the method will contain the word "create" in this case), or when you call updateAttribute() on a record. In the case of the latter, only the attribute you update will be saved, the rest of the record will not be persisted to the database, even if changes have been made. Calling save() may add an "id" property to the record if it does not exist, but if there are no errors, it's state will otherwise be unchanged. You can call refresh() on any record to ensure it is not out of synch with your database at any time.

Finding Records

If you created the User class using the define() method you automatically have free "finder" methods:

 
  1. User.findByUsername('Jessica');  
  2. User.findAllByPassword(''); //finds all with blank passwords  

Otherwise you can use the base find() method, which takes a hash of options, a numeric id or a complete SQL string:

 
  1. var posts = Post.find({  
  2.     all: true,  
  3.     order: 'id DESC',  
  4.     limit: 10  
  5. });