7

I have an existing Firebase application (Which was built for quick prototyping and which is now grown big, I don't intend to move because existing dependencies and also because ease of use and authentication tied up) and I am trying to build a Rest API using FeatherJS.

It seems it is really easy to work with FeathersJS if you are using a standard database (MongoDB, etc). How can I integrate Firebase with Feathers keeping the best practices in place (Service Architecture, AOP)? Can I override service in Feathers and map it to my Firebase Rest endpoint?

I created a custom service in Feathers and tried doing this :

  get(id, params) {
    return Promise.resolve(FirebaseRef.child(id).once('value'));
  }

I get the:

Converting circular structure to JSON error

Is what I am doing correct?

4

2 回答 2

5

这有效:

return Promise.resolve(FirebaseRef.child('userId1').once('value').then(function (snap) {
          return snap.val();
        }));

我仍然不确定这是否是我将 Firebase 与 FeathersJs 集成的最佳方式

于 2016-09-06T05:12:55.550 回答
2

Firebase 和 Feathers 实时之间的主要架构区别在于 Firebase 使用键值观察,而 Feathers 使用您在遵循REST 架构时自动获得的实时事件。第一步是将通用 Firebase 操作包装在它自己的自定义服务中,看起来像这样(未经测试):

class FirebaseService {
  constructor(name) {
    this.name = name;
  }

  ref(id) {
    return firebase.database().ref(`${this.name}/${id}`);
  }

  async get(id) {
    const ref = await this.ref(id).once('value');

    return ref.value;
  }

  async create(data) {
    const id = firebase.database().ref().child(`/${this.name}`).push().key;

    return this.update(id, data);
  }

  async update(id, data) {
    this.ref(id).set(data);

    return this.get(id);
  }

  async remove(id) {
    const deletedEntry = await this.get(id);

    this.ref(id).remove();

    return deletedEntry;
  }
}

这将为您提供一个 Feathers Firebase 集成,只要您通过 Feathers API 使用它,它就会自动发送实时事件。如果 Firebase 在服务之外得到更新并且您想通知客户端,您可以使用值侦听器并调用update和/或patch服务器上的服务:

const id = <some user id>;
firebase.database().ref(`/users/${id}`).on('value', snapshot => {
  app.service('users').update(id, snapshot.value);
});
于 2018-10-27T16:25:08.667 回答