Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dms-service
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Roshan Suvarnkar
dms-service
Commits
e4413f8a
Commit
e4413f8a
authored
1 month ago
by
roshan
Browse files
Options
Downloads
Patches
Plain Diff
minor changes
parent
f2813f16
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
eslint.config.mjs
+2
-0
2 additions, 0 deletions
eslint.config.mjs
prisma/splitPrismaModels.ts
+76
-1
76 additions, 1 deletion
prisma/splitPrismaModels.ts
with
78 additions
and
1 deletion
eslint.config.mjs
+
2
−
0
View file @
e4413f8a
...
...
@@ -38,6 +38,8 @@ export default [
},
rules
:
{
...
safeGoogleRules
,
"
object-curly-spacing
"
:
[
"
error
"
,
"
always
"
],
'
new-cap
'
:
[
'
error
'
,
{
newIsCap
:
true
,
capIsNew
:
false
,
// Allow capitalized functions like decorators
...
...
This diff is collapsed.
Click to expand it.
prisma/splitPrismaModels.ts
+
76
−
1
View file @
e4413f8a
...
...
@@ -55,7 +55,48 @@ while ((modelMatch = modelRegex.exec(schema)) !== null) {
enums
[
name
]
??
types
[
name
]
??
''
);
const
fileContent
=
[...
dependencyBlocks
,
modelBlock
].
join
(
'
\n\n
'
);
let
enhancedModelBlock
=
modelBlock
;
enhancedModelBlock
=
enhancedModelBlock
.
replace
(
/^
(\s
*
)(\w
+
)\s
+String
(
.*
?)
$/gm
,
(
match
,
indent
,
field
,
rest
)
=>
{
const
isIdField
=
field
.
toLowerCase
()
===
'
id
'
||
field
.
toLowerCase
().
endsWith
(
'
_id
'
);
const
alreadyHasId
=
/@id/
.
test
(
rest
);
const
alreadyHasDefault
=
/@default/
.
test
(
rest
);
const
alreadyHasDb
=
/@db
\.
/
.
test
(
rest
);
if
(
isIdField
&&
alreadyHasId
)
{
return
`
${
indent
}${
field
}
String @id @default(uuid()) @db.Uuid`
;
}
return
match
;
}
);
// Enhance createdAt field
enhancedModelBlock
=
enhancedModelBlock
.
replace
(
/^
(\s
*
)(
created_at|createdAt
)\s
+DateTime
(
.*
?)
$/gim
,
(
match
,
indent
,
field
,
rest
)
=>
{
const
alreadyHasDefault
=
/@default
\(
.+
\)
/
.
test
(
rest
);
if
(
!
alreadyHasDefault
)
{
return
`
${
indent
}${
field
}
DateTime @default(now())`
;
}
return
match
;
}
);
// Enhance updatedAt field
enhancedModelBlock
=
enhancedModelBlock
.
replace
(
/^
(\s
*
)(
updated_at|updatedAt
)\s
+DateTime
(
.*
?)
$/gim
,
(
match
,
indent
,
field
,
rest
)
=>
{
const
alreadyHasUpdatedAt
=
/@updatedAt/
.
test
(
rest
);
if
(
!
alreadyHasUpdatedAt
)
{
return
`
${
indent
}${
field
}
DateTime @updatedAt`
;
}
return
match
;
}
);
enhancedModelBlock
=
alignModelBlock
(
enhancedModelBlock
);
const
fileContent
=
[...
dependencyBlocks
,
enhancedModelBlock
].
join
(
'
\n\n
'
);
const
outputPath
=
path
.
join
(
outputDir
,
`
${
modelName
}
.prisma`
);
fs
.
writeFileSync
(
outputPath
,
fileContent
,
'
utf-8
'
);
...
...
@@ -76,3 +117,37 @@ while ((modelMatch = modelRegex.exec(schema)) !== null) {
// fs.writeFileSync(inputPath, headerBlocks + '\n');
fs
.
writeFileSync
(
inputPath
,
''
+
'
\n
'
);
console
.
log
(
`Cleaned introspected.prisma (preserved generator & datasource)`
);
function
alignModelBlock
(
block
:
string
):
string
{
const
lines
=
block
.
trim
().
split
(
'
\n
'
);
const
modelStart
=
lines
[
0
];
// should be `model Something {`
const
modelEnd
=
lines
[
lines
.
length
-
1
];
// should be `}`
const
fieldLines
=
lines
.
slice
(
1
,
-
1
);
let
maxField
=
0
;
let
maxType
=
0
;
for
(
const
line
of
fieldLines
)
{
const
match
=
line
.
trim
().
match
(
/^
(\w
+
)\s
+
([^\s\[\]]
+
)(\s
|
\[
|$
)
/
);
if
(
match
)
{
maxField
=
Math
.
max
(
maxField
,
match
[
1
].
length
);
maxType
=
Math
.
max
(
maxType
,
match
[
2
].
length
);
}
}
const
formattedFields
=
fieldLines
.
map
(
line
=>
{
const
trimmed
=
line
.
trim
();
const
match
=
trimmed
.
match
(
/^
(\w
+
)\s
+
([^\s\[\]]
+
)(
.*
)
/
);
if
(
!
match
)
return
'
'
+
trimmed
;
const
[,
field
,
type
,
rest
]
=
match
;
const
paddedField
=
field
.
padEnd
(
maxField
+
1
);
const
paddedType
=
type
.
padEnd
(
maxType
+
1
);
return
`
${
paddedField
}${
paddedType
}${
rest
.
trim
()}
`
;
});
return
[
modelStart
,
...
formattedFields
,
modelEnd
].
join
(
'
\n
'
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment