Skip to content

Commit

Permalink
Merge pull request #866 from DmitryPodpryatov/update_docs
Browse files Browse the repository at this point in the history
Update docs
  • Loading branch information
sviezypan authored Jul 27, 2023
2 parents 062fec6 + 7020ffe commit c505116
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
41 changes: 27 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ Values that will represent tables in DSL are then created by calling `defineTabl
In order for `defineTable` to work, user need to provide implicit `Schema` of data type.

```scala
import java.util.UUID
import zio.schema.DeriveSchema
import zio.sql.postgresql.PostgresJdbcModule
import zio.sql.table.Table._

import java.time._
import java.util.UUID

object Repository extends PostgresJdbcModule {
final case class Product(id: UUID, name: String, price: BigDecimal)
Expand Down Expand Up @@ -140,43 +143,43 @@ Using the previous example with `Product` and `Order` table
```scala
val (id, name, price) = products.columns

val (orderId, productId, quantity) = orders.columns
val (orderId, productId, quantity, date) = orders.columns
```

## Selects

Simple select.

```scala
val allProducts = select(productId, name, price).from(products)
val allProducts = select(id, name, price).from(products)
```

Using `where` clause.

```scala
def productById(id: UUID) =
select(productId, name, price).from(products).where(productId === id)
def productById(uuid: UUID) =
select(id, name, price).from(products).where(id === uuid)
```

Inner join.

```scala
val ordersWithProductNames =
select(orderId, name).from(products.join(orders).on(productId === fkProductId))
select(orderId, name).from(products.join(orders).on(productId === id))
```

Left outer join.

```scala
val leftOuter =
select(orderId, name).from(products.leftOuter(orders).on(productId === fkProductId))
select(orderId, name).from(products.leftOuter(orders).on(productId === id))
```

Right outer join.

```scala
val rightOuter =
select(orderId, name).from(products.rightOuter(orders).on(productId === fkProductId))
select(orderId, name).from(products.rightOuter(orders).on(productId === id))
```

Using `limit` and `offset`
Expand All @@ -185,26 +188,36 @@ Using `limit` and `offset`
val limitedResults =
select(orderId, name)
.from(products.join(orders)
.on(productId === fkProductId))
.on(productId === id))
.limit(5)
.offset(10)
```

## Inserts

```scala
insertInto(products)
(productId, name, price)
.values((UUID.randomUUID(), "Zionomicon", 10.5))
def insertProduct(uuid: UUID) =
insertInto(products)(id, name, price)
.values((uuid, "Zionomicon", 10.5))
```

## Updates

TODO: details
```scala
def updateProduct(uuid: UUID) =
update(products)
.set(name, "foo")
.set(price, price * 1.1)
.where(id === uuid)
```

## Deletes

TODO: details
```scala
def deleteProduct(uuid: UUID) =
deleteFrom(products)
.where(id === uuid)
```

## Transactions

Expand Down
41 changes: 27 additions & 14 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ Values that will represent tables in DSL are then created by calling `defineTabl
In order for `defineTable` to work, user need to provide implicit `Schema` of data type.

```scala
import java.util.UUID
import zio.schema.DeriveSchema
import zio.sql.postgresql.PostgresJdbcModule
import zio.sql.table.Table._

import java.time._
import java.util.UUID

object Repository extends PostgresJdbcModule {
final case class Product(id: UUID, name: String, price: BigDecimal)
Expand Down Expand Up @@ -140,43 +143,43 @@ Using the previous example with `Product` and `Order` table
```scala
val (id, name, price) = products.columns

val (orderId, productId, quantity) = orders.columns
val (orderId, productId, quantity, date) = orders.columns
```

## Selects

Simple select.

```scala
val allProducts = select(productId, name, price).from(products)
val allProducts = select(id, name, price).from(products)
```

Using `where` clause.

```scala
def productById(id: UUID) =
select(productId, name, price).from(products).where(productId === id)
def productById(uuid: UUID) =
select(id, name, price).from(products).where(id === uuid)
```

Inner join.

```scala
val ordersWithProductNames =
select(orderId, name).from(products.join(orders).on(productId === fkProductId))
select(orderId, name).from(products.join(orders).on(productId === id))
```

Left outer join.

```scala
val leftOuter =
select(orderId, name).from(products.leftOuter(orders).on(productId === fkProductId))
select(orderId, name).from(products.leftOuter(orders).on(productId === id))
```

Right outer join.

```scala
val rightOuter =
select(orderId, name).from(products.rightOuter(orders).on(productId === fkProductId))
select(orderId, name).from(products.rightOuter(orders).on(productId === id))
```

Using `limit` and `offset`
Expand All @@ -185,26 +188,36 @@ Using `limit` and `offset`
val limitedResults =
select(orderId, name)
.from(products.join(orders)
.on(productId === fkProductId))
.on(productId === id))
.limit(5)
.offset(10)
```

## Inserts

```scala
insertInto(products)
(productId, name, price)
.values((UUID.randomUUID(), "Zionomicon", 10.5))
def insertProduct(uuid: UUID) =
insertInto(products)(id, name, price)
.values((uuid, "Zionomicon", 10.5))
```

## Updates

TODO: details
```scala
def updateProduct(uuid: UUID) =
update(products)
.set(name, "foo")
.set(price, price * 1.1)
.where(id === uuid)
```

## Deletes

TODO: details
```scala
def deleteProduct(uuid: UUID) =
deleteFrom(products)
.where(id === uuid)
```

## Transactions

Expand Down

0 comments on commit c505116

Please sign in to comment.