Skip to content

Commit

Permalink
Merge pull request #391 from medz/v5-dev
Browse files Browse the repository at this point in the history
Prisma ORM for Dart v5
  • Loading branch information
Seven Du authored Jun 6, 2024
2 parents ef21ec4 + 252d0a5 commit 08f6221
Show file tree
Hide file tree
Showing 46 changed files with 1,508 additions and 1,162 deletions.
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "pub"
directory: "/packages/orm"
schedule:
interval: "daily"
- package-ecosystem: "pub"
directory: "/packages/orm_flutter"
schedule:
interval: "daily"
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Prisma Dart v5.0.0 & Flutter integration 0.1.0

To install Prisma ORM for Dart v5.0.0 run this command:

```bash
dart pub add orm:^5.0.0
```

or update your `pubspec.yaml` file:

```yaml
dependencies:
orm: ^5.0.0
```
---
FLutter integration:
```yaml
dependencies:
orm_flutter: ^0.1.0
```
[**"Prisma Dart v4 -> v5 & Flutter integration" Upgrade Guides**](https://prisma.pub/getting-started/upgrade_guides.html)
## What's Changed
* refactor: throws prisma client errors
* refactor(engine): Refactoring binary engines
* refactor(client): add base client and client options
* feat(client): add `env` support
* feat(client): add logging
* refactor(Flutter integration): Refactoring the Flutter integration engine.

**Full Changelog**: https://github.com/medz/prisma-dart/compare/orm-v4.4.0...orm-v5.0.0+orm_flutter-v0.1.0
13 changes: 3 additions & 10 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineConfig({
},
title: "Prisma Dart",
description:
"Prisma Client Dart is an auto-generated type-safe ORM. It uses Prisma Engine as the data access layer and is as consistent as possible with the Prisma Prisma Client JS/TS APIs.",
"Prisma Client Dart is an auto-generated type-safe ORM. It uses Prisma Engine as the data access layer and is as consistent as possible with the Prisma Client JS/TS APIs.",
lastUpdated: true,
head: [
[
Expand Down Expand Up @@ -60,6 +60,8 @@ export default defineConfig({
{ text: "Setup", link: "/getting-started/setup" },
{ text: "Schema", link: "/getting-started/schema" },
{ text: "Flutter Integration", link: "/getting-started/flutter" },
{ text: "Deployment", link: "/getting-started/deployment" },
{ text: "Upgrade Guides", link: "/getting-started/upgrade_guides" },
],
},
{
Expand Down Expand Up @@ -104,15 +106,6 @@ export default defineConfig({
},
],
},
{
text: "Deployment",
items: [
{
text: "Docker",
link: "/deployment/docker",
},
],
},
],
},
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Docker
# Deployment

This page presents how to deploy Dart server application into a docker container avoiding common issues.

Expand Down
71 changes: 20 additions & 51 deletions docs/getting-started/flutter.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,16 @@ dependencies:
## Integration
In the regular method of `orm`, we use `PrismaClient(...)` to create the client, but this does not work in Flutter, so we now use the `PrismaClient.use` method to create the client:
Set your generator engine type to `flutter` in your Prisma schema (`schema.prisma`):

```dart
import '<You generated client path>/client.dart';
final prisma = PrismaClient.use((schema, datasources) {
final dbFilePath = "<You custon db file path>";
return engine = PrismaFlutterEngine(schema: schema, datasources: {
...datasources,
'db': 'file:$dbFilePath',
// ...More options
});
});
```prisma
generator client {
provider = "dart run orm"
output = "../lib/_generated_prisma_client"
engineType = "flutter" // [!code focus]
}
```

> Note: In Flutter, the client will not automatically connect to the database, and you need to manually call `prisma.$connect()` to establish a connection with the database.

## Migrations

Unlike server-side, databases in Flutter are not typically handled by you in the Prisma CLI before building.
Expand Down Expand Up @@ -106,7 +98,7 @@ flutter:
### Run migration

```dart
final engine = PrismaFlutterEngine(...);
final engine = prisma.$engine as LibraryEngine;
await engine.applyMigrations(
path: 'prisma/migrations/', // Your define in `flutter.assets` .igrations root dir
Expand Down Expand Up @@ -140,31 +132,30 @@ We install the database in the `<Application Support Directory>/database.sqlite`
```dart [lib/prisma.dart]
import 'package:flutter/widgets.dart';
import 'package:orm_flutter/orm_flutter.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:orm_flutter/orm_flutter.dart';
import '_generated_prisma_client/client.dart';
late final PrismaClient prisma;
Future<void> initPrismaClient() async {
WidgetsFlutterBinding.ensureInitialized();
WidgetsFlutterBinding.ensureInitialized();
final supportDir = await getApplicationSupportDirectory();
final database = join(supportDir.path, 'database.sqlite');
final supportDir = await getApplicationSupportDirectory();
final database = join(supportDir.path, 'database.sqlite.db');
late final PrismaFlutterEngine engine;
prisma = PrismaClient.use((schema, datasources) {
return engine = PrismaFlutterEngine(schema: schema, datasources: {
...datasources,
'db': 'file:$database',
});
});
prisma = PrismaClient(datasourceUrl: 'file:$database');
final engine = switch (prisma.$engine) {
LibraryEngine engine => engine,
_ => null,
};
await prisma.$connect();
await engine.applyMigrations(path: 'prisma/migrations');
await prisma.$connect();
await engine?.applyMigrations(path: 'prisma/migrations');
}
```
```dart [lib/main.dart]
Expand All @@ -179,28 +170,6 @@ Future<void> main() async {

:::

## Platform specific engines

The generator will generate references to all engines by default, but usually you will not use other engines. If you only want to use the Flutter integration engine, you should configure the engine type in the Prisma schema:

::: code-group

```prisma [schema.prisma]
generator client {
provider = "dart run orm"
engineType = "flutter" // [!code focus]
}
datasource db {
provider = "sqlite"
url = "file:./db.sqlite"
}
```

:::

When you configure `engineType` in `generator` to `flutter`, the binary engine will no longer be referenced.

## Example App

We provide you with a demo App that integrates Prisma ORM in Flutter 👉 [Flutter with ORM](https://github.com/medz/prisma-dart/tree/main/examples/flutter_with_orm)
97 changes: 97 additions & 0 deletions docs/getting-started/upgrade_guides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Upgrade Guides

## Prisma Dart v4 -> v5 & Flutter integration

Upgrading from Prisma Dart v4 to v5 is relatively simple. Although it is a major version change, there are no changes to user APIs.

### `engineType` in Prisma schema

Now, in the Flutter integration, you tell the generator that you want to generate a client for Flutter:

```prisma
generator client {
provider = "dart run orm"
output = "../lib/_generated_prisma_client"
engineType = "flutter" // [!code focus]
}
```

### `PrismaClient`

Major changes to Prisma Client:

1. Changed from an isolated type to a self-dependent type with `Class PrismaClient extends BasePrismaClient<PrismaClient>`.
2. Deprecated `PrismaClient.use` method.

Before:

```dart
final prisma = PrismaClient.use((schema, datasoruce) {
/// Your codes, create engine and returns.
});
```

Now:

```dart
final engine = ...; // You created engine instandce.
final prisma = PrismaClient(engine: engine);
```

### Logging

Previously, we relied on the `logging` package for logging. Then you needed to listen for the logs and output them yourself.

Now, Prisma Dart implements the same logging as the Prisma TS/JS client.

For more logging information, see 👉 [Prisma Logging](https://www.prisma.io/docs/orm/prisma-client/observability-and-logging/logging).

#### Output to console

Before:

```dart
import 'package:loging/loging.dart';
Logger.root.onRecore.listen((log) {
print(log.message);
});
```

Now:

```dart
final prisma = PrismaClient(
log: {
(LogLevel.query, LogEmit.stdout),
// ... More level configs
},
);
```

#### Log event

Before:

```dart
import 'package:loging/loging.dart';
Logger.root.onRecore.listen((log) {
//... You custon.
});
```

Now:

```dart
final prisma = PrismaClient(
log: {
(LogLevel.query, LogEmit.event),
// ... More level configs
},
);
prisma.$on(LogLevel.query, (event) {
// ...
});
```
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout: home
hero:
name: Prisma Client Dart
text: Auto-generated type-safe ORM
tagline: It uses Prisma Engine as the data access layer and is as consistent as possible with the Prisma Prisma Client JS/TS APIs
tagline: It uses Prisma Engine as the data access layer and is as consistent as possible with the Prisma Client JS/TS APIs
image:
src: /prisma-dart.logo.svg
alt: Prisma Dart client logo
Expand Down
16 changes: 7 additions & 9 deletions examples/flutter_with_orm/lib/prisma.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ Future<void> initPrismaClient() async {
WidgetsFlutterBinding.ensureInitialized();

final supportDir = await getApplicationSupportDirectory();
final database = join(supportDir.path, 'database.sqlite');
final database = join(supportDir.path, 'database.sqlite.db');

late final PrismaFlutterEngine engine;
prisma = PrismaClient.use((schema, datasources) {
return engine = PrismaFlutterEngine(schema: schema, datasources: {
...datasources,
'db': 'file:$database',
});
});
prisma = PrismaClient(datasourceUrl: 'file:$database');
final engine = switch (prisma.$engine) {
LibraryEngine engine => engine,
_ => null,
};

await prisma.$connect();
await engine.applyMigrations(path: 'prisma/migrations');
await engine?.applyMigrations(path: 'prisma/migrations');
}
20 changes: 10 additions & 10 deletions examples/flutter_with_orm/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ packages:
path: "../../packages/orm"
relative: true
source: path
version: "4.3.0"
version: "5.0.0"
orm_flutter:
dependency: "direct main"
description:
path: "../../packages/orm_flutter"
relative: true
source: path
version: "0.0.5"
version: "0.0.6"
package_config:
dependency: transitive
description:
Expand Down Expand Up @@ -385,22 +385,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.2"
recase:
rc:
dependency: transitive
description:
name: recase
sha256: e4eb4ec2dcdee52dcf99cb4ceabaffc631d7424ee55e56f280bc039737f89213
name: rc
sha256: "0c2d562c5925b68687ae86b387d847cfc175a5d1d2fce2956a9acc461bd2f33e"
url: "https://pub.dev"
source: hosted
version: "4.1.0"
retry:
version: "0.3.3"
recase:
dependency: transitive
description:
name: retry
sha256: "822e118d5b3aafed083109c72d5f484c6dc66707885e07c0fbcb8b986bba7efc"
name: recase
sha256: e4eb4ec2dcdee52dcf99cb4ceabaffc631d7424ee55e56f280bc039737f89213
url: "https://pub.dev"
source: hosted
version: "3.1.2"
version: "4.1.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down
3 changes: 2 additions & 1 deletion examples/with_sqlite/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="file:./dev.db"
DATABASE_URL="file:./prisma/dev.db" # Dart rintime using
DIRECT_DATABASE_URL="file:./dev.db" # Prisma CLI using
Loading

0 comments on commit 08f6221

Please sign in to comment.