You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-9Lines changed: 28 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,25 @@ A super lightweight TypeScript types generator that respects your laziness and l
23
23
24
24
Zero runtime dependencies, just types. This is just a super thin wrapper around [sqlc](https://sqlc.dev/) and a file generator - all the real magic is in sqlc. It just makes it more convenient to use in TypeScript projects.
25
25
26
-
## Demo 🚀
26
+
## TLDR
27
+
28
+
-`pg_dump --schema-only postgres://user:password@localhost:5432/database > schema.sql` to dump your schema
29
+
- Run `npx sqlc-typescript watch` (`src/**/*.ts` is default glob and `schema.sql` is default schema file)
30
+
- Write SQL queries in your TypeScript files using the `/*sql*/` comment and `sqlc` function e.g.
31
+
32
+
```typescript
33
+
const result =awaitsqlc(/*sql*/`
34
+
SELECT customer_id, first_name, last_name
35
+
FROM customer
36
+
WHERE customer_id = @customer_id
37
+
`).exec(client, {
38
+
customer_id: 1,
39
+
});
40
+
```
41
+
42
+
-Importthegenerated`sqlc`function and get perfect types 🔥
Ifyou're like me - you just want to write SQL, ship features and not deal with heavy abstractions or spend hours reading documentation (even if it'sreallygood). That's exactly why this exists.
Unfortunately, wecan't use tagged template literals like `` sql`SELECT * FROM users` `` for proper syntax highlighting. TypeScript template literals [can'tbegeneric](https://github.com/microsoft/TypeScript/issues/33304), so we can use the `/*sql*/` comment approach instead. Your IDE or SQL plugin will still provide syntax highlighting!
144
162
145
-
### Comparison with Other Tools 🔍
163
+
### 🔍 ComparisonwithOtherTools
146
164
147
165
- [pgTyped](https://github.com/adelsz/pgtyped): Requires separate SQL files and function imports. It uses PostgreSQL wire protocol for type inference which requires a database connection and can't handle nullability well.
148
166
- [PrismaTypedSQL](https://www.prisma.io/docs/orm/prisma-client/using-raw-sql/typedsql): SQL files are separate and require function imports and it's Prisma 🫠.
149
167
- [SafeQL](https://github.com/ts-safeql/safeql): Great tool but requires ESLint and database connection for type inference.
168
+
- [Drizzle](https://orm.drizzle.team/): SQL-like a great query builder but it's not just SQL. I don't want to learn another syntax even if it's very close to SQL. I can't copy-past my queries from psql back and forth.
150
169
151
170
Thekeydifference:Weusesqlc's SQL parser instead of PostgreSQL wire protocol for type inference, which means:
0 commit comments