Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience getType functions, bulk load function, addOptions function #49

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Add comment for logical labs added test functions
  • Loading branch information
patakijv committed Sep 18, 2018
commit 3bab17d44f053f511f48e083fd426cae51043de1
34 changes: 31 additions & 3 deletions joli.js
Expand Up @@ -45,6 +45,7 @@ var joliCreator = function() {
return typeof obj;
}
},
// [Logical Labs] add some convenience type test functions
isUndefined: function(obj) {
return obj === void 0;
},
Expand Down Expand Up @@ -136,6 +137,13 @@ var joliCreator = function() {
if (!joli.getType(val)) {
return "NULL";
} else {
// [Logical Labs] allow for nested data (objects, arrays) in val
// to be serialized as a string for faster db interaction of large
// amounts of data - requires you to handle (jsonParse) the data
// after its retrieved when the data is expected to be serialized
if ((joli.isObject(val)) || (joli.isArray(val))) {
val = JSON.stringify(val);
}
if (joli.getType(val) === "string") {
// escape single quotes and dollar signs.
// quotes are escaped for SQLite
Expand Down Expand Up @@ -163,13 +171,16 @@ var joliCreator = function() {
joli.Connection = function(database, file) {
this.dbname = database;

Ti.API.debug("opening database for connection using database:"+database+", file:"+file);
if (file) {
this.database = Titanium.Database.install(file, this.dbname);
} else {
this.database = Titanium.Database.open(this.dbname);
}
Ti.API.debug("should be opened...");

this.database.execute('PRAGMA read_uncommitted=true');
Ti.API.debug("after PRAGMA read_uncommitted=true...");
};

joli.Connection.prototype = {
Expand Down Expand Up @@ -245,6 +256,10 @@ var joliCreator = function() {
};

joli.model.prototype = {
// return a query object for this model
query: function() {
return new joli.query().from(this.table);
},
all: function(constraints) {
var q = new joli.query().select().from(this.table);

Expand Down Expand Up @@ -330,6 +345,19 @@ var joliCreator = function() {
return result[0];
}
},
// testing extending joli model with multiple where conditions and order together
findOneUsingCompoundWhereOrderedBy: function(field,value,order) {
var q = new joli.query().select().from(this.table);
joli.each(constraints.where, function(value, field) {
q.where(field, value);
});
var result = q.order(order).limit(1).execute();
if (result.length === 0) {
return false;
} else {
return result[0];
}
},
findOneById: function(value) {
return this.findOneBy('id', value);
},
Expand Down Expand Up @@ -377,7 +405,7 @@ var joliCreator = function() {

return q.execute();
},
// load function for bulk loading many records at once and
// [Logical Labs] function for bulk loading many records at once and
// is optimizable with transaction via useTransaction flag (consider as default?)
load: function(recordsDataArray, options, callback) {
var transaction = false;
Expand Down Expand Up @@ -414,7 +442,7 @@ var joliCreator = function() {
truncate: function() {
new joli.query().destroy().from(this.table).execute();
},
// add options to post model construction - for adding methods later
// [Logical Labs] add options to post model construction - for adding methods later
addOptions: function(options) {
if (options.methods) {
joli.each(options.methods, function(method, name) {
Expand Down Expand Up @@ -985,4 +1013,4 @@ if (typeof exports === 'object' && exports) {

return joli;
};
}
}