@typesql/drizzle 0.1.5 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +22 -194
- package/dist/casing.d.ts +9 -3
- package/dist/casing.d.ts.map +1 -1
- package/dist/client.d.ts +2 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +2 -1
- package/dist/client.js.map +1 -1
- package/dist/from-drizzle.d.ts +9 -11
- package/dist/from-drizzle.d.ts.map +1 -1
- package/dist/index.d.ts +3 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/pg-type.d.ts.map +1 -1
- package/dist/pg-type.js.map +1 -1
- package/package.json +3 -3
- package/src/casing.ts +71 -16
- package/src/client.ts +36 -19
- package/src/from-drizzle.ts +198 -107
- package/src/index.ts +23 -9
- package/src/pg-type.ts +6 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @typesql/drizzle
|
|
2
2
|
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- The README is shorter: install, one example and links to the docs site, which now has the details it used to repeat.
|
|
8
|
+
|
|
9
|
+
- Updated dependencies:
|
|
10
|
+
- @typesql/core@0.6.0
|
|
11
|
+
|
|
12
|
+
## 0.2.0
|
|
13
|
+
|
|
14
|
+
### Minor Changes
|
|
15
|
+
|
|
16
|
+
- `NoResultError`, `Executor` and `TransactionalSql` are no longer re-exported (from `@typesql/drizzle` also `Sql`, `Query`, `RowType`, `ParamsType`, `DefaultTypeMap` and `TypeMap`); import them from `@typesql/core`.
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- Updated dependencies:
|
|
21
|
+
- @typesql/core@0.5.0
|
|
22
|
+
|
|
3
23
|
## 0.1.5
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,41 +1,32 @@
|
|
|
1
1
|
# @typesql/drizzle
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
SQL you write is checked against the same tables Drizzle manages.
|
|
3
|
+
Write [TypeSQL](https://www.npmjs.com/package/@typesql/core) queries against your [Drizzle](https://orm.drizzle.team) schema, and run them on your Drizzle database. The schema is converted in the type checker: nothing is generated.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
7
6
|
|
|
8
7
|
```sh
|
|
9
|
-
npm install @typesql/
|
|
8
|
+
npm install @typesql/core @typesql/drizzle drizzle-orm
|
|
10
9
|
```
|
|
11
10
|
|
|
12
|
-
`drizzle-orm`
|
|
13
|
-
databases.
|
|
11
|
+
Requires `drizzle-orm` 0.45 or later, on node-postgres or postgres.js.
|
|
14
12
|
|
|
15
13
|
## Usage
|
|
16
14
|
|
|
17
15
|
```ts
|
|
18
|
-
// schema.ts
|
|
19
|
-
import {
|
|
20
|
-
import { integer, pgEnum, pgTable, serial, text, timestamp } from "drizzle-orm/pg-core";
|
|
21
|
-
|
|
22
|
-
export const mood = pgEnum("mood", ["happy", "sad"]);
|
|
16
|
+
// schema.ts: your Drizzle schema
|
|
17
|
+
import { integer, pgTable, serial, text } from "drizzle-orm/pg-core";
|
|
23
18
|
|
|
24
19
|
export const users = pgTable("users", {
|
|
25
20
|
id: serial().primaryKey(),
|
|
26
21
|
email: text().notNull(),
|
|
27
22
|
displayName: text("display_name"),
|
|
28
|
-
mood: mood(),
|
|
29
23
|
});
|
|
30
24
|
|
|
31
25
|
export const posts = pgTable("posts", {
|
|
32
26
|
id: serial().primaryKey(),
|
|
33
27
|
authorId: integer("author_id").notNull().references(() => users.id),
|
|
34
28
|
title: text().notNull(),
|
|
35
|
-
createdAt: timestamp("created_at").notNull().defaultNow(),
|
|
36
29
|
});
|
|
37
|
-
|
|
38
|
-
export const usersRelations = relations(users, ({ many }) => ({ posts: many(posts) }));
|
|
39
30
|
```
|
|
40
31
|
|
|
41
32
|
```ts
|
|
@@ -48,188 +39,25 @@ import * as schema from "./schema";
|
|
|
48
39
|
const db = drizzle(new Pool(), { schema });
|
|
49
40
|
const sql = createSql<typeof schema>(db);
|
|
50
41
|
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// id and created_at have defaults, so they can be left out
|
|
59
|
-
|
|
60
|
-
await sql.transaction(async (tx) => {
|
|
61
|
-
await tx("UPDATE posts SET title = upper(title) WHERE author_id = $1", 1).execute();
|
|
62
|
-
});
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
Queries use SQL names: `display_name`, not `displayName`. Relations and any other
|
|
66
|
-
non-table exports are ignored, so `typeof schema` can be passed as is.
|
|
67
|
-
|
|
68
|
-
`createSql<Schema, TypeMap, Options>(db)` uses core's `DefaultTypeMap` unless you pass
|
|
69
|
-
another one, such as `PgTypeMap` from `@typesql/pg` for node-postgres' exact types:
|
|
70
|
-
`createSql<typeof schema, PgTypeMap>(db)`.
|
|
71
|
-
|
|
72
|
-
## Running queries
|
|
73
|
-
|
|
74
|
-
`db` is a Drizzle database on node-postgres or postgres.js (`drizzle-orm/node-postgres`,
|
|
75
|
-
`drizzle-orm/postgres-js`), or a transaction from `db.transaction(fn)`. Queries run on the
|
|
76
|
-
driver client Drizzle holds: the `Pool`, `Client` or postgres.js `sql` you gave `drizzle()`, or
|
|
77
|
-
the transaction's connection. Rows are what the driver returns; Drizzle's column mappers and
|
|
78
|
-
its logger are not involved.
|
|
79
|
-
|
|
80
|
-
| | |
|
|
81
|
-
|---|---|
|
|
82
|
-
| `q.execute()`, `q.executeTakeFirst()`, `q.executeTakeFirstOrThrow()`, `q.executeCount()` | `Promise<Row[]>`, `Promise<Row \| undefined>`, `Promise<Row>` (`NoResultError` on 0 rows), `Promise<number>` (the driver's row count) |
|
|
83
|
-
| `sql.transaction(fn)` | `db.transaction(fn)` with `tx` bound to the transaction; `tx.transaction(fn)` is Drizzle's nested transaction (a savepoint) |
|
|
84
|
-
| `executor(db)` | an `Executor` for `q.execute(executor)` and core's `createSql(executor)` |
|
|
85
|
-
| `DrizzleSql<Schema, TM, Options>`, `DrizzleDatabase` | the type `createSql` returns, and what it accepts |
|
|
86
|
-
|
|
87
|
-
To mix ORM and SQL queries in one transaction, bind a `sql` to Drizzle's `tx`:
|
|
88
|
-
|
|
89
|
-
```ts
|
|
90
|
-
await db.transaction(async (tx) => {
|
|
91
|
-
await tx.insert(schema.users).values({ email: "a@b.c" });
|
|
92
|
-
await createSql<typeof schema>(tx)("UPDATE users SET mood = 'happy' WHERE email = $1", "a@b.c").execute();
|
|
93
|
-
});
|
|
42
|
+
const rows = await sql(
|
|
43
|
+
`SELECT u.email, u.display_name, p.title
|
|
44
|
+
FROM users u JOIN posts p ON p.author_id = u.id
|
|
45
|
+
WHERE u.id = $1`,
|
|
46
|
+
1,
|
|
47
|
+
).execute();
|
|
48
|
+
// { email: string; display_name: string | null; title: string }[]
|
|
94
49
|
```
|
|
95
50
|
|
|
96
|
-
|
|
97
|
-
that driver `date`, `timestamp`, `timestamptz` and `interval` come back as strings: pass a type
|
|
98
|
-
map that says so.
|
|
51
|
+
Good to know:
|
|
99
52
|
|
|
100
|
-
|
|
101
|
-
|
|
53
|
+
- SQL uses the database names (`display_name`), not the TypeScript keys (`displayName`).
|
|
54
|
+
- `.$defaultFn()` and `.$default()` run in Drizzle, not in Postgres, so a SQL `INSERT` must set those columns.
|
|
55
|
+
- Drizzle's types don't record `withTimezone`, so `timestamp()` columns are `timestamp`. Pin a column with `pgType` from this package: `pgType(timestamp("at", { withTimezone: true }), "timestamptz")`.
|
|
102
56
|
|
|
103
|
-
|
|
104
|
-
import { createSql } from "@typesql/pg";
|
|
105
|
-
import type { FromDrizzle } from "@typesql/drizzle";
|
|
106
|
-
import type * as schema from "./schema";
|
|
57
|
+
## Documentation
|
|
107
58
|
|
|
108
|
-
|
|
109
|
-
```
|
|
59
|
+
See [From Kysely, Drizzle or Prisma](https://typesql.karsli.net/guide/orms#drizzle) for how the schema is converted, running queries and overrides, and [Limits](https://typesql.karsli.net/guide/limits#orm-schemas) for what the conversion can't tell.
|
|
110
60
|
|
|
111
|
-
|
|
112
|
-
tables (`FromDrizzle<{ users: typeof users; posts: typeof posts }>`).
|
|
113
|
-
|
|
114
|
-
## What gets converted
|
|
115
|
-
|
|
116
|
-
| Drizzle | TypeSQL |
|
|
117
|
-
|---|---|
|
|
118
|
-
| `pgTable("users", …)` | `DB["users"]`, keyed by the SQL table name, not the export name |
|
|
119
|
-
| `displayName: text("display_name")` | column `display_name`; columns without an explicit name use the key |
|
|
120
|
-
| `.notNull()`, `.primaryKey()`, `serial`, identity | `NOT NULL`; otherwise the column is nullable |
|
|
121
|
-
| `.default(…)`, `.defaultNow()`, `.defaultRandom()`, `serial`, `generatedAlwaysAsIdentity()`, `generatedByDefaultAsIdentity()`, `generatedAlwaysAs(…)` | has a default: optional in `INSERT` |
|
|
122
|
-
| `.$defaultFn(…)` / `.$default(…)` | **no** default: these run in Drizzle, not in Postgres |
|
|
123
|
-
| `export const mood = pgEnum("mood", […])` | `DB["$types"]["mood"]`, keyed by the **export name** (see below) |
|
|
124
|
-
| `pgSchema("auth").table("users", …)` | `DB["auth"]["users"]` (`auth.users` in SQL) |
|
|
125
|
-
| `pgView(…)`, `pgMaterializedView(…)` | a `View<…>`: readable, rejected as an `INSERT`/`UPDATE`/`DELETE` target |
|
|
126
|
-
| `relations(…)`, sequences, other exports | ignored |
|
|
127
|
-
|
|
128
|
-
## Column types
|
|
129
|
-
|
|
130
|
-
The Postgres type comes from Drizzle's column kind. Drizzle's `mode` options change the JS
|
|
131
|
-
value Drizzle produces, but TypeSQL queries go straight to the driver, so the row type comes
|
|
132
|
-
from the type map: `bigint({ mode: "number" })` is `int8`, a `string` under node-postgres.
|
|
133
|
-
|
|
134
|
-
| Drizzle | Postgres |
|
|
135
|
-
|---|---|
|
|
136
|
-
| `smallint`, `smallserial` | `int2` |
|
|
137
|
-
| `integer`, `serial` | `int4` |
|
|
138
|
-
| `bigint`, `bigserial` (any mode) | `int8` |
|
|
139
|
-
| `numeric` / `decimal` (any mode) | `numeric` |
|
|
140
|
-
| `real` | `float4` |
|
|
141
|
-
| `doublePrecision` | `float8` |
|
|
142
|
-
| `boolean` | `bool` |
|
|
143
|
-
| `text` | `text` |
|
|
144
|
-
| `varchar` | `varchar` |
|
|
145
|
-
| `char` | `bpchar` |
|
|
146
|
-
| `uuid` | `uuid` |
|
|
147
|
-
| `date` (any mode) | `date` |
|
|
148
|
-
| `time` | `time`, or `timetz` (see below) |
|
|
149
|
-
| `timestamp` (any mode) | `timestamp`, or `timestamptz` (see below) |
|
|
150
|
-
| `interval` | `interval` |
|
|
151
|
-
| `json`, `jsonb` | `json`, `jsonb` |
|
|
152
|
-
| `inet`, `cidr`, `macaddr`, `macaddr8` | same name |
|
|
153
|
-
| `point` (any mode), `line` (any mode) | `point`, `line` |
|
|
154
|
-
| `geometry` (PostGIS) | `geometry` |
|
|
155
|
-
| `vector`, `halfvec`, `sparsevec`, `bit` (pgvector) | `vector`, `halfvec`, `sparsevec`, `bit` |
|
|
156
|
-
| an enum column | the enum's export name, values in `$types` |
|
|
157
|
-
| `customType(…)` | `unknown`, unless pinned with `pgType` |
|
|
158
|
-
| `.array()`, `.array().array()` | `T[]`, `T[][]` |
|
|
159
|
-
|
|
160
|
-
Types TypeSQL has no TS mapping for (`geometry`, `vector`, …) read as `unknown` unless your
|
|
161
|
-
type map has an entry for them.
|
|
162
|
-
|
|
163
|
-
### `timestamp` with time zone, and other types Drizzle's types do not record
|
|
164
|
-
|
|
165
|
-
`timestamp("at", { withTimezone: true })` and `timestamp("at")` have the same TypeScript type
|
|
166
|
-
in Drizzle, so `FromDrizzle` cannot tell them apart. Both read as `Date` under the default
|
|
167
|
-
type maps, but `timestamp` and `timestamptz` behave differently in SQL (`AT TIME ZONE`,
|
|
168
|
-
`date_trunc`, comparisons with `now()`). Either set the default for all of them:
|
|
169
|
-
|
|
170
|
-
```ts
|
|
171
|
-
createSql<typeof schema, DefaultTypeMap, { timestamp: "timestamptz"; time: "timetz" }>(db);
|
|
172
|
-
// or: FromDrizzle<typeof schema, { timestamp: "timestamptz" }>
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
or pin a column's type with `pgType`, which returns the builder unchanged at runtime:
|
|
176
|
-
|
|
177
|
-
```ts
|
|
178
|
-
import { pgType } from "@typesql/drizzle";
|
|
179
|
-
|
|
180
|
-
export const events = pgTable("events", {
|
|
181
|
-
at: pgType(timestamp("at", { withTimezone: true }).notNull(), "timestamptz"),
|
|
182
|
-
handle: pgType(citext("handle"), "text"), // a customType
|
|
183
|
-
});
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
`pgType` takes precedence over everything else and works for any column. For an array
|
|
187
|
-
column, wrap the whole array: `pgType(citext().array(), "text[]")`.
|
|
188
|
-
|
|
189
|
-
### Enums
|
|
190
|
-
|
|
191
|
-
Drizzle's types do not carry an enum's SQL name (`pgEnum("mood", …)` is typed as
|
|
192
|
-
`string`), so enums are registered in `$types` under their **export name**, and an enum
|
|
193
|
-
column is typed by finding the exported enum with the same values. To use casts such as
|
|
194
|
-
`'happy'::mood`, name the export like the SQL type (`export const mood = pgEnum("mood", …)`).
|
|
195
|
-
Object enums need `as const` (`pgEnum("role", { Admin: "admin" } as const)`) for their
|
|
196
|
-
values to be known. An enum column whose enum is not exported (or when you pass only a
|
|
197
|
-
table or a record of tables) is typed as `text`.
|
|
198
|
-
|
|
199
|
-
### `casing`
|
|
200
|
-
|
|
201
|
-
If you create Drizzle with `drizzle(client, { casing: "snake_case" })`, pass the same
|
|
202
|
-
setting so column names match what Drizzle sends to Postgres:
|
|
203
|
-
|
|
204
|
-
```ts
|
|
205
|
-
createSql<typeof schema, DefaultTypeMap, { casing: "snake_case" }>(db);
|
|
206
|
-
// createdAt: timestamp() → column created_at
|
|
207
|
-
```
|
|
61
|
+
## License
|
|
208
62
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
- **Generated-always columns** (`generatedAlwaysAs`, `generatedAlwaysAsIdentity`) are treated
|
|
212
|
-
as columns with a default. TypeSQL has no read-only columns, so an `INSERT` that sets one
|
|
213
|
-
type-checks but fails in Postgres (unless `OVERRIDING SYSTEM VALUE` for identities).
|
|
214
|
-
- **`$onUpdate` / `$onUpdateFn`** mark the column as having a default in Drizzle's types, with
|
|
215
|
-
no way to tell them from a SQL default, so such a column is optional in `INSERT` even
|
|
216
|
-
though Postgres will not fill it.
|
|
217
|
-
- **`.$type<T>()`** is ignored: rows use the type map (`jsonb().$type<Settings>()` is
|
|
218
|
-
`unknown` under the default map). Branded ids lose their brand.
|
|
219
|
-
- **`text({ enum: [...] })`, `varchar({ enum: [...] })`** are plain `text` / `varchar`: the
|
|
220
|
-
values are only a TypeScript constraint in Drizzle, not a Postgres type.
|
|
221
|
-
- **Views** take their column types from the selected Drizzle columns. Columns from the
|
|
222
|
-
outer side of a join keep their own nullability, and `sql\`…\`.as("x")` fields are
|
|
223
|
-
`unknown`. Views created with `pgSchema(…).view(…)` are placed at the top level, because
|
|
224
|
-
Drizzle's view types do not record the schema.
|
|
225
|
-
- **`pgTableCreator`** prefixes are applied at runtime only; tables appear under the name
|
|
226
|
-
passed to the table function.
|
|
227
|
-
- **`casing`** is applied to every column name, including explicitly named ones; Drizzle
|
|
228
|
-
applies it only to columns named after their key. These agree unless an explicit name is
|
|
229
|
-
not already in the target casing.
|
|
230
|
-
- Multi-dimensional arrays are typed `T[][]`; Postgres itself does not distinguish them
|
|
231
|
-
from `T[]`.
|
|
232
|
-
|
|
233
|
-
## Examples
|
|
234
|
-
|
|
235
|
-
[`examples/schema.ts`](examples/schema.ts) and [`examples/basic.ts`](examples/basic.ts) walk through the features above; they are type-checked with the package (`pnpm typecheck`).
|
|
63
|
+
[MIT](LICENSE)
|
package/dist/casing.d.ts
CHANGED
|
@@ -3,15 +3,21 @@ type Upper = Uppercase<Lower>;
|
|
|
3
3
|
type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
|
|
4
4
|
type Cls<C extends string> = C extends Lower ? "l" : C extends Digit ? "d" : C extends Upper ? "u" : "o";
|
|
5
5
|
/** A new word starts at `C` (class `K`) after a char of class `P`, `R` being the rest of the input. */
|
|
6
|
-
type Starts<P, K, R extends string> = K extends "u" ?
|
|
6
|
+
type Starts<P, K, R extends string> = K extends "u" ? P extends "l" | "d" ? true : P extends "u" ? R extends `${Lower}${string}` ? true : false : false : K extends "d" ? P extends "u" ? true : false : false;
|
|
7
7
|
type Push<A extends string[], W extends string> = W extends "" ? A : [...A, W];
|
|
8
8
|
/**
|
|
9
9
|
* Words of an identifier, as Drizzle's `casing` splits them
|
|
10
10
|
* (`/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g`): `"userID2fa"` → `["user", "ID", "2fa"]`.
|
|
11
11
|
*/
|
|
12
12
|
export type Words<S extends string, W extends string = "", Acc extends string[] = [], P = "o"> = S extends `${infer C}${infer R}` ? Cls<C> extends "o" ? Words<R, "", Push<Acc, W>, "o"> : Starts<P, Cls<C>, R> extends true ? Words<R, C, Push<Acc, W>, Cls<C>> : Words<R, `${W}${C}`, Acc, Cls<C>> : Push<Acc, W>;
|
|
13
|
-
type Snake<Ws extends string[], Acc extends string = ""> = Ws extends [
|
|
14
|
-
|
|
13
|
+
type Snake<Ws extends string[], Acc extends string = ""> = Ws extends [
|
|
14
|
+
infer W extends string,
|
|
15
|
+
...infer R extends string[]
|
|
16
|
+
] ? Snake<R, Acc extends "" ? Lowercase<W> : `${Acc}_${Lowercase<W>}`> : Acc;
|
|
17
|
+
type Camel<Ws extends string[], Acc extends string = ""> = Ws extends [
|
|
18
|
+
infer W extends string,
|
|
19
|
+
...infer R extends string[]
|
|
20
|
+
] ? Camel<R, Acc extends "" ? Lowercase<W> : `${Acc}${Capitalize<W>}`> : Acc;
|
|
15
21
|
/** Drizzle's `toSnakeCase`: `"displayName"` → `"display_name"`. */
|
|
16
22
|
export type ToSnakeCase<S extends string> = Snake<Words<S>>;
|
|
17
23
|
/** Drizzle's `toCamelCase`: `"display_name"` → `"displayName"`. */
|
package/dist/casing.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"casing.d.ts","sources":["../src/casing.ts"],"names":[],"mappings":"AAAA,KAAK,KAAK,
|
|
1
|
+
{"version":3,"file":"casing.d.ts","sources":["../src/casing.ts"],"names":[],"mappings":"AAAA,KAAK,KAAK,GACP,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,GACH,GAAG,CAAC;AACP,KAAK,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;AAC9B,KAAK,KAAK,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AAEvE,KAAK,GAAG,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,KAAK,GACzC,GAAG,GACH,CAAC,SAAS,KAAK,GACd,GAAG,GACH,CAAC,SAAS,KAAK,GACd,GAAG,GACH,GAAG,CAAC;AAET,uGAAuG;AACvG,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,GAAG,GAChD,CAAC,SAAS,GAAG,GAAG,GAAG,GAClB,IAAI,GACJ,CAAC,SAAS,GAAG,GACZ,CAAC,SAAS,GAAG,KAAK,GAAG,MAAM,EAAE,GAC5B,I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,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,IAAI,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,GAClE,GAAG,CAAC;AACP,KAAK,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,EAAE,GAAG,SAAS,MAAM,GAAG,EAAE,IAAI,EAAE,SAAS;IACrE,MAAM,CAAC,SAAS,MAAM;IACtB,GAAG,MAAM,CAAC,SAAS,MAAM,EAAE;CAC3B,GACE,KAAK,CAAC,CAAC,EAAE,GAAG,SAAS,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,GAAG,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,GAClE,GAAG,CAAC;AAEP,mEAAmE;AACnE,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAC5D,mEAAmE;AACnE,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC"}
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Executor
|
|
1
|
+
import type { Executor } from "@typesql/core";
|
|
2
|
+
import type { SqlDriver } from "@typesql/core/adapter";
|
|
2
3
|
/**
|
|
3
4
|
* What the adapter uses from a Drizzle database (`drizzle(client)` from `drizzle-orm/node-postgres`,
|
|
4
5
|
* `drizzle-orm/postgres-js`, ...) or a transaction from `db.transaction(fn)`.
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAEvD;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,CAAC,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IACzC,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,eAAe,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CACpE;AAyBD;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,EAAE,EAAE,eAAe,GAAG,QAAQ,CAgBtD;AAWD,yDAAyD;AACzD,eAAO,MAAM,MAAM,EAAE,SAAS,CAAC,eAAe,CAA6B,CAAC"}
|
package/dist/client.js
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
function clientOf(db) {
|
|
7
7
|
const client = db._.session.client;
|
|
8
|
-
if (client &&
|
|
8
|
+
if (client &&
|
|
9
|
+
(typeof client.unsafe === "function" || typeof client.query === "function"))
|
|
9
10
|
return client;
|
|
10
11
|
throw new Error("typesql: createSql(db) needs a Drizzle database on node-postgres or postgres.js (its session client has no query or unsafe method)");
|
|
11
12
|
}
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sources":["../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.js","sources":["../src/client.ts"],"names":[],"mappings":"AAkBA;;;;GAIG;AACH,SAAS,QAAQ,CAAC,EAAmB;IACpC,MAAM,MAAM,GAAI,EAAE,CAAC,CAAC,CAAC,OAA8C,CAAC,MAAM,CAAC;IAC3E,IACC,MAAM;QACN,CAAC,OAAO,MAAM,CAAC,MAAM,KAAK,UAAU,IAAI,OAAO,MAAM,CAAC,KAAK,KAAK,UAAU,CAAC;QAE3E,OAAO,MAAM,CAAC;IACf,MAAM,IAAI,KAAK,CACd,oIAAoI,CACpI,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,EAAmB;IAC3C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;QAC7B,MAAM,MAAM,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC;QAC5B,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YACnB,MAAM,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,CAClC,IAAI,EACJ,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAClB,CAAmD,CAAC;YACrD,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;QACrE,CAAC;QACD,MAAM,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,KAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAG5D,CAAC;QACF,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,IAAI,EAAE,CAAC;IACjE,CAAC,CAAC;AACH,CAAC;AAED,qGAAqG;AACrG,SAAS,WAAW,CACnB,EAAmB,EACnB,MAAc,EACd,EAAuC;IAEvC,OAAO,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;AAC3B,CAAC;AAED,yDAAyD;AACzD,MAAM,CAAC,MAAM,MAAM,GAA+B,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC"}
|
package/dist/from-drizzle.d.ts
CHANGED
|
@@ -86,18 +86,16 @@ type ColumnConfig = {
|
|
|
86
86
|
/** Values of an exported `pgEnum` (array or object form). */
|
|
87
87
|
type EnumValues<X> = X extends {
|
|
88
88
|
readonly enumValues: readonly [string, ...string[]];
|
|
89
|
-
} ? X["enumValues"][number] : X extends {
|
|
90
|
-
|
|
91
|
-
readonly
|
|
92
|
-
readonly data: infer D extends string;
|
|
93
|
-
};
|
|
89
|
+
} ? X["enumValues"][number] : X extends () => {
|
|
90
|
+
readonly _: {
|
|
91
|
+
readonly data: infer D extends string;
|
|
94
92
|
};
|
|
95
93
|
} ? D : never;
|
|
96
94
|
/** `$types` registry: exported enums by export name. */
|
|
97
95
|
type Enums<S> = {
|
|
98
96
|
-readonly [K in keyof S as S[K] extends EnumLike ? K : never]: EnumValues<S[K]>;
|
|
99
97
|
};
|
|
100
|
-
type LastOf<U> = (U extends unknown ? (x: () => U) => void : never) extends (x: infer F) => void ?
|
|
98
|
+
type LastOf<U> = (U extends unknown ? (x: () => U) => void : never) extends (x: infer F) => void ? F extends () => infer L ? L : never : never;
|
|
101
99
|
/** Name of the exported enum whose values are exactly `V` (any one when several match), else `text`. */
|
|
102
100
|
type EnumName<V, E> = {
|
|
103
101
|
[K in keyof E]: [E[K]] extends [V] ? ([V] extends [E[K]] ? K : never) : never;
|
|
@@ -108,10 +106,10 @@ type Opt<Opts, K extends keyof FromDrizzleOptions, D> = Opts extends {
|
|
|
108
106
|
/** Postgres type of a column config. */
|
|
109
107
|
type PgTypeOf<D extends ColumnConfig, Opts = {}, E = {}> = D extends {
|
|
110
108
|
readonly typesqlType: infer T extends string;
|
|
111
|
-
} ? T : D["columnType"] extends keyof DrizzleColumnTypes ? DrizzleColumnTypes[D["columnType"]] : D["columnType"] extends "PgArray" ?
|
|
109
|
+
} ? T : D["columnType"] extends keyof DrizzleColumnTypes ? DrizzleColumnTypes[D["columnType"]] : D["columnType"] extends "PgArray" ? D["baseColumn"] extends ColumnLike ? `${PgTypeOf<D["baseColumn"]["_"], Opts, E>}[]` : "unknown[]" : D["columnType"] extends "PgTimestamp" | "PgTimestampString" ? Opt<Opts, "timestamp", "timestamp"> : D["columnType"] extends "PgTime" ? Opt<Opts, "time", "time"> : D["columnType"] extends "PgEnumColumn" ? EnumName<(D["enumValues"] & readonly string[])[number], E> : D["columnType"] extends "PgEnumObjectColumn" ? EnumName<D["data"], E> : "unknown";
|
|
112
110
|
/** The descriptor as the matching core helper (`Col`, `Nullable`, `Generated`, `NullableGenerated`). */
|
|
113
|
-
type Desc<T extends string, N, H> = N extends true ?
|
|
114
|
-
type ColOf<D extends ColumnConfig, Opts, E> = Desc<PgTypeOf<D, Opts, E>, D["notNull"] extends true ? false : true, D["hasDefault"] extends true ?
|
|
111
|
+
type Desc<T extends string, N, H> = N extends true ? H extends true ? NullableGenerated<T> : Nullable<T> : H extends true ? Generated<T> : Col<T>;
|
|
112
|
+
type ColOf<D extends ColumnConfig, Opts, E> = Desc<PgTypeOf<D, Opts, E>, D["notNull"] extends true ? false : true, D["hasDefault"] extends true ? D["hasRuntimeDefault"] extends true ? false : true : false>;
|
|
115
113
|
type Cased<N extends string, Opts> = Opts extends {
|
|
116
114
|
readonly casing: "snake_case";
|
|
117
115
|
} ? ToSnakeCase<N> : Opts extends {
|
|
@@ -133,8 +131,8 @@ type Relation<X, Opts, E> = X extends TableLike ? Columns<X["_"]["columns"], Opt
|
|
|
133
131
|
} : never;
|
|
134
132
|
/** Postgres schema of a table outside `public`. */
|
|
135
133
|
type SchemaOf<X> = X extends TableLike ? Exclude<X["_"]["schema"], undefined | "public"> : never;
|
|
136
|
-
type NameIn<X, N> = X extends TableLike ?
|
|
137
|
-
type TopName<X> = X extends TableLike ?
|
|
134
|
+
type NameIn<X, N> = X extends TableLike ? X["_"]["schema"] extends N ? X["_"]["name"] : never : never;
|
|
135
|
+
type TopName<X> = X extends TableLike ? [SchemaOf<X>] extends [never] ? X["_"]["name"] : never : X extends ViewLike ? X["_"]["name"] : never;
|
|
138
136
|
type Build<S, Opts, E = Enums<S>> = {
|
|
139
137
|
-readonly [K in keyof S as TopName<S[K]>]: Relation<S[K], Opts, E>;
|
|
140
138
|
} & {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"from-drizzle.d.ts","sources":["../src/from-drizzle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
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AI,EAAE,CAAC,CAAC;CAClE,GAAG;KACF,CAAC,IAAI;SAAG,CAAC,IAAI,MAAM,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KAAE,CAAC,MAAM,CAAC,CAAC,GAAG;QACnD,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;KACpE;CACD,GAAG,CAAC,MAAM,CAAC,SAAS,KAAK,GAAG,EAAE,GAAG;IAAE,MAAM,EAAE,CAAC,CAAA;CAAE,CAAC,CAAC;AAEjD,KAAK,IAAI,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,EAAE,CAAC;AAE7C;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,CACtB,MAAM,EACN,IAAI,SAAS,kBAAkB,GAAG,EAAE,IACjC,MAAM,SAAS,SAAS,GAAG,QAAQ,GACpC,IAAI,CAAC,KAAK,CAAC;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,GAAG,MAAM;CAAE,EAAE,IAAI,CAAC,CAAC,GACzD,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { DefaultTypeMap, TransactionalSql } from "@typesql/core";
|
|
2
2
|
import { type DrizzleDatabase } from "./client.js";
|
|
3
3
|
import type { FromDrizzle, FromDrizzleOptions } from "./from-drizzle.js";
|
|
4
|
-
export
|
|
4
|
+
export { type DrizzleDatabase, executor } from "./client.js";
|
|
5
|
+
export type { DrizzleColumnTypes, FromDrizzle, FromDrizzleOptions, } from "./from-drizzle.js";
|
|
5
6
|
export { pgType } from "./pg-type.js";
|
|
6
|
-
export { executor, type DrizzleDatabase } from "./client.js";
|
|
7
|
-
export type { Sql, Query, RowType, ParamsType, DefaultTypeMap, TypeMap, Executor, TransactionalSql } from "@typesql/core";
|
|
8
|
-
export { NoResultError } from "@typesql/core";
|
|
9
7
|
/** What `createSql<typeof schema>(db)` returns: core's `Sql` over `FromDrizzle<Schema, Opts>`, with `transaction(fn)`. */
|
|
10
8
|
export type DrizzleSql<Schema, TM = DefaultTypeMap, Opts extends FromDrizzleOptions = {}> = TransactionalSql<FromDrizzle<Schema, Opts>, TM>;
|
|
11
9
|
/**
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEtE,OAAO,EAAE,KAAK,eAAe,EAAU,MAAM,aAAa,CAAC;AAC3D,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEzE,OAAO,EAAE,KAAK,eAAe,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC7D,YAAY,EACX,kBAAkB,EAClB,WAAW,EACX,kBAAkB,GAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,0HAA0H;AAC1H,MAAM,MAAM,UAAU,CACrB,MAAM,EACN,EAAE,GAAG,cAAc,EACnB,IAAI,SAAS,kBAAkB,GAAG,EAAE,IACjC,gBAAgB,CAAC,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;AAEpD;;;;;GAKG;AACH,wBAAgB,SAAS,CACxB,MAAM,EACN,EAAE,GAAG,cAAc,EACnB,IAAI,SAAS,kBAAkB,GAAG,EAAE,EACnC,EAAE,CAAC,EAAE,eAAe,GAAG,UAAU,CAAC,MAAM,EAAE,EAAE,EAAE,IAAI,CAAC,CAKpD"}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { createTransactionalSql } from "@typesql/core";
|
|
1
|
+
import { createTransactionalSql } from "@typesql/core/adapter";
|
|
2
2
|
import { driver } from "./client.js";
|
|
3
|
-
export { pgType } from "./pg-type.js";
|
|
4
3
|
export { executor } from "./client.js";
|
|
5
|
-
export {
|
|
4
|
+
export { pgType } from "./pg-type.js";
|
|
6
5
|
/**
|
|
7
6
|
* `createSql` from core with the schema converted from Drizzle definitions:
|
|
8
7
|
* `createSql<typeof schema>(db)`, or `createSql<typeof schema, PgTypeMap>(db)` for a driver's type map.
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EAAwB,MAAM,EAAE,MAAM,aAAa,CAAC;AAG3D,OAAO,EAAwB,QAAQ,EAAE,MAAM,aAAa,CAAC;AAM7D,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAStC;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAIvB,EAAoB;IACrB,OAAO,sBAAsB,CAC5B,EAAE,EACF,MAAM,CAC0B,CAAC;AACnC,CAAC"}
|
package/dist/pg-type.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg-type.d.ts","sources":["../src/pg-type.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,SAAS,MAAM,
|
|
1
|
+
{"version":3,"file":"pg-type.d.ts","sources":["../src/pg-type.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,SAAS,MAAM,EAC/C,OAAO,EAAE,CAAC,EACV,IAAI,EAAE,CAAC,GACL,CAAC,GAAG;IAAE,QAAQ,CAAC,CAAC,EAAE;QAAE,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAA;KAAE,CAAA;CAAE,CAGjD"}
|
package/dist/pg-type.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pg-type.js","sources":["../src/pg-type.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"pg-type.js","sources":["../src/pg-type.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,MAAM,CACrB,OAAU,EACV,IAAO;IAEP,KAAK,IAAI,CAAC;IACV,OAAO,OAA0D,CAAC;AACnE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typesql/drizzle",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Use Drizzle ORM table definitions as a TypeSQL schema.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -28,14 +28,14 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@typesql/core": "0.
|
|
31
|
+
"@typesql/core": "0.6.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"drizzle-orm": ">=0.45"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/pg": "^8.23.1",
|
|
38
|
-
"@typesql/pg": "0.1
|
|
38
|
+
"@typesql/pg": "0.2.1",
|
|
39
39
|
"drizzle-orm": "^0.45.4",
|
|
40
40
|
"pg": "^8.23.1",
|
|
41
41
|
"postgres": "^3.4.9",
|
package/src/casing.ts
CHANGED
|
@@ -1,14 +1,55 @@
|
|
|
1
|
-
type Lower =
|
|
1
|
+
type Lower =
|
|
2
|
+
| "a"
|
|
3
|
+
| "b"
|
|
4
|
+
| "c"
|
|
5
|
+
| "d"
|
|
6
|
+
| "e"
|
|
7
|
+
| "f"
|
|
8
|
+
| "g"
|
|
9
|
+
| "h"
|
|
10
|
+
| "i"
|
|
11
|
+
| "j"
|
|
12
|
+
| "k"
|
|
13
|
+
| "l"
|
|
14
|
+
| "m"
|
|
15
|
+
| "n"
|
|
16
|
+
| "o"
|
|
17
|
+
| "p"
|
|
18
|
+
| "q"
|
|
19
|
+
| "r"
|
|
20
|
+
| "s"
|
|
21
|
+
| "t"
|
|
22
|
+
| "u"
|
|
23
|
+
| "v"
|
|
24
|
+
| "w"
|
|
25
|
+
| "x"
|
|
26
|
+
| "y"
|
|
27
|
+
| "z";
|
|
2
28
|
type Upper = Uppercase<Lower>;
|
|
3
29
|
type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
|
|
4
30
|
|
|
5
|
-
type Cls<C extends string> = C extends Lower
|
|
31
|
+
type Cls<C extends string> = C extends Lower
|
|
32
|
+
? "l"
|
|
33
|
+
: C extends Digit
|
|
34
|
+
? "d"
|
|
35
|
+
: C extends Upper
|
|
36
|
+
? "u"
|
|
37
|
+
: "o";
|
|
6
38
|
|
|
7
39
|
/** A new word starts at `C` (class `K`) after a char of class `P`, `R` being the rest of the input. */
|
|
8
|
-
type Starts<P, K, R extends string> =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
40
|
+
type Starts<P, K, R extends string> = K extends "u"
|
|
41
|
+
? P extends "l" | "d"
|
|
42
|
+
? true
|
|
43
|
+
: P extends "u"
|
|
44
|
+
? R extends `${Lower}${string}`
|
|
45
|
+
? true
|
|
46
|
+
: false
|
|
47
|
+
: false
|
|
48
|
+
: K extends "d"
|
|
49
|
+
? P extends "u"
|
|
50
|
+
? true
|
|
51
|
+
: false
|
|
52
|
+
: false;
|
|
12
53
|
|
|
13
54
|
type Push<A extends string[], W extends string> = W extends "" ? A : [...A, W];
|
|
14
55
|
|
|
@@ -16,17 +57,31 @@ type Push<A extends string[], W extends string> = W extends "" ? A : [...A, W];
|
|
|
16
57
|
* Words of an identifier, as Drizzle's `casing` splits them
|
|
17
58
|
* (`/[\da-z]+|[A-Z]+(?![a-z])|[A-Z][\da-z]+/g`): `"userID2fa"` → `["user", "ID", "2fa"]`.
|
|
18
59
|
*/
|
|
19
|
-
export type Words<
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
60
|
+
export type Words<
|
|
61
|
+
S extends string,
|
|
62
|
+
W extends string = "",
|
|
63
|
+
Acc extends string[] = [],
|
|
64
|
+
P = "o",
|
|
65
|
+
> = S extends `${infer C}${infer R}`
|
|
66
|
+
? Cls<C> extends "o"
|
|
67
|
+
? Words<R, "", Push<Acc, W>, "o">
|
|
68
|
+
: Starts<P, Cls<C>, R> extends true
|
|
69
|
+
? Words<R, C, Push<Acc, W>, Cls<C>>
|
|
70
|
+
: Words<R, `${W}${C}`, Acc, Cls<C>>
|
|
71
|
+
: Push<Acc, W>;
|
|
25
72
|
|
|
26
|
-
type Snake<Ws extends string[], Acc extends string = ""> =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
73
|
+
type Snake<Ws extends string[], Acc extends string = ""> = Ws extends [
|
|
74
|
+
infer W extends string,
|
|
75
|
+
...infer R extends string[],
|
|
76
|
+
]
|
|
77
|
+
? Snake<R, Acc extends "" ? Lowercase<W> : `${Acc}_${Lowercase<W>}`>
|
|
78
|
+
: Acc;
|
|
79
|
+
type Camel<Ws extends string[], Acc extends string = ""> = Ws extends [
|
|
80
|
+
infer W extends string,
|
|
81
|
+
...infer R extends string[],
|
|
82
|
+
]
|
|
83
|
+
? Camel<R, Acc extends "" ? Lowercase<W> : `${Acc}${Capitalize<W>}`>
|
|
84
|
+
: Acc;
|
|
30
85
|
|
|
31
86
|
/** Drizzle's `toSnakeCase`: `"displayName"` → `"display_name"`. */
|
|
32
87
|
export type ToSnakeCase<S extends string> = Snake<Words<S>>;
|
package/src/client.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import type { Executor
|
|
1
|
+
import type { Executor } from "@typesql/core";
|
|
2
|
+
import type { SqlDriver } from "@typesql/core/adapter";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* What the adapter uses from a Drizzle database (`drizzle(client)` from `drizzle-orm/node-postgres`,
|
|
5
6
|
* `drizzle-orm/postgres-js`, ...) or a transaction from `db.transaction(fn)`.
|
|
6
7
|
*/
|
|
7
8
|
export interface DrizzleDatabase {
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
readonly _: { readonly session: object };
|
|
10
|
+
transaction<T>(fn: (tx: DrizzleDatabase) => Promise<T>): Promise<T>;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
/** The driver client a Drizzle session holds: node-postgres' `query` or postgres.js' `unsafe`. */
|
|
13
14
|
interface DriverClient {
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
query?(text: string, values: unknown[]): Promise<unknown>;
|
|
16
|
+
unsafe?(text: string, values: unknown[]): PromiseLike<unknown>;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
/**
|
|
@@ -21,9 +22,15 @@ interface DriverClient {
|
|
|
21
22
|
* `session.client`.
|
|
22
23
|
*/
|
|
23
24
|
function clientOf(db: DrizzleDatabase): DriverClient {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
const client = (db._.session as { readonly client?: DriverClient }).client;
|
|
26
|
+
if (
|
|
27
|
+
client &&
|
|
28
|
+
(typeof client.unsafe === "function" || typeof client.query === "function")
|
|
29
|
+
)
|
|
30
|
+
return client;
|
|
31
|
+
throw new Error(
|
|
32
|
+
"typesql: createSql(db) needs a Drizzle database on node-postgres or postgres.js (its session client has no query or unsafe method)",
|
|
33
|
+
);
|
|
27
34
|
}
|
|
28
35
|
|
|
29
36
|
/**
|
|
@@ -32,20 +39,30 @@ function clientOf(db: DrizzleDatabase): DriverClient {
|
|
|
32
39
|
* `{ rows, rowCount }` so `q.executeCount()` works.
|
|
33
40
|
*/
|
|
34
41
|
export function executor(db: DrizzleDatabase): Executor {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
return async (text, values) => {
|
|
43
|
+
const client = clientOf(db);
|
|
44
|
+
if (client.unsafe) {
|
|
45
|
+
const result = (await client.unsafe(
|
|
46
|
+
text,
|
|
47
|
+
Array.from(values),
|
|
48
|
+
)) as unknown[] & { readonly count?: number | null };
|
|
49
|
+
return { rows: Array.from(result), rowCount: result.count ?? null };
|
|
50
|
+
}
|
|
51
|
+
const result = (await client.query!(text, Array.from(values))) as {
|
|
52
|
+
rows: unknown[];
|
|
53
|
+
rowCount?: number | null;
|
|
54
|
+
};
|
|
55
|
+
return { rows: result.rows, rowCount: result.rowCount ?? null };
|
|
56
|
+
};
|
|
44
57
|
}
|
|
45
58
|
|
|
46
59
|
/** Run `fn` inside `db.transaction(fn)`; Drizzle nests a transaction inside one with a savepoint. */
|
|
47
|
-
function transaction<T>(
|
|
48
|
-
|
|
60
|
+
function transaction<T>(
|
|
61
|
+
db: DrizzleDatabase,
|
|
62
|
+
_depth: number,
|
|
63
|
+
fn: (tx: DrizzleDatabase) => Promise<T>,
|
|
64
|
+
): Promise<T> {
|
|
65
|
+
return db.transaction(fn);
|
|
49
66
|
}
|
|
50
67
|
|
|
51
68
|
/** How `createSql(db)` runs queries and transactions. */
|
package/src/from-drizzle.ts
CHANGED
|
@@ -1,146 +1,233 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
Col,
|
|
3
|
+
Generated,
|
|
4
|
+
Nullable,
|
|
5
|
+
NullableGenerated,
|
|
6
|
+
} from "@typesql/core";
|
|
2
7
|
import type { ToCamelCase, ToSnakeCase } from "./casing.js";
|
|
3
8
|
|
|
4
9
|
/** Settings of your Drizzle setup that the column types do not record. */
|
|
5
10
|
export interface FromDrizzleOptions {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
/** The `casing` passed to `drizzle()`: applied to column names, as Drizzle does at runtime. */
|
|
12
|
+
readonly casing?: "snake_case" | "camelCase" | undefined;
|
|
13
|
+
/** Type of `timestamp()` columns not marked with `pgType` (Drizzle's types drop `withTimezone`). Default `"timestamp"`. */
|
|
14
|
+
readonly timestamp?: "timestamp" | "timestamptz" | undefined;
|
|
15
|
+
/** Type of `time()` columns not marked with `pgType`. Default `"time"`. */
|
|
16
|
+
readonly time?: "time" | "timetz" | undefined;
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
/** Postgres type of each pg-core `columnType` with a fixed SQL type. */
|
|
15
20
|
export interface DrizzleColumnTypes {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
21
|
+
PgSmallInt: "int2";
|
|
22
|
+
PgInteger: "int4";
|
|
23
|
+
PgBigInt53: "int8";
|
|
24
|
+
PgBigInt64: "int8";
|
|
25
|
+
PgSmallSerial: "int2";
|
|
26
|
+
PgSerial: "int4";
|
|
27
|
+
PgBigSerial53: "int8";
|
|
28
|
+
PgBigSerial64: "int8";
|
|
29
|
+
PgNumeric: "numeric";
|
|
30
|
+
PgNumericNumber: "numeric";
|
|
31
|
+
PgNumericBigInt: "numeric";
|
|
32
|
+
PgReal: "float4";
|
|
33
|
+
PgDoublePrecision: "float8";
|
|
34
|
+
PgBoolean: "bool";
|
|
35
|
+
PgText: "text";
|
|
36
|
+
PgVarchar: "varchar";
|
|
37
|
+
PgChar: "bpchar";
|
|
38
|
+
PgUUID: "uuid";
|
|
39
|
+
PgDate: "date";
|
|
40
|
+
PgDateString: "date";
|
|
41
|
+
PgInterval: "interval";
|
|
42
|
+
PgJson: "json";
|
|
43
|
+
PgJsonb: "jsonb";
|
|
44
|
+
PgInet: "inet";
|
|
45
|
+
PgCidr: "cidr";
|
|
46
|
+
PgMacaddr: "macaddr";
|
|
47
|
+
PgMacaddr8: "macaddr8";
|
|
48
|
+
PgPointTuple: "point";
|
|
49
|
+
PgPointObject: "point";
|
|
50
|
+
PgLine: "line";
|
|
51
|
+
PgLineABC: "line";
|
|
52
|
+
PgGeometry: "geometry";
|
|
53
|
+
PgGeometryObject: "geometry";
|
|
54
|
+
PgVector: "vector";
|
|
55
|
+
PgHalfVector: "halfvec";
|
|
56
|
+
PgSparseVector: "sparsevec";
|
|
57
|
+
PgBinaryVector: "bit";
|
|
58
|
+
PgCustomColumn: "unknown";
|
|
54
59
|
}
|
|
55
60
|
|
|
56
|
-
type TableLike = {
|
|
57
|
-
|
|
58
|
-
|
|
61
|
+
type TableLike = {
|
|
62
|
+
readonly _: {
|
|
63
|
+
readonly brand: "Table";
|
|
64
|
+
readonly name: string;
|
|
65
|
+
readonly schema: string | undefined;
|
|
66
|
+
readonly columns: object;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
type ViewLike = {
|
|
70
|
+
readonly _: {
|
|
71
|
+
readonly brand: "View";
|
|
72
|
+
readonly name: string;
|
|
73
|
+
readonly selectedFields: object;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
type EnumLike = {
|
|
77
|
+
readonly enumName: string;
|
|
78
|
+
readonly enumValues: readonly string[];
|
|
79
|
+
};
|
|
59
80
|
type ColumnLike = { readonly _: ColumnConfig };
|
|
60
81
|
type ColumnConfig = {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
82
|
+
readonly brand: "Column";
|
|
83
|
+
readonly name: string;
|
|
84
|
+
readonly columnType: string;
|
|
85
|
+
readonly notNull: boolean;
|
|
86
|
+
readonly hasDefault: boolean;
|
|
87
|
+
readonly hasRuntimeDefault: boolean;
|
|
88
|
+
readonly enumValues: readonly string[] | undefined;
|
|
89
|
+
readonly data: unknown;
|
|
90
|
+
readonly baseColumn: unknown;
|
|
70
91
|
};
|
|
71
92
|
|
|
72
93
|
/** Values of an exported `pgEnum` (array or object form). */
|
|
73
|
-
type EnumValues<X> =
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
94
|
+
type EnumValues<X> = X extends {
|
|
95
|
+
readonly enumValues: readonly [string, ...string[]];
|
|
96
|
+
}
|
|
97
|
+
? X["enumValues"][number]
|
|
98
|
+
: X extends () => { readonly _: { readonly data: infer D extends string } }
|
|
99
|
+
? D
|
|
100
|
+
: never;
|
|
77
101
|
|
|
78
102
|
/** `$types` registry: exported enums by export name. */
|
|
79
|
-
type Enums<S> = {
|
|
103
|
+
type Enums<S> = {
|
|
104
|
+
-readonly [K in keyof S as S[K] extends EnumLike ? K : never]: EnumValues<
|
|
105
|
+
S[K]
|
|
106
|
+
>;
|
|
107
|
+
};
|
|
80
108
|
|
|
81
|
-
type LastOf<U> = (U extends unknown ? (x: () => U) => void : never) extends (
|
|
109
|
+
type LastOf<U> = (U extends unknown ? (x: () => U) => void : never) extends (
|
|
110
|
+
x: infer F,
|
|
111
|
+
) => void
|
|
112
|
+
? F extends () => infer L
|
|
113
|
+
? L
|
|
114
|
+
: never
|
|
115
|
+
: never;
|
|
82
116
|
|
|
83
117
|
/** Name of the exported enum whose values are exactly `V` (any one when several match), else `text`. */
|
|
84
|
-
type EnumName<V, E> =
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
118
|
+
type EnumName<V, E> = {
|
|
119
|
+
[K in keyof E]: [E[K]] extends [V] ? ([V] extends [E[K]] ? K : never) : never;
|
|
120
|
+
}[keyof E] extends infer N
|
|
121
|
+
? [N] extends [never]
|
|
122
|
+
? "text"
|
|
123
|
+
: LastOf<N> & string
|
|
124
|
+
: never;
|
|
125
|
+
|
|
126
|
+
type Opt<Opts, K extends keyof FromDrizzleOptions, D> = Opts extends {
|
|
127
|
+
readonly [P in K]: infer V extends string;
|
|
128
|
+
}
|
|
129
|
+
? V
|
|
130
|
+
: D;
|
|
90
131
|
|
|
91
132
|
/** Postgres type of a column config. */
|
|
92
|
-
type PgTypeOf<D extends ColumnConfig, Opts = {}, E = {}> =
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
133
|
+
type PgTypeOf<D extends ColumnConfig, Opts = {}, E = {}> = D extends {
|
|
134
|
+
readonly typesqlType: infer T extends string;
|
|
135
|
+
}
|
|
136
|
+
? T
|
|
137
|
+
: D["columnType"] extends keyof DrizzleColumnTypes
|
|
138
|
+
? DrizzleColumnTypes[D["columnType"]]
|
|
139
|
+
: D["columnType"] extends "PgArray"
|
|
140
|
+
? D["baseColumn"] extends ColumnLike
|
|
141
|
+
? `${PgTypeOf<D["baseColumn"]["_"], Opts, E>}[]`
|
|
142
|
+
: "unknown[]"
|
|
143
|
+
: D["columnType"] extends "PgTimestamp" | "PgTimestampString"
|
|
144
|
+
? Opt<Opts, "timestamp", "timestamp">
|
|
145
|
+
: D["columnType"] extends "PgTime"
|
|
146
|
+
? Opt<Opts, "time", "time">
|
|
147
|
+
: D["columnType"] extends "PgEnumColumn"
|
|
148
|
+
? EnumName<(D["enumValues"] & readonly string[])[number], E>
|
|
149
|
+
: D["columnType"] extends "PgEnumObjectColumn"
|
|
150
|
+
? EnumName<D["data"], E>
|
|
151
|
+
: "unknown";
|
|
101
152
|
|
|
102
153
|
/** The descriptor as the matching core helper (`Col`, `Nullable`, `Generated`, `NullableGenerated`). */
|
|
103
|
-
type Desc<T extends string, N, H> =
|
|
104
|
-
|
|
154
|
+
type Desc<T extends string, N, H> = N extends true
|
|
155
|
+
? H extends true
|
|
156
|
+
? NullableGenerated<T>
|
|
157
|
+
: Nullable<T>
|
|
158
|
+
: H extends true
|
|
159
|
+
? Generated<T>
|
|
160
|
+
: Col<T>;
|
|
105
161
|
|
|
106
162
|
type ColOf<D extends ColumnConfig, Opts, E> = Desc<
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
163
|
+
PgTypeOf<D, Opts, E>,
|
|
164
|
+
D["notNull"] extends true ? false : true,
|
|
165
|
+
D["hasDefault"] extends true
|
|
166
|
+
? D["hasRuntimeDefault"] extends true
|
|
167
|
+
? false
|
|
168
|
+
: true
|
|
169
|
+
: false
|
|
110
170
|
>;
|
|
111
171
|
|
|
112
|
-
type Cased<N extends string, Opts> =
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
172
|
+
type Cased<N extends string, Opts> = Opts extends {
|
|
173
|
+
readonly casing: "snake_case";
|
|
174
|
+
}
|
|
175
|
+
? ToSnakeCase<N>
|
|
176
|
+
: Opts extends { readonly casing: "camelCase" }
|
|
177
|
+
? ToCamelCase<N>
|
|
178
|
+
: N;
|
|
116
179
|
|
|
117
180
|
type Columns<C, Opts, E> = {
|
|
118
|
-
|
|
181
|
+
-readonly [K in keyof C as C[K] extends ColumnLike
|
|
182
|
+
? Cased<C[K]["_"]["name"], Opts>
|
|
183
|
+
: never]: C[K] extends ColumnLike ? ColOf<C[K]["_"], Opts, E> : never;
|
|
119
184
|
};
|
|
120
185
|
|
|
121
186
|
type Aliased = { readonly _: { readonly brand: "SQL.Aliased" } };
|
|
122
187
|
type ViewColumns<F, Opts, E> = {
|
|
123
|
-
|
|
124
|
-
|
|
188
|
+
-readonly [K in keyof F as F[K] extends ColumnLike
|
|
189
|
+
? Cased<F[K]["_"]["name"], Opts>
|
|
190
|
+
: F[K] extends Aliased
|
|
191
|
+
? K
|
|
192
|
+
: never]: F[K] extends ColumnLike
|
|
193
|
+
? Desc<
|
|
194
|
+
PgTypeOf<F[K]["_"], Opts, E>,
|
|
195
|
+
F[K]["_"]["notNull"] extends true ? false : true,
|
|
196
|
+
false
|
|
197
|
+
>
|
|
198
|
+
: Nullable<"unknown">;
|
|
125
199
|
};
|
|
126
200
|
|
|
127
|
-
type Relation<X, Opts, E> =
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
201
|
+
type Relation<X, Opts, E> = X extends TableLike
|
|
202
|
+
? Columns<X["_"]["columns"], Opts, E>
|
|
203
|
+
: X extends ViewLike
|
|
204
|
+
? { readonly $view: ViewColumns<X["_"]["selectedFields"], Opts, E> }
|
|
205
|
+
: never;
|
|
131
206
|
|
|
132
207
|
/** Postgres schema of a table outside `public`. */
|
|
133
|
-
type SchemaOf<X> = X extends TableLike
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
208
|
+
type SchemaOf<X> = X extends TableLike
|
|
209
|
+
? Exclude<X["_"]["schema"], undefined | "public">
|
|
210
|
+
: never;
|
|
211
|
+
type NameIn<X, N> = X extends TableLike
|
|
212
|
+
? X["_"]["schema"] extends N
|
|
213
|
+
? X["_"]["name"]
|
|
214
|
+
: never
|
|
215
|
+
: never;
|
|
216
|
+
type TopName<X> = X extends TableLike
|
|
217
|
+
? [SchemaOf<X>] extends [never]
|
|
218
|
+
? X["_"]["name"]
|
|
219
|
+
: never
|
|
220
|
+
: X extends ViewLike
|
|
221
|
+
? X["_"]["name"]
|
|
222
|
+
: never;
|
|
223
|
+
|
|
224
|
+
type Build<S, Opts, E = Enums<S>> = {
|
|
225
|
+
-readonly [K in keyof S as TopName<S[K]>]: Relation<S[K], Opts, E>;
|
|
226
|
+
} & {
|
|
227
|
+
[N in { [K in keyof S]: SchemaOf<S[K]> }[keyof S]]: {
|
|
228
|
+
-readonly [K in keyof S as NameIn<S[K], N>]: Relation<S[K], Opts, E>;
|
|
229
|
+
};
|
|
230
|
+
} & (keyof E extends never ? {} : { $types: E });
|
|
144
231
|
|
|
145
232
|
type Flat<T> = { [K in keyof T]: T[K] } & {};
|
|
146
233
|
|
|
@@ -152,5 +239,9 @@ type Flat<T> = { [K in keyof T]: T[K] } & {};
|
|
|
152
239
|
* exported `pgEnum`s are registered under `$types` by export name. Anything else exported
|
|
153
240
|
* (relations, sequences, helpers) is ignored.
|
|
154
241
|
*/
|
|
155
|
-
export type FromDrizzle<
|
|
156
|
-
|
|
242
|
+
export type FromDrizzle<
|
|
243
|
+
Schema,
|
|
244
|
+
Opts extends FromDrizzleOptions = {},
|
|
245
|
+
> = Schema extends TableLike | ViewLike
|
|
246
|
+
? Flat<Build<{ [K in Schema["_"]["name"]]: Schema }, Opts>>
|
|
247
|
+
: Flat<Build<Schema, Opts>>;
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import type { DefaultTypeMap, TransactionalSql } from "@typesql/core";
|
|
2
|
+
import { createTransactionalSql } from "@typesql/core/adapter";
|
|
3
|
+
import { type DrizzleDatabase, driver } from "./client.js";
|
|
3
4
|
import type { FromDrizzle, FromDrizzleOptions } from "./from-drizzle.js";
|
|
4
5
|
|
|
5
|
-
export
|
|
6
|
+
export { type DrizzleDatabase, executor } from "./client.js";
|
|
7
|
+
export type {
|
|
8
|
+
DrizzleColumnTypes,
|
|
9
|
+
FromDrizzle,
|
|
10
|
+
FromDrizzleOptions,
|
|
11
|
+
} from "./from-drizzle.js";
|
|
6
12
|
export { pgType } from "./pg-type.js";
|
|
7
|
-
export { executor, type DrizzleDatabase } from "./client.js";
|
|
8
|
-
export type { Sql, Query, RowType, ParamsType, DefaultTypeMap, TypeMap, Executor, TransactionalSql } from "@typesql/core";
|
|
9
|
-
export { NoResultError } from "@typesql/core";
|
|
10
13
|
|
|
11
14
|
/** What `createSql<typeof schema>(db)` returns: core's `Sql` over `FromDrizzle<Schema, Opts>`, with `transaction(fn)`. */
|
|
12
|
-
export type DrizzleSql<
|
|
15
|
+
export type DrizzleSql<
|
|
16
|
+
Schema,
|
|
17
|
+
TM = DefaultTypeMap,
|
|
18
|
+
Opts extends FromDrizzleOptions = {},
|
|
19
|
+
> = TransactionalSql<FromDrizzle<Schema, Opts>, TM>;
|
|
13
20
|
|
|
14
21
|
/**
|
|
15
22
|
* `createSql` from core with the schema converted from Drizzle definitions:
|
|
@@ -17,6 +24,13 @@ export type DrizzleSql<Schema, TM = DefaultTypeMap, Opts extends FromDrizzleOpti
|
|
|
17
24
|
* The queries' `execute*` methods run on `db` (a Drizzle database or transaction, on node-postgres
|
|
18
25
|
* or postgres.js), and `sql.transaction(fn)` runs `fn` with a `sql` bound to `db.transaction`.
|
|
19
26
|
*/
|
|
20
|
-
export function createSql<
|
|
21
|
-
|
|
27
|
+
export function createSql<
|
|
28
|
+
Schema,
|
|
29
|
+
TM = DefaultTypeMap,
|
|
30
|
+
Opts extends FromDrizzleOptions = {},
|
|
31
|
+
>(db?: DrizzleDatabase): DrizzleSql<Schema, TM, Opts> {
|
|
32
|
+
return createTransactionalSql<FromDrizzle<Schema, Opts>, TM, DrizzleDatabase>(
|
|
33
|
+
db,
|
|
34
|
+
driver,
|
|
35
|
+
) as DrizzleSql<Schema, TM, Opts>;
|
|
22
36
|
}
|
package/src/pg-type.ts
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
* cannot tell it: `pgType(timestamp("at", { withTimezone: true }), "timestamptz")`, or a
|
|
4
4
|
* `customType` column. Returns the builder unchanged at runtime.
|
|
5
5
|
*/
|
|
6
|
-
export function pgType<B, const T extends string>(
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
export function pgType<B, const T extends string>(
|
|
7
|
+
builder: B,
|
|
8
|
+
type: T,
|
|
9
|
+
): B & { readonly _: { readonly typesqlType: T } } {
|
|
10
|
+
void type;
|
|
11
|
+
return builder as B & { readonly _: { readonly typesqlType: T } };
|
|
9
12
|
}
|