rmp135 / sql-ts

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

Keep interface field order the same as it's in Database #127

Closed zydmayday closed 1 year ago

zydmayday commented 1 year ago

Hi thanks for your tool, really amazing!

I have an issue that the fields in generated interface are sorted by alphabet order.

Here is an example to reproduce the issue. We are using Aurora(MySQL 5.7)

> npx @rmp135/sql-ts -c mysql2.json

content in mysql2.json is below

{
  "client": "mysql2",
  "connection": {
    "host": "host-name",
    "user": "db user",
    "password": "db password",
    "database": "database name"
  },
  "folder": "generateTypes",
  "filename": "table.d",
  "interfaceNameFormat": "${table}Table",
  "tableNameCasing": "pascal",
  "columnNameCasing": "lower",
  "globalOptionality": "required"
}

The generated interface fields are in alphabet order. For example

export interface CciOrganizationInfoTable {
  nd_org_code: string;
  org_code: string;
  org_level: string;
  org_name: string;
  upper_org_code: string;
  valid_date: string;
}

where the ddl of this table is

CREATE TABLE `cci_organization_info` (
  `org_code` varchar(8) NOT NULL,
  `valid_date` varchar(8) NOT NULL,
  `org_name` text NOT NULL,
  `upper_org_code` varchar(8) NOT NULL DEFAULT '',
  `org_level` varchar(1) NOT NULL DEFAULT '',
  `nd_org_code` varchar(3) NOT NULL DEFAULT '',
  PRIMARY KEY (`org_code`,`valid_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
rmp135 commented 1 year ago

Added an option in 1.18.0 to specify "columnSortOrder". Can be "alphapetical" or "source" to retain the order from the query.