import { expect, test } from 'vitest' import { list } from '@keystone-6/core' import { allowAll } from '@keystone-6/core/access' import { text } from '@keystone-6/core/fields' import { globSync } from 'node:fs' import { resolve } from 'node:path' import { dbProvider, getPrismaSchema } from './utils.ts' test('db.map at the list level adds @@map with the value to the Prisma schema', async () => { const prismaSchema = await getPrismaSchema({ lists: { SomeList: list({ access: allowAll, db: { map: 'some_table_name', }, fields: { someField: text(), }, }), }, }) expect(prismaSchema) .toEqual(`// This file is automatically generated by Keystone, do not modify it manually. // Modify your Keystone config when you want to change this. datasource ${dbProvider} { provider = "${dbProvider}" } generator client { provider = "prisma-client" output = "generated/prisma" } model SomeList { id String @id @default(cuid()) someField String @default("") @@map("some_table_name") } `) }) const testModules = globSync(`packages/*/src/**/test-fixtures.{js,ts}`).map(modulePath => resolve(modulePath) ) for (const modulePath of testModules) { const mod = await import(modulePath) if (mod.supportsDbMap) continue if (mod.unSupportedAdapterList?.includes(dbProvider)) continue test(`db.map for the field ${mod.name} adds @map with the value to the Prisma schema`, async () => { const prismaSchema = await getPrismaSchema({ lists: { SomeList: list({ access: allowAll, fields: { someField: mod.typeFunction({ ...mod.fieldConfig?.(), db: { map: 'db_map_field', }, }), }, }), }, }) expect(prismaSchema).toContain(' @map("db_map_field")') }) } // since we can only check whether the prisma schema contains the @map // for all the fields since they all generate different prisma schemas // this makes sure we generate it in the right place test(`db.map for the field text field adds @map with the value to the Prisma schema`, async () => { const prismaSchema = await getPrismaSchema({ lists: { SomeList: list({ access: allowAll, fields: { someField: text({ db: { map: 'db_map_field', }, }), }, }), }, }) expect(prismaSchema) .toEqual(`// This file is automatically generated by Keystone, do not modify it manually. // Modify your Keystone config when you want to change this. datasource ${dbProvider} { provider = "${dbProvider}" } generator client { provider = "prisma-client" output = "generated/prisma" } model SomeList { id String @id @default(cuid()) someField String @default("") @map("db_map_field") } `) })