Is it possible to write a webservice(REST API) with meteor on top of a SQL server database?

@robfallows thank you for your time i will check everything out and build a simple Rest api to test things out

1 Like

@robfallows hi again sorry for the bother, just a little precision about the rest api.When you do so at your job, the workflow is:
1/ using sequelize to connect to mssql and retrieve the data
2/link then the data to mongodb collections
3/ make your endpoints available through method and publication like in simple-rest…
Is that it or you can skip step two and directly go to step 3 ?

thank you

Some of our data is copied to MongoDB (where we want our application to respond reactively), but most queries are SQL to endpoint only (depending on the functionality these may be any combination of REST, publication, method).

@robfallows Ok i finally get it …for this web service im mostly interested in SQL to endpoint…is it doable with simple:rest package?

The 3rd party that will be accessing the api will have his app built on the telephone network provider…so maybe ,i dont know yet but i think it will be SOAP instead of HTTP any ways i think the procedure must be the same.

Mostly, SOAP is sent over HTTP - but it’s not a requirement - any suitable transport layer can be used. It might be prudent to find out what the 3rd party expects, since we’re talking HTTP here.

@robfallows Exactly i just sent a mail to my manager asking about clarifying which transport protocol would be used or preferred by the 3rd party.Sincerely you have been a great help guiding me throughout all this stuff.My last question to you (at least for today :smiley: ) about the SQL to endpoint direct connection,is this possible with any of those meteor packages (simple:rest, restivus) ?

There is not any “magic” direct connection - but yes, it’s possible to write a REST endpoint in Meteor which exposes data/actions on a SQL backend database using one of those packages.

You just ask yourself “how would I do that with a Meteor method?” - because that’s all you really have to do.

@robfallows You mister deserve all my respect…You really are one of a kind.Most people would just leave one blurry answer and move on leaving me the asker with new questions and more clueless than at the beginning.You took time to guide me through all the steps and now i really have a clear idea of what ive got to do.
1-check between simple:rest and restivus which one i feel comfortable woth
2-read sequelize docs for the connection and requesting the datas
3-quickly review the REST rules in general
4-implement all of that asking myself how meteor method should return my sql server datas
5-produce a quick local demo without much security to test it all out

i am good to go.Thanks a bunch

1 Like

Thanks for kind words.

There is one more thing to note. The sequelize api works with promises - which is fine in Meteor - but your code will need to be written with this in mind.

@robfallows good to know.ill keep that in mind and get used to write them…

@zjjt, besides @robfallows always helpful responses (he has also helped me with another question I had) , I’d strongly suggest you opted for an HTTP (RESTfull) API rather than a SOAP one, if possible at all.

I work in an API management company (that supports both REST and SOAP API’s, so, no bias here) with hundreds of customers using APIS and definitely there has to be a very good reason to discard HTTP for SOAP.

@josegorchs,Definitely…i plan to demo it to my boss using http for REST but i dont know what that 3rd party’s is using as a technologie.i do hope they do their stuff with http…Thanks

@robfallows.Hi i hope you do fine…i still couldnt figure out how to use both technologies together(sorry)…can you help me with a sample code about how i could return sequelize queries inside my publications for use with simple:rest?

Meteor.publish("sqltable", function (index) { return sqlRecord(index);//this function should send a query via sequelize and return the row for that index }, { url: "sqltable/:0", httpMethod: "post" });

function sqlRecord(id){ return SQL.find(id).toJson()// i assume this is just an example }

thank you.

Publications are going to be trickier than methods, because publications return a cursor, not a value. If you have not written publications using the api before (added, changed, removed, ready) I strongly recommend sticking with methods. There are a lot of moving parts you will need to keep track of if you want a publication to behave as seamlessly as minimongo does. Also, there are no (Meteor) solutions for reactive (livedata) MSSQL queries.

Here’s a cut-down example using a client call with a server method:

On the client:

Meteor.call('dbqry', (err, res) => {
  if (err) throw err;
  console.log(res[0].count); // See notes below
});

On the server:

import { Meteor } from 'meteor/meteor';
import Sequelize from 'sequelize';

Meteor.startup(() => {
  const sequelize = new Sequelize('mssql:/user:password@host:1422/database');

  Meteor.methods({
    dbqry() {
      return sequelize.query("SELECT COUNT(*) AS [count] FROM [users]", { type: sequelize.QueryTypes.SELECT});
    }
  });
});

Notes:

  1. For this simple example, I’ve taken advantage of being able to return a Promise directly from a method. Normally, you would probably need to use a sequence of await operations inside an async function.
  2. The query returns an array of objects - in this case it’s an array of 1 element containing the count: [{count:1234}].

@robfallows…Now i have a clear picture of the whole process…thank you …am about to cry :joy: . I should then strictly stick to methods with mssql.while trying things out this weekend i used publications with simple:rest it worked but only for mongo…
One more thing i have a problem with authentification…how should i handle it?
mine should be custom and not rely on meteor.user i think.this 3rd party is a bank that will send messages through the api that payment has been done and create an account in our system.i think for that i need to authenticate the bank…

Thanks a Looooot (if only the O’s could really express my gratitude)

@serkandurusoy has posted an answer (which may help) to a similar question here:

If that’s not answering your question you’ll have to be more specific. Writing custom authentication is hard to get right - and if a bank is involved it will need to be demonstrably secure.

1 Like

@robfallows. Indeed…ill describe the flow of the data… the end user via a mobile money platform apply to create an account with us by entering his pin code for his mobile money account and the amount he wants to put in.this payload is then sent over to the bank webservice(which is that my rest api will interact with).the bank takes care of everything payment related and just sends us a response saying the client has paid and it also provides us with his name,telephoneno and amount EX:
{ name:'xxxx', telephone:'+0000000', amount:'100000' paymentDone:true }.

Upon reception of the paymentDone flag through the api it should trigger an account creation with the data.Once the account is created i return a code 200 to the bank which in turns return a "Success message "to the end user through the mobile money platform.Other routes for that webservice are:

-allowing the end user to check his account status(active,suspended,inactive)
-top up his account
-withdraw money from his account

From what i could gather in the document ive been given.The bank shall handle mostly everything money related.I will only receive their ok to let them through the api to create stuff of request stuff.

Our sql backend is as i said above MSSQL.

Here is the whole story.

@robfallows…i could do the above using what you told me…simple:rest and the other packages if i were dealing with mongodb…Authentication is the part i havent figured out yet…Anyways i guess if it comes down to this…ill ask if we can host a mongo database on our server that will only exist for that web service and then schedule a task that will copy its content the weekend into the main sqlServer…
Baf no this would require much more work i think.

Yeah - I think you’re in for some work there!

You may be able to get some ideas from the meteor/postgres-packages code. The MongoDB accounts code was moved over to PostgreSQL for this project.

Thanks a lot Rob…ill dive in those snippets at once…thanks again for your time

1 Like