Skip to content

Commit

Permalink
feat: improve the defaultValue parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmezencio authored and AhmedElywa committed Nov 6, 2024
1 parent f7bba8f commit 9c2d8ea
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
"fields": [
{
"create": false,
"defaultValue": "@default(autoincrement())",
"defaultValue": "autoincrement()",
"editor": false,
"filter": true,
"id": "User.id",
Expand All @@ -43,7 +43,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": false,
"defaultValue": "@default(now())",
"defaultValue": "now()",
"editor": false,
"filter": true,
"id": "User.createdAt",
Expand Down Expand Up @@ -131,7 +131,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": true,
"defaultValue": "@default(\"{}\")",
"defaultValue": "\"{}\"",
"editor": false,
"filter": true,
"id": "User.permissions",
Expand Down Expand Up @@ -187,7 +187,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
"fields": [
{
"create": false,
"defaultValue": "@default(autoincrement())",
"defaultValue": "autoincrement()",
"editor": false,
"filter": true,
"id": "Post.id",
Expand All @@ -209,7 +209,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": true,
"defaultValue": "@default(false)",
"defaultValue": "false",
"editor": false,
"filter": true,
"id": "Post.published",
Expand Down Expand Up @@ -296,7 +296,7 @@ exports[`Generate Json Object For Admin Tables Should back with Json Settings 1`
},
{
"create": false,
"defaultValue": "@default(now())",
"defaultValue": "now()",
"editor": false,
"filter": true,
"id": "Post.createdAt",
Expand Down
4 changes: 4 additions & 0 deletions packages/schema/src/PrismaReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export class PrismaReader {
return undefined;
}

getDefaultValue(line: string) {
return line.match(/@default\((.*)\)/)?.[1];
}

getRelation(line: string) {
const relationString = line.match(/@relation\((.*?)\)/);
if (relationString) {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +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')),
defaultValue: this.getDefaultValue(lines[i]),
list: line[1].includes('[]'),
required: !line[1].includes('[]') && !line[1].includes('?'),
kind: this.getKind(type),
Expand Down
12 changes: 6 additions & 6 deletions packages/schema/tests/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('generate Json object from prisma schema', () => {
"documentation": "// User modal documentation",
"fields": [
{
"defaultValue": "@default(autoincrement())",
"defaultValue": "autoincrement()",
"documentation": "",
"isId": true,
"kind": "scalar",
Expand All @@ -33,7 +33,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": "@default(now())",
"defaultValue": "now()",
"documentation": "",
"isId": false,
"kind": "scalar",
Expand Down Expand Up @@ -86,7 +86,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": "@default("{}")",
"defaultValue": ""{}"",
"documentation": "",
"isId": false,
"kind": "scalar",
Expand Down Expand Up @@ -118,7 +118,7 @@ test('generate Json object from prisma schema', () => {
"documentation": "",
"fields": [
{
"defaultValue": "@default(autoincrement())",
"defaultValue": "autoincrement()",
"documentation": "",
"isId": true,
"kind": "scalar",
Expand All @@ -131,7 +131,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": "@default(false)",
"defaultValue": "false",
"documentation": "",
"isId": false,
"kind": "scalar",
Expand Down Expand Up @@ -190,7 +190,7 @@ test('generate Json object from prisma schema', () => {
"unique": false,
},
{
"defaultValue": "@default(now())",
"defaultValue": "now()",
"documentation": "",
"isId": false,
"kind": "scalar",
Expand Down

0 comments on commit 9c2d8ea

Please sign in to comment.