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

new docs [wip] #1308

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
x
elitan committed Jan 3, 2025
commit a3ba90838664e17cc202f0142989066dd5705ef3
10 changes: 10 additions & 0 deletions site-new/content/docs/data/delete.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Delete
description: Learn how to use the delete method in Kysely.
---

## Basic usage

```ts
await db.deleteFrom('person').where('id', '=', 1).execute()
```
Original file line number Diff line number Diff line change
@@ -107,7 +107,7 @@ export type PetUpdate = Updateable<PetTable>

For production applications, it's highly recommended to automatically generate your `Database` interface by introspecting your database schema or Prisma schema. This ensures your TypeScript types stay in sync with your actual database structure.

You can learn more about the available code generation options in the [Generating Types](/docs/generating-types) guide.
You can learn more about the available code generation options in the [Generate Types](/docs/generate-types) guide.

## Dialects

File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions site-new/content/docs/data/transactions.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: Transactions
description: Learn how to use transactions in Kysely.
---

## Basic usage

```ts
const result = await db.transaction().execute(async (trx) => {
// First, create our superhero
const batman = await trx
.insertInto('person')
.values({
first_name: 'Bruce',
last_name: 'Wayne',
age: 35,
})
.returning('id')
.executeTakeFirstOrThrow()

// Then add his trusty sidekick
const robin = await trx
.insertInto('pet')
.values({
owner_id: batman.id,
name: 'Robin',
species: 'human',
is_favorite: true, // Don't tell the other sidekicks
})
.returningAll()
.executeTakeFirst()

// Finally add his noble steed
const batmobile = await trx
.insertInto('pet')
.values({
owner_id: batman.id,
name: 'Batmobile',
species: 'car',
is_favorite: true, // He loves it equally
})
.returningAll()
.executeTakeFirst()

return { robin, batmobile }
})
```
20 changes: 20 additions & 0 deletions site-new/content/docs/data/update.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Update
description: Learn how to use the update method in Kysely.
---

## Basic usage

```ts
await db
.updateTable('person')
.set({
first_name: 'Bruce',
last_name: 'Wayne',
age: 35,
occupation: 'Billionaire Vigilante',
})
.where('secret_identity', '=', 'Batman')
.where('city', '=', 'Gotham')
.execute()
```
122 changes: 122 additions & 0 deletions site-new/content/docs/data/where.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
title: Where
description: Learn how to use the where method in Kysely.
---

Where clauses are used to filter rows in a database. It can be used in `select`, `update`, `delete` and `insert` queries.

## Basic usage

```ts
// Select
const persons = await db
.selectFrom('person')
.where('first_name', '=', 'Arnold')
.execute()

// Update
await db
.updateTable('person')
.set({
first_name: 'Jennifer',
})
.where('id', '=', 1)
.execute()

// Delete
await db.deleteFrom('person').where('id', '=', 1).execute()
```

## Where in

```ts
const persons = await db
.selectFrom('person')
.selectAll()
.where('first_name', 'in', ['Arnold', 'Jennifer'])
.execute()
```

## Where AND or OR

```ts
// AND example
const persons = await db
.selectFrom('person')
.selectAll()
// 1. Using the `and` method on the expression builder:
.where((eb) =>
eb.and([
eb('first_name', '=', 'Jennifer'),
eb('last_name', '=', 'Aniston'),
]),
)
// 2. Chaining expressions using the `and` method on the
// created expressions:
.where((eb) =>
eb('first_name', '=', 'Jennifer').and('last_name', '=', 'Aniston'),
)
.execute()

// OR example
const persons = await db
.selectFrom('person')
.selectAll()
// 1. Using the `or` method on the expression builder:
.where((eb) =>
eb.or([
eb('first_name', '=', 'Jennifer'),
eb('first_name', '=', 'Sylvester'),
]),
)
// 2. Chaining expressions using the `or` method on the
// created expressions:
.where((eb) =>
eb('last_name', '=', 'Aniston').or('last_name', '=', 'Stallone'),
)
.execute()
```

## Conditinal where

You can conditionally add where clauses to your query.

```ts
import { Expression, SqlBool } from 'kysely'

const firstName: string | undefined = 'Jennifer'
const lastName: string | undefined = 'Aniston'
const under18 = true
const over60 = true

let query = db.selectFrom('person').selectAll()

if (firstName) {
// The query builder is immutable. Remember to reassign
// the result back to the query variable.
query = query.where('first_name', '=', firstName)
}

if (lastName) {
query = query.where('last_name', '=', lastName)
}

if (under18 || over60) {
// Conditional OR expressions can be added like this.
query = query.where((eb) => {
const ors: Expression<SqlBool>[] = []

if (under18) {
ors.push(eb('age', '<', 18))
}

if (over60) {
ors.push(eb('age', '>', 60))
}

return eb.or(ors)
})
}

const persons = await query.execute()
```
12 changes: 12 additions & 0 deletions site-new/content/docs/guides/ai.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: AI
description: Learn how to use AI in Kysely.
---

## Basic usage

```ts
const embeddings = [...]

await db.insertInto('embeddings').values(embeddings).execute()
```
10 changes: 10 additions & 0 deletions site-new/content/docs/guides/relations.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Relations
description: Learn how to use relations in Kysely.
---

## Basic usage

```ts

```
11 changes: 8 additions & 3 deletions site-new/content/docs/meta.json
Original file line number Diff line number Diff line change
@@ -9,8 +9,13 @@
"generate-types",
"plugins",
"---Data---",
"select",
"insert",
"plugin-system"
"data/select",
"data/insert",
"data/update",
"data/delete",
"data/where",
"data/transactions",
"---Guides---",
"guides/relations"
]
}