|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { createTestClient } from '../utils'; |
| 3 | + |
| 4 | +describe('Computed fields tests', () => { |
| 5 | + it('works with non-optional fields', async () => { |
| 6 | + const db = await createTestClient( |
| 7 | + ` |
| 8 | +model User { |
| 9 | + id Int @id @default(autoincrement()) |
| 10 | + name String |
| 11 | + upperName String @computed |
| 12 | +} |
| 13 | +`, |
| 14 | + { |
| 15 | + computedFields: { |
| 16 | + User: { |
| 17 | + upperName: (eb) => eb.fn('upper', ['name']), |
| 18 | + }, |
| 19 | + }, |
| 20 | + } as any, |
| 21 | + ); |
| 22 | + |
| 23 | + await expect( |
| 24 | + db.user.create({ |
| 25 | + data: { id: 1, name: 'Alex' }, |
| 26 | + }), |
| 27 | + ).resolves.toMatchObject({ |
| 28 | + upperName: 'ALEX', |
| 29 | + }); |
| 30 | + |
| 31 | + await expect( |
| 32 | + db.user.findUnique({ |
| 33 | + where: { id: 1 }, |
| 34 | + select: { upperName: true }, |
| 35 | + }), |
| 36 | + ).resolves.toMatchObject({ |
| 37 | + upperName: 'ALEX', |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('is typed correctly for non-optional fields', async () => { |
| 42 | + await createTestClient( |
| 43 | + ` |
| 44 | +model User { |
| 45 | + id Int @id @default(autoincrement()) |
| 46 | + name String |
| 47 | + upperName String @computed |
| 48 | +} |
| 49 | +`, |
| 50 | + { |
| 51 | + extraSourceFiles: { |
| 52 | + main: ` |
| 53 | +import { ZenStackClient } from '@zenstackhq/runtime'; |
| 54 | +import { schema } from './schema'; |
| 55 | +
|
| 56 | +async function main() { |
| 57 | + const client = new ZenStackClient(schema, { |
| 58 | + dialectConfig: {} as any, |
| 59 | + computedFields: { |
| 60 | + User: { |
| 61 | + upperName: (eb) => eb.fn('upper', ['name']), |
| 62 | + }, |
| 63 | + } |
| 64 | + }); |
| 65 | +
|
| 66 | + const user = await client.user.create({ |
| 67 | + data: { id: 1, name: 'Alex' } |
| 68 | + }); |
| 69 | + console.log(user.upperName); |
| 70 | + // @ts-expect-error |
| 71 | + user.upperName = null; |
| 72 | +} |
| 73 | +
|
| 74 | +main(); |
| 75 | +`, |
| 76 | + }, |
| 77 | + }, |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('works with optional fields', async () => { |
| 82 | + const db = await createTestClient( |
| 83 | + ` |
| 84 | +model User { |
| 85 | + id Int @id @default(autoincrement()) |
| 86 | + name String |
| 87 | + upperName String? @computed |
| 88 | +} |
| 89 | +`, |
| 90 | + { |
| 91 | + computedFields: { |
| 92 | + User: { |
| 93 | + upperName: (eb) => eb.lit(null), |
| 94 | + }, |
| 95 | + }, |
| 96 | + } as any, |
| 97 | + ); |
| 98 | + |
| 99 | + await expect( |
| 100 | + db.user.create({ |
| 101 | + data: { id: 1, name: 'Alex' }, |
| 102 | + }), |
| 103 | + ).resolves.toMatchObject({ |
| 104 | + upperName: null, |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('is typed correctly for optional fields', async () => { |
| 109 | + await createTestClient( |
| 110 | + ` |
| 111 | +model User { |
| 112 | + id Int @id @default(autoincrement()) |
| 113 | + name String |
| 114 | + upperName String? @computed |
| 115 | +} |
| 116 | +`, |
| 117 | + { |
| 118 | + extraSourceFiles: { |
| 119 | + main: ` |
| 120 | +import { ZenStackClient } from '@zenstackhq/runtime'; |
| 121 | +import { schema } from './schema'; |
| 122 | +
|
| 123 | +async function main() { |
| 124 | + const client = new ZenStackClient(schema, { |
| 125 | + dialectConfig: {} as any, |
| 126 | + computedFields: { |
| 127 | + User: { |
| 128 | + upperName: (eb) => eb.lit(null), |
| 129 | + }, |
| 130 | + } |
| 131 | + }); |
| 132 | +
|
| 133 | + const user = await client.user.create({ |
| 134 | + data: { id: 1, name: 'Alex' } |
| 135 | + }); |
| 136 | + console.log(user.upperName); |
| 137 | + user.upperName = null; |
| 138 | +} |
| 139 | +
|
| 140 | +main(); |
| 141 | +`, |
| 142 | + }, |
| 143 | + }, |
| 144 | + ); |
| 145 | + }); |
| 146 | +}); |
0 commit comments