-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from fdarian/fix-one-to-one-relation-optionality
fix(generator): one-to-one optionality
- Loading branch information
Showing
9 changed files
with
194 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { schema } from 'prisma/drizzle/schema' | ||
import { db } from 'src/lib/postgres' | ||
import type { Db, Schema } from 'src/lib/types' | ||
import { testOneToOneType } from './shared/testOneToOneType' | ||
import type { TestContext } from './utils/types' | ||
|
||
const ctx: TestContext = { | ||
db: db as unknown as Db, | ||
schema: schema as unknown as Schema, | ||
provider: 'mysql', | ||
} | ||
|
||
testOneToOneType(ctx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { schema } from 'prisma/drizzle/schema' | ||
import { db } from 'src/lib/postgres' | ||
import { testOneToOneType } from './shared/testOneToOneType' | ||
import type { TestContext } from './utils/types' | ||
|
||
const ctx: TestContext = { db, schema, provider: 'postgres' } | ||
|
||
testOneToOneType(ctx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { createId } from '@paralleldrive/cuid2' | ||
import { eq } from 'drizzle-orm' | ||
import { throwIfnull } from 'tests/utils/query' | ||
import type { TestContext } from 'tests/utils/types' | ||
|
||
export function testOneToOneType({ db, schema }: TestContext) { | ||
const b: typeof schema.oneToOneBs.$inferInsert = { id: createId() } | ||
const a: typeof schema.oneToOneAs.$inferInsert = { id: createId(), bId: b.id } | ||
const c: typeof schema.oneToOneCs.$inferInsert = { id: createId(), bId: b.id } | ||
|
||
describe('one to one (type)', () => { | ||
beforeAll(async () => { | ||
await db.insert(schema.oneToOneBs).values(b) | ||
await db.insert(schema.oneToOneCs).values(c) | ||
await db.insert(schema.oneToOneAs).values(a) | ||
}) | ||
|
||
afterAll(async () => { | ||
await db.delete(schema.oneToOneAs).where(eq(schema.oneToOneAs.id, a.id)) | ||
await db.delete(schema.oneToOneCs).where(eq(schema.oneToOneCs.id, c.id)) | ||
await db.delete(schema.oneToOneBs).where(eq(schema.oneToOneBs.id, b.id)) | ||
}) | ||
|
||
test('a.b (holds foreign key - optional)', async () => { | ||
const result = await db.query.oneToOneAs | ||
.findFirst({ | ||
where: (oneToOneAs, { eq }) => eq(oneToOneAs.id, a.id), | ||
with: { | ||
b: true, | ||
}, | ||
}) | ||
.then(throwIfnull) | ||
|
||
expectTypeOf(result.b).toBeNullable() | ||
}) | ||
|
||
test('c.b (holds foreign key - required)', async () => { | ||
const result = await db.query.oneToOneCs | ||
.findFirst({ | ||
where: (oneToOneCs, { eq }) => eq(oneToOneCs.id, c.id), | ||
with: { | ||
b: true, | ||
}, | ||
}) | ||
.then(throwIfnull) | ||
|
||
expectTypeOf(result.b).not.toBeNullable() | ||
}) | ||
|
||
test('b.{a,c} (being referenced)', async () => { | ||
const result = await db.query.oneToOneBs | ||
.findFirst({ | ||
where: (oneToOneBs, { eq }) => eq(oneToOneBs.id, b.id), | ||
with: { | ||
a: true, | ||
c: true, | ||
}, | ||
}) | ||
.then(throwIfnull) | ||
|
||
expectTypeOf(result.a).toBeNullable() | ||
expectTypeOf(result.c).toBeNullable() | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { schema } from 'prisma/sqlite/drizzle/schema' | ||
import { db } from 'src/lib/sqlite' | ||
import type { Db, Schema } from 'src/lib/types' | ||
import { testOneToOneType } from './shared/testOneToOneType' | ||
import type { TestContext } from './utils/types' | ||
|
||
const ctx: TestContext = { | ||
db: db as unknown as Db, | ||
schema: schema as unknown as Schema, | ||
provider: 'sqlite', | ||
} | ||
|
||
testOneToOneType(ctx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters