Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/upstream'
Browse files Browse the repository at this point in the history
  • Loading branch information
ikxin committed Nov 18, 2024
2 parents 2a7f24e + 7a0072c commit 3149fa5
Show file tree
Hide file tree
Showing 9 changed files with 745 additions and 546 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "orm-drizzle-docs-astro",
"type": "module",
"version": "0.0.1",
"packageManager": "pnpm@9.12.3",
"scripts": {
"preinstall": "npx only-allow pnpm",
"dev": "astro dev --host",
Expand Down Expand Up @@ -54,4 +53,4 @@
"vite": "^5.4.10",
"vitest": "^2.1.4"
}
}
}
716 changes: 359 additions & 357 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

Binary file added public/images/sheetjs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/images/dark-gradient.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/images/light-gradient.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 90 additions & 1 deletion src/content/docs/insert.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ await db.insert(users)
.onDuplicateKeyUpdate({ set: { id: sql`id` } });
```

## WITH 插入子句
## `with insert` 子句

<Callout>
查看如何使用 WITH 语句与 [select](/docs/select#with-clause)[update](/docs/update#with-update-clause)[delete](/docs/delete#with-delete-clause)
Expand Down Expand Up @@ -216,3 +216,92 @@ values ($1, ((select * from "user_count") = 0))
returning "admin"
```
</Section>


## Insert into ... select

As the SQLite documentation mentions:

<Callout>
The second form of the INSERT statement contains a SELECT statement instead of a VALUES clause.
A new entry is inserted into the table for each row of data returned by executing the SELECT statement.
If a column-list is specified, the number of columns in the result of the SELECT must be the same as
the number of items in the column-list. Otherwise, if no column-list is specified, the number of
columns in the result of the SELECT must be the same as the number of columns in the table.
Any SELECT statement, including compound SELECTs and SELECT statements with ORDER BY and/or LIMIT clauses,
may be used in an INSERT statement of this form.
</Callout>
<Callout type='warning'>
To avoid a parsing ambiguity, the SELECT statement should always contain a WHERE clause, even if that clause is simply "WHERE true", if the upsert-clause is present. Without the WHERE clause, the parser does not know if the token "ON" is part of a join constraint on the SELECT, or the beginning of the upsert-clause.
</Callout>

As the PostgreSQL documentation mentions:
<Callout>
A query (SELECT statement) that supplies the rows to be inserted
</Callout>

And as the MySQL documentation mentions:

<Callout>
With INSERT ... SELECT, you can quickly insert many rows into a table from the result of a SELECT statement, which can select from one or many tables
</Callout>

Drizzle supports the current syntax for all dialects, and all of them share the same syntax. Let's review some common scenarios and API usage.
There are several ways to use select inside insert statements, allowing you to choose your preferred approach:

- You can pass a query builder inside the select function.
- You can use a query builder inside a callback.
- You can pass an SQL template tag with any custom select query you want to use


<Tabs items={["Query Builder", "Callback", "SQL template tag"]}>
<Tab>
<Section>
```ts
const insertedEmployees = await db
.insert(employees)
.select(
db.select({ name: users.name }).from(users).where(eq(users.role, 'employee'))
)
.returning({
id: employees.id,
name: employees.name
});
```
```ts
const qb = new QueryBuilder();
await db.insert(employees).select(
qb.select({ name: users.name }).from(users).where(eq(users.role, 'employee'))
);
```
</Section>
</Tab>
<Tab>
<Section>
```ts
await db.insert(employees).select(
() => db.select({ name: users.name }).from(users).where(eq(users.role, 'employee'))
);
```
```ts
await db.insert(employees).select(
(qb) => qb.select({ name: users.name }).from(users).where(eq(users.role, 'employee'))
);
```
</Section>
</Tab>
<Tab>
<Section>
```ts
await db.insert(employees).select(
sql`select "users"."name" as "name" from "users" where "users"."role" = 'employee'`
);
```
```ts
await db.insert(employees).select(
() => sql`select "users"."name" as "name" from "users" where "users"."role" = 'employee'`
);
```
</Section>
</Tab>
</Tabs>
69 changes: 68 additions & 1 deletion src/content/docs/update.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const updatedUserId: { updatedId: number }[] = await db.update(users)
.returning({ updatedId: users.id });
```

## 使用 UPDATE 子句
## `with update`子句

<Callout>
查看如何在 [select](/docs/select#with-clause)[insert](/docs/insert#with-insert-clause)[delete](/docs/delete#with-delete-clause) 中使用 WITH 语句
Expand Down Expand Up @@ -96,3 +96,70 @@ where "products"."price" < (select * from "average_price")
returning "id"
```
</Section>

## Update ... from

<IsSupportedChipGroup chips={{ 'PostgreSQL': true, 'MySQL': false, 'SQLite': true }} />

As the SQLite documentation mentions:

> The UPDATE-FROM idea is an extension to SQL that allows an UPDATE statement to be driven by other tables in the database.
The "target" table is the specific table that is being updated. With UPDATE-FROM you can join the target table
against other tables in the database in order to help compute which rows need updating and what
the new values should be on those rows

Similarly, the PostgreSQL documentation states:

> A table expression allowing columns from other tables to appear in the WHERE condition and update expressions
Drizzle also supports this feature starting from version `drizzle-orm@0.36.3`

<Section>
```ts
await db
.update(users)
.set({ cityId: cities.id })
.from(cities)
.where(and(eq(cities.name, 'Seattle'), eq(users.name, 'John')))
```
```sql
update "users" set "city_id" = "cities"."id"
from "cities"
where ("cities"."name" = $1 and "users"."name" = $2)

-- params: [ 'Seattle', 'John' ]
```
</Section>

You can also alias tables that are joined (in PG, you can also alias the updating table too).
<Section>
```ts
const c = alias(cities, 'c');
await db
.update(users)
.set({ cityId: c.id })
.from(c);
```
```sql
update "users" set "city_id" = "c"."id"
from "cities" "c"
```
</Section>

<IsSupportedChipGroup chips={{ 'PostgreSQL': true, 'MySQL': false, 'SQLite': false }} />

In Postgres, you can also return columns from the joined tables.
<Section>
```ts
const updatedUsers = await db
.update(users)
.set({ cityId: cities.id })
.from(cities)
.returning({ id: users.id, cityName: cities.name });
```
```sql
update "users" set "city_id" = "cities"."id"
from "cities"
returning "users"."id", "cities"."name"
```
</Section>
15 changes: 15 additions & 0 deletions src/data/custom-sponsors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,19 @@ export const customSponsors: ISponsor[] = [
isActive: true,
imageType: ImageType.IMAGE,
},
{
tier: {
name: "$250 a month",
isOneTime: false,
},
sponsorEntity: {
__typename: "Organization",
login: "sheetjs.com",
name: "SheetJS",
avatarUrl: "/images/sheetjs.png",
},
createdAt: "2024-08-13T16:35:56Z",
isActive: true,
imageType: ImageType.IMAGE,
},
];
Loading

0 comments on commit 3149fa5

Please sign in to comment.