MeteorJS subscription mechanism - node.js

I have a question about subscription/publication in meteor.
When you subscribe to a publication in meteor, is the client database updated or only a copy at one moment of the server database?
In fact, will Meteor update the local database after a subscription, or will it just be an image of the server's database at a T moment where T is the moment of subscription ? And can I get the last data when I call Collection.find() on client side ?

The quick answer is that as long as your subscription is still active, Meteor will keep the client in sync with the server per the rule you defined in the publish() method. It is not a copy at time T, it starts at time T and is amended as the server collection changes.
For example:
/server/publish/people.js:
Meteor.publish('people', function() {
return People.find();
}
/client/app.js:
Meteor.subscribe('people');
The publish()'s 'People.find()' will be monitored and any changes to the query will be replicated on the client. If you have reactive queries (People.find() in a template helper for example) on the client, those will be re-executed automatically and the template updated (see Tracker).
Good reference for you: https://www.discovermeteor.com/blog/understanding-meteor-publications-and-subscriptions/

Related

Monitor updates on a MongoDB collection (Post update hook on db layer)

One of my collection is getting updated by different micro-services. And the updates are being done by either monk or mongoose libs.
So I wanted to monitor updates done on this collection. Also I do not have access to those services so I cannot add post update hooks on each services but I have access to db where I can do whatever I want. I am open to use any ORMs or libs.
So basically - if any update query is triggered from any of the
services, my monitoring (hook or something) code should be triggered.
Thanks.
You can use mongodb Change Streams to listen for data changes check out a sample app https://gist.github.com/riodw/74a839ab6964bceda8ff799d3ad33442
Mongodb docs https://docs.mongodb.com/v3.6/changeStreams/#change-streams
You can try the change stream from mongoose :
https://mongoosejs.com/docs/models.html#change-streams
An example i just tried and it works :
let Congress = require("../models/congress");
// change stream
Congress.watch().
on('change', data => console.log('change stream',data));
//

Syncing Azure Easy Table with WHERE clause

I'm developing a Xamarin.Forms app which uses an Azure app service with SQL database linked through EasyTables. I've run the samples and successfully tested querying tables etc on the server and enabled offline sync so as a localdb is created.
I've created the store, defined the table & sync'd it, however I want to be able to query it somehow with a where clause - is that possible? Can I add a where clause to the client.GetSyncTable line?
var store = new MobileServiceSQLiteStore("localstore.db");
store.DefineTable<Journey_Stages>();
client.SyncContext.InitializeAsync(store);
tbl_Stages = client.GetSyncTable<Journey_Stages>();
Some of the tables I'm pulling down will grow over time & are linked to individual user profiles, so I only want data which belongs to that user and I don't want to be bringing down masses of data each time, preferably let the server handle that and only bring down what I need on a user by user basis.
Thanks,
Steve
You should add this filtering logic on the server side, so that each user's data isn't exposed to all your other users. See for example this sample if you are using the Node.js backend -- line 17 adds a WHERE clause for the table read query. If you have the .Net backend, similar logic would go in your table controller.
// Configure specific code when the client does a request
// READ - only return records belonging to the authenticated user
table.read(function (context) {
context.query.where({ userId: context.user.id });
return context.execute();
});

How to display values from database in real time without refreshing the webpage

I'm working on an Angular2 project (Resource Management) which requires data from the database (mongoDB) in real time. The basic function of the application is to drag a resource from bench and drop it to a project. After which a button is clicked which saves these values to the database. The problem is once the database is hit with the values, the same is not showing up unless the page is refreshed. Is there a way where we can pull data from the database without manually refreshing the entire page or pulling the data from DB at regular intervals so that the services have the latest data. I'm attaching my code here: Resource Management (drag n drop)
Thank you!!
If you want to get the data periodically.You can use timer.
ngOnInit() {
this.timer = Observable.timer(2000,5000);
// subscribing to a observable returns a subscription object
this.sub = this.timer.subscribe(t => this.geData(t));
}
geData(t) {
console.log(t)
}
You Could use socket.io for real time update with angular2 and mongodb

Azure Mobile Service (node backend) __updatedAt property not automatically set after edit

After reading the documentation I was expecting that this field is automatically set by Azure Mobile Services. Apparently it isn't.
Should I configure something extra?
Other options that I see (to do for each table):
* add an axtra line to the node js update(item, user, request) function:
item.__updatedAt = new Date();
* create an update trigger in the database
Anybody experience with this?
Thx!
The __updatedAt column is updated by a trigger created in the underlying SQL Server database, so it should be updated any time a row is updated. Note that this requires a database operation to occur for it to be updated.

Sail.js requires server restart after running command to refresh database

From this question, Sails js using models outside web server I learned how to run a command from the terminal to update records. However, when I do this the changes don't show up until I restart the server. I'm using the sails-disk adapter and v0.9
According to the source code, the application using sails-disk adapter loads the data from file only once, when the corresponding Waterline collection is being created. After that all the updates and destroys happen in the memory, then the data is being dumped to the file, but not being re-read.
That said, what's happening in your case is that once your server is running, it doesn't matter if you are changing the DB file (.tmp/disk.db) using your CLI instance, 'cause lifted Sails server won't know about the changes until it's restarted.
Long story short, the solution is simple: use another adapter. I would suggest you to check out sails-mongo or sails-redis (though the latter is still in development), for both Mongo and Redis have data auto expiry functionality (http://docs.mongodb.org/manual/tutorial/expire-data/, http://redis.io/commands/expire). Besides, sails-disk is not production-suitable anyways, so sooner or later you would need something else.
One way to accomplish deleting "expired records" over time is by rolling your own "cron-like job" in /config/bootstrap.js. In psuedo code it would look something like this:
module.exports.bootstrap = function (cb) {
setInterval(function() { < Insert Model Delete Code here> }, 300000);
cb();
};
The downside to this approach is if it throws it will stop the server. You might also take a look at kue.

Resources