comic-strips / api-comicstrips

1 stars 0 forks source link

Fork loopback-connector-firestore #57

Closed seanttaylor closed 6 years ago

seanttaylor commented 6 years ago

Some modifications to loopback-connector-firestore were required to in order to manually set the document id of offers pushed to firestore. Specifically this refactor of the Firestore.create method:

create(model, data, callback) {
  if (data.custom_id) {
     const {custom_id, ...requestData} = data;
     this.db.collection(model).doc(custom_id.toString()).set(requestData)
     .then(ref => callback(null, ref.id)).catch(err => callback(err));
  } else {
     this.db.collection(model).add(data).then(ref => callback(null, ref.id)).catch(err => callback(err));   
  }
}

We'll need to fork loopback-connector-firestore and publish separately in order to continue using Loopback's datasources interface with this module.

seanttaylor commented 6 years ago

We also need ensure the updateAttributes method is refactored. The loopback-connector-firestore package was calling set on the document instead of update which caused updates to existing documents to be toasted when the intent was to update a single field witihin a document.

updateAttributes(model, id, data, options, callback) {
  const self = this;
  this.exists(model, id, null, (err, res) => {
  if (err) callback(err);
  if (res) {
    self.db.collection(model).doc(id).update(data).then(() => {
      // Document updated successfully.
      callback(null, []);
    });
  } else {
    callback('Document not found');
  }
 });
}
seanttaylor commented 6 years ago

We also need to modify the replaceById method as follows.

replaceById(model, id, data, options, callback) {
  const self = this;
/*For whatever reason this method does not do string conversion on the id with the result that Firestore cannot find the document even though the id reference is valid. This method also includes an id property with throws an error on the "before save" and "loaded" hooks. The id prorperty is deleted here. -- STT 03.25.2018 */
  const __id = id.toString();
  delete data.id;

  this.exists(model, __id, null, (err, res) => {
  if (err) callback(err);
  if (res) {
      self.db.collection(model).doc(__id).update(data).then(() => {
        // Document updated successfully.
        callback(null, []);
       });
    } else {
      callback('Document not found');
    }
  });
}
seanttaylor commented 6 years ago

Forking loopback-firestore was unnecessary. Migrated data persistence to Google Firestore instead.