Skip to content

Commit

Permalink
More under-the-hood db changes
Browse files Browse the repository at this point in the history
Start on schema update change (v5), to include the 2 new tables (user and bookmark). Still need to include 1 user entry (current user) and 1 bookmark entry per project.
  • Loading branch information
eb1 committed Oct 31, 2024
1 parent 152bc93 commit fda1b7b
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 30 deletions.
19 changes: 0 additions & 19 deletions www/js/models/json/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,6 @@ define(function (require) {
return "http://localhost:3042/projects";
},

// parse: function (data) {
// if (_.isObject(data.results)) {
// console.log("Parsing project data results: " + data.results);
// return data.results;
// } else {
// console.log("Parsing project data: " + data);
// return data;
// }
// },

fetch: function(options) {

console.log("Fetching project");
Expand All @@ -275,15 +265,6 @@ define(function (require) {
return Backbone.Collection.prototype.fetch.call(this, options);
}

// sync: function (method, model, options) {
// if (method === "read") {
// // special case for sql - remove the blank name property for json
// if (options.data.hasOwnProperty('name') && options.data.name === "") {
// delete options.data.name;
// }
// }
// }

});

return {
Expand Down
8 changes: 4 additions & 4 deletions www/js/models/json/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ define(function (require) {
UserCollection = Backbone.Collection.extend({
model: User,
url: "/users"
});
}),

// Represents a placeholder in a project (book, chapter, and source phrase location). Can be more than 1 per user.
Bookmark = Backbone.Model.extend({
Expand All @@ -52,13 +52,13 @@ define(function (require) {
bookid: 0,
chapterid: 0,
spid: ""
},
});
}
}),

BookmarkCollection = Backbone.Collection.extend({
model: Bookmark,
url: "/bookmarks"
})
});

return {
User: User,
Expand Down
36 changes: 34 additions & 2 deletions www/js/models/sql/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ define(function (require) {
Backbone = require('backbone'),
i18n = require('i18n'),
projects = [],
CURRSCHEMA = 4,
CURRSCHEMA = 5,

// ---
// STATIC METHODS
Expand Down Expand Up @@ -344,6 +344,39 @@ define(function (require) {
deferred.resolve();
});
}
if (fromVersion === 4) {
// AIM version 1.18 (user, bookmark tables)
window.Application.db.transaction(function (tx) {
var theSQL = "";
// version table exists (see logic above), but is at version 4; update it here
tx.executeSql('UPDATE version SET schemaver=? WHERE id=?;', [CURRSCHEMA, 1], function (tx, res) {
console.log("version table updated -- schema version: " + CURRSCHEMA);
}, function (err) {
console.log("failed to set the version schema: " + err);
});
// update changes for 1.18
// 2 new tables -- user and bookmark
theSQL = 'CREATE TABLE user (id integer primary key, username text, userid text, roles text, CopySource integer, WrapUSFM integer, StopAtBoundaries integer, AllowEditBlankSP integer, ShowTranslationChecks integer, DefaultFTTarget integer, UILang integer, DarkMode integer, bookmarks Text, WordSpacing integer);';
tx.executeSql(theSQL, [], function (tx, res) {
console.log("UpgradeSchema() - user table created");
}, function (err) {
// exception thrown -- assume table doesn't exist
console.log("upgradeSchema: error: " + err.message);
});
theSQL = 'CREATE TABLE IF NOT EXISTS bookmark (id integer primary key, bookmarkid text, projectid text, bookname text, bookid integer, chapterid integer, spid text);';
tx.executeSql(theSQL, [], function (tx, res) {
console.log("UpgradeSchema() - bookmark table created");
}, function (err) {
// exception thrown -- assume table doesn't exist
console.log("upgradeSchema: error: " + err.message);
});

}, function (e) {
console.log("upgradeSchema error: " + e.message);
}, function () {
deferred.resolve();
});
}
return deferred.promise();
},
// checkSchema
Expand Down Expand Up @@ -950,7 +983,6 @@ define(function (require) {
break;
}
}

}),

ProjectCollection = Backbone.Collection.extend({
Expand Down
115 changes: 110 additions & 5 deletions www/js/models/sql/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define(function (require) {
var $ = require('jquery'),
Backbone = require('backbone'),
users = [],
bookmarks = [],
wordSpacingEnum = {
NONE: 0,
SMALL: 1,
Expand All @@ -37,7 +38,16 @@ define(function (require) {
deferred.resolve(results);
return deferred.promise();
},


findByBookmarkId = function (searchKey) {
var deferred = $.Deferred();
var results = bookmarks.filter(function (element) {
return element.attributes.bookmarkid === searchKey;
});
deferred.resolve(results);
return deferred.promise();
},

User = Backbone.Model.extend({
defaults: {
username: "",
Expand Down Expand Up @@ -118,7 +128,7 @@ define(function (require) {
break;
}
}
});
}),

UserCollection = Backbone.Collection.extend({
model: User,
Expand Down Expand Up @@ -229,7 +239,7 @@ define(function (require) {
}
}
}
});
}),

// Represents a placeholder in a project (book, chapter, and source phrase location). Can be more than 1 per user.
Bookmark = Backbone.Model.extend({
Expand Down Expand Up @@ -304,11 +314,106 @@ define(function (require) {
break;
}
}
});
}),

BookmarkCollection = Backbone.Collection.extend({
model: Bookmark,
url: "/bookmarks"

resetFromDB: function () {
var deferred = $.Deferred(),
i = 0,
len = 0;

window.Application.db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS bookmark (id integer primary key, bookmarkid text, projectid text, bookname text, bookid integer, chapterid integer, spid text);');
tx.executeSql("SELECT * from bookmark;", [], function (tx, res) {
for (i = 0, len = res.rows.length; i < len; ++i) {
// add the bookmark
var bookmark = new Bookmark();
bookmark.off("change");
bookmark.set(res.rows.item(i));
bookmarks.push(bookmark);
bookmark.on("change", bookmark.save, bookmark);
}
console.log("SELECT ok: " + res.rows.length + " bookmark items");
});
}, function (e) {
deferred.reject(e);
}, function () {
deferred.resolve();
});
return deferred.promise();
},

initialize: function () {
return this.resetFromDB();
},

// Removes all bookmarks from the collection (and database)
clearAll: function () {
window.Application.db.transaction(function (tx) {
tx.executeSql('DELETE from bookmark;'); // clear out the table
bookmarks.length = 0; // delete local copy
}, function (err) {
console.log("DELETE error: " + err.message);
});
},

sync: function (method, model, options) {
if (method === "read") {
if (options.data.hasOwnProperty('bookmarkid')) {
findByBookmarkId(options.data.bookmarkid).done(function (data) {
options.success(data);
});
} else if (options.data.hasOwnProperty('projectid')) {
var deferred = $.Deferred();
var username = options.data.projectid;
var len = 0;
var i = 0;
var retValue = null;
// special case -- empty name query ==> reset local copy so we force a retrieve
// from the database
if (projectid === "") {
bookmarks.length = 0;
}
var results = users.filter(function (element) {
return element.attributes.projectid === projectid;
});
if (results.length === 0) {
// not in collection -- retrieve them from the db
window.Application.db.transaction(function (tx) {
tx.executeSql("SELECT * FROM bookmark;", [], function (tx, res) {
var tmpString = "";
// populate the chapter collection with the query results
for (i = 0, len = res.rows.length; i < len; ++i) {
// add the book
var bookmark = new Bookmark();
bookmark.off("change");
bookmark.set(res.rows.item(i));
bookmarks.push(bookmark);
bookmark.on("change", bookmark.save, bookmark);
}
// return the filtered results (now that we have them)
retValue = users.filter(function (element) {
return element.attributes.projectid === projectid;
});
options.success(retValue);
deferred.resolve(retValue);
});
}, function (e) {
options.error();
deferred.reject(e);
});
} else {
// results already in collection -- return them
options.success(results);
deferred.resolve(results);
}
// return the promise
return deferred.promise();
}
}
}
});


Expand Down

0 comments on commit fda1b7b

Please sign in to comment.