Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bigint issue with tedious #24

Open
usernamewasfree opened this issue Mar 1, 2021 · 3 comments
Open

Bigint issue with tedious #24

usernamewasfree opened this issue Mar 1, 2021 · 3 comments

Comments

@usernamewasfree
Copy link

usernamewasfree commented Mar 1, 2021

I'm pretty new with node so forgive me if I'm asking in the wrong place.
After following the readme, I'm running into this error: RequestError: Validation failed for parameter '0'. Value must be between -2147483648 and 2147483647

The query it is trying to run:
query failed: SELECT "session"."id" AS "session_id", "session"."expiredAt" AS "session_expiredAt", "session"."json" AS "session_json" FROM "session" "session" WHERE "session"."expiredAt" > @0 AND "session"."id" = @1 -- PARAMETERS: [1614629900663,"2XztQAQI5icnM_us9nQvu-VramBgMxQ_"]

After debuging some I found that issue seems to be at the Request.validateParameters method for the tedious library is getting a type INT8 instead of a type BigInt.

name:'1'
output:false
precision:undefined
scale:undefined
type:{id: 127, type: 'INT8', name: 'BigInt', declaration: ƒ, writeTypeInfo: ƒ, …}
value:1614724349837

This is trying to process it as an int instead of the string that tedious needs as described here:
http://tediousjs.github.io/tedious/api-datatypes.html

Any idea on how to pass the ExpiresAt property as a string to tedious?

Code:

//indxes.ts
import { ApolloServer } from 'apollo-server-express';
import express from 'express';
import expressSession from 'express-session';
// import { Db } from 'typeorm-static';
import { createConnection } from 'typeorm';
import { Session } from './src/typeorm/entity/Session';
import { TypeormStore } from 'connect-typeorm';
import schema from './src/schema';
const sessionSecret = "keyboard cat"; //TODO: move to .env

//typeorm createConnection uses settings in ormconfig.json
createConnection().then(async (connection) => {
  const server = new ApolloServer({
    schema,
    context: req => ({
      ...req,
    })
  });

  const app = express();
  const sessionRepository = connection.getRepository(Session);
  const session = expressSession({
    name: "midas-4-auth",
    resave: false,
    saveUninitialized: false,
    store: new TypeormStore({
      cleanupLimit: 2,
      ttl: 86400,
    }).connect(sessionRepository),
    secret: sessionSecret,
  });
  app.use(session);

  server.applyMiddleware({
    app,
    cors: {credentials: true, origin: true }
  });
  app.listen({ port: 4000 });
}).catch((error) => 
  console.log(error)
);
import { ISession } from "connect-typeorm";
import { Column, Entity, Index, PrimaryColumn } from "typeorm";

@Entity()
export class Session implements ISession {
  @PrimaryColumn("varchar", { length: 255 })
  public id: string;

  @Index()
  @Column("bigint")
  public expiredAt: number;

  @Column({ type: "varchar", length: 250 })
  public json: string;
}

Use

req.session.user = { username };

Thanks

@nykula
Copy link
Contributor

nykula commented Mar 2, 2021

Hello, thanks for a detailed question. Please try using the TypeORM transformer property according to the Entities documentation:

  @Index()
  @Column("bigint", {
    transformer: { 
      from: (db: string) => Number(db),
      to: (js: number) => String(js)
    }
  })
  public expiredAt: number;

I don't currently have an SQL Server instance to test this, so if you find you need to make any edit for it to work, please share your changes.

@usernamewasfree
Copy link
Author

Thank you for the prompt response. The transformer property allowed for the session to be inserted to the database successfully; however, when reading the session on a second call the error is still thrown. I will continue to work with it to see if there is something obvious that I am missing. If I don't find success I will create a minimum repo that reproduces the error and post it here.

@usernamewasfree
Copy link
Author

I created a repo to demonstrate the issue. Let me know if there are any issues.
https://github.com/usernamewasfree/connect-typeorm-tedious-bug-demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants