angular-odm

angular-odm

AngularJS Object Database Model brings abstract model handling with locale storage "database" support. Pure model handling with local storage used as persistence. Angular-odm provides a bit logic of active record model handling known from popular MVC Frameworks.

Installation

bower install angular-odm

Demo

Check out this demo application here http://www.linslin.org/angular-odm-demo/. Demo sourcecodes can be found here https://github.com/linslin/ng-odm-angularjs-demo.

Quick controller examples

Load user data

Add a single record

Delete a single record

Delete all records

Handle your model abstract:

  • model.save();
  • model.update();
  • model.findAll();
  • model.findByPk();
  • model.findByAttributes();
  • model.findAllByAttributes();
  • model.countAll();
  • model.countByAttributes();
  • model.deleteByPk();
  • model.deleteByAttributes();
  • model.deleteAll();
  • model.validate();

Configuration

Note that you need to configure your database and models to make angular-odm work. Here is a example structure on how your AnguarJS application could be build up with angular-odm. Maybe it helps. . +-- images +-- scripts | +-- config | +-- odm.config.js | +-- controllers | +-- main.controller.js | +-- models | +-- user.model.js +-- views | +--main.html +-- bower_components | +-- . | +-- .. +-- bower.json +-- index.html

Include the scripts (may included by bower automatically).

One dependency over here: localStorageDB.

Create ODM configuration

The configuration module needs to be named ODMConfiguration with a constant defined as ODM. For example in a app/config directory. Naming is strict. All Database configurations will be placed here. Here is a example database setup with three tables in it. Database schema will automatically created on AngularJS application startup.

Create your models.

You will need to define your model and its attributes which will associate with localStorageDB. You could do that in a app/model directory. Define your model attributes by using self._attributes. Hint that self._attributes object needs to be defined database configuration as well, else the model will not be persisted in locale storage. Change self._table to connect a model to a datebase table which should be defined in ODM configuration constant.

Include application module dependencies

Add those modules into your application main module like in the following example. Don't forget to load your resources into dom. =)

Documentation

Abtract hierarchy object diagram

Fow

Primary key handling

A primary key and its attribute is automatically generated by localStorageDB on all models. You can access the primary key via model.ID.

DB configuration

You need to setup a strict database configuration AngularJS module named ODMConfiguration including a constant named ODM. Please add ODMConfiguration module into your main application module like angular.module('angularApp', ['ODMConfiguration']).

Model configuration

You will need to define your model and its attributes which will associate with localStorageDB. You could do that in a app/model directory. Define your model attributes by using self._attributes. Hint that self._attributes object needs to be defined database configuration as well, else the model will not be persisted in locale storage. Change self._table to connect a model to a datebase table which should be defined in ODM configuration constant.

Model API

model.ID

Holds ID (pk) of model.

{null|number}

model.save();

Function will save a model values into local storage.

{Boolean}

model.update();

Function will update a single model record depending on attribute values.

{Boolean}

model.findAll();

Function will return array of all rows in database.

{Array}

model.findByPk();

Function will search a single model record depending on primary key.

{Object}

model.findByAttributes();

Function will search a single model record depending on search attributes.

{Object}

model.findAllByAttributes();

Function will search multiple rows depending on search attributes.

{Array}

model.countAll();

Function will count all records of a model in database.

{Number}

model.countByAttributes();

Function will count all record of a model in database filtered by attributes.

{Number}

model.deleteByPk();

Function will delete a record by primary key.

{Boolean}

model.deleteByAttributes();

Function will delete all records with matching attribute values.

{Boolean}

model.deleteAll();

Function will delete all records of a model.

{Boolean}

model.validate();

You can call validate manually. It will validate model attribute values depending on model attribute definition. Is automatically called before a database transaction.

{Boolean}

Changelog

Version 0.0.1

  • First stable release
comments powered by Disqus
This page was last updated about 2 years ago.