Skip to content

Instantly share code, notes, and snippets.

@c3ry5
Last active December 19, 2015 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c3ry5/6009229 to your computer and use it in GitHub Desktop.
Save c3ry5/6009229 to your computer and use it in GitHub Desktop.
Web sql setup using underscore js
(function () {
db = {};
db.map = {
'jobs': {
'create': {
'sql': 'CREATE TABLE "jobs" (id unique, json)'
},
'insert': {
'sql': 'INSERT INTO "jobs" (id, json) VALUES (?,?)'
},
'update': {
'sql': 'UPDATE "jobs" SET json=? WHERE id=?'
},
'selectAll': {
'sql': 'SELECT * FROM "jobs"'
},
'select': {
'sql': 'SELECT * FROM "jobs" WHERE id=?'
},
'remove': {
'sql': 'DELETE FROM "jobs" WHERE id=?'
},
'drop': {
'sql': 'DROP TABLE "jobs"'
}
}
}
db.websql = window.websql = function (table, map) {
this.config = {
'TABLE': table,
'MAP' : map || db.map,
'SETUP': {
'DB': 'mydb',
'VERSION': '1.0',
'DESC': 'database',
'SIZE': 5 * 1024 * 1024
}
}
}
_.extend(db.websql.prototype, {
connection: function () {
return openDatabase(this.config.SETUP.DB, this.config.SETUP.VERSION, this.config.SETUP.DESC, this.config.SETUP.SIZE)
},
map: function (fn) {
return db.map[this.config.TABLE][fn].sql.toString()
},
callback: function (result, options) {
var _this = this;
if (typeof (options) !== 'undefined') {
if (typeof (callback) !== 'undefined') {
options.callback.call(_this, result)
} else {
console.log(result)
}
} else {
console.log(result)
}
},
error: function (tx, error) {
return error.message
},
create: function () {
var _this = this;
_this.connection().transaction(function (tx) {
tx.executeSql(_this.map('create'), [], function (tx, result) {
_this.callback(result)
}, _this.Error)
})
},
insert: function (options) {
var _this = this;
_this.connection().transaction(function (tx) {
tx.executeSql(_this.map('insert'), options.values, function (tx, result) {
_this.callback(result, options)
}, _this.Error)
})
},
update: function (options) {
var _this = this;
_this.connection().transaction(function (tx) {
tx.executeSql(_this.map('update'), options.values, function (tx, result) {
if (result.rowsAffected > 0) {
_this.callback(result, options)
} else {
_this.insert(options)
}
}, _this.Error)
})
},
selectAll: function (options) {
var _this = this;
_this.connection().transaction(function (tx) {
tx.executeSql(_this.map('selectAll'), [], function (tx, results) {
var len = results.rows.length,
i, result = [];
for (i = 0; i < len; i++) {
var row = results.rows.item(i);
result.push(row)
}
_this.callback(result, options)
}, _this.Error)
})
},
select: function (options) {
var _this = this;
_this.connection().transaction(function (tx) {
tx.executeSql(_this.map('select'), options.values, function (tx, results) {
var len = results.rows.length,
i, result = [];
for (i = 0; i < len; i++) {
var row = results.rows.item(i);
result.push(row)
}
_this.callback(result, options)
}, _this.Error)
})
},
remove: function (options) {
var _this = this;
_this.connection().transaction(function (tx) {
tx.executeSql(_this.map('remove'), options.values, function (tx, result) {
_this.callback(result, options)
}, _this.Error)
})
},
drop: function (options) {
var _this = this;
_this.connection().transaction(function (tx) {
tx.executeSql(_this.map('drop'), [], function (tx, result) {
_this.callback(result, options)
}, _this.Error)
})
}
})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment