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.
- Modern browsers (Google Chrome and Safari)
- Cordova-sqlite-storage inside Cordova applications
- Other browsers supporting
WebSQL
(e.g. Firefox)
- 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>
-
Using directly from source:
git clone [email protected]:Mirodil/SimpleORM.js.git
Copy directories you will need following almost the same instructions above.
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);
});
});
});
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
andVARCHAR
: for textual dataINTEGER
,DOUBLE
,FLOAT
,REAL
andNUMERIC
: for numeric valuesBOOL
: for boolean values (true
orfalse
)DATE
andDATETIME
: 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>]
}
var Books = database.Tables('books');
Books.insert({name:'You Don’t Know JS: Up & Going', author:'Someone', published: true}, function(err, res){
// TODO: ...
});
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: ...
});
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
}
var Books = database.Tables('books');
Books.delete('id=1', function(err, res){
// TODO: ...
});
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.
This work is licensed under the MIT license.