Skip to content

Commit

Permalink
feat: extract default values from Prisma schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmezencio authored and AhmedElywa committed Nov 5, 2024
1 parent 859ddc8 commit 72e2591
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
"fields": [
{
"create": false,
"defaultValue": "@default(autoincrement())",
"editor": false,
"filter": true,
"id": "User.id",
Expand All @@ -42,6 +43,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": false,
"defaultValue": "@default(now())",
"editor": false,
"filter": true,
"id": "User.createdAt",
Expand All @@ -63,6 +65,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": true,
"defaultValue": undefined,
"editor": false,
"filter": true,
"id": "User.email",
Expand All @@ -84,6 +87,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": true,
"defaultValue": undefined,
"editor": false,
"filter": true,
"id": "User.name",
Expand All @@ -105,6 +109,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": true,
"defaultValue": undefined,
"editor": false,
"filter": true,
"id": "User.password",
Expand All @@ -126,6 +131,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": true,
"defaultValue": "@default(\"{}\")",
"editor": false,
"filter": true,
"id": "User.permissions",
Expand All @@ -147,6 +153,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": true,
"defaultValue": undefined,
"editor": false,
"filter": true,
"id": "User.posts",
Expand Down Expand Up @@ -180,6 +187,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
"fields": [
{
"create": false,
"defaultValue": "@default(autoincrement())",
"editor": false,
"filter": true,
"id": "Post.id",
Expand All @@ -201,6 +209,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": true,
"defaultValue": "@default(false)",
"editor": false,
"filter": true,
"id": "Post.published",
Expand All @@ -222,6 +231,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": true,
"defaultValue": undefined,
"editor": false,
"filter": true,
"id": "Post.title",
Expand All @@ -243,6 +253,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": true,
"defaultValue": undefined,
"editor": false,
"filter": true,
"id": "Post.author",
Expand All @@ -263,6 +274,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": false,
"defaultValue": undefined,
"editor": false,
"filter": true,
"id": "Post.authorId",
Expand All @@ -284,6 +296,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": false,
"defaultValue": "@default(now())",
"editor": false,
"filter": true,
"id": "Post.createdAt",
Expand All @@ -305,6 +318,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": false,
"defaultValue": undefined,
"editor": false,
"filter": true,
"id": "Post.updatedAt",
Expand Down
1 change: 1 addition & 0 deletions packages/schema/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class ConvertSchemaToObject extends PrismaReader {
type,
isId: !!line.find((part) => part.startsWith('@id')),
unique: !!line.find((part) => part.startsWith('@unique')),
defaultValue: line.find((part) => part.startsWith('@default')),
list: line[1].includes('[]'),
required: !line[1].includes('[]') && !line[1].includes('?'),
kind: this.getKind(type),
Expand Down
15 changes: 15 additions & 0 deletions packages/schema/tests/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from 'path';

test('generate Json object from prisma schema', () => {
const generate = new ConvertSchemaToObject(join(__dirname, './schemas/schema.prisma')).run();

expect(generate).toMatchInlineSnapshot(`
{
"enums": [
Expand All @@ -19,6 +20,7 @@ test('generate Json object from prisma schema', () => {
"documentation": "// User modal documentation",
"fields": [
{
"defaultValue": "@default(autoincrement())",
"documentation": "",
"isId": true,
"kind": "scalar",
Expand All @@ -31,6 +33,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": "@default(now())",
"documentation": "",
"isId": false,
"kind": "scalar",
Expand All @@ -43,6 +46,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": undefined,
"documentation": "",
"isId": false,
"kind": "scalar",
Expand All @@ -55,6 +59,7 @@ test('generate Json object from prisma schema', () => {
"unique": true,
},
{
"defaultValue": undefined,
"documentation": "",
"isId": false,
"kind": "scalar",
Expand All @@ -67,6 +72,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": undefined,
"documentation": "// password documentation
// password second line documentation",
"isId": false,
Expand All @@ -80,6 +86,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": "@default(\"{}\")",

Check failure on line 89 in packages/schema/tests/json.test.ts

View workflow job for this annotation

GitHub Actions / test

Unnecessary escape character: \"

Check failure on line 89 in packages/schema/tests/json.test.ts

View workflow job for this annotation

GitHub Actions / test

Unnecessary escape character: \"
"documentation": "",
"isId": false,
"kind": "scalar",
Expand All @@ -92,6 +99,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": undefined,
"documentation": "",
"isId": false,
"kind": "object",
Expand All @@ -110,6 +118,7 @@ test('generate Json object from prisma schema', () => {
"documentation": "",
"fields": [
{
"defaultValue": "@default(autoincrement())",
"documentation": "",
"isId": true,
"kind": "scalar",
Expand All @@ -122,6 +131,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": "@default(false)",
"documentation": "",
"isId": false,
"kind": "scalar",
Expand All @@ -134,6 +144,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": undefined,
"documentation": "",
"isId": false,
"kind": "scalar",
Expand All @@ -146,6 +157,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": undefined,
"documentation": "",
"isId": false,
"kind": "object",
Expand All @@ -165,6 +177,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": undefined,
"documentation": "",
"isId": false,
"kind": "scalar",
Expand All @@ -177,6 +190,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": "@default(now())",
"documentation": "",
"isId": false,
"kind": "scalar",
Expand All @@ -189,6 +203,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": undefined,
"documentation": "",
"isId": false,
"kind": "scalar",
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/generatedSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Field {
isId: boolean;
unique: boolean;
kind: 'object' | 'enum' | 'scalar';
defaultValue?: string;
map?: string;
relationField?: boolean;
documentation?: string;
Expand Down

0 comments on commit 72e2591

Please sign in to comment.