Skip to content

Commit 02015fa

Browse files
committed
chore: add tests
1 parent b44df85 commit 02015fa

2 files changed

Lines changed: 532 additions & 1 deletion

File tree

packages/cli/test/ts-schema-gen.test.ts

Lines changed: 283 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,5 +747,287 @@ model Post {
747747
authType: 'User',
748748
plugins: {}
749749
});
750-
})
750+
});
751+
752+
it('supports specifying fields for @updatedAt', async () => {
753+
const { schema } = await generateTsSchema(`
754+
model User {
755+
id String @id @default(uuid())
756+
name String
757+
email String @unique
758+
createdAt DateTime @default(now())
759+
updatedAt DateTime @updatedAt(fields: [email])
760+
posts Post[]
761+
762+
@@map('users')
763+
}
764+
765+
model Post {
766+
id String @id @default(cuid())
767+
title String
768+
published Boolean @default(false)
769+
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
770+
authorId String
771+
}
772+
`);
773+
774+
expect(schema).toMatchObject({
775+
provider: {
776+
type: 'sqlite'
777+
},
778+
models: {
779+
User: {
780+
name: 'User',
781+
fields: {
782+
id: {
783+
name: 'id',
784+
type: 'String',
785+
id: true,
786+
attributes: [
787+
{
788+
name: '@id'
789+
},
790+
{
791+
name: '@default',
792+
args: [
793+
{
794+
name: 'value',
795+
value: {
796+
kind: 'call',
797+
function: 'uuid'
798+
}
799+
}
800+
]
801+
}
802+
],
803+
default: {
804+
kind: 'call',
805+
function: 'uuid'
806+
}
807+
},
808+
name: {
809+
name: 'name',
810+
type: 'String'
811+
},
812+
email: {
813+
name: 'email',
814+
type: 'String',
815+
unique: true,
816+
attributes: [
817+
{
818+
name: '@unique'
819+
}
820+
]
821+
},
822+
createdAt: {
823+
name: 'createdAt',
824+
type: 'DateTime',
825+
attributes: [
826+
{
827+
name: '@default',
828+
args: [
829+
{
830+
name: 'value',
831+
value: {
832+
kind: 'call',
833+
function: 'now'
834+
}
835+
}
836+
]
837+
}
838+
],
839+
default: {
840+
kind: 'call',
841+
function: 'now'
842+
}
843+
},
844+
updatedAt: {
845+
name: 'updatedAt',
846+
type: 'DateTime',
847+
updatedAt: {
848+
fields: [
849+
'email'
850+
]
851+
},
852+
attributes: [
853+
{
854+
name: '@updatedAt',
855+
args: [
856+
{
857+
name: 'fields',
858+
value: {
859+
kind: 'array',
860+
items: [
861+
{
862+
kind: 'field',
863+
field: 'email'
864+
}
865+
]
866+
}
867+
}
868+
]
869+
}
870+
]
871+
},
872+
posts: {
873+
name: 'posts',
874+
type: 'Post',
875+
array: true,
876+
relation: {
877+
opposite: 'author'
878+
}
879+
}
880+
},
881+
attributes: [
882+
{
883+
name: '@@map',
884+
args: [
885+
{
886+
name: 'name',
887+
value: {
888+
kind: 'literal',
889+
value: 'users'
890+
}
891+
}
892+
]
893+
}
894+
],
895+
idFields: [
896+
'id'
897+
],
898+
uniqueFields: {
899+
id: {
900+
type: 'String'
901+
},
902+
email: {
903+
type: 'String'
904+
}
905+
}
906+
},
907+
Post: {
908+
name: 'Post',
909+
fields: {
910+
id: {
911+
name: 'id',
912+
type: 'String',
913+
id: true,
914+
attributes: [
915+
{
916+
name: '@id'
917+
},
918+
{
919+
name: '@default',
920+
args: [
921+
{
922+
name: 'value',
923+
value: {
924+
kind: 'call',
925+
function: 'cuid'
926+
}
927+
}
928+
]
929+
}
930+
],
931+
default: {
932+
kind: 'call',
933+
function: 'cuid'
934+
}
935+
},
936+
title: {
937+
name: 'title',
938+
type: 'String'
939+
},
940+
published: {
941+
name: 'published',
942+
type: 'Boolean',
943+
attributes: [
944+
{
945+
name: '@default',
946+
args: [
947+
{
948+
name: 'value',
949+
value: {
950+
kind: 'literal',
951+
value: false
952+
}
953+
}
954+
]
955+
}
956+
],
957+
default: false
958+
},
959+
author: {
960+
name: 'author',
961+
type: 'User',
962+
attributes: [
963+
{
964+
name: '@relation',
965+
args: [
966+
{
967+
name: 'fields',
968+
value: {
969+
kind: 'array',
970+
items: [
971+
{
972+
kind: 'field',
973+
field: 'authorId'
974+
}
975+
]
976+
}
977+
},
978+
{
979+
name: 'references',
980+
value: {
981+
kind: 'array',
982+
items: [
983+
{
984+
kind: 'field',
985+
field: 'id'
986+
}
987+
]
988+
}
989+
},
990+
{
991+
name: 'onDelete',
992+
value: {
993+
kind: 'literal',
994+
value: 'Cascade'
995+
}
996+
}
997+
]
998+
}
999+
],
1000+
relation: {
1001+
opposite: 'posts',
1002+
fields: [
1003+
'authorId'
1004+
],
1005+
references: [
1006+
'id'
1007+
],
1008+
onDelete: 'Cascade'
1009+
}
1010+
},
1011+
authorId: {
1012+
name: 'authorId',
1013+
type: 'String',
1014+
foreignKeyFor: [
1015+
'author'
1016+
]
1017+
}
1018+
},
1019+
idFields: [
1020+
'id'
1021+
],
1022+
uniqueFields: {
1023+
id: {
1024+
type: 'String'
1025+
}
1026+
}
1027+
}
1028+
},
1029+
authType: 'User',
1030+
plugins: {}
1031+
});
1032+
});
7511033
});

0 commit comments

Comments
 (0)