Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: guides #381

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/content/guides/conditional-filters-in-query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import Prerequisites from "@mdx/Prerequisites.astro";
To pass a conditional filter in query you can use `.where()` method and logical operator like below:

<Section>
```ts copy {9}
```ts copy {10}
import { ilike } from 'drizzle-orm';
import { posts } from './schema';

const db = drizzle(...)
const db = drizzle(...);

const searchPosts = async (term?: string) => {
await db
Expand All @@ -43,8 +44,11 @@ select * from posts where title ilike 'AI';
To combine conditional filters you can use `and()` or `or()` operators like below:

<Section>
```ts copy {7,8,9,10,11,12,13}
```ts copy {10,11,12,13,14,15,16}
import { and, gt, ilike, inArray } from 'drizzle-orm';
import { posts } from './schema';

const db = drizzle(...);

const searchPosts = async (term?: string, categories: string[] = [], views = 0) => {
await db
Expand Down Expand Up @@ -76,8 +80,11 @@ select * from posts

If you need to combine conditional filters in different part of the project you can create a variable, push filters and then use it in `.where()` method with `and()` or `or()` operators like below:

```ts copy {7,10}
import { SQL, ... } from 'drizzle-orm';
```ts copy {10,13}
import { and, gt, ilike, inArray, SQL, } from 'drizzle-orm';
import { posts } from './schema';

const db = drizzle(...);

const searchPosts = async (filters: SQL[]) => {
await db
Expand All @@ -98,8 +105,11 @@ Drizzle has useful and flexible API, which lets you create your custom solutions

<Section>

```ts copy {5,14}
import { AnyColumn, ... } from 'drizzle-orm';
```ts copy {7,17}
import { and, AnyColumn, gt, sql } from 'drizzle-orm';
import { posts } from './schema';

const db = drizzle(...);

// length less than
const lenlt = (column: AnyColumn, value: number) => {
Expand Down
26 changes: 21 additions & 5 deletions src/content/guides/count-rows.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ To count all rows in table you can use `count()` function or `sql` operator like
To count rows where the specified column contains non-NULL values you can use `count()` function with a column:

<Section>
```ts copy {1}
```ts copy {6}
import { count } from "drizzle-orm";
import { products } from './schema';

const db = drizzle(...);

await db.select({ count: count(products.discount) }).from(products);
```

Expand All @@ -80,8 +85,11 @@ select count("discount") from products;
Drizzle has simple and flexible API, which lets you create your custom solutions. In PostgreSQL and MySQL `count()` function returns bigint, which is interpreted as string by their drivers, so it should be casted to integer:

<Section>
```ts copy {5,7,11,12}
```ts copy {8,10,14,15}
import { AnyColumn, sql } from 'drizzle-orm';
import { products } from './schema';

const db = drizzle(...);

const customCount = (column?: AnyColumn) => {
if (column) {
Expand All @@ -104,8 +112,11 @@ select cast(count("discount") as integer) from products;
In SQLite, `count()` result returns as integer.

<Section>
```ts copy {3,4}
```ts copy {6,7}
import { sql } from 'drizzle-orm';
import { products } from './schema';

const db = drizzle(...);

await db.select({ count: sql<number>`count(*)` }).from(products);
await db.select({ count: sql<number>`count(${products.discount})` }).from(products);
Expand All @@ -128,8 +139,11 @@ If you need to apply runtime transformations to the returned value, you can use
To count rows that match a condition you can use `.where()` method:

<Section>
```ts copy {4,6}
```ts copy {7,9}
import { count, gt } from 'drizzle-orm';
import { products } from './schema';

const db = drizzle(...);

await db
.select({ count: count() })
Expand All @@ -146,9 +160,11 @@ This is how you can use `count()` function with joins and aggregations:

<CodeTabs items={["index.ts", "schema.ts"]}>
<CodeTab>
```ts copy {8,11,12,13}
```ts copy {10,13,14,15}
import { count, eq } from 'drizzle-orm';
import { countries, cities } from './schema';

const db = drizzle(...);

// Count cities in each country
await db
Expand Down
33 changes: 23 additions & 10 deletions src/content/guides/cursor-based-pagination.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ This guide demonstrates how to implement `cursor-based` pagination in Drizzle:

If you need dynamic order by you can do like below:

```ts copy {6,8}
```ts copy {11,13}
import { gt, lt, asc, desc } from "drizzle-orm";
import { users } from './schema';

const db = drizzle(...);

const nextUserPage = async (order: 'asc' | 'desc' = 'asc', cursor?: number, pageSize = 3) => {
await db
.select()
Expand All @@ -133,8 +138,11 @@ The main idea of this pagination is to use cursor as a pointer to a specific row
If you need to order by a non-unique and non-sequential column, you can use multiple columns for cursor. This is how you can do it:

<Section>
```ts copy {14,15,16,17,18,19,22}
```ts copy {16-23, 25}
import { and, asc, eq, gt, or } from 'drizzle-orm';
import { users } from './schema';

const db = drizzle(...);

const nextUserPage = async (
cursor?: {
Expand Down Expand Up @@ -199,21 +207,22 @@ select * from users
Make sure to create indices for the columns that you use for cursor to make query efficient.

<Section>
```ts copy {7,8}
import { index, ...imports } from 'drizzle-orm/pg-core';
```ts copy {9,10}
import { index, pgTable, serial, text} from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
// columns declaration
id: serial('id').primaryKey(),
firstName: text('first_name').notNull(),
lastName: text('last_name').notNull(),
},
(t) => ({
firstNameIndex: index('first_name_index').on(t.firstName).asc(),
firstNameAndIdIndex: index('first_name_and_id_index').on(t.firstName, t.id).asc(),
firstNameIndex: index('first_name_index').on(t.firstName.asc()),
firstNameAndIdIndex: index('first_name_and_id_index').on(t.firstName.asc(), t.id.asc()),
}),
);
```

```sql
-- As of now drizzle-kit only supports index name and on() param, so you have to add order manually
CREATE INDEX IF NOT EXISTS "first_name_index" ON "users" ("first_name" ASC);
CREATE INDEX IF NOT EXISTS "first_name_and_id_index" ON "users" ("first_name" ASC,"id" ASC);
```
Expand All @@ -223,7 +232,11 @@ If you are using primary key which is not sequential (e.g. `UUIDv4`), you should
This is how you can do it:

<Section>
```ts copy {12,13,14,15,16,17,18,21}
```ts copy {16-24,26}
import { or, gt, and, eq, asc } from "drizzle-orm";
import { users } from "./schema";

const db = drizzle(...);

const nextUserPage = async (
cursor?: {
Expand Down Expand Up @@ -259,7 +272,7 @@ await nextUserPage({
Drizzle has useful relational queries API, that lets you easily implement `cursor-based` pagination:

```ts copy {7,8,9}
import * as schema from './db/schema';
import * as schema from './schema';

const db = drizzle(..., { schema });

Expand Down
13 changes: 9 additions & 4 deletions src/content/guides/decrementing-a-value.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: SQL Decrement value
slug: decrementing-a-value
---

import Section from "@mdx/Section.astro";
import IsSupportedChipGroup from "@mdx/IsSupportedChipGroup.astro";
import Prerequisites from "@mdx/Prerequisites.astro";
Expand All @@ -17,10 +18,11 @@ import Prerequisites from "@mdx/Prerequisites.astro";
To decrement a column value you can use `update().set()` method like below:

<Section>
```ts copy {8}
```ts copy {9}
import { eq, sql } from 'drizzle-orm';
import { table } from './schema';

const db = drizzle(...)
const db = drizzle(...);

await db
.update(table)
Expand All @@ -37,8 +39,11 @@ update "table" set "counter" = "counter" - 1 where "id" = 1;

Drizzle has simple and flexible API, which lets you easily create custom solutions. This is how you do custom decrement function:

```ts copy {4,10,11}
import { AnyColumn } from 'drizzle-orm';
```ts copy {6,13,14}
import { AnyColumn, eq, sql } from 'drizzle-orm';
import { table } from './schema';

const db = drizzle(...);

const decrement = (column: AnyColumn, value = 1) => {
return sql`${column} - ${value}`;
Expand Down
43 changes: 34 additions & 9 deletions src/content/guides/include-or-exclude-columns.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ Drizzle has flexible API for including or excluding columns in queries. To inclu
To include specific columns you can use `.select()` method like this:

<Section>
```ts copy {1}
```ts copy {5}
import { posts } from './schema';

const db = drizzle(...);

await db.select({ title: posts.title }).from(posts);
```

Expand All @@ -73,8 +77,11 @@ To include specific columns you can use `.select()` method like this:
To include all columns with extra columns you can use `getTableColumns()` utility function like this:

<Section>
```ts copy {5,6}
```ts copy {8,9}
import { getTableColumns, sql } from 'drizzle-orm';
import { posts } from './schema';

const db = drizzle(...);

await db
.select({
Expand All @@ -99,9 +106,12 @@ To include all columns with extra columns you can use `getTableColumns()` utilit
To exclude columns you can use `getTableColumns()` utility function like this:

<Section>
```ts copy {3,5}
```ts copy {6,8}
import { getTableColumns } from 'drizzle-orm';
import { posts } from './schema';

const db = drizzle(...);

const { content, ...rest } = getTableColumns(posts); // exclude "content" column

await db.select({ ...rest }).from(posts); // select all other columns
Expand All @@ -121,9 +131,11 @@ This is how you can include or exclude columns with joins:

<CodeTabs items={["index.ts", "schema.ts"]}>
<CodeTab>
```ts copy {5,9,10,11}
```ts copy {7,11,12,13}
import { eq, getTableColumns } from 'drizzle-orm';
import { comments, posts, users } from './db/schema';
import { comments, posts, users } from './schema';

const db = drizzle(...);

// exclude "userId" and "postId" columns from "comments"
const { userId, postId, ...rest } = getTableColumns(comments);
Expand Down Expand Up @@ -222,7 +234,11 @@ Drizzle has useful relational queries API, that lets you easily include or exclu
This is how you can include specific columns using relational queries:

<Section>
```ts copy {2,3,4}
```ts copy {6,7,8}
import * as schema from './schema';

const db = drizzle(..., { schema });

await db.query.posts.findMany({
columns: {
title: true,
Expand All @@ -241,8 +257,11 @@ This is how you can include specific columns using relational queries:
This is how you can include all columns with extra columns using relational queries:

<Section>
```ts copy {4,5,6}
```ts copy {7,8,9}
import { sql } from 'drizzle-orm';
import * as schema from './schema';

const db = drizzle(..., { schema });

await db.query.posts.findMany({
extras: {
Expand All @@ -266,7 +285,11 @@ This is how you can include all columns with extra columns using relational quer
This is how you can exclude columns using relational queries:

<Section>
```ts copy {2,3,4}
```ts copy {6,7,8}
import * as schema from './schema';

const db = drizzle(..., { schema });

await db.query.posts.findMany({
columns: {
content: false,
Expand Down Expand Up @@ -374,9 +397,11 @@ This is how you can create custom solution for conditional select:

<CodeTabs items={["index.ts", "schema.ts"]}>
<CodeTab>
```ts copy {7}
```ts copy {9}
import { posts } from './schema';

const db = drizzle(...);

const searchPosts = async (withTitle = false) => {
await db
.select({
Expand Down
12 changes: 8 additions & 4 deletions src/content/guides/incrementing-a-value.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import Prerequisites from "@mdx/Prerequisites.astro";
To increment a column value you can use `update().set()` method like below:

<Section>
```ts copy {8}
```ts copy {9}
import { eq, sql } from 'drizzle-orm';
import { table } from './schema';

const db = drizzle(...)
const db = drizzle(...);

await db
.update(table)
Expand All @@ -37,8 +38,11 @@ update "table" set "counter" = "counter" + 1 where "id" = 1;

Drizzle has simple and flexible API, which lets you easily create custom solutions. This is how you do custom increment function:

```ts copy {4,10,11}
import { AnyColumn } from 'drizzle-orm';
```ts copy {6,13,14}
import { AnyColumn, eq, sql } from 'drizzle-orm';
import { table } from './schema';

const db = drizzle(...);

const increment = (column: AnyColumn, value = 1) => {
return sql`${column} + ${value}`;
Expand Down
Loading