Get started
- Install typesql-cli as a dev dependency
npm install --save-dev typesql-cli
- Add the typesql.json configuration file in project root folder. You can generate an template with cli command
npx typesql init
.
{ "databaseUri": "mysql://root:password@localhost/mydb", "sqlDir": "./sqls", "target": "node", "includeCrudTables": []}
- Write your queries in the folder specified in the configuration file. You can also use the cli to scaffold the queries.
select-products.sqlinsert-product.sqlupdate-product.sql
- Then run
typesql compile --watch
to start typesql in watch mode. After that you will have one Typescript file for each query file.
select-products.sqlselect-products.tsinsert-product.sqlinsert-product.tsupdate-product.sqlupdate-product.ts
- Now you can import and use the generated code.
const products = await selectProducts(...
const updateResult = await updateProduct(...