NodeJS Emit when a row is added to database table - mysql

I am quite new to NodeJS but I've written a few apps (like a chat, real time page updater etc) And I've used mysql to read from my database and emit() information to my webpage, but how can I have nodeJS watch a database table and emit() whenever a row is added?
I have no idea where to start and google didn't produce much results. However there must be an include I can require() that will watch the database in someway.

The answer really isn't a nodejs question, but a mysql question. If mysql itself can tell you ( via an event or log ) that a row was added, nodejs could read and consume that data. Unfortunately, that doesn't seem to be possible, based on this answer. The answer suggests that the only thing you can do with mysql is to poll for new rows.
IF you're in control of inserting the data rows from nodejs itself, there shouldn't be any problem emitting those events after you get the confirmation it wrote, but I do not have enough information on your project to know what constraints you have.
UPDATE: Nice little npm package https://www.npmjs.com/package/live-sql seems to solve your problem I hope

Related

Query large database and serve line by line

I ask this question, because I don't even have a clue what to google for. I have a MariaDB database which I access through node.js' mysql module. I write my code in TS.
My problem is, that the database I try to access will collect millions of datasets over time, and querying it might take awhile. I want would want to find a way to parse the database and serve one dataset whenever it is found instead of querying the whole database first and then sending an accumulated result.
Do you have any clue how I can solve this or what to google / YouTube search for?

Nhibernate id generator or MySQL auto-increment

I am just starting my adventure with writing an app that has to work with database. I've started by designing my database in MySQL Workbench, exported this project to the server and now I want to try writing some code, that will be able to talk with this server.
I've decided to try using NHibernate, but I'am stuck at writing mappings for the Id/Primary key property/column. I've read, that I must specify the generator, but does that mean, that I have to remove auto-increment from this column in a database?
If answer to previous question is negative, then which NH generator will work correctly with server driven auto-increment generator. If it's positive, then which generator is preferable? Or maybe, even if NH can work with server's AI, you still recommend to remove AI from columns and use client based generator (so again, which?).
Ok, since there was no direct answer to this question (there was comment that gave me the answer instead) and I want to close it, I'll write one.
My problem here was the understanding of the functionality name - GENERATOR. When you have a server based generator (auto increment), why do you need another in your app? That was the source of my mistake. Actually, this in app generator does not have to really generate anything, it can fetch data from database, but still, for this app, it will have no difference.
So, TL;DR
As #RadimKöhler wrote in his comment to my question, both native and identity work well with autoincrement/identity (or whatever name is used by your database engine) column in database.
now, time for quote:
NOTE: I for years live with identity.. but if I could choose, sequence or HILO would be my choice. It does not require real INSERT to let DB generate the key...
... think about it :) I haven't gone this way, because I am writing a small app which will be used by single user most of the time. Usage of an actual in-app generator let's you work with data kept in memory, which can be flushed in one go to the database.

"unconcatenating" a field and pivoting mysql or talend

I am new to mysql and databases in general. I've come across a problem that I can't seem to find a solution too. I'm not sure even how to word the scenario.
Basically I have a concatenated field that I need to take "explode" and make each value its of that field its own row. I'm pulling this data from a daily feed, so this needs to be automated as either a trigger, stored procedure, or scheduled job in Talend. Here is a simplified version of the situation. Please see the "current file format" and the "desired outcome" pics. Sorry- this is my first post here! Any help would be appreciated!
I am using a mysql database. Currently the file is coming in via csv export, but I will be connecting via Talend Open Studio to sync the data. See the links below for and example. Here is a sample.
A precise answer is difficult without seeing your code, and the sample link returns 404 for me. You should be able to do this within Talend. Try using Talend’s tExtractDelimitedFields or tExtractRegexFields commands to achieve the result you want.

Trigger node.js when changes made in apache mysql

I'm building a simple commenting system using node and i need to configure this in a PHP project running in Apache server. So, i need to trigger node.js when some changes made in MySQL database table present in the Apache server. So, i need to know whether it is possible to do this in a Apache server? If so, then how to do that? Any idea or suggestions on this are greatly welcome. Please help...
I guess there are few options you could take, but I don't think you can get some sort of triggered action from within MySQL or Apache. IMHO, you these are the approaches you can take:
you can expose a HTTP API from node and every time you need to notify the node app, you could simply insert the data into MySQL using PHP and then issue a simple GET request to trigger node.
You could use some sort of queuing system (rabbitmq, redis, etc.) to manage the messages to and from the two application, hence orchestrating the flow of the data between the two apps (and later the db).
you could poll the database from node and check for new rows to be available. This is fairly inefficient and quite tricky, but it sounds more close to what you want.

How do you write a Rails cron job Whenever gem?

The whenever gem is installed. I see all kinds of cron and whenever tutorials about scheduling tasks with minutes, hours, days etc. I watched the railscast video on cron/whenever.
But I've yet to find any examples about how to write a job itself, other than rake tasks.
I want to schedule a task that checks the database for changes. The idea is that the database will have tags that tell you which particular row has changed. Whenever should poll the database periodically to check for these changes. Then it hopefully can push, or let the client know it needs to update the page dynamically using ajax.
If I were doing this manually, I'd use commands like:
rails dbconsole
select blah from blah;
Is there a way to write mysql commands in whenever? Is this the correct/best way to poll the database for changes?
I know there are ways to poll a database from mysql itself, but I've been specifically told to do it from the rails side.
I'm a newbie to all of these technologies (Rails, databases, ajax) so that's probably why the answer isn't clear to me.
On the client end, I have buttons that use jquery to add/delete/change row data, just to assure myself I know how to change things in the table once I can get stuff from the database. Those buttons will eventually be removed.
Right now, the page uses ajax to refresh the entire html table. But they would like just a row refresh/update through ajax.
Look at the RailsCast for cron/whenever again. You'll notice an example line of code like this:
runner "MyModel.some_process"
The code in the strings is evaluated and run. So whatever you want whenever to run, just write that code yourself and have a way for it to be called.
So maybe you create a class named DatabaseWatcher and store it in lib which has a class level method named .run, you'd do the following:
runner "DatabaseWatcher.run"
And that's it. In your .run method is where you'd put your logic. As for how to actually write that code, that depends on your requirements. Are you looking for if the updated_at time is within 1 minute of now? Do you store a time when you last checked the DB, and then you can see if the updated_at time is greater than that? Do you have a table that stores every time the model is changed? That all depends on you.