Skip to content

Commit

Permalink
allow to install an existing database
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierlacot committed Jun 20, 2012
1 parent 97a99a0 commit 4c513e8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
13 changes: 9 additions & 4 deletions README.markdown
Expand Up @@ -12,7 +12,7 @@ joli.js is widely unit-tested. Go check [the demo application](https://github.co
The source code of joli.js is [available on GitHub](https://github.com/xavierlacot/joli.js). Just grab it and include it in your Titanium project using either `Titanium.include()` or the CommonJS `require()`statement:

Titanium.include('joli.js');

or (please note the missing ".js" suffix):

var joli = require('path/to/joli').connect('your_database_name');
Expand All @@ -23,13 +23,18 @@ The latter integration mode must be prefered, as it helps sandboxing external li
var database1 = joliLibrary.connect('first_database');
var database2 = joliLibrary.connect('second_database');

It is also possible to install existing databases bundled with the application:

var joli = require('path/to/joli').connect('your_database_name', '/path/to/database.sqlite');


## Configuration

### Database connection creation
If you included joli.js with `Titanium.include()`, there is one single required configuration step: configuring the database name. This can be done in only one line, which has to be put before every call to joli.js's API:

joli.connection = new joli.Connection('your_database_name');

If you prefered to load joli.js as a CommonJS module, it is not necessary to write this configuration instruction. However, you still may want to change the database name of a connection, and in that case you'll want to use this command.

### Models configuration
Expand Down Expand Up @@ -270,15 +275,15 @@ It is possible to use several databases with joli.js by loading the library as a
var database2 = joliLibrary.connect('second_database');

## Credits and support
joli.js has been developed by [Xavier Lacot](http://lacot.org/) and is
licensed under the MIT license.
joli.js has been developed by [Xavier Lacot](http://lacot.org/) and is licensed under the MIT license. The joli.js project is sponsored by [JoliCode](http://jolicode.com/).

Please use GitHub in order to report bugs, but you may also ask for help on how to use joli.js by sending me a mail directly. My email address is xavier@lacot.org.

## Changelog

### master
* turned joli.js as a commonjs module
* added the possibility to install an existing database bundled with the app
* added a .as() method for building queries with join() (thanks nicjansma)
* fixed a bug in the query where() method, when a value was 0 or '' (thanks nicjansma)
* added a toArray() method on record instances
Expand Down
25 changes: 19 additions & 6 deletions joli.js
Expand Up @@ -145,13 +145,22 @@ var joliCreator = function() {
/**
* Connection
*/
joli.Connection = function(database) {
joli.Connection = function(database, file) {
this.dbname = database;
this.database = Titanium.Database.open(this.dbname);

if (file) {
this.database = Titanium.Database.install(file, this.dbname);
} else {
this.database = Titanium.Database.open(this.dbname);
}

this.database.execute('PRAGMA read_uncommitted=true');
};

joli.Connection.prototype = {
disconnect: function() {
this.database.close();
},
execute: function(query) {
// Titanium.API.log('info', query);
return this.database.execute(query);
Expand Down Expand Up @@ -832,7 +841,6 @@ var joliCreator = function() {
};



/**
* Global joli object for non-CommonJS usage:
* Ti.include('joli.js);
Expand All @@ -842,15 +850,20 @@ var joli = joliCreator();


/**
* In case joli.js is loaded as a CommonJS module:
* In case joli.js is loaded as a CommonJS module
* var joli = require('joli').connect('your_database_name');
* var joli = require('joli').connect('your_database_name', '/path/to/database.sqlite');
*/
if (typeof exports === 'object' && exports) {
exports.connect = function(database) {
exports.connect: function(database, file) {
var joli = joliCreator();

if (database) {
joli.connection = new joli.Connection(database);
if (file) {
joli.connection = new joli.Connection(database, file);
} else {
joli.connection = new joli.Connection(database);
}
}

return joli;
Expand Down

0 comments on commit 4c513e8

Please sign in to comment.