[Emberfire] How to check for unique records

Hey guys, i’m trying to import a bunch of scraped data for my app and i’m currently having problems regarding data duplication.

I figure it was simply a matter querying to the firebase database during the import before creating a new record.

// this function is called elsewhere from inside a forEach loop
_createShop(data) {
  // is something wrong with the query perhaps?
  return this.store.query('shop', { orderBy: 'name', equalTo: data.shopName }).then((shop) => {
    if (shop.get('length')) {
      // if exists, just return the object
      return shop.get('firstObject');
    } else {
      // or continue creating a new record
      return this.store.createRecord('shop', {
        name: data.shopName,
        // some other stuff
      }).save();
    }
  });
}

But i’m still getting multiple records with the same name. Any ideas?

Not sure what exactly your use case is here, and you’d want to be careful how you implemented it, but you could create your own ids to try and guarantee uniqueness. Essentially take some of the scraped data, munge it a little bit (maybe use something like a slug, etc) and set is as the id field in createRecord. One nice side effect is that then you could use findRecord or peekRecord instead of query for the lookup.

1 Like

ah. okay, worth a try. just a note, all the munging happens inside the createRecord() step. which i didn’t show. a few small cleanup tasks including creating a slug.

but let me give it a try and get back to you.

1 Like