Skip to content

Commit

Permalink
fix: Rename db next to db and add upgrade page. (#73)
Browse files Browse the repository at this point in the history
* fix: update documentation to reflect removal of legacy db and dbNext renaming.

* fix: add upgrade page for 2.0.

* review: add example.
  • Loading branch information
SandPod authored Feb 14, 2024
1 parent 45cdfcb commit c077e1f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/05-concepts/06-database/05-crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fields:
:::note
You can also access the database methods through the session object under the field `dbNext`. However, this is typically only recommended if you want to do custom queries where you explicitly type out your SQL queries. The `db` field on `Session` contains legacy methods that are included for compatibility. In version 2 of Serverpod, the old legacy methods will be removed and `db` will be replaced by `dbNext`.
You can also access the database methods through the session object under the field `db`. However, this is typically only recommended if you want to do custom queries where you explicitly type out your SQL queries.

:::

Expand Down
4 changes: 2 additions & 2 deletions docs/05-concepts/06-database/08-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

The essential point of a database transaction is that it bundles multiple steps into a single, all-or-nothing operation. The intermediate states between the steps are not visible to other concurrent transactions, and if some failure occurs that prevents the transaction from completing, then none of the steps affect the database at all.

Serverpod handles database transactions through the `session.dbNext.transaction` method. The transaction takes a method that performs any database queries or other operations and optionally returns a value.
Serverpod handles database transactions through the `session.db.transaction` method. The transaction takes a method that performs any database queries or other operations and optionally returns a value.

Simply pass the transaction object to each database operation method to include them in the same atomic operation:

```dart
var result = await session.dbNext.transaction((transaction) async {
var result = await session.db.transaction((transaction) async {
// Do some database queries here.
await Company.db.insertRow(session, company, transaction: transaction);
await Employee.db.insertRow(session, employee, transaction: transaction);
Expand Down
6 changes: 3 additions & 3 deletions docs/05-concepts/06-database/10-raw-access.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Sometimes more advanced tasks need to be performed on the database. For those oc
Use the `unsafeQuery` method when querying for simple data. The method returns a `List<List<dynamic>>` with rows and columns and is useful if you want a simple result without joining any data.

```dart
PostgreSQLResult result = await session.dbNext.unsafeQuery(
PostgreSQLResult result = await session.db.unsafeQuery(
'SELECT * FROM mytable WHERE id = 1',
);
```
Expand All @@ -25,15 +25,15 @@ Use the `unsafeQueryMappedResults` method when making complex queries with joins
Above is a visual aid of the data structure.

```dart
List<Map<String, Map<String, dynamic>>> result = await session.dbNext.unsafeQueryMappedResults(
List<Map<String, Map<String, dynamic>>> result = await session.db.unsafeQueryMappedResults(
'SELECT * FROM mytable LEFT JOIN othertable ON mytable.id = othertable.mytableid',
);
```

Use the `unsafeExecute` method when no result is needed. The return value represents the number of rows that changed.

```dart
int result = await session.dbNext.unsafeExecute(
int result = await session.db.unsafeExecute(
'DELETE FROM mytable WHERE id = 1',
);
```
Expand Down
15 changes: 15 additions & 0 deletions docs/12-upgrading/01-upgrade-to-two.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Upgrade to 2.0

## Changes to the Session Object

With Serverpod 2.0, we have removed the deprecated legacy database layer from the `Session` object. The `Session` object now incorporates the new database layer, accessed via the `dbNext` field in Serverpod 1.2, under the `db` field.

```dart
session.dbNext.find(...);
```

becomes

```dart
session.db.find(...);
```

0 comments on commit c077e1f

Please sign in to comment.