Skip to content

Commit

Permalink
Update how we generate Prisma models.
Browse files Browse the repository at this point in the history
Shorten backlink names to `bk_<link>_<Type>` (cannot start with
underscore, so needs `bk` prefix).
Recognize and reflect array types.
Sort fields by name.
  • Loading branch information
vpetrovykh committed Feb 3, 2025
1 parent 208be79 commit 00c4eb2
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 71 deletions.
7 changes: 7 additions & 0 deletions integration-tests/prisma/dbschema/default.esdl
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ module default {
type UserGroup extending Named {
multi link users: User;
}

type AssortedScalars {
required name: str;
vals: array<str>;
ts: datetime;
bstr: bytes;
}
}
10 changes: 10 additions & 0 deletions integration-tests/prisma/dbschema/migrations/00002-m1l3ekc.edgeql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE MIGRATION m1l3ekcdknbfrm2zn5gucofoh7pyt5uxnbeoze7kkmlfp7b7nr3ira
ONTO m1b5kogggyycxixgy2mcrqu3ntk3hhagdcospowiernk6ddu6op6ia
{
CREATE TYPE default::AssortedScalars {
CREATE PROPERTY bstr: std::bytes;
CREATE REQUIRED PROPERTY name: std::str;
CREATE PROPERTY ts: std::datetime;
CREATE PROPERTY vals: array<std::str>;
};
};
131 changes: 106 additions & 25 deletions integration-tests/prisma/prisma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe("prisma", () => {
const res = await prisma.user.findMany({
select: {
name: true,
back_to_Post: {
bk_author_Post: {
select: {
body: true,
},
Expand All @@ -125,27 +125,27 @@ describe("prisma", () => {
assert.deepEqual(res, [
{
name: "Alice",
back_to_Post: [{ body: "Hello" }, { body: "I'm Alice" }],
bk_author_Post: [{ body: "Hello" }, { body: "I'm Alice" }],
},
{
name: "Billie",
back_to_Post: [],
bk_author_Post: [],
},
{
name: "Cameron",
back_to_Post: [{ body: "I'm Cameron" }],
bk_author_Post: [{ body: "I'm Cameron" }],
},
{
name: "Dana",
back_to_Post: [],
bk_author_Post: [],
},
{
name: "Elsa",
back_to_Post: [{ body: "*magic stuff*" }],
bk_author_Post: [{ body: "*magic stuff*" }],
},
{
name: "Zoe",
back_to_Post: [],
bk_author_Post: [],
},
]);
});
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("prisma", () => {
const res = await prisma.user.findMany({
select: {
name: true,
back_to_GameSession: {
bk_players_GameSession: {
select: {
source: {
select: {
Expand All @@ -200,27 +200,27 @@ describe("prisma", () => {
assert.deepEqual(res, [
{
name: "Alice",
back_to_GameSession: [{ source: { num: 123 } }],
bk_players_GameSession: [{ source: { num: 123 } }],
},
{
name: "Billie",
back_to_GameSession: [{ source: { num: 123 } }],
bk_players_GameSession: [{ source: { num: 123 } }],
},
{
name: "Cameron",
back_to_GameSession: [],
bk_players_GameSession: [],
},
{
name: "Dana",
back_to_GameSession: [{ source: { num: 456 } }],
bk_players_GameSession: [{ source: { num: 456 } }],
},
{
name: "Elsa",
back_to_GameSession: [],
bk_players_GameSession: [],
},
{
name: "Zoe",
back_to_GameSession: [],
bk_players_GameSession: [],
},
]);
});
Expand Down Expand Up @@ -266,7 +266,7 @@ describe("prisma", () => {
const res = await prisma.user.findMany({
select: {
name: true,
back_to_UserGroup: {
bk_users_UserGroup: {
select: {
source: {
select: {
Expand All @@ -281,33 +281,52 @@ describe("prisma", () => {
assert.deepEqual(res, [
{
name: "Alice",
back_to_UserGroup: [
bk_users_UserGroup: [
{ source: { name: "red" } },
{ source: { name: "green" } },
],
},
{
name: "Billie",
back_to_UserGroup: [
bk_users_UserGroup: [
{ source: { name: "red" } },
{ source: { name: "green" } },
],
},
{
name: "Cameron",
back_to_UserGroup: [{ source: { name: "red" } }],
bk_users_UserGroup: [{ source: { name: "red" } }],
},
{
name: "Dana",
back_to_UserGroup: [{ source: { name: "red" } }],
bk_users_UserGroup: [{ source: { name: "red" } }],
},
{
name: "Elsa",
back_to_UserGroup: [],
bk_users_UserGroup: [],
},
{
name: "Zoe",
back_to_UserGroup: [],
bk_users_UserGroup: [],
},
]);
});

testIfVersionGTE(6)("check read models 12", async () => {
const res = await prisma.assortedScalars.findMany({
select: {
name: true,
vals: true,
bstr: true,
ts: true,
},
});
assert.deepEqual(res, [
{
name: "hello world",
vals: ['brown', 'fox'],
bstr: new Uint8Array([119, 111, 114, 100, 0, 11]),
ts: new Date('2025-01-26T20:13:45Z'),
},
]);
});
Expand Down Expand Up @@ -359,7 +378,7 @@ describe("prisma", () => {
name: name,
},
include: {
back_to_UserGroup: {
bk_users_UserGroup: {
include: {
source: true,
},
Expand All @@ -368,7 +387,7 @@ describe("prisma", () => {
});

assert.equal(res!.name, name);
assert.equal(res!.back_to_UserGroup[0].source.name, "cyan");
assert.equal(res!.bk_users_UserGroup[0].source.name, "cyan");
assert.ok(res!.id);
}

Expand Down Expand Up @@ -501,12 +520,12 @@ describe("prisma", () => {
},
select: {
name: true,
back_to_Post: true,
bk_author_Post: true,
},
});
assert.deepEqual(res, {
name: "Elsa",
back_to_Post: [],
bk_author_Post: [],
});

throw new Rollback();
Expand Down Expand Up @@ -626,4 +645,66 @@ describe("prisma", () => {
}
}
});

testIfVersionGTE(6)("check update models 02", async () => {
try {
await prisma.$transaction(async (tx) => {
const scal = await tx.assortedScalars.findFirst({
where: {
name: "hello world",
},
});

const scal_id = scal!.id;
assert.ok(scal_id);
assert.equal(scal?.name, "hello world");

// name is not unique so deleteMany is used
await tx.assortedScalars.update({
where: {
id: scal.id,
},
data: {
name: 'New Name',
vals: scal.vals.concat('jumped'),
bstr: new Uint8Array([1, 115, 117, 99, 99, 101, 115, 115, 2]),
ts: new Date('2025-01-20T20:13:45Z'),
},
});

const nope = await tx.assortedScalars.findMany({
where: {
name: "hello world",
},
});
assert.deepEqual(nope, []);

const res = await tx.assortedScalars.findMany({
select: {
name: true,
vals: true,
bstr: true,
ts: true,
},
where: {
name: "New Name",
},
});
assert.deepEqual(res, [
{
name: "New Name",
vals: ['brown', 'fox', 'jumped'],
bstr: new Uint8Array([1, 115, 117, 99, 99, 101, 115, 115, 2]),
ts: new Date('2025-01-20T20:13:45Z'),
},
]);

throw new Rollback();
});
} catch (err) {
if (!(err instanceof Rollback)) {
throw err;
}
}
});
});
6 changes: 6 additions & 0 deletions integration-tests/prisma/setupTeardown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ export async function setupTests() {
author := assert_single((select User filter .name = 'Elsa')),
body := '*magic stuff*',
};
insert AssortedScalars {
name:= 'hello world',
vals := ['brown', 'fox'],
bstr := b'word\x00\x0b',
ts:=<datetime>'2025-01-26T20:13:45+00:00',
};
`);

return {
Expand Down
Loading

0 comments on commit 00c4eb2

Please sign in to comment.