Last active
August 29, 2015 14:06
-
-
Save djfarrelly/8fb6af9b7a79879c2f8c to your computer and use it in GitHub Desktop.
db.js last modified
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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