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

docs: modify docs for working with endpoints and models #74

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
13 changes: 0 additions & 13 deletions docs/05-concepts/01-working-with-endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ var client = Client('http://$localhost:8080/')

If you run the app in an Android emulator, the `localhost` parameter points to `10.0.2.2`, rather than `127.0.0.1` as this is the IP address of the host machine. To access the server from a different device on the same network (such as a physical phone) replace `localhost` with the local ip address. You can find the local ip by running `ifconfig` (Linux/MacOS) or `ipconfig` (Windows).

Make sure to also update the `publicHost` in the development config to make sure the server always serves the client with the correct path to assets etc.

```yaml
# your_project_server/config/development.yaml

apiServer:
port: 8080
publicHost: localhost # Change this line
publicPort: 8080
publicScheme: http
...
```

:::info

You can pass the `--watch` flag to `serverpod generate` to watch for changed files and generate code whenever your source files are updated. This is useful during the development of your server.
Expand Down
49 changes: 37 additions & 12 deletions docs/05-concepts/02-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,6 @@ fields:
Serverpod's models can easily be saved to or read from the database. You can read more about this in the [Database](database/models) section.
:::

## Exception

The Serverpod models supports creating exceptions that can be thrown in endpoints by using the `exception` keyword. For more in-depth description on how to work with exceptions see [Error handling and exceptions](exceptions).

```yaml
exception: MyException
fields:
message: String
errorType: MyEnum
```

## Enum

It is easy to add custom enums with serialization support by using the `enum` keyword.
Expand Down Expand Up @@ -86,6 +75,17 @@ It's recommended to always set `serialized` to `byName` in any new Enum models,

:::

## Exception

The Serverpod models supports creating exceptions that can be thrown in endpoints by using the `exception` keyword. For more in-depth description on how to work with exceptions see [Error handling and exceptions](exceptions).

```yaml
exception: MyException
fields:
message: String
errorType: MyEnum
```

## Adding documentation

Serverpod allows you to add documentation to your serializable objects in a similar way that you would add documentation to your Dart code. Use three hashes (###) to indicate that a comment should be considered documentation.
Expand Down Expand Up @@ -117,7 +117,32 @@ var john = User(name: 'John Doe', age: 25);
var jane = john.copyWith(name: 'Jane Doe');
```

The `copyWith` method generates a deep copy of an object, preserving all original fields unless explicitly modified. It can distinguish between a field set to `null` and a field left unspecified (undefined). When using `copyWith`, any field you don't update remains unchanged in the new object.
In this example, we're using `copyWith` to create 'jane', a new user instance with the name changed to 'Jane Doe' while retaining the original age.

The `copyWith` method generates a deep copy of an object, preserving all original fields unless explicitly modified. It can distinguish between a field set to `null` and a field left unspecified (undefined).

```dart
var alice = User(name: 'Alice');
var aliceWithAge = alice.copyWith(age: 30);
```

In the above example, we created a user 'alice' with a name but no age specified. Then we used `copyWith` to create a new user based on 'alice' but with an updated age. Here, 'aliceWithAge' is a new user instance with the specified age of 30. Notice that we didn't explicitly provide a name when calling 'copyWith'. In this case, 'copyWith' preserves the original name since it wasn't explicitly modified in the method call.

To understand how `copyWith` handle fields set to null, let's consider another scenario where we have a 'User' instance named 'bob' with both name and age set to null:

```dart
var bob = User(name: null, age: null);
```

If we want to create a new user instance based on 'bob' but with an updated age, 'copyWith' handles it gracefully:

```dart
var bobWithAge = bob.copyWith(age: 40);
```

In this example, 'bobWithAge' is a new user instance with the specified age of 40, while the name remains null, as it wasn't explicitly modified.

When using `copyWith`, any field you don't update remains unchanged in the new object.

### toJson / fromJson

Expand Down
Loading