Skip to content

Commit

Permalink
Merge pull request #396 from medz/#395
Browse files Browse the repository at this point in the history
chore(version): orm-5.0.1 & orm_flutter-0.1.1
  • Loading branch information
Seven Du authored Jun 9, 2024
2 parents d304eaa + cb52977 commit 6711cd4
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 64 deletions.
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
# Prisma Dart v5.0.1 & Flutter integration 0.1.1

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

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

or update your `pubspec.yaml` file:

```yaml
dependencies:
orm: ^5.0.1
```
---
FLutter integration:
```yaml
dependencies:
orm_flutter: ^0.1.1
```
* [**"Prisma Dart v4 -> v5 & Flutter integration" Upgrade Guides**](https://prisma.pub/getting-started/upgrade_guides.html)
* [**"Flutter Integration FAQ**](https://prisma.pub/getting-started/flutter.html#faq)
## What's Changed
* Remove the `isProxy` for `Prisma.validateDatasourceURL` utils.
* When building the client, automatically download the Prisma C-ABI engine if `engineType` is `flutter`.

**Full Changelog**: https://github.com/medz/prisma-dart/compare/orm-v5.0.0+orm_flutter-v0.1.0...orm-v5.0.1+orm_flutter-v0.1.1

# Prisma Dart v5.0.0 & Flutter integration 0.1.0

To install Prisma ORM for Dart v5.0.0 run this command:
Expand Down
47 changes: 47 additions & 0 deletions docs/getting-started/flutter.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,50 @@ Future<void> main() async {
## 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)

## FAQ

### Error (Xcode): Undefined symbol: `prisma_*`

This is due to a library compilation failure, which will persist even if you download a new fixed version.

Solution: Run the command:

```bash
flutter clean
```

### Not fond `*/query_engine/*/libquery_engine.a` file

This is because the automatic download of the Prisma static query engine library failed.

To fix it, run the following command:

```bash
dart run orm_flutter:dl_engine
```

### Other unknown error solutions:

Most problems can be solved by using the following combination of commands:

```bash
flutter clean # Clean flutter cache files
flutter pub get # Reinstall deps
dart run orm_flutter:dl_engine # Download prisma engine static library
<bun/npx/pnpx/yarn> prisma generate # Regenerate prisma client
```

### Every time you update the `orm_flutter` version

Since the Prisma C-ABI engine is available on CDN, the Flutter plugin does not support running a script before `build`. Plus, `pub.dev` has a package size limit.

So, after you install the new `orm_flutter` package, it does not include the Prisma Engine, and you need to run the following command to download it:

```bash
dart run orm_flutter:dl_engine
```

It is best to run `flutter clean` to reset the native build after re-downloading the engine. Note that the cleanup is only required for iOS.

If you have installed both `orm` and `orm_flutter` dependencies and generated the Prisma client for Flutter, you only need to run `prisma generate`, which will automatically find and download the engine. This greatly simplifies the initialization work.
2 changes: 1 addition & 1 deletion examples/flutter_with_orm/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
orm_flutter: 079441401e12c7f51f0080462dd959ad7a4b7197
orm_flutter: af9dae2b195d82487326dc0e1a014159f6b21887
path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46

PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796
Expand Down
12 changes: 2 additions & 10 deletions examples/flutter_with_orm/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.0"
logging:
dependency: transitive
description:
name: logging
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
matcher:
dependency: transitive
description:
Expand Down Expand Up @@ -281,14 +273,14 @@ packages:
path: "../../packages/orm"
relative: true
source: path
version: "5.0.0"
version: "5.0.1"
orm_flutter:
dependency: "direct main"
description:
path: "../../packages/orm_flutter"
relative: true
source: path
version: "0.0.6"
version: "0.1.1"
package_config:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion examples/with_sqlite/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ environment:

# Add regular dependencies here.
dependencies:
logging: ^1.2.0
orm:
path: ../../packages/orm

Expand Down
11 changes: 2 additions & 9 deletions packages/orm/bin/orm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:path/path.dart';

import 'src/generator.dart';
import 'src/utils/is_flutter_engine_type.dart';
// import 'src/generator+client.dart';
import 'src/download_engine.dart';

void main() async {
final app = GeneratorApp.stdio(stdin: stdin, stdout: stderr);
Expand Down Expand Up @@ -54,14 +54,7 @@ Future<void> generate(GeneratorOptions options) async {
await output.writeAsString(formated);
}

if (!isFlutterEngineType(options.generator.config)) {
// Copy prisma query engine.
final engineDownloadPath =
options.binaryPaths.queryEngine?.values.firstOrNull;
if (engineDownloadPath != null) {
await File(engineDownloadPath).copy('prisma-query-engine');
}
}
await downloadEngine(options);
}

extension on File {
Expand Down
75 changes: 75 additions & 0 deletions packages/orm/bin/src/download_engine.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'dart:io';

import 'package:yaml/yaml.dart';
import 'package:orm/generator_helper.dart';
import 'package:orm/src/_internal/project_directory.dart';
import 'package:path/path.dart' as path;

import 'utils/is_flutter_engine_type.dart';

Future<void> downloadEngine(GeneratorOptions options) async {
if (isFlutterEngineType(options.generator.config)) {
return downloadFlutterQueryEngine(options);
}

final sourcePath = options.binaryPaths.queryEngine?.values.firstOrNull;
if (sourcePath == null) {
print("⚠️[orm] Binary engine not found.");
return;
}

final source = File(sourcePath);
if (!await source.exists()) {
print(
'⚠️[orm] Prisma provided the engine path, but the file does not exist');
return;
}

final target =
File(path.join(options.generator.output!.value, 'prisma-query-engine'));
if (await target.exists()) {
await target.delete(recursive: true);
}

if (!await target.parent.exists()) {
await target.parent.create(recursive: true);
}

await source.copy(target.path);
}

Future<void> downloadFlutterQueryEngine(GeneratorOptions options) async {
final projectDir = findProjectDirectory(path.dirname(options.schemaPath));
if (projectDir == null) {
throw "⚠️[orm] Please generate a client in your project directory";
}

final pubspec = loadYaml(
await File(path.join(projectDir.path, 'pubspec.yaml')).readAsString());
if (pubspec case {"dependencies": {"orm_flutter": _}}) {
final process = await Process.run(
'dart',
['run', 'orm_flutter:dl_engine'],
workingDirectory: projectDir.path,
includeParentEnvironment: true,
);
if (process.exitCode == 0) {
return;
}

print(process.stdout);
print(process.stderr);
return;
}

throw '''
You are generating a Prisma client for Flutter
It is detected that you do not have the `orm_flutter` integration package installed.
**Please use the following command to install:**
flutter pub add orm_flutter
After the installation is complete, please regenerate the client.
''';
}
10 changes: 8 additions & 2 deletions packages/orm/lib/src/_internal/project_directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import 'dart:io';

import 'package:path/path.dart' as path;

Directory? findProjectDirectory() =>
_nestFindPubspecDirectory(File.fromUri(Platform.script).parent);
Directory? findProjectDirectory([String? directory]) {
if (directory != null) {
return _nestFindPubspecDirectory(Directory(directory));
}

return _nestFindPubspecDirectory(Directory.current) ??
_nestFindPubspecDirectory(File.fromUri(Platform.script).parent);
}

Directory? _nestFindPubspecDirectory(Directory directory) {
try {
Expand Down
13 changes: 4 additions & 9 deletions packages/orm/lib/src/datasources/_validate_datasource_url.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import '../errors.dart';

const _allowdProtocels = <String>[
'file',
'mysql',
'postgresql',
'mongodb',
'sqlserver'
'sqlserver',
'prisma'
];

PrismaClientInitializationError _createInvalidDatasourceError(String message) =>
PrismaClientInitializationError(errorCode: 'P1013', message: message);

String validateDatasourceURL(String datasourceUrl, {bool isProxy = false}) {
String validateDatasourceURL(String datasourceUrl) {
final url = Uri.tryParse(datasourceUrl);

if (isProxy && url?.scheme == 'prisma') {
return url.toString();
} else if (isProxy) {
throw _createInvalidDatasourceError(
"Proxy connection URL must use the `prisma://` protocol");
}

if (url?.scheme == 'file') {
throw _createInvalidDatasourceError(
"The current platform does not support SQLite, please change the datasource URL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import '../_internal/project_directory.dart';
import '../errors.dart';
import '_validate_datasource_url.dart' as shared;

String validateDatasourceURL(String datasourceUrl, {bool isProxy = false}) {
String validateDatasourceURL(String datasourceUrl) {
final url = Uri.tryParse(datasourceUrl);
if (url?.scheme != 'file' || url == null) {
return shared.validateDatasourceURL(datasourceUrl, isProxy: isProxy);
return shared.validateDatasourceURL(datasourceUrl);
}

final pwd = findProjectDirectory()?.path ?? Directory.current.path;
Expand Down
5 changes: 2 additions & 3 deletions packages/orm/lib/src/datasources/prisma+datasource_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import '_validate_datasource_url.dart'
as validate_datasource_url;

extension Prisma$DatasourceUtils on PrismaNamespace {
String validateDatasourceURL(String datasourceUrl, {bool isProxy = false}) =>
validate_datasource_url.validateDatasourceURL(datasourceUrl,
isProxy: isProxy);
String validateDatasourceURL(String datasourceUrl) =>
validate_datasource_url.validateDatasourceURL(datasourceUrl);
}
24 changes: 17 additions & 7 deletions packages/orm/lib/src/engines/binary_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,14 @@ extension on BinaryEngine {
if (options.datasourceUrl != null) {
return MapEntry(
name,
Prisma.validateDatasourceURL(options.datasourceUrl!, isProxy: false),
Prisma.validateDatasourceURL(options.datasourceUrl!),
);
}

if (options.datasources?.containsKey(name) == true) {
return MapEntry(
name,
Prisma.validateDatasourceURL(options.datasources![name]!,
isProxy: false),
Prisma.validateDatasourceURL(options.datasources![name]!),
);
}

Expand All @@ -231,12 +230,23 @@ extension on BinaryEngine {
),
};

return MapEntry(name, Prisma.validateDatasourceURL(url, isProxy: false));
return MapEntry(name, Prisma.validateDatasourceURL(url));
});

final datasources = overwriteDatasources.entries
.map((e) => {'name': e.key, 'url': e.value})
.toList();
Map<String, String> generateDatasourceItem(MapEntry<String, String> e) {
if (e.value.startsWith('prisma://')) {
throw PrismaClientInitializationError(
errorCode: 'P1013',
message:
'The binary engine does not support Prisma Proxy connection URL',
);
}

return {'name': e.key, 'url': e.value};
}

final datasources =
overwriteDatasources.entries.map(generateDatasourceItem).toList();

return base64.encode(utf8.encode(json.encode(datasources)));
}
Expand Down
5 changes: 3 additions & 2 deletions packages/orm/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "dart run orm"
output = "client"
provider = "dart run orm"
output = "client"
engineType = "binary"
}

datasource db {
Expand Down
3 changes: 2 additions & 1 deletion packages/orm/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: orm
description: Next-generation ORM for Dart & Flutter | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB.
version: 5.0.0
version: 5.0.1
homepage: https://prisma.pub
repository: https://github.com/medz/prisma-dart
funding:
Expand All @@ -23,6 +23,7 @@ dependencies:
recase: ^4.1.0
stream_channel: ^2.1.2
webfetch: ^0.0.17
yaml: ^3.1.2

dev_dependencies:
lints: ^4.0.0
Expand Down
9 changes: 1 addition & 8 deletions packages/orm_flutter/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ else()
set (QUERY_ENGINE_LIB ${CMAKE_CURRENT_SOURCE_DIR}/query_engine/${ANDROID_ABI}/libquery_engine.a)
endif()

add_custom_command(OUTPUT ${QUERY_ENGINE_LIB}
COMMAND dart run orm_flutter:dl_engine
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../
)

add_custom_target(query_engine DEPENDS ${QUERY_ENGINE_LIB})

# add_library(query_engine STATIC IMPORTED)
add_library(query_engine STATIC IMPORTED)

set_target_properties(query_engine PROPERTIES
IMPORTED_LOCATION ${QUERY_ENGINE_LIB}
Expand Down
Loading

0 comments on commit 6711cd4

Please sign in to comment.