Javascript 在FeatherJS应用程序之外编辑数据库时如何更新客户端

Javascript 在FeatherJS应用程序之外编辑数据库时如何更新客户端,javascript,feathersjs,Javascript,Feathersjs,我有两个服务器端应用程序编辑同一个数据库 一台服务器是feathersjs应用程序,另一台是简单的nodejs应用程序。前端应用程序通过feathersjs客户端连接到feathersjs应用程序 当nodejs应用程序编辑数据库时,我如何更新连接到FeatherJS应用程序的客户端?目前,featherjs应用程序之外所做的任何更改都不会反映在featherjs客户端上 我是否可以以某种方式触发已修补的事件并强制客户端下拉更新的数据 正如您所指出的,只有通过Feathers API所做的更改才

我有两个服务器端应用程序编辑同一个数据库

一台服务器是feathersjs应用程序,另一台是简单的nodejs应用程序。前端应用程序通过feathersjs客户端连接到feathersjs应用程序

当nodejs应用程序编辑数据库时,我如何更新连接到FeatherJS应用程序的客户端?目前,featherjs应用程序之外所做的任何更改都不会反映在featherjs客户端上


我是否可以以某种方式触发已修补的事件并强制客户端下拉更新的数据

正如您所指出的,只有通过Feathers API所做的更改才会反映出来,但在服务器上,您始终可以通过以下方式发出所需的事件:

此处需要注意的事项也在以下章节中讨论:

不会有关于方法调用的用户或任何其他信息 数据不会通过任何服务挂钩运行
正如您所指出的,只有通过Feathers API所做的更改才会反映出来,但在服务器上,您始终可以通过以下方式发出所需的事件:

此处需要注意的事项也在以下章节中讨论:

不会有关于方法调用的用户或任何其他信息 数据不会通过任何服务挂钩运行
如果您将mongodb与WiredTiger storageEngine一起使用,则可以使用collection.watch功能,并在feathers应用程序中添加一个监视器,如下所示

   //src/services/monitor.js 
   module.exports = function(app){
      //get mongo client
      const mongoClient = app.get('mongoClient');
      //connect db
      mongoClient.then(db => {

        //collection to watch
        const collection = db.collection('your_collection_name')

        //watch collection
        const changeStream = collection.watch({ fullDocument: 'updateLookup' });

        //on some data changed
        changeStream.on('change', data => {
          console.log ( 'something is changed in your collection' , data )
          //your service.emit
        });
      })
    }
然后我在/src/services/index.js中添加了这个简单的监视器,可能不是正确的方法,但它可以工作

//src/services/index.js
...
const monitor = require('./monitor.js');
module.exports = function (app) {
   ...
   app.configure(monitor);
   ...
}; 
每次更改集合时返回的数据

{ _id:
   { _data:
      '825C7F03230000001C29295A100490DEF2C65812410FABF0DE46F9C89D7246645F696400645C1AC097B189CBD5D4A24D330004' },
  operationType: 'replace',
  clusterTime:
   Timestamp { _bsontype: 'Timestamp', low_: 28, high_: 1551827747 },
  fullDocument:
   { _id: 5c1ac097b189cbd5d4a24d33,
     id: '12',
     language: 'it-IT',
     category: 'some data',
     slug: '',
     description: 'some data',
     src:'',
     color: 'card',
     status: true,
     home: true,
     order_int: '3',
     visual: 'card' },
  ns: { db: 'mydb', coll: 'mycollection' },
  documentKey: { _id: 5c1ac097b189cbd5d4a24d33 } }

此处的更多信息

如果您将mongodb与WiredTiger storageEngine结合使用,您可以使用collection.watch功能,并在feathers应用程序中添加一个类似的监视器

   //src/services/monitor.js 
   module.exports = function(app){
      //get mongo client
      const mongoClient = app.get('mongoClient');
      //connect db
      mongoClient.then(db => {

        //collection to watch
        const collection = db.collection('your_collection_name')

        //watch collection
        const changeStream = collection.watch({ fullDocument: 'updateLookup' });

        //on some data changed
        changeStream.on('change', data => {
          console.log ( 'something is changed in your collection' , data )
          //your service.emit
        });
      })
    }
然后我在/src/services/index.js中添加了这个简单的监视器,可能不是正确的方法,但它可以工作

//src/services/index.js
...
const monitor = require('./monitor.js');
module.exports = function (app) {
   ...
   app.configure(monitor);
   ...
}; 
每次更改集合时返回的数据

{ _id:
   { _data:
      '825C7F03230000001C29295A100490DEF2C65812410FABF0DE46F9C89D7246645F696400645C1AC097B189CBD5D4A24D330004' },
  operationType: 'replace',
  clusterTime:
   Timestamp { _bsontype: 'Timestamp', low_: 28, high_: 1551827747 },
  fullDocument:
   { _id: 5c1ac097b189cbd5d4a24d33,
     id: '12',
     language: 'it-IT',
     category: 'some data',
     slug: '',
     description: 'some data',
     src:'',
     color: 'card',
     status: true,
     home: true,
     order_int: '3',
     visual: 'card' },
  ns: { db: 'mydb', coll: 'mycollection' },
  documentKey: { _id: 5c1ac097b189cbd5d4a24d33 } }
更多信息请点击这里