Skip to content

Commit

Permalink
fix: Relations are throwing error when you use include #324
Browse files Browse the repository at this point in the history
  • Loading branch information
Seven Du committed Jan 18, 2024
1 parent 9e2349a commit b0862e7
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Test

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

# Install Node.js
- uses: actions/setup-node@v2
with:
node-version: 18

# Install Dart SDK
- uses: dart-lang/setup-dart@v1
with:
sdk: stable

- name: Install dependencies
run: dart pub get && npm install

- name: Generate test client & create test db
run: npx prisma db push --schema test/schema.prisma --accept-data-loss --force-reset

- name: Analyze
run: dart analyze
- name: Test
run: dart test
11 changes: 9 additions & 2 deletions bin/src/generate_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,22 @@ extension on Generator {
type: field.outputType.type,
));

final fromJson = Method((builder) {
builder.lambda = true;
builder.requiredParameters.add(Parameter((builder) {
builder.name = 'json';
}));
builder.body = type.property('fromJson').call([refer('json')]).code;
});

return refer('json')
.index(literalString(field.name))
.asA(TypeReference((type) {
type.symbol = 'Iterable';
type.types.add(refer('Map'));
type.isNullable = true;
}))
.nullSafeProperty('map')
.call([type.property('fromJson')]);
.call([fromJson.closure]);
}

Expression generateFromJsonEnumField(dmmf.OutputField field) {
Expand Down
2 changes: 2 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
generated/
test.db
47 changes: 47 additions & 0 deletions test/find_many_include_ref_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:orm/orm.dart';
import 'package:test/test.dart';

import 'generated/client.dart';
import 'generated/prisma.dart';

void main() {
late PrismaClient client;

setUpAll(() async {
client = PrismaClient(datasourceUrl: 'file:test/test.db');
await client.$connect();

// Clear database
await client.user.deleteMany();

// Seed database
await client.user.create(
data: PrismaUnion.$2(
UserUncheckedCreateInput(
name: "Seven",
posts: PostUncheckedCreateNestedManyWithoutAuthorInput(
create: PrismaUnion.$2(
PrismaUnion.$1([
PostCreateWithoutAuthorInput(title: "First post"),
PostCreateWithoutAuthorInput(title: "Second post"),
]),
),
),
),
),
);
});

tearDownAll(() async {
await client.$disconnect();
});

test('findMany include list refer', () async {
final users = await client.user.findMany(
include: UserInclude(posts: PrismaUnion.$1(true)),
);

expect(users.length, 1);
expect(users.first.posts?.length, 2);
});
}
22 changes: 22 additions & 0 deletions test/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
generator client {
provider = "dart run orm"
output = "generated"
}

datasource db {
provider = "sqlite"
url = "file:./test.db"
}

model User {
id Int @id @default(autoincrement())
name String
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
title String
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
authorId Int
}

1 comment on commit b0862e7

@vercel
Copy link

@vercel vercel bot commented on b0862e7 Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.