diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..8532f661 --- /dev/null +++ b/.github/workflows/test.yml @@ -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 diff --git a/bin/src/generate_output.dart b/bin/src/generate_output.dart index 15416400..69495bd2 100644 --- a/bin/src/generate_output.dart +++ b/bin/src/generate_output.dart @@ -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) { diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 00000000..7f535902 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +generated/ +test.db \ No newline at end of file diff --git a/test/find_many_include_ref_test.dart b/test/find_many_include_ref_test.dart new file mode 100644 index 00000000..7d1ecab7 --- /dev/null +++ b/test/find_many_include_ref_test.dart @@ -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); + }); +} diff --git a/test/schema.prisma b/test/schema.prisma new file mode 100644 index 00000000..47f70ae5 --- /dev/null +++ b/test/schema.prisma @@ -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 +}