Skip to content

Instantly share code, notes, and snippets.

@djfarrelly
Last active August 29, 2015 14:06
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 djfarrelly/8fb6af9b7a79879c2f8c to your computer and use it in GitHub Desktop.
Save djfarrelly/8fb6af9b7a79879c2f8c to your computer and use it in GitHub Desktop.
db.js last modified
// deps:
// db.js = shared/libs/db.js
buffer.localDatabase = {
// Increase this to clear the db entirely
dbVersion: 1,
open: function() {
return db.open({
server: 'bufferdb',
version: 2,
schema: {
dbVersion: {
key: { keyPath: 'id', autoIncrement: true },
},
updates: {
key: { keyPath: 'id', autoIncrement: true },
indexes: {
sent_at: {},
profile_id: {}
}
},
profiles: {
key: { keyPath: 'id', autoIncrement: true },
indexes: {
sent_at: {}
}
}
}
}).done(function(s){
return new Promise(function(resolve, reject){
// check if it's an old version
s.dbVersion.query()
.all()
.execute()
.then(function(versions){
var currentVersion = versions.length ? versions[0].version : 0;
if (currentVersion >= buffer.localDatabase.dbVersion) {
// Pass the server variable down to the callee
resolve(s);
} else {
// 1 - Clear the DB
s.updates.clear();
s.profiles.clear();
s.dbVersion.clear();
// 2 - Set the newest verison
s.dbVersion.add({
version: buffer.localDatabase.dbVersion,
}).then(function(version) {
// version updated & db is clear, we resolve the promise
resolve(s);
});
}
});
});
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment