Created
June 9, 2015 01:02
-
-
Save bdevel/a00b0146ff4610de7efc to your computer and use it in GitHub Desktop.
Ember-pouch adapter with custom document IDs
This file contains hidden or 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
import PouchDB from 'pouchdb'; | |
import { Adapter } from 'ember-pouch'; | |
var db = new PouchDB('app-database'); | |
export default Adapter.extend({ | |
db: db, | |
// Generates something like: 9f6kf-xz6by | |
// Much nicer than a full uuid | |
_generateId: function () { | |
var chars = 'abcdefghjkmnpqrstuvwxyz0123456789'; | |
var out = ''; | |
var segments = 2; | |
var segLength = 5; | |
for(let i = 0; i < segments; i++){ | |
for(let x = 0; x < segLength; x++){ | |
var r = Math.floor(Math.random() * chars.length);// int 0 - chars.length | |
out += chars[r]; | |
} | |
if (i < segments - 1){out += '-';} | |
} | |
return out; | |
}, | |
createRecord: function(store, type, object) {// object= snapshot | |
if (Ember.isBlank(object.id) ){ | |
object.id = this._generateId(); | |
} | |
return this._super(store, type, object); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment