wsporto/typesql

`INTEGER`s are mistakenly converted to typescript `string`s

Closed this issue · 3 comments

Hi there,

I have this schema:

-- Roles
CREATE TABLE IF NOT EXISTS roles (
  id INTEGER PRIMARY KEY NOT NULL,
  name TEXT NOT NULL CHECK(LENGTH(name) <= 30)
) STRICT;

-- Users
CREATE TABLE IF NOT EXISTS users (
  id TEXT PRIMARY KEY NOT NULL CHECK(LENGTH(id) == 36),
  -- ...
  role_id INTEGER NOT NULL,
 
  -- ...
  FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE RESTRICT
) STRICT;

When I run typesql cli, it produces this type:

export type InsertIntoUsersParams = {
	id: string;
        // ...
	role_id: string;
        // ...
}

Mistakenly converting the INTEGER to a string rather than a number.

Am I doing something wrong?

This is my typesql.json file for reference:

{
    "databaseUri": "./local.db",
    "sqlDir": "./src/data/repos/",
    "client": "libsql",
    "target": "node",
    "includeCrudTables": ["*"]
}

I'm using:

  • Bun 1.1.18
  • typesql-cli 0.10.2

Hello,

I couldn't reproduce this with the provided schema.
The generated type is:

export type InsertIntoUsersParams = {
  id: string;
  role_id: number;
}

Could you run this to confirm the role_id type in the ./local.db database?

PRAGMA table_info('users'); 

I ran it and this is the output (it confirms it's an integer):

cid  name              type     notnull  dflt_value                   pk
---  ----------------  -------  -------  ---------------------------  --
0    id                TEXT     1                                     1 
13   role_id           INTEGER  1                                     0 

Another thing I have noticed is that conversion works just fine in some of the other tables. For example, I have this table:

-- Subscription Tiers
CREATE TABLE IF NOT EXISTS games (
  id INTEGER PRIMARY KEY NOT NULL,
  -- ...
  duration INTEGER NOT NULL CHECK(duration > 0),
  -- ...
) STRICT;

And this is the type produced for it:

export type InsertIntoGamesParams = {
	id?: number;
        // ...
	duration: number;
        // ...
}

Not sure what happened but I nuked the entire db and re-created it again, and everything seems to be working fine now.