Configuration
Prisma client Dart requires no other configuration, just configure the data model in the schema.prisma
file, but there are always some options!
Node package manager
By default, you are not required to specify the path to the npm executable. We will search in the global $PATH
.
If your NPM is not installed globally (after installing NodeJS globally, NPM is available globally), then you need to tell the generator the path of NPM:
generator client {
provider = "dart run orm"
npm = "/path/to/npm"
}
Other package managers
If you are using a package manager other than NPM, you can specify the path to the executable or the package manager name:
/// Yarn
generator client {
provider = "dart run orm"
npm = "yarn"
}
/// PNPM
generator client {
provider = "dart run orm"
npm = "pnpm"
}
Prisma client output
You can change the output path by configuring the output
option:
generator client {
provider = "dart run orm"
output = "../lib/generated" // Output path is `lib/generated/prisma_client.dart`
}
generator client {
provider = "dart run orm"
output = "../lib/custom_generated.dart" // Output path is `lib/custom_generated.dart
}
About database connection
After the Prisma CLI initializes the project, there will be a .env
file, which contains the configuration of the database connection. You can configure the database connection in this file:
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"
However, Prisma client Dart does not support it, it is only used by Prisma CLI.
You can configure it in the schema.prisma
file:
datasource db {
provider = "postgresql"
url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"
}
But it is not recommended to configure the database connection in the schema.prisma
file, because it will expose the information of the database connection, you can use the --define
parameter of Dart cli to configure the database connection:
dart run --define=DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA" bin/main.dart
and then use it in your code:
final prisma = PrismaClient(
datasources: Datasources(
db: const String.forEnvironment('DATABASE_URL'),
),
);
If you compiled your program, you can use Platform.environment
of dart:io
to get environment variables:
import 'dart:io';
final prisma = PrismaClient(
datasources: Datasources(
db: Platform.environment['DATABASE_URL'],
),
);
DATABASE_URL="postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA" bin/main.exe
Of course, you have other options, such as dotenv and so on.