Git Product home page Git Product logo

Comments (6)

dhensby avatar dhensby commented on June 7, 2024 1

The right way to do it is to have a transformer that will map the internal DB type to a type that is useful in your frontend. You don't want this to be coupled in this way at all.

If I wanted to do this (which isn't how I'd approach it, I'd just use the native JS types in the frontend, no need for the backend to dictate types), I'd use a translation layer to map the internal DB types to a type the frontend will understand (ie: your API schema). A simple switch statement would do.

const colTypes = result.columns[0].map((col) => {
  switch (col.type) {
    case sql.Text:
      return 'string';
    default:
      return 'unknown';
  }
});

from node-mssql.

MarkFarmiloe avatar MarkFarmiloe commented on June 7, 2024

The undefined fields probably shouldn't be getting returned either.
All part of the same problem, I expect.

from node-mssql.

dhensby avatar dhensby commented on June 7, 2024

This is all working as intended.

Do you have any specific questions as I'm not really sure what solution you're looking for here?

from node-mssql.

MarkFarmiloe avatar MarkFarmiloe commented on June 7, 2024

Displaying my lack of expertise with JS I expect.
However I was using res.send(JSON.stringify(result)) to send this back to the client, which does not stringify the type field.
My workround was to replace the type value with the type.declaration value which gives me the JS type.
Not sure how I would get the '[sql.VarChar]' value though.

from node-mssql.

dhensby avatar dhensby commented on June 7, 2024

The result certainly is not intended to be exposed to a front end in that way. The object doesn't implement a toJSON() method and it could contain all sorts of things that you wouldn't want exposed publicly.

It's never good practice just to dump backend data to the frontend without first sanitising it first. You'd want to whitelist and format the data in a predictable way for the frontend otherwise you aren't in control of your API contract and you're delegating that responsibility to this library instead.

from node-mssql.

MarkFarmiloe avatar MarkFarmiloe commented on June 7, 2024

I'm writing a generic read-only data viewer which will build the table based on column info, rows (returned and calculated). It will need the datatype to allow column searches to be smart.
I will limit what is sent eventually, but I'm in dev mode at the moment 😁
Am I right in thinking that I need

result.columns[0][0].type[Symbol.for('nodejs.util.inspect.custom')]()

to get the '[sql.VarChar]' info out of the type?

from node-mssql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.