Created
July 31, 2021 13:18
-
-
Save mrbrianevans/34f2947974d372b76c9b926dc6756ae5 to your computer and use it in GitHub Desktop.
A groovy script to generate typescript definitions in Jetbrains IDE's. Put this file in your \JetBrains\{ProductName}\extensions\com.intellij.database\schema directory, and right click a database table > scripted extensions to use it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.intellij.database.model.DasTable | |
import com.intellij.database.util.Case | |
import com.intellij.database.util.DasUtil | |
/* | |
* Available context bindings: | |
* SELECTION Iterable<DasObject> | |
* PROJECT project | |
* FILES files helper | |
*/ | |
typeMapping = [ | |
(~/(?i)int/) : "number", | |
(~/(?i)float|double|decimal|real/): "number", | |
(~/(?i)timestamp/) : "number", | |
(~/(?i)datetime|date/) : "Date", | |
(~/(?i)time/) : "string", | |
(~/(?i)text/) : "string", | |
(~/(?i)jsonb/) : 'Object', | |
(~/(?i)/) : 'unknown' | |
] | |
arrayModMapping = [ | |
(~/(?i)\[\]/) : '[]', | |
(~/(?i)/) : "", | |
] | |
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated definition file") { dir -> | |
SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) } | |
} | |
def generate(table, dir) { | |
def className = javaName(table.getName(), true) | |
def fields = calcFields(table) | |
new File(dir, "I${className}DatabaseItem.ts").withPrintWriter { out -> generate(out, className, fields) } | |
} | |
def generate(out, className, fields) { | |
out.println "// generated typescript definitions from database using groovy script" | |
out.println "" | |
out.println "export interface I${className}DatabaseItem {" | |
fields.each() { | |
out.println "\t${it.name}?: ${it.type}${it.arrayMod};" | |
} | |
out.println "}" | |
out.println "" | |
out.println "export interface I${className}Item {" | |
fields.each() { | |
out.println "\t${it.typeScriptName}?: ${it.type}${it.arrayMod};" | |
} | |
out.println "}" | |
out.println "" | |
out.println "export function convert${className}DatabaseItemToItem(databaseItem: I${className}DatabaseItem): I${className}Item {" | |
out.println "\tconst item = {" | |
fields.each() { | |
out.println "\t\t${it.typeScriptName}: databaseItem.${it.name}," | |
} | |
out.println "\t}" | |
out.println "\treturn item;" | |
out.println "}" | |
} | |
def calcFields(table) { | |
DasUtil.getColumns(table).reduce([]) { fields, col -> | |
def spec = Case.LOWER.apply(col.getDataType().getSpecification()) | |
def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value | |
def arrayMod = arrayModMapping.find {p, t -> p.matcher(spec).find() }.value | |
fields += [[ | |
name : col.getName(), | |
typeScriptName: getTypeScriptName(col.getName(), false), | |
type : typeStr, | |
arrayMod: arrayMod]] | |
} | |
} | |
def getTypeScriptName(str, capitalize) { | |
def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str) | |
.collect { Case.LOWER.apply(it).capitalize() } | |
.join("") | |
.replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_") | |
capitalize || s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment