hekireki 0.8.4 → 0.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/ajv.js +1 -1
- package/dist/bin/arktype.js +1 -1
- package/dist/bin/dbml.js +1 -1
- package/dist/bin/docs.js +1 -1
- package/dist/bin/drizzle.js +1 -1
- package/dist/bin/ecto.js +1 -1
- package/dist/bin/effect.js +1 -1
- package/dist/bin/gorm.js +1 -1
- package/dist/bin/mermaid-er.js +1 -1
- package/dist/bin/sea-orm.js +1 -1
- package/dist/bin/sqlalchemy.js +1 -1
- package/dist/bin/typebox.js +1 -1
- package/dist/bin/valibot.js +1 -1
- package/dist/bin/zod.js +1 -1
- package/dist/{bin-5xZho8bJ.js → bin-Dcg9ZO7c.js} +30 -19
- package/package.json +1 -1
package/dist/bin/ajv.js
CHANGED
package/dist/bin/arktype.js
CHANGED
package/dist/bin/dbml.js
CHANGED
package/dist/bin/docs.js
CHANGED
package/dist/bin/drizzle.js
CHANGED
package/dist/bin/ecto.js
CHANGED
package/dist/bin/effect.js
CHANGED
package/dist/bin/gorm.js
CHANGED
package/dist/bin/mermaid-er.js
CHANGED
package/dist/bin/sea-orm.js
CHANGED
package/dist/bin/sqlalchemy.js
CHANGED
package/dist/bin/typebox.js
CHANGED
package/dist/bin/valibot.js
CHANGED
package/dist/bin/zod.js
CHANGED
|
@@ -1774,8 +1774,18 @@ function generateImports(imports, provider) {
|
|
|
1774
1774
|
...[...imports.ext.entries()].map(([pkg, fns]) => `import { ${[...fns].sort().join(", ")} } from '${pkg}'`)
|
|
1775
1775
|
].filter(Boolean).join("\n");
|
|
1776
1776
|
}
|
|
1777
|
-
function
|
|
1778
|
-
return name.
|
|
1777
|
+
function snakeToCamel(name) {
|
|
1778
|
+
return name.replace(/_+([a-zA-Z0-9])/g, (_, c) => c.toUpperCase());
|
|
1779
|
+
}
|
|
1780
|
+
function resolveTableName(model) {
|
|
1781
|
+
return model.dbName ?? makeSnakeCase(model.name);
|
|
1782
|
+
}
|
|
1783
|
+
function resolveVarName(model) {
|
|
1784
|
+
return snakeToCamel(resolveTableName(model));
|
|
1785
|
+
}
|
|
1786
|
+
function resolveVarNameByType(type, models) {
|
|
1787
|
+
const target = models.find((m) => m.name === type);
|
|
1788
|
+
return snakeToCamel(target ? resolveTableName(target) : makeSnakeCase(type));
|
|
1779
1789
|
}
|
|
1780
1790
|
function isFieldDefault(v) {
|
|
1781
1791
|
return typeof v === "object" && v !== null && "name" in v;
|
|
@@ -1921,17 +1931,17 @@ const PRISMA_ACTION_MAP = {
|
|
|
1921
1931
|
NoAction: "no action",
|
|
1922
1932
|
SetDefault: "set default"
|
|
1923
1933
|
};
|
|
1924
|
-
function makeFkReference(field, model) {
|
|
1934
|
+
function makeFkReference(field, model, models) {
|
|
1925
1935
|
const relField = model.fields.find((f) => f.kind === "object" && f.relationFromFields && f.relationFromFields.includes(field.name));
|
|
1926
1936
|
if (!(relField?.relationFromFields && relField.relationToFields)) return "";
|
|
1927
1937
|
if (relField.type === model.name) return "";
|
|
1928
|
-
const targetVar =
|
|
1938
|
+
const targetVar = resolveVarNameByType(relField.type, models);
|
|
1929
1939
|
const toCol = relField.relationToFields[0] ?? "id";
|
|
1930
1940
|
const onDelete = relField.relationOnDelete;
|
|
1931
1941
|
const drizzleAction = onDelete ? PRISMA_ACTION_MAP[onDelete] : void 0;
|
|
1932
1942
|
return `.references(() => ${targetVar}.${toCol}${drizzleAction ? `, { onDelete: '${drizzleAction}' }` : ""})`;
|
|
1933
1943
|
}
|
|
1934
|
-
function makeColumn(field, model, provider, imports, enums) {
|
|
1944
|
+
function makeColumn(field, model, models, provider, imports, enums) {
|
|
1935
1945
|
if (field.kind === "object") return null;
|
|
1936
1946
|
if (field.kind === "unsupported") return `// unsupported type: ${field.name}`;
|
|
1937
1947
|
const isAutoincrement = isFieldDefault(field.default) && field.default.name === "autoincrement";
|
|
@@ -1941,7 +1951,7 @@ function makeColumn(field, model, provider, imports, enums) {
|
|
|
1941
1951
|
field.isId && !hasCompositePK ? isAutoincrement && provider === "sqlite" ? ".primaryKey({ autoIncrement: true })" : ".primaryKey()" : "",
|
|
1942
1952
|
field.isRequired && !field.isId && !(isAutoincrement && provider === "postgresql") ? ".notNull()" : "",
|
|
1943
1953
|
field.isUnique ? ".unique()" : "",
|
|
1944
|
-
makeFkReference(field, model),
|
|
1954
|
+
makeFkReference(field, model, models),
|
|
1945
1955
|
isAutoincrement ? provider === "mysql" ? ".autoincrement()" : "" : field.isUpdatedAt && (field.default === void 0 || field.default === null) ? (() => {
|
|
1946
1956
|
const r = resolveUpdatedAtDefault(provider);
|
|
1947
1957
|
if (r.needsSql) imports.orm.add("sql");
|
|
@@ -1972,18 +1982,18 @@ function makeCompositeConstraints(model, imports, indexes, tableName) {
|
|
|
1972
1982
|
].filter((l) => l !== null);
|
|
1973
1983
|
return all.length > 0 ? all.join(", ") : null;
|
|
1974
1984
|
}
|
|
1975
|
-
function makeTable(model, provider, imports, enums, indexes) {
|
|
1985
|
+
function makeTable(model, models, provider, imports, enums, indexes) {
|
|
1976
1986
|
const tableFunc = provider === "postgresql" ? "pgTable" : provider === "mysql" ? "mysqlTable" : "sqliteTable";
|
|
1977
1987
|
imports.core.add(tableFunc);
|
|
1978
|
-
const varName =
|
|
1979
|
-
const tableName =
|
|
1980
|
-
const columns = model.fields.map((field) => makeColumn(field, model, provider, imports, enums)).filter((c) => c !== null).join(", ");
|
|
1988
|
+
const varName = resolveVarName(model);
|
|
1989
|
+
const tableName = resolveTableName(model);
|
|
1990
|
+
const columns = model.fields.map((field) => makeColumn(field, model, models, provider, imports, enums)).filter((c) => c !== null).join(", ");
|
|
1981
1991
|
const constraints = makeCompositeConstraints(model, imports, indexes, tableName);
|
|
1982
1992
|
return constraints ? `export const ${varName} = ${tableFunc}('${tableName}', { ${columns} }, (table) => [${constraints}])` : `export const ${varName} = ${tableFunc}('${tableName}', { ${columns} })`;
|
|
1983
1993
|
}
|
|
1984
|
-
function makeRelationField(field, model,
|
|
1985
|
-
const targetVar =
|
|
1986
|
-
const modelVar =
|
|
1994
|
+
function makeRelationField(field, model, models, relFields) {
|
|
1995
|
+
const targetVar = resolveVarNameByType(field.type, models);
|
|
1996
|
+
const modelVar = resolveVarName(model);
|
|
1987
1997
|
const needsAlias = relFields.filter((f) => f.type === field.type).length > 1 && field.relationName;
|
|
1988
1998
|
if (field.relationFromFields && field.relationFromFields.length > 0) {
|
|
1989
1999
|
const fromCol = field.relationFromFields[0];
|
|
@@ -2005,7 +2015,7 @@ function makeRelations(models, imports) {
|
|
|
2005
2015
|
return modelsWithRels.map((model) => {
|
|
2006
2016
|
const relFields = model.fields.filter((f) => f.kind === "object");
|
|
2007
2017
|
const fieldLines = relFields.map((field) => makeRelationField(field, model, models, relFields)).join(", ");
|
|
2008
|
-
const modelVar =
|
|
2018
|
+
const modelVar = resolveVarName(model);
|
|
2009
2019
|
const needsOne = relFields.some((f) => f.relationFromFields && f.relationFromFields.length > 0 || !f.isList);
|
|
2010
2020
|
const needsMany = relFields.some((f) => f.isList && !(f.relationFromFields && f.relationFromFields.length > 0));
|
|
2011
2021
|
return `export const ${modelVar}Relations = relations(${modelVar}, ({ ${[needsOne ? "one" : "", needsMany ? "many" : ""].filter(Boolean).join(", ")} }) => ({ ${fieldLines} }))`;
|
|
@@ -2016,7 +2026,7 @@ function makeRelations(models, imports) {
|
|
|
2016
2026
|
function drizzleSchema(datamodel, provider, indexes) {
|
|
2017
2027
|
const db = resolveDbProvider(provider);
|
|
2018
2028
|
const imports = createImports();
|
|
2019
|
-
const tableLines = datamodel.models.map((model) => makeTable(model, db, imports, datamodel.enums, indexes));
|
|
2029
|
+
const tableLines = datamodel.models.map((model) => makeTable(model, datamodel.models, db, imports, datamodel.enums, indexes));
|
|
2020
2030
|
const relationsLines = makeRelations(datamodel.models, imports);
|
|
2021
2031
|
const tableLinesWithGap = tableLines.flatMap((line, i) => i < tableLines.length - 1 ? [line, ""] : [line]);
|
|
2022
2032
|
const relationsLinesWithGap = relationsLines.flatMap((line, i) => i < relationsLines.length - 1 ? [line, ""] : [line]);
|
|
@@ -2735,8 +2745,8 @@ const RELATIONSHIPS = {
|
|
|
2735
2745
|
"zero-many": "}o",
|
|
2736
2746
|
many: "}|"
|
|
2737
2747
|
};
|
|
2738
|
-
function erRelationLine(relation) {
|
|
2739
|
-
return ` ${relation.from.model} ${RELATIONSHIPS[relation.from.cardinality]}--${RELATIONSHIPS[relation.to.cardinality]} ${relation.to.model} : "(${relation.from.field}) - (${relation.to.field})"`;
|
|
2748
|
+
function erRelationLine(relation, resolveName = (model) => model) {
|
|
2749
|
+
return ` ${resolveName(relation.from.model)} ${RELATIONSHIPS[relation.from.cardinality]}--${RELATIONSHIPS[relation.to.cardinality]} ${resolveName(relation.to.model)} : "(${relation.from.field}) - (${relation.to.field})"`;
|
|
2740
2750
|
}
|
|
2741
2751
|
function modelFields(model) {
|
|
2742
2752
|
const fkFields = new Set(model.fields.filter((f) => f.relationFromFields && f.relationFromFields.length > 0).flatMap((f) => f.relationFromFields ?? []));
|
|
@@ -2750,7 +2760,7 @@ function modelFields(model) {
|
|
|
2750
2760
|
}
|
|
2751
2761
|
function modelInfo(model) {
|
|
2752
2762
|
return [
|
|
2753
|
-
` ${model.name} {`,
|
|
2763
|
+
` ${model.dbName ?? model.name} {`,
|
|
2754
2764
|
...modelFields(model),
|
|
2755
2765
|
" }"
|
|
2756
2766
|
];
|
|
@@ -2760,7 +2770,8 @@ function modelInfo(model) {
|
|
|
2760
2770
|
const ER_HEADER = ["```mermaid", "erDiagram"];
|
|
2761
2771
|
const ER_FOOTER = ["```"];
|
|
2762
2772
|
function erContent(models) {
|
|
2763
|
-
const
|
|
2773
|
+
const resolveName = (model) => models.find((m) => m.name === model)?.dbName ?? model;
|
|
2774
|
+
const relations = mergeERRelations(models).map((relation) => erRelationLine(relation, resolveName));
|
|
2764
2775
|
const modelInfos = models.flatMap(modelInfo);
|
|
2765
2776
|
return [
|
|
2766
2777
|
...ER_HEADER,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hekireki",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.5",
|
|
4
4
|
"description": "Hekireki is a tool that generates validation schemas for Zod, Valibot, ArkType, and Effect Schema, as well as ER diagrams and DBML, from Prisma schemas annotated with comments.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ajv",
|