DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Squiff88
Squiff88

Posted on

Need a little help here.

I've been wondering how to setup my Rest API with node express , to retrieve data from MS SQL database.
From my research so far , i see that a popular approach is to use tedious and mssql packs for node , to establish a connection between the API and the database.
Can you suggest any less painful and easy to use approach ?
I don't really have much experience consuming data from ms sql via node ,
so i will appreciate any help :)

Top comments (8)

Collapse
 
pojntfx profile image
Felicitas Pojtinger

I have no idea why one would use the proprietary Microsoft SQL server, but in case you really need to use it, consider using TypeOrm - a very simple and straight forward ORM.

TypeOrm Git Repo

As for the API server, I always recommend using GraphQL nowadays:

GraphQL Yoga Server Git Repo

If you however still wanna use Rest, check out LoopBack:

Loopback Website

Let me know if you have more questions, and if you are interested in a rapid MongoDB + GraphQL workflow, you could try out my little @libresat/service framework - see the Git Repo for more info, it's really easy to use!

Collapse
 
squiff88 profile image
Squiff88 Author

Thanks for sharing this info with me Felix.
I will definitely look up into TypeOrm.
I am kinda stuck with MS SQL and Node , as its not up to
me to choose the technology stack but thanks
again for you suggestions.

Collapse
 
adam_cyclones profile image
Adam Crockett

Wait typeorm is Typescript (excellent suggestion if using Typescript) but perhaps the OP has not mentioned ts in which case bookshelf or sqlize are good ORM to try. Or if you want there are drivers to help you write raw queries. Coming from an MS stack I would highly recommend learning about Typescript for the future.

Collapse
 
louy2 profile image
Yufan Lou • Edited on

This is old but just putting this out there:

If you don't want to deal with ORM or GraphQL, and just need a quick start, then just do

npm i tedious sequelize sql-template-string

tedious is the low-level library for connecting to MSSQL, while sequelize offers a nice interface and supports sql-template-string.

You can then just do this:

const Sequelize = require('sequelize');
const sql = require('sql-template-string');
const sequelize = new Sequelize('database', 'username', 'password', {
  dialect: 'mssql',
  dialectOptions: {
    options: {
      useUTC: false,
      dateFirst: 1,
    }
  }
})

async function main() {
  const [results, metadata] = await sequelize.query(sql`SELECT 1`);
}

main();
Collapse
 
bgadrian profile image
Adrian B.G.

Most of the new REST APIs nowdays are generated with Open API (old swagger), it can generate even the JS handlers (for server and client) for you and the documentation from a simple YAML config file.

As for the node-ms sql connection I have no idea.

Collapse
 
squiff88 profile image
Squiff88 Author

Great, tnx for the heads up.
But again my struggle here cannot be settled via
changing the stack , i really need an easy to use and configure
pack or tool to retrieve data with ms_sql via node(express).

Collapse
 
bgadrian profile image
Adrian B.G.

Read more about Open API, it is a standard, it can be implemented with any stack, is just a theory, a common practice.

Thread Thread
 
squiff88 profile image
Squiff88 Author

I am surely going to read the docs.
I have limited time to decide from which direction
to tackle this problem.
Appreciate your help.

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. πŸ›