Skip to content

Commit 10aa7ee

Browse files
committed
Merge commit '2121f65f7ba78d27d74d5bdc5a58abc5e2534819'
2 parents b470061 + 2121f65 commit 10aa7ee

File tree

7 files changed

+204
-51
lines changed

7 files changed

+204
-51
lines changed

src/content/documentation/docs/connect-cloudflare-d1.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import CodeTabs from "@mdx/CodeTabs.astro";
1010

1111
<Prerequisites>
1212
- Database [connection basics](/docs/connect-overview) with Drizzle
13-
- D1 Database - [website](https://docs.turso.tech/introduction)
14-
- D1 driver - [website](https://docs.turso.tech/sdk/ts/reference) & [GitHub](https://github.com/tursodatabase/libsql-client-ts)
13+
- D1 Database - [website](https://developers.cloudflare.com/d1/)
14+
- D1 driver - [website](https://developers.cloudflare.com/d1/build-with-d1/d1-client-api/)
1515
</Prerequisites>
1616

1717
According to the **[official website](https://developers.cloudflare.com/d1/)**,

src/content/documentation/docs/delete.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,42 @@ And you can delete with filters and conditions:
1212
await db.delete(users).where(eq(users.name, 'Dan'));
1313
```
1414

15+
### Limit
16+
17+
<IsSupportedChipGroup chips={{ 'PostgreSQL': false, 'MySQL': true, 'SQLite': true }} />
18+
19+
Use `.limit()` to add `limit` clause to the query - for example:
20+
<Section>
21+
```typescript
22+
await db.delete(users).where(eq(users.name, 'Dan')).limit(2);
23+
```
24+
```sql
25+
delete from "users" where "users"."name" = $1 limit $2;
26+
```
27+
</Section>
28+
29+
### Order By
30+
Use `.orderBy()` to add `order by` clause to the query, sorting the results by the specified fields:
31+
<Section>
32+
```typescript
33+
import { asc, desc } from 'drizzle-orm';
34+
35+
await db.delete(users).where(eq(users.name, 'Dan')).orderBy(users.name);
36+
await db.delete(users).where(eq(users.name, 'Dan')).orderBy(desc(users.name));
37+
38+
// order by multiple fields
39+
await db.delete(users).where(eq(users.name, 'Dan')).orderBy(users.name, users.name2);
40+
await db.delete(users).where(eq(users.name, 'Dan')).orderBy(asc(users.name), desc(users.name2));
41+
```
42+
```sql
43+
delete from "users" where "users"."name" = $1 order by "name";
44+
delete from "users" where "users"."name" = $1 order by "name" desc;
45+
46+
delete from "users" where "users"."name" = $1 order by "name", "name2";
47+
delete from "users" where "users"."name" = $1 order by "name" asc, "name2" desc;
48+
```
49+
</Section>
50+
1551
### Delete with return
1652
<IsSupportedChipGroup chips={{ 'PostgreSQL': true, 'SQLite': true, 'MySQL': false }} />
1753
You can delete a row and get it back in PostgreSQL and SQLite:

src/content/documentation/docs/indexes-constraints.mdx

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,95 @@ If you define a `CHECK` constraint on a column it will allow only certain values
317317

318318
If you define a `CHECK` constraint on a table it can limit the values in certain columns based on values in other columns in the row.
319319

320-
<Callout type="error" emoji="🛑">
321-
NOT YET IMPLEMENTED IN DRIZZLE ORM
322-
</Callout>
320+
<Tabs items={['PostgreSQL', 'MySQL', 'SQLite']}>
321+
<Tab>
322+
<Section>
323+
```typescript copy
324+
import { sql } from "drizzle-orm";
325+
import { check, integer, pgTable, text, uuid } from "drizzle-orm/pg-core";
326+
327+
export const users = pgTable(
328+
"users",
329+
{
330+
id: uuid().defaultRandom().primaryKey(),
331+
username: text().notNull(),
332+
age: integer(),
333+
},
334+
(table) => ({
335+
checkConstraint: check("age_check1", sql`${table.age} > 21`),
336+
})
337+
);
338+
```
339+
```sql
340+
CREATE TABLE IF NOT EXISTS "users" (
341+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
342+
"username" text NOT NULL,
343+
"age" integer,
344+
CONSTRAINT "age_check1" CHECK ("users"."age" > 21)
345+
);
346+
```
347+
</Section>
348+
349+
</Tab>
350+
<Tab>
351+
<Section>
352+
```typescript copy
353+
import { sql } from "drizzle-orm";
354+
import { check, int, mysqlTable, text } from "drizzle-orm/mysql-core";
355+
356+
export const users = mysqlTable(
357+
"users",
358+
{
359+
id: int().primaryKey(),
360+
username: text().notNull(),
361+
age: int(),
362+
},
363+
(table) => ({
364+
checkConstraint: check("age_check1", sql`${table.age} > 21`),
365+
})
366+
);
367+
```
368+
```sql
369+
CREATE TABLE `users` (
370+
`id` int NOT NULL,
371+
`username` text NOT NULL,
372+
`age` int,
373+
CONSTRAINT `users_id` PRIMARY KEY(`id`),
374+
CONSTRAINT `age_check1` CHECK(`users`.`age` > 21)
375+
);
376+
```
377+
</Section>
378+
</Tab>
379+
<Tab>
380+
<Section>
381+
```typescript copy
382+
import { sql } from "drizzle-orm";
383+
import { check, int, sqliteTable, text } from "drizzle-orm/sqlite-core";
384+
385+
export const users = sqliteTable(
386+
"users",
387+
{
388+
id: int().primaryKey(),
389+
username: text().notNull(),
390+
age: int(),
391+
},
392+
(table) => ({
393+
checkConstraint: check("age_check1", sql`${table.age} > 21`),
394+
})
395+
);
396+
```
397+
```sql
398+
CREATE TABLE `users` (
399+
`id` integer PRIMARY KEY NOT NULL,
400+
`username` text NOT NULL,
401+
`age` integer,
402+
CONSTRAINT "age_check1" CHECK("users"."age" > 21)
403+
);
404+
```
405+
</Section>
406+
407+
</Tab>
408+
</Tabs>
323409

324410
### Primary Key
325411

src/content/documentation/docs/update.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,42 @@ await db.update(users)
2020
.where(eq(users.name, 'Dan'));
2121
```
2222

23+
### Limit
24+
25+
<IsSupportedChipGroup chips={{ 'PostgreSQL': false, 'MySQL': true, 'SQLite': true }} />
26+
27+
Use `.limit()` to add `limit` clause to the query - for example:
28+
<Section>
29+
```typescript
30+
await db.update(usersTable).set({ verified: true }).limit(2);
31+
```
32+
```sql
33+
update "users" set "verified" = $1 limit $2;
34+
```
35+
</Section>
36+
37+
### Order By
38+
Use `.orderBy()` to add `order by` clause to the query, sorting the results by the specified fields:
39+
<Section>
40+
```typescript
41+
import { asc, desc } from 'drizzle-orm';
42+
43+
await db.update(usersTable).set({ verified: true }).orderBy(usersTable.name);
44+
await db.update(usersTable).set({ verified: true }).orderBy(desc(usersTable.name));
45+
46+
// order by multiple fields
47+
await db.update(usersTable).set({ verified: true }).orderBy(usersTable.name, usersTable.name2);
48+
await db.update(usersTable).set({ verified: true }).orderBy(asc(usersTable.name), desc(usersTable.name2));
49+
```
50+
```sql
51+
update "users" set "verified" = $1 order by "name";
52+
update "users" set "verified" = $1 order by "name" desc;
53+
54+
update "users" set "verified" = $1 order by "name", "name2";
55+
update "users" set "verified" = $1 order by "name" asc, "name2" desc;
56+
```
57+
</Section>
58+
2359
### Update with returning
2460
<IsSupportedChipGroup chips={{ 'PostgreSQL': true, 'SQLite': true, 'MySQL': false }} />
2561
You can update a row and get it back in PostgreSQL and SQLite:

src/content/documentation/docs/views.mdx

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import IsSupportedChipGroup from '@components/markdown/IsSupportedChipGroup.astr
44
import Callout from '@components/markdown/Callout.astro';
55
import Section from '@components/markdown/Section.astro';
66

7-
# Views (WIP)
8-
<Callout type="warning" emoji="⚠️">
9-
Views are currently only implemented in the `drizzle-orm`, `drizzle-kit` does not support views yet.
10-
You can query the views that already exist in the database, but they won't be added to `drizzle-kit` migrations or `db push` as of now.
11-
</Callout>
12-
7+
# Views
138
## Views declaration
149
There're several ways you can declare views with Drizzle ORM.
1510

@@ -28,11 +23,11 @@ yet when you use `sql` you have to explicitly declare view columns schema.
2823
import { pgTable, pgView, serial, text, timestamp } from "drizzle-orm/pg-core";
2924

3025
export const user = pgTable("user", {
31-
id: serial("id"),
32-
name: text("name"),
33-
email: text("email"),
34-
password: text("password"),
35-
role: text("role").$type<"admin" | "customer">(),
26+
id: serial(),
27+
name: text(),
28+
email: text(),
29+
password: text(),
30+
role: text().$type<"admin" | "customer">(),
3631
createdAt: timestamp("created_at"),
3732
updatedAt: timestamp("updated_at"),
3833
});
@@ -52,11 +47,11 @@ yet when you use `sql` you have to explicitly declare view columns schema.
5247
import { text, mysqlTable, mysqlView, int, timestamp } from "drizzle-orm/mysql-core";
5348

5449
export const user = mysqlTable("user", {
55-
id: int("id").primaryKey().autoincrement(),
56-
name: text("name"),
57-
email: text("email"),
58-
password: text("password"),
59-
role: text("role").$type<"admin" | "customer">(),
50+
id: int().primaryKey().autoincrement(),
51+
name: text(),
52+
email: text(),
53+
password: text(),
54+
role: text().$type<"admin" | "customer">(),
6055
createdAt: timestamp("created_at"),
6156
updatedAt: timestamp("updated_at"),
6257
});
@@ -76,11 +71,11 @@ yet when you use `sql` you have to explicitly declare view columns schema.
7671
import { integer, text, sqliteView, sqliteTable } from "drizzle-orm/sqlite-core";
7772

7873
export const user = sqliteTable("user", {
79-
id: integer("id").primaryKey({ autoIncrement: true }),
80-
name: text("name"),
81-
email: text("email"),
82-
password: text("password"),
83-
role: text("role").$type<"admin" | "customer">(),
74+
id: integer().primaryKey({ autoIncrement: true }),
75+
name: text(),
76+
email: text(),
77+
password: text(),
78+
role: text().$type<"admin" | "customer">(),
8479
createdAt: integer("created_at"),
8580
updatedAt: integer("updated_at"),
8681
});
@@ -124,11 +119,11 @@ You can also declare views using `standalone query builder`, it works exactly th
124119
const qb = new QueryBuilder();
125120

126121
export const user = pgTable("user", {
127-
id: serial("id"),
128-
name: text("name"),
129-
email: text("email"),
130-
password: text("password"),
131-
role: text("role").$type<"admin" | "customer">(),
122+
id: serial(),
123+
name: text(),
124+
email: text(),
125+
password: text(),
126+
role: text().$type<"admin" | "customer">(),
132127
createdAt: timestamp("created_at"),
133128
updatedAt: timestamp("updated_at"),
134129
});
@@ -150,11 +145,11 @@ You can also declare views using `standalone query builder`, it works exactly th
150145
const qb = new QueryBuilder();
151146

152147
export const user = mysqlTable("user", {
153-
id: int("id").primaryKey().autoincrement(),
154-
name: text("name"),
155-
email: text("email"),
156-
password: text("password"),
157-
role: text("role").$type<"admin" | "customer">(),
148+
id: int().primaryKey().autoincrement(),
149+
name: text(),
150+
email: text(),
151+
password: text(),
152+
role: text().$type<"admin" | "customer">(),
158153
createdAt: timestamp("created_at"),
159154
updatedAt: timestamp("updated_at"),
160155
});
@@ -176,11 +171,11 @@ You can also declare views using `standalone query builder`, it works exactly th
176171
const qb = new QueryBuilder();
177172

178173
export const user = sqliteTable("user", {
179-
id: integer("id").primaryKey({ autoIncrement: true }),
180-
name: text("name"),
181-
email: text("email"),
182-
password: text("password"),
183-
role: text("role").$type<"admin" | "customer">(),
174+
id: integer().primaryKey({ autoIncrement: true }),
175+
name: text(),
176+
email: text(),
177+
password: text(),
178+
role: text().$type<"admin" | "customer">(),
184179
createdAt: integer("created_at"),
185180
updatedAt: integer("updated_at"),
186181
});
@@ -221,11 +216,11 @@ When you're provided with a read only access to an existing view in the database
221216
`drizzle-kit` will ignore and will not generate a `create view` statement in the generated migration.
222217
```ts
223218
export const user = pgTable("user", {
224-
id: serial("id"),
225-
name: text("name"),
226-
email: text("email"),
227-
password: text("password"),
228-
role: text("role").$type<"admin" | "customer">(),
219+
id: serial(),
220+
name: text(),
221+
email: text(),
222+
password: text(),
223+
role: text().$type<"admin" | "customer">(),
229224
createdAt: timestamp("created_at"),
230225
updatedAt: timestamp("updated_at"),
231226
});

src/data/progress.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default 40;
1+
export default 70;

src/data/roadmap.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
- [x] Brocli integration to Drizzle Kit
66
- [ ] Generated SQL migration strict mode without `try catches` and `if not exists`
77
- [x] Drizzle Kit goes OSS steam 🎉
8-
- [ ] PostgreSQL enums alternations improvements
8+
- [x] PostgreSQL enums alternations improvements
99
- [ ] PostgreSQL RLS support
10-
- [ ] `check` constraint support in Drizzle Kit
10+
- [x] `check` constraint support in Drizzle Kit
1111
- [x] Exposed API for programmatic access in Drizzle Kit
12-
- [ ] `materialised views` support in Drizzle Kit
12+
- [x] `materialised views` support in Drizzle Kit
1313
- [ ] Drizzle Kit support for consuming schemas and migrations from Prisma, TypeORM and Sequelise to enrich ->Drizzle migration process
1414
- [ ] Drizzle Studio `mkcert` fixes for dockerised environments
1515

@@ -22,7 +22,7 @@
2222
- [ ] Down migrations, better rollbacks and improvements to `migrate` experience in Drizzle Kit
2323
- [x] Optional database aliases for columns in table declarations -> `id: serial()`
2424
- [ ] PostgreSQL RLS support
25-
- [ ] Relational Querie V2 API
25+
- [ ] Relational Queries V2 API
2626
- [x] `drizzle('pg', ...)` driver instantiation to lower the entrance learning curve
2727
- [ ] CockroachDB support (supported with strict SQL migrations)
2828
- [ ] Native seeding

0 commit comments

Comments
 (0)