DEV Community

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
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
squiff88 profile image
Squiff88

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

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

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

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