rmp135 / sql-ts

Generate TypeScript interfaces from a SQL database.
MIT License
466 stars 64 forks source link

Support data-type boolean #149

Open renne opened 2 months ago

renne commented 2 months ago

Please support data-type boolean in sql-ts.

rmp135 commented 1 month ago

It should already support boolean types using the TypeMap.

Do you have a database adapter and column definition I can test with, it might be the internal type name needs adding to the map.

renne commented 8 hours ago

The database adapter is MariaDB Node.js connector. Example:

CREATE TABLE IF NOT EXISTS `users` (
        `created`                       TIMESTAMP        NOT NULL  DEFAULT CURRENT_TIMESTAMP  COMMENT 'Creation date',
        `email`                         TEXT(256) UNIQUE NOT NULL  DEFAULT ''      COMMENT 'E-mail address',
        `institution`                   TEXT(128)        NOT NULL  DEFAULT ''      COMMENT 'Institution',
        `isEditor`                      BOOLEAN          NOT NULL  DEFAULT FALSE   COMMENT 'Editor status',
        `lastChanged`                   TIMESTAMP        NOT NULL  DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP  COMMENT 'Last change date',
        `name`                          TEXT(128)        NOT NULL  DEFAULT ''      COMMENT 'Username (prenames surname)',
        `phone`                         TEXT(16)         NOT NULL  DEFAULT ''      COMMENT 'Phone number',
        `userID`                        UUID             NOT NULL  DEFAULT UUID()  COMMENT 'UserID',
        PRIMARY KEY (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci  COMMENT 'List of users';

isEditor becomes tinyint(1) in the MariaDB DBMS:

MariaDB [backend]> show columns from users;
+-------------+------------+------+-----+---------------------+-------------------------------+
| Field       | Type       | Null | Key | Default             | Extra                         |
+-------------+------------+------+-----+---------------------+-------------------------------+
| created     | timestamp  | NO   |     | current_timestamp() |                               |
| email       | text       | NO   | UNI | ''                  |                               |
| institution | text       | NO   |     | ''                  |                               |
| isEditor    | tinyint(1) | NO   |     | 0                   |                               |
| lastChanged | timestamp  | NO   |     | current_timestamp() | on update current_timestamp() |
| name        | text       | NO   |     | ''                  |                               |
| phone       | tinytext   | NO   |     | ''                  |                               |
| userID      | uuid       | NO   | PRI | uuid()              |                               |
+-------------+------------+------+-----+---------------------+-------------------------------+
8 rows in set (0.003 sec)

SQL-TS interpretes it as data-type number:

/* Editor status */
  'isEditor'?: number;