Code Monkey home page Code Monkey logo

simple-orm's Introduction

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

simple-orm's People

Contributors

alexrothenberg avatar

Stargazers

Robert J Samson avatar Amit Kumar avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

imamabudaud

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.