Modelize
Modelize is a simple ORM written in JavaScript.
License
Apache License (version 2)
Prerequisites
Modelize requires version 0.6.x of Node.js or higher. If you want to run the tests or work on model, you'll want Vows.
NPM
Installing withnpm install modelize
Adapters
Modelize currently implements an in-memory store. More adapters will follow, a socket.io binding is also planned.
Defining models
Modelize uses a simple syntax for defining a model. It will look like this:
var UserModel = 'User' { this; this; this; this; this;};
The following data types are currently supported:
- string
- number
- array
- object
Creating instances
Now you can create instances of your new fancy User model. It is easy too:
var user = UserModel;
Getting and setting properties
Just use the following syntax:
// Getting propertiesconsole;console; // Setting propertiesuser;
If you try to get a property which was not initialized yet, it will return the default value or undefined if there is none.
Saving & Deleting of model instances
Remember: You must initialize/set all required properties before saving the model. Otherwise Modelize will throw an error.
user; // Saves the model instance into the storeuser;user; // Updates the model instance in the storeuser; // Removes the model instance from the store
Querying
Modelize offers a simple API for finding and sorting existing items/instances. Please remember, that an instance can only be found, if instance.save()
was called.
The API is (as you might expect for a NodeJS library) asynchronous and the methods for querying are static methods on each model constructor.
Shared instances
Instances are always shared to save memory usage and keep the usage simple. This means:
var user1 = UserModel;var user2 = UserModel; UserModel;
Finding a single item
Use the findOne
method to find a single item. You can specify a set of query parameters in the form of an object-literal. In the case of multiple results, it will only return the first one. If no result can be found, the return value is undefined
.
UserModel;
Collection of items
Use the find
method to find lots of items. Pass it a set of query parameters in the form of an object-literal, where each key is a field to compare and the value is a simple value for comparison. (equal to)
UserModel;
If you want to compare an object, you can also give a set of query parameters like this: {stats: {experience: 37, age: 19}}
Modelize Javascript ORM - © 2012-2013 P. Mathis (pmathis@snapserv.net)