0.8.0-alpha.9 • Published 10 days ago

typesql-cli v0.8.0-alpha.9

Weekly downloads
1
License
MIT
Repository
-
Last release
10 days ago

TypeSQL: An alternative to access MySQL databases without an ORM. Write your queries in raw SQL and TypeSQL will generate a type-safe API to execute the queries.

Example

Having the following query in select-products.sql file.

SELECT
  id,
  product_name,
  list_price
FROM products
WHERE discontinued = 0
  AND list_price BETWEEN :minPrice AND :maxPrice

TypeSQL will generate the types and function in the file select-products.ts. Then you can import the generate code and execute as following:

deno syntax:

npm.io

Some features:

  • Do not restrict the use of SQL You dont need to learn any new query language, you can use SQL with all its power and expressiveness.

  • Infer parameters and columns types. SELECT DATEDIFF(:date1, :date2) as days_stayed will resolve the date1 and date2 parameters to the type Date and the function return type as number.

  • Infer parameter and column nullability. The nullable database column email will generate a nullable field for the query SELECT email FROM mytable, but will generate a non-nullable field for the query SELECT email FROM mytable WHERE email is not null;

  • Infer the query return type (single row vs multiple rows). If the id is a primary key or unique key, then function for the query SELECT * FROM Books where id = :id will return Book|null, instead of Book[]. The same is true for filters with LIMIT 1;

  • Allow the use of dynamic ORDER BY with auto-completion and compile-time verification. See here.

Usage

  1. npm install -g typesql-cli

  2. Add the typesql.json configuration file in project root folder. You can generate an template with cli command typesql init.

{
  "databaseUri": "mysql://root:password@localhost/mydb",
  "sqlDir": "./sqls",
  "target": "node"
}
  1. Write your queries in the folder specified in the configuration file. You can also use the cli to scaffold the queries.
sqls\
    select-products.sql
    insert-product.sql
    update-product.sql
  1. Then run typesql compile --watch to start typesql in watch mode. After that you will have one Typescript file for each query file.
sqls\
    select-products.sql
    select-products.ts
    insert-product.sql
    insert-product.ts
    update-product.sql
    update-product.ts
  1. Now you can import and use the generated code.
const products = await selectProducts(...

const updateResult = await updateProduct(...

Examples

Query scaffolding

INSERT

IN/NOT IN Clause

MySQL FUNCTIONS

ORDER BY and LIMIT clauses

LIKE

Nested Query Result

Project status

WARNING: This is a WIP experimental project. It is under active development and its API might change.

Issues reports and feature requests are welcome.

0.8.0-alpha.9

10 days ago

0.8.0-alpha.8

12 days ago

0.8.0-alpha.7

12 days ago

0.8.0-alpha.6

14 days ago

0.8.0-alpha.5

19 days ago

0.8.0-alpha.4

20 days ago

0.8.0-alpha.3

20 days ago

0.8.0-alpha.2

22 days ago

0.8.0-alpha.1

22 days ago

0.7.4

1 month ago

0.7.3

1 month ago

0.7.2

1 month ago

0.7.1

2 months ago

0.7.0

2 months ago

0.6.20

2 months ago

0.6.19

2 months ago

0.6.18

3 months ago

0.6.17

4 months ago

0.6.12

6 months ago

0.6.14

5 months ago

0.6.13

5 months ago

0.6.16

5 months ago

0.6.15

5 months ago

0.6.7

6 months ago

0.6.6

6 months ago

0.6.9

6 months ago

0.6.8

6 months ago

0.6.10

6 months ago

0.6.11

6 months ago

0.6.3

6 months ago

0.6.2

6 months ago

0.6.5

6 months ago

0.6.4

6 months ago

0.6.1

7 months ago

0.5.10

9 months ago

0.5.11

9 months ago

0.5.8

9 months ago

0.4.9

10 months ago

0.5.7

10 months ago

0.5.9

9 months ago

0.5.18

7 months ago

0.5.17

8 months ago

0.5.15

8 months ago

0.5.12

8 months ago

0.5.13

8 months ago

0.4.10

10 months ago

0.4.11

10 months ago

0.5.4

10 months ago

0.5.3

10 months ago

0.5.6

10 months ago

0.5.5

10 months ago

0.5.0

10 months ago

0.5.2

10 months ago

0.5.1

10 months ago

0.4.8

1 year ago

0.4.5

3 years ago

0.4.7

2 years ago

0.4.6

3 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.2.0

4 years ago

0.1.8

4 years ago

0.1.9

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago