Skip to content

Instantly share code, notes, and snippets.

@bdevel
Created June 9, 2015 01:02
Show Gist options
  • Save bdevel/a00b0146ff4610de7efc to your computer and use it in GitHub Desktop.
Save bdevel/a00b0146ff4610de7efc to your computer and use it in GitHub Desktop.
Ember-pouch adapter with custom document IDs
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