import { autoProperty, autoPropertySet, autoRelation, mapFromPrismaSchema, prismaSchemaBuilder, } from './'; // Define our model types type AddressModel = { country: string; }; type UserModel = { name: string; email: string; age?: number; address: AddressModel; }; // Define Prisma input types (simplified for this example) type AddressCreateInput = AddressModel; type AddressUpdateInput = Partial; type UserCreateInput = Omit & { address: { create?: AddressCreateInput | null; connect?: AddressUpdateInput | null; }; }; type UserUpdateInput = Partial> & { address?: { update?: AddressUpdateInput | null; connect?: AddressUpdateInput | null; } | null; }; // Create schemas export const addressSchema = prismaSchemaBuilder< AddressCreateInput, AddressUpdateInput, AddressModel >({ props: { country: autoProperty({ set: true }), }, create: {}, update: {}, }); export const userSchema = prismaSchemaBuilder< UserCreateInput, UserUpdateInput, UserModel >({ props: { name: autoPropertySet(), email: autoPropertySet(), age: autoPropertySet(), address: autoRelation(() => addressSchema.relation()), }, create: {}, update: {}, }); describe('Demo Prisma Inputs', () => { // Sample test data const user = { name: 'John Doe', email: 'john@example.com', age: 30, address: { country: 'USA', }, }; describe('Create Operations', () => { it('should map a user model to create input', () => { if (!userSchema.createSchema) { throw new Error('Create schema is required'); } const createInput = mapFromPrismaSchema({ schema: userSchema.createSchema, value: user, }); expect(createInput).toEqual({ name: 'John Doe', email: 'john@example.com', age: 30, address: { create: { country: 'USA', }, }, }); }); it('should handle create input without optional fields', () => { if (!userSchema.createSchema) { throw new Error('Create schema is required'); } const partialUser = { name: 'Jane Doe', email: 'jane@example.com', address: { country: 'Canada', }, }; const createInput = mapFromPrismaSchema({ schema: userSchema.createSchema, value: partialUser, }); expect(createInput).toEqual({ name: 'Jane Doe', email: 'jane@example.com', address: { create: { country: 'Canada', }, }, }); }); }); describe('Update Operations', () => { it('should map partial updates correctly', () => { if (!userSchema.updateSchema) { throw new Error('Update schema is required'); } const updateData = { email: 'johndoe@example.com', address: { country: 'Switzerland', }, }; const updateInput = mapFromPrismaSchema({ schema: userSchema.updateSchema, value: updateData, oldValue: user, }); expect(updateInput).toEqual({ email: { set: 'johndoe@example.com' }, address: { update: { country: { set: 'Switzerland' }, }, }, }); }); it('should handle multiple field updates', () => { if (!userSchema.updateSchema) { throw new Error('Update schema is required'); } const updateData = { age: 31, address: { country: 'Italy', }, }; const updateInput = mapFromPrismaSchema({ schema: userSchema.updateSchema, value: updateData, oldValue: user, }); expect(updateInput).toEqual({ age: { set: 31 }, address: { update: { country: { set: 'Italy' }, }, }, }); }); }); describe('Address Schema', () => { it('should handle address create operations', () => { if (!addressSchema.createSchema) { throw new Error('Create schema is required'); } const address = { country: 'France', }; const createInput = mapFromPrismaSchema({ schema: addressSchema.createSchema, value: address, }); expect(createInput).toEqual({ country: 'France', }); }); it('should handle address update operations', () => { if (!addressSchema.updateSchema) { throw new Error('Update schema is required'); } const oldAddress = { country: 'Germany', }; const newAddress = { country: 'Austria', }; const updateInput = mapFromPrismaSchema({ schema: addressSchema.updateSchema, value: newAddress, oldValue: oldAddress, }); expect(updateInput).toEqual({ country: { set: 'Austria' }, }); }); }); });