Skip to content

Commit

Permalink
Merge pull request #17 from odroe/feat-15-runtime-configuration
Browse files Browse the repository at this point in the history
Feat 15 runtime configuration
  • Loading branch information
Seven Du authored Sep 8, 2022
2 parents 8c3ab9e + 08966b6 commit bf3de28
Show file tree
Hide file tree
Showing 23 changed files with 547 additions and 283 deletions.
105 changes: 105 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,108 @@
## 2.1.0

🌟 Help us spread the word about [Prisma ORM for Dart](https://github.com/odroe/prisma-dart) by starring the repo or [Tweeting](https://twitter.com/intent/tweet?text=Check%20out%20the%20latest%20Prisma%20ORM%20for%20Dart%20release%20v2.0.0🚀%0D%0A%0D%0Ahttps://github.com/odroe/prisma-dart/releases/tag/2.1.0) about the release. 🌟

### Major improvements:

#### Runtime configuration

Previously, use `prisma.yaml` to configure Prisma:
```yaml
environment:
DATABASE_URL: postgres://user:password@localhost:5432/mydb
```
Now, we have introduced a new configuration method, **Runtime Configuration** compatible with dotenv format:
```
# .prismarc
DATABASE_URL=postgres://user:password@localhost:5432/mydb

# Database host
DATABASE_HOST=postgres://user:password@localhost:5432
DATABASE_URL=${DATABASE_HOST}/mydb
```

If you have dotenv in mind, you just need to add the database URL configuration to `.env`:
```dotenv
DATABASE_URL=postgres://user:password@localhost:5432/mydb
```

For more usage of runtime configuration, please see 👉 https://pub.dev/packages/rc

#### Custom configuration

Now, you can customize your prisma project configuration in `pubspec.yaml`:
```yaml
...
prisma:
prismarc: path/a/b/c/custom.prismarc
dotenv: path/a/b/c/custom.env
schema: path/a/b/c/custom.prisma
...
```

| Parameter | Description | Default |
| --- | --- | --- |
`prismarc` | Custom runtime configuration path | `.prismarc`
| `dotenv` | Custom dotenv path | `.env` |
| `schema` | Custom Prisma schema path | `prisma/schema.prisma` |

#### Custom engine path

Previously, the Prisma engine was downloaded into the `.dart_tool/prisma` directory, now you can customize it.

##### Custom engines path with runtime configuration

```
# Query binary engine
PRISMA_QUERY_ENGINE_BINARY(path) = custom-engines/query-engine
# Migration binary engine
PRISMA_MIGRATION_ENGINE_BINARY(path) = custom-engines/migration-engine
# Introspection binary engine
PRISMA_INTROSPECTION_ENGINE_BINARY(path) = custom-engines/introspection-engine
# Format binary engine
PRISMA_FMT_BINARY(path) = custom-engines/prisma-fmt
```

##### Custom engines path with dotenv

```
PRISMA_QUERY_ENGINE_BINARY=path/to/custom-engines/query-engine
PRISMA_MIGRATION_ENGINE_BINARY=path/to/custom-engines/migration-engine
PRISMA_INTROSPECTION_ENGINE_BINARY=path/to/custom-engines/introspection-engine
PRISMA_FMT_BINARY=path/to/custom-engines/prisma-fmt
```

#### Refactored `package:orm/configure.dart`

Previously, we have `package:orm/configure.dart` to configure Prisma, now we have refactored it to `package:orm/prisma.dart`:
```dart
import 'package:orm/configure.dart';
print(configure('DATABASE_URL'));
```

Now, you can use `package:orm/configure.dart` to configure Prisma:
```dart
import 'package:orm/configure.dart';
print(environment.DATABASE_URL);
```

### Bug fixes:

1. Fix map and throws binary errors - [#16](https://github.com/odroe/prisma-dart/pull/16)
2. Problems using Model with relation - [#14](https://github.com/odroe/prisma-dart/pull/14)

### Features:

1. Generator generate import support show.
2. Prisma CLI debug allow set to dotenv or runtime configuration.
3. Prisma CLI debug print stack trace.

## 2.0.1

### CLI
Expand Down
3 changes: 2 additions & 1 deletion bin/orm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'package:orm/configure.dart';
import 'package:orm/version.dart';

import 'src/commands/db/db_command.dart';
Expand Down Expand Up @@ -44,7 +45,7 @@ void main(List<String> args) async {
try {
await runner.runCommand(results);
} catch (error, stackTrace) {
if (!results.wasParsed('debug')) {
if (!results.wasParsed('debug') || environment.DEBUG != null) {
print(error);
print(stackTrace);
exit(1);
Expand Down
37 changes: 27 additions & 10 deletions bin/src/binary_engine/binary_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dart:isolate';

import 'package:archive/archive_io.dart';
import 'package:http/http.dart';
import 'package:orm/configure.dart';
import 'package:orm/configure.dart' as configure;

import '../utils/chmod.dart';
import '../utils/find_project.dart';
Expand All @@ -26,14 +26,30 @@ class BinaryEngine {
String get _archiveExtension =>
platform == BinaryEnginePlatform.windows ? '.exe.gz' : '.gz';

/// archive file name.
String get _archiveBasename => type.value + _archiveExtension;

/// Archive file path.
String get _archive => joinPaths(['.dart_tool', 'prisma', _archiveBasename]);
String get _archive => executable + _archiveExtension;

/// Executable file path.
String get executable => joinPaths(['.dart_tool', 'prisma', type.value]);
String get executable {
switch (type) {
case BinaryEngineType.query:
return configure.environment.PRISMA_QUERY_ENGINE_BINARY ??
_defaultEnginePathBuilder('query-engine');
case BinaryEngineType.migration:
return configure.environment.PRISMA_MIGRATION_ENGINE_BINARY ??
_defaultEnginePathBuilder('migration-engine');
case BinaryEngineType.introspection:
return configure.environment.PRISMA_INTROSPECTION_ENGINE_BINARY ??
_defaultEnginePathBuilder('introspection-engine');
case BinaryEngineType.format:
return configure.environment.PRISMA_FMT_BINARY ??
_defaultEnginePathBuilder('prisma-fmt');
}
}

/// Default engine path builder.
String _defaultEnginePathBuilder(String name) =>
joinPaths(['.dart_tool', 'prisma', name]);

/// Has the binary engine been downloaded.
Future<bool> get hasDownloaded async {
Expand Down Expand Up @@ -64,7 +80,7 @@ class BinaryEngine {
workingDirectory: projectDirectory,
includeParentEnvironment: false,
environment: <String, String>{
...configure.environment,
...configure.environment.all,
...environment,
},
);
Expand All @@ -75,7 +91,7 @@ class BinaryEngine {
arguments,
workingDirectory: projectDirectory,
includeParentEnvironment: false,
environment: configure.environment,
environment: configure.environment.all,
);

/// Download the binary engine.
Expand All @@ -90,12 +106,13 @@ class BinaryEngine {
await _clean();

// Create download url.
final Uri url = Uri.parse('https://binaries.prisma.sh').replace(
final Uri url =
Uri.parse(configure.environment.PRISMA_ENGINES_MIRROR).replace(
pathSegments: [
'all_commits',
version,
platform.value,
_archiveBasename,
type.value + _archiveExtension,
],
);

Expand Down
6 changes: 2 additions & 4 deletions bin/src/commands/db/subs/db_pull_sub_command.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:orm/configure.dart';
import 'package:orm/configure.dart' as configure;
import 'package:orm/version.dart';

import '../../../binary_engine/binary_engine.dart';
Expand All @@ -10,7 +10,6 @@ import '../../../binary_engine/binray_engine_type.dart';
import '../../../internal/introspection_engine.dart';
import '../../../internal/json_rpc.dart';
import '../../../utils/ansi_progress.dart';
import '../../../utils/find_project.dart';

class DbPullSubCommand extends Command<void> {
@override
Expand All @@ -28,8 +27,7 @@ class DbPullSubCommand extends Command<void> {
'schema',
help: 'Custom path to your Prisma schema',
valueHelp: 'path',
defaultsTo:
configure('schema', joinRelativePaths(['prisma', 'schema.prisma'])),
defaultsTo: configure.schema,
);
argParser.addOption(
'composite-type-depth',
Expand Down
6 changes: 2 additions & 4 deletions bin/src/commands/db/subs/db_push_sub_command.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:orm/configure.dart';
import 'package:orm/configure.dart' as configure;
import 'package:orm/version.dart';

import '../../../binary_engine/binary_engine.dart';
Expand All @@ -10,7 +10,6 @@ import '../../../binary_engine/binray_engine_type.dart';
import '../../../internal/json_rpc.dart';
import '../../../internal/migrate_engine.dart';
import '../../../utils/ansi_progress.dart';
import '../../../utils/find_project.dart';

class DbPushSubCommand extends Command {
@override
Expand All @@ -29,8 +28,7 @@ class DbPushSubCommand extends Command {
'schema',
help: 'Schema file path.',
valueHelp: 'path',
defaultsTo:
configure('schema', joinRelativePaths(['prisma', 'schema.prisma'])),
defaultsTo: configure.schema,
);
}

Expand Down
5 changes: 2 additions & 3 deletions bin/src/commands/format_command.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:orm/configure.dart';
import 'package:orm/configure.dart' as configure;
import 'package:orm/version.dart';

import '../binary_engine/binary_engine.dart';
Expand All @@ -16,8 +16,7 @@ class FormatCommand extends Command {
'schema',
help: 'Schema file path.',
valueHelp: 'path',
defaultsTo:
configure('schema', joinRelativePaths(['prisma', 'schema.prisma'])),
defaultsTo: configure.schema,
);
}

Expand Down
6 changes: 2 additions & 4 deletions bin/src/commands/generate_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:convert';
import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:orm/configure.dart';
import 'package:orm/configure.dart' as configure;
import 'package:orm/dmmf.dart';
import 'package:orm/generator_helper.dart';
import 'package:orm/orm.dart';
Expand All @@ -14,7 +14,6 @@ import '../binary_engine/binray_engine_type.dart';
import '../generator/generator.dart';
import '../generator/generator_options.dart';
import '../utils/ansi_progress.dart';
import '../utils/find_project.dart';

class GenerateCommand extends Command {
@override
Expand All @@ -28,8 +27,7 @@ class GenerateCommand extends Command {
'schema',
help: 'Custom path to your Prisma schema',
valueHelp: 'path',
defaultsTo:
configure('schema', joinRelativePaths(['prisma', 'schema.prisma'])),
defaultsTo: configure.schema,
);
}

Expand Down
9 changes: 2 additions & 7 deletions bin/src/commands/init_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,12 @@ class InitCommand extends Command {

/// Create prisma config file.
void createPrismaConfigFile(Uri uri) {
final File config = File(joinPaths(['prisma.yaml']));
final File config = File(joinPaths(['.prismarc']));
if (!config.existsSync()) {
config.createSync(recursive: true);
}

final String template = r'''
environment:
DATABASE_URL: {url}
''';
final String content = template.replaceAll('{url}', uri.toString());
config.writeAsStringSync(content);
config.writeAsStringSync('DATABASE_URL = ${uri.toString()}');
}

/// Create prisma schema file.
Expand Down
2 changes: 1 addition & 1 deletion bin/src/generator/client_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PrismaClient {
datasources: datasources?.toOverwrites() ?? const <String, runtime.Datasource> {},
dmmf: _dmmf,
schema: _schema,
environment: configure.environment,
environment: configure.environment.all,
executable: _executable,
);
Expand Down
8 changes: 4 additions & 4 deletions bin/src/generator/imports_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const List<_Import> _imports = <_Import>[
alias: 'convert',
show: ['json'],
),
_Import('package:orm/configure.dart'),
_Import('package:orm/configure.dart', alias: 'configure'),
_Import('package:orm/orm.dart', alias: 'runtime'),
_Import('package:orm/dmmf.dart', alias: 'dmmf'),
_Import('package:json_annotation/json_annotation.dart'),
Expand All @@ -29,9 +29,9 @@ Future<String> importsGenerator() async {
if (element.alias != null) {
imports.write(' as ${element.alias}');
}
// if (element.show.isNotEmpty) {
// imports.write(' show ${element.show.join(', ')}');
// }
if (element.show.isNotEmpty) {
imports.write(' show ${element.show.join(', ')}');
}

imports.writeln(';');
}
Expand Down
1 change: 1 addition & 0 deletions example/.prismarc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL = "postgres://seven@localhost:5432/demo?schema=public"
Loading

0 comments on commit bf3de28

Please sign in to comment.