alexrothenberg / simple-orm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simple-ORM.js

ActiveRecord is an awesome pattern to let you work with objects and avoid writing SQL even when storing data in a relational database. This library lets you work with the browser's Web SQL API (not officially a part of HTML5 anymore but in browsers)

Download

simple-orm.js

Examples

Defining your model object

Article = new Model.Base('articles', ['title', 'content']);

A database table is automatically created when the model is defined (sort of like a Rails migration)

Creating an object

Article.create({
  id: 17,
  title: 'A good Article', 
  content: 'This is about lots of stuff...'
})

Finding an object

article = Article.find(17)
// we do not yet know whether the article exists because the sql call is asynchronous

// article is a "promise" that will tell us when it is ready
article.done(function() {
  if (article.id) {
    // Found the article.  article.title == 'A good Article'
  } else {
    // No article with id 17 exists
  }
})

Get list of all objects

articles = Article.all()
// articles == [] because the sql call is asynchronous

// articles is a "promise" that will tell us when it is filled in
articles.done(function() {
  // Now the articles are there and we can do something
})

Delete all objects

Article.delete_all()

Thanks

Thanks to Ruby on Rails and its ActiveRecord pattern for inspriing me to bring this small piece to javascript.

Want to help?

Please do!

Create an issue, submit a pull request or just let me know what you think @alexrothenberg

Legal

Licensed under the MIT license

About

License:MIT License


Languages

Language:JavaScript 97.1%Language:CoffeeScript 2.5%Language:Ruby 0.4%