Coder Social home page Coder Social logo

simpleorm.js's Introduction

SimpleORM.js

SimpleORM.js is an asynchronous Javascript object-relational mapper library for WwbSQL. It can be used in the web browser and on the Cordova applications with Cordova-sqlite-storage. It currently supports HTML5 WebSQL database, a somewhat controversial part of HTML5 that is supported in Webkit browsers, specifically on mobile devices, including iPhone, Android and Palm's WebOS.

SimpleORM.js has no dependencies on any other frameworks.

The project is actively in development.

Using SimpleORM.js

Browser support

  • Modern browsers (Google Chrome and Safari)
  • Cordova-sqlite-storage inside Cordova applications
  • Other browsers supporting WebSQL (e.g. Firefox)

Setting up

  • Using bower:
bower install https://github.com/Mirodil/SimpleORM.js.git

Add a <script> to your index.html:

build/orm.js needs to be added, as well as any data stores you want to use:

<script src="/bower_components/simpleormjs/build/orm.js"></script>

Copy directories you will need following almost the same instructions above.

Setup your database

You need to explicitly configure the data store you want to use and create:

/// for web browsers
var db = openDatabase('books.db','3.0','Books database', 65536);
/// for codova applications with SQLitePlugin pulgin
var db = openDatabase('books.db','3.0','Books database', 65536);

then provide create db to SimpleORM.js as first argument.

var database = new Database(db);
database.addTable(BookSchema);

// the first argument is define whether recreate database tables or not
database.up(true, function(err){
    if(err)
        return console.log(err);
    
    var Books = database.Tables('books');
    
    Books.insert({name:'You Don’t Know JS: Up & Going', author:'Someone', published: true});
    Books.update({id:1, name:'You Don’t Know JS: Up & Going'});
    
    Books.select({}, function(err, books){
        console.table(books);
        
        books.forEach(function(book){
            Books.delate(book.id);
        });
    });

});

Schema definition

A data model is declared using Database.Table. The following definitions define a BookSchema entity with a few simple properties. The property types are based on SQLite types:

  • TEXT, CHAR and VARCHAR: for textual data
  • INTEGER, DOUBLE, FLOAT, REAL and NUMERIC: for numeric values
  • BOOL: for boolean values (true or false)
  • DATE and DATETIME: for date/time value (with precision of 1 second)

Example use:

var BookSchema = Database.Table({
    /**
    * table name for creating and accessing to the table
    */
    name:'books', 
    columns:{ 
        /**
        * this will create `id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE`
        */
        id:{
            type: 'INTEGER', //Number
            primaryKey: true,
            allowNull: false,
            unique:true,
            autoinc:true
        },
        /**
        * this wil create `name NVARCHAR(100)`
        */
        name:{
            type: 'NVARCHAR(100)'
        },
        /**
        * this will create `published BOOL`
        */
        published:{
            type: 'BOOL',
        },
        /**
        * this will create `created DATETIME DEFAULT CURRENT_TIMESTAMP`
        */
        created:{
            type:'DATETIME',
            default: 'CURRENT_TIMESTAMP'
        }
    }
});

The created schema should be add to database and can be used to create new instances of these entities later.

database.addTable(BookSchema);

The columns support the following fields:

{ 
    type: 'INTEGER', 
    primaryKey: true, 
    allowNull: false, 
    unique:true, 
    autoinc:true, 
    default:[CURRENT_DATE|CURRENT_TIME|CURRENT_TIMESTAMP|<any>]
}

Creating and manipulating objects

Inserting

var Books = database.Tables('books');
Books.insert({name:'You Don’t Know JS: Up & Going', author:'Someone', published: true}, function(err, res){
    // TODO: ...
});

Updating

var Books = database.Tables('books');
Books.update({name:'You Don’t Know JS: Up & Going'}, 'id=1', function(err, res){
    // TODO: ...
});
Books.update({id:1, name:'You Don’t Know JS: Up & Going'}, function(err, res){
    // TODO: ...
});

Query collections

var Books = database.Tables('books');
Books.select({}, function(err, rows){
    // TODO: ...
});

The select supports the following fields:

{
   columns:'', // default '*'
   where:'',   // default empty
   group:'',   // default empty
   having:'',  // default empty
   order:'',   // default empty
   limit:0,    // default empty
   offset: 0   // default empty
}

Deleting

var Books = database.Tables('books');
Books.delete('id=1', function(err, res){
    // TODO: ...
});

Bugs and Contributions

If you find a bug, please report it. or fork the project, fix the problem and send me a pull request.

For support and discussion, please drop me an email.

License

This work is licensed under the MIT license.

simpleorm.js's People

Contributors

mirodil avatar

Stargazers

 avatar  avatar j fer avatar

Watchers

James Cloos avatar  avatar

Forkers

lykmapipo yolao

simpleorm.js's Issues

unique not managed

Hello,

build/orm.js:

--- a/<html>orm.js (<b>Today 13:13:07</b>)</html>
+++ b/<html><b>Current File</b></html>
@@ -294,6 +294,9 @@
                 q += ' AUTOINCREMENT';
             }
         }
+        if (ops.unique === true) {
+            q += ' UNIQUE';
+        }
         if (ops.allowNull === false) {
             q += ' NOT NULL';
         }
@@ -318,12 +321,6 @@

         db.transaction(function (tx) {
             tx.executeSql(query, values, sucessCallback, errorCallback);
-        }, function (err) {
-            console.log(err);
-            errorCallback(null, err);
-            return false;
-        }, function () {
-            console.log('transaction finished');
\ No newline at end of file
         });
     };

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.