hekireki 0.42.0 → 0.43.0
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/activerecord.js +1 -1
- package/dist/bin/ajv.js +1 -1
- package/dist/bin/arktype.js +1 -1
- package/dist/bin/atlas.js +1 -1
- package/dist/bin/django.js +1 -1
- package/dist/bin/drizzle.js +1 -1
- package/dist/bin/ecto.js +1 -1
- package/dist/bin/efcore.js +1 -1
- package/dist/bin/effect.js +1 -1
- package/dist/bin/eloquent.js +1 -1
- package/dist/bin/er.js +1 -1
- package/dist/bin/exposed.js +1 -1
- package/dist/bin/gorm.js +1 -1
- package/dist/bin/hekireki.js +1 -1
- package/dist/bin/kysely.js +1 -1
- package/dist/bin/openapi.js +1 -1
- package/dist/bin/pydantic.js +1 -1
- package/dist/bin/sea-orm.js +1 -1
- package/dist/bin/seed.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-CW-vHdsl.js → bin-Bs0hPt0P.js} +138 -43
- package/package.json +1 -1
package/dist/bin/activerecord.js
CHANGED
package/dist/bin/ajv.js
CHANGED
package/dist/bin/arktype.js
CHANGED
package/dist/bin/atlas.js
CHANGED
package/dist/bin/django.js
CHANGED
package/dist/bin/drizzle.js
CHANGED
package/dist/bin/ecto.js
CHANGED
package/dist/bin/efcore.js
CHANGED
package/dist/bin/effect.js
CHANGED
package/dist/bin/eloquent.js
CHANGED
package/dist/bin/er.js
CHANGED
package/dist/bin/exposed.js
CHANGED
package/dist/bin/gorm.js
CHANGED
package/dist/bin/hekireki.js
CHANGED
|
@@ -6,7 +6,7 @@ import { Console, Data, Effect, Schema } from "effect";
|
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import { CliError, Command, Flag } from "effect/cli";
|
|
8
8
|
// package.json
|
|
9
|
-
var version = "0.
|
|
9
|
+
var version = "0.43.0";
|
|
10
10
|
// src/schema/path.ts
|
|
11
11
|
/** Where Prisma looks for a schema when neither the command nor prisma.config.ts names one. */
|
|
12
12
|
const DEFAULT_SCHEMA_PATHS = ["prisma/schema.prisma", "schema.prisma"];
|
package/dist/bin/kysely.js
CHANGED
package/dist/bin/openapi.js
CHANGED
package/dist/bin/pydantic.js
CHANGED
package/dist/bin/sea-orm.js
CHANGED
package/dist/bin/seed.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
|
@@ -2721,8 +2721,57 @@ const CHOICES_RESERVED = /* @__PURE__ */ new Set([
|
|
|
2721
2721
|
"names",
|
|
2722
2722
|
"mro"
|
|
2723
2723
|
]);
|
|
2724
|
+
const STR_METHODS = /* @__PURE__ */ new Set([
|
|
2725
|
+
"capitalize",
|
|
2726
|
+
"casefold",
|
|
2727
|
+
"center",
|
|
2728
|
+
"count",
|
|
2729
|
+
"encode",
|
|
2730
|
+
"endswith",
|
|
2731
|
+
"expandtabs",
|
|
2732
|
+
"find",
|
|
2733
|
+
"format",
|
|
2734
|
+
"format_map",
|
|
2735
|
+
"index",
|
|
2736
|
+
"isalnum",
|
|
2737
|
+
"isalpha",
|
|
2738
|
+
"isascii",
|
|
2739
|
+
"isdecimal",
|
|
2740
|
+
"isdigit",
|
|
2741
|
+
"isidentifier",
|
|
2742
|
+
"islower",
|
|
2743
|
+
"isnumeric",
|
|
2744
|
+
"isprintable",
|
|
2745
|
+
"isspace",
|
|
2746
|
+
"istitle",
|
|
2747
|
+
"isupper",
|
|
2748
|
+
"join",
|
|
2749
|
+
"ljust",
|
|
2750
|
+
"lower",
|
|
2751
|
+
"lstrip",
|
|
2752
|
+
"maketrans",
|
|
2753
|
+
"partition",
|
|
2754
|
+
"removeprefix",
|
|
2755
|
+
"removesuffix",
|
|
2756
|
+
"replace",
|
|
2757
|
+
"rfind",
|
|
2758
|
+
"rindex",
|
|
2759
|
+
"rjust",
|
|
2760
|
+
"rpartition",
|
|
2761
|
+
"rsplit",
|
|
2762
|
+
"rstrip",
|
|
2763
|
+
"split",
|
|
2764
|
+
"splitlines",
|
|
2765
|
+
"startswith",
|
|
2766
|
+
"strip",
|
|
2767
|
+
"swapcase",
|
|
2768
|
+
"title",
|
|
2769
|
+
"translate",
|
|
2770
|
+
"upper",
|
|
2771
|
+
"zfill"
|
|
2772
|
+
]);
|
|
2724
2773
|
function enumMemberName(valueName) {
|
|
2725
|
-
return PYTHON_KEYWORDS.has(valueName) || CHOICES_RESERVED.has(valueName) ? `${valueName}_` : valueName;
|
|
2774
|
+
return PYTHON_KEYWORDS.has(valueName) || CHOICES_RESERVED.has(valueName) || STR_METHODS.has(valueName) ? `${valueName}_` : valueName;
|
|
2726
2775
|
}
|
|
2727
2776
|
function resolveDjangoField(field) {
|
|
2728
2777
|
const base = prismaTypeToDjangoField(field.type);
|
|
@@ -3125,9 +3174,9 @@ const ON_DELETE = {
|
|
|
3125
3174
|
NoAction: "models.DO_NOTHING",
|
|
3126
3175
|
SetDefault: "models.SET_DEFAULT"
|
|
3127
3176
|
};
|
|
3128
|
-
function onDeleteFor(
|
|
3129
|
-
if (
|
|
3130
|
-
return
|
|
3177
|
+
function onDeleteFor(relationField, scalarField) {
|
|
3178
|
+
if (relationField.relationOnDelete && ON_DELETE[relationField.relationOnDelete]) return ON_DELETE[relationField.relationOnDelete];
|
|
3179
|
+
return scalarField.isRequired ? "models.RESTRICT" : "models.SET_NULL";
|
|
3131
3180
|
}
|
|
3132
3181
|
function generateForeignKeyField(model, relationField, scalarField, allModels, names, isPk) {
|
|
3133
3182
|
const attrName = attrNameOf(names, model, relationField);
|
|
@@ -3143,12 +3192,12 @@ function generateForeignKeyField(model, relationField, scalarField, allModels, n
|
|
|
3143
3192
|
const toField = referencedField && targetModel && !(targetPk.length === 1 && targetPk[0] === referencedName) ? `to_field=${toPythonString(attrNameOf(names, targetModel, referencedField))}` : null;
|
|
3144
3193
|
return ` ${attrName} = models.${ctor}(${[
|
|
3145
3194
|
target,
|
|
3146
|
-
`on_delete=${onDeleteFor(relationField)}`,
|
|
3195
|
+
`on_delete=${onDeleteFor(relationField, scalarField)}`,
|
|
3147
3196
|
`related_name=${relatedName}`,
|
|
3148
3197
|
toField,
|
|
3149
3198
|
isPk ? "primary_key=True" : null,
|
|
3150
3199
|
columnName !== `${attrName}_id` ? `db_column=${toPythonString(columnName)}` : null,
|
|
3151
|
-
!
|
|
3200
|
+
!scalarField.isRequired ? "null=True" : null,
|
|
3152
3201
|
ctor === "ForeignKey" ? "db_index=False" : null,
|
|
3153
3202
|
...formatDefaultArgs(model, scalarField, isPk, names)
|
|
3154
3203
|
].filter((a) => a !== null).join(", ")})`;
|
|
@@ -3270,7 +3319,7 @@ function generateMetaLines(model, indexes, names) {
|
|
|
3270
3319
|
] : [],
|
|
3271
3320
|
...indexEntries.length > 0 ? [
|
|
3272
3321
|
" indexes = [",
|
|
3273
|
-
...indexEntries.map((i) => ` ${i},`),
|
|
3322
|
+
...[...new Set(indexEntries)].map((i) => ` ${i},`),
|
|
3274
3323
|
" ]"
|
|
3275
3324
|
] : []
|
|
3276
3325
|
];
|
|
@@ -4708,16 +4757,17 @@ function ectoSchemas(models, app, allModels, enums, options = {}) {
|
|
|
4708
4757
|
if (a.references !== "id") opts.push(`references: :${a.references}`);
|
|
4709
4758
|
return ` belongs_to(:${snakeAssocName}, ${appName}.${makePascalCase(a.targetModel)}, ${opts.join(", ")})`;
|
|
4710
4759
|
});
|
|
4760
|
+
const namesReferences = (references) => references !== "id" || pk.line === "@primary_key false";
|
|
4711
4761
|
const hasOneLines = associations.hasOne.map((a) => {
|
|
4712
4762
|
const snakeFk = makeSnakeCase(a.foreignKey);
|
|
4713
4763
|
const snakeAssocName = makeSnakeCase(a.name);
|
|
4714
|
-
const referencesOpt = a.references
|
|
4764
|
+
const referencesOpt = namesReferences(a.references) ? `, references: :${a.references}` : "";
|
|
4715
4765
|
return ` has_one(:${snakeAssocName}, ${appName}.${makePascalCase(a.targetModel)}, foreign_key: :${snakeFk}${referencesOpt})`;
|
|
4716
4766
|
});
|
|
4717
4767
|
const hasManyLines = associations.hasMany.map((a) => {
|
|
4718
4768
|
const snakeFk = makeSnakeCase(a.foreignKey);
|
|
4719
4769
|
const snakeAssocName = makeSnakeCase(a.name);
|
|
4720
|
-
const referencesOpt = a.references
|
|
4770
|
+
const referencesOpt = namesReferences(a.references) ? `, references: :${a.references}` : "";
|
|
4721
4771
|
return ` has_many(:${snakeAssocName}, ${appName}.${makePascalCase(a.targetModel)}, foreign_key: :${snakeFk}${referencesOpt})`;
|
|
4722
4772
|
});
|
|
4723
4773
|
const manyToManyLines = associations.manyToMany.map((a) => {
|
|
@@ -11611,7 +11661,7 @@ function dateTimeGoType(field) {
|
|
|
11611
11661
|
const element = nativeName === "Date" ? "Date" : nativeName === "Time" || nativeName === "Timetz" ? "TimeOfDay" : "DateTime";
|
|
11612
11662
|
return field.isList ? `${element}List` : element;
|
|
11613
11663
|
}
|
|
11614
|
-
function buildGormTags(field, isPk, isCompositePk, compositeIndexTags, enums, provider) {
|
|
11664
|
+
function buildGormTags(field, isPk, isCompositePk, compositeIndexTags, enums, provider, fieldName = goFieldName(field.name)) {
|
|
11615
11665
|
const columnName = field.dbName ?? field.name;
|
|
11616
11666
|
const isUuidDefault = isFunctionDefault(field.default) && field.default.name === "uuid";
|
|
11617
11667
|
const isUlidDefault = isFunctionDefault(field.default) && field.default.name === "ulid";
|
|
@@ -11633,8 +11683,7 @@ function buildGormTags(field, isPk, isCompositePk, compositeIndexTags, enums, pr
|
|
|
11633
11683
|
return provider === "sqlite" ? `'${iso.replace("Z", "+00:00")}'` : `'${iso.slice(0, 10)} ${iso.slice(11, 23)}'`;
|
|
11634
11684
|
})();
|
|
11635
11685
|
const defaultVal = dbGeneratedExpr ?? dateTimeDefault ?? ((!isPk || isCompositePk) && !isNowDefault && !field.isUpdatedAt ? enumMappedDefault ?? formatGoDefault(field.default, field.type, field.kind) : null);
|
|
11636
|
-
const
|
|
11637
|
-
const isDateTime = isDateTimeScalar(field);
|
|
11686
|
+
const isTracked = isDateTimeScalar(field) || ["Int", "BigInt"].includes(field.type);
|
|
11638
11687
|
return `\`gorm:"${[
|
|
11639
11688
|
`column:${columnName}`,
|
|
11640
11689
|
isPk ? "primaryKey" : null,
|
|
@@ -11646,8 +11695,8 @@ function buildGormTags(field, isPk, isCompositePk, compositeIndexTags, enums, pr
|
|
|
11646
11695
|
isDateTimeList(field) ? `type:${nativeType ?? "timestamp(3)"}[]` : includeNativeType ? `type:${nativeType}` : null,
|
|
11647
11696
|
field.isList && field.kind !== "object" && !isDateTimeList(field) ? "serializer:json" : null,
|
|
11648
11697
|
defaultVal !== null ? `default:${defaultVal}` : null,
|
|
11649
|
-
|
|
11650
|
-
|
|
11698
|
+
isTracked && fieldName === "CreatedAt" ? "autoCreateTime:false" : null,
|
|
11699
|
+
isTracked && fieldName === "UpdatedAt" ? "autoUpdateTime:false" : null,
|
|
11651
11700
|
field.isRequired && !isPk ? "not null" : null
|
|
11652
11701
|
].filter((p) => p !== null).join(";")}" json:"${columnName}"\``;
|
|
11653
11702
|
}
|
|
@@ -11725,6 +11774,22 @@ function goFieldName(name) {
|
|
|
11725
11774
|
const pascal = splitGoWords(name).join("");
|
|
11726
11775
|
return GENERATED_METHOD_NAMES.has(pascal) ? `${pascal}_` : pascal;
|
|
11727
11776
|
}
|
|
11777
|
+
/**
|
|
11778
|
+
* The Go name of each field of a model. Two fields can become one name (`createdAt` beside the
|
|
11779
|
+
* `created_at` an OAuth provider returns, both `CreatedAt`): the first declared keeps it and the
|
|
11780
|
+
* other is numbered (`CreatedAt1`), its `column:` tag still naming its own column. Scalars are
|
|
11781
|
+
* named before relations, so a relation never moves a column's field.
|
|
11782
|
+
*/
|
|
11783
|
+
function goFieldNames(model) {
|
|
11784
|
+
return allocate([...model.fields.filter((f) => f.kind !== "object"), ...model.fields.filter((f) => f.kind === "object")].map((f) => ({
|
|
11785
|
+
key: f.name,
|
|
11786
|
+
candidate: goFieldName(f.name)
|
|
11787
|
+
})), [], (name) => name);
|
|
11788
|
+
}
|
|
11789
|
+
function goFieldNameIn(models, modelName, fieldName) {
|
|
11790
|
+
const model = models.find((m) => m.name === modelName);
|
|
11791
|
+
return (model ? goFieldNames(model).get(fieldName) : void 0) ?? goFieldName(fieldName);
|
|
11792
|
+
}
|
|
11728
11793
|
function goModelName(name) {
|
|
11729
11794
|
return splitGoWords(name).join("");
|
|
11730
11795
|
}
|
|
@@ -11756,9 +11821,9 @@ function createdTimeExpr(field) {
|
|
|
11756
11821
|
const at = new Date(field.default);
|
|
11757
11822
|
return `time.Date(${at.getUTCFullYear()}, ${at.getUTCMonth() + 1}, ${at.getUTCDate()}, ${at.getUTCHours()}, ${at.getUTCMinutes()}, ${at.getUTCSeconds()}, ${at.getUTCMilliseconds() * 1e6}, time.UTC)`;
|
|
11758
11823
|
}
|
|
11759
|
-
function generateBeforeCreateHook(model) {
|
|
11824
|
+
function generateBeforeCreateHook(model, names) {
|
|
11760
11825
|
const assignments = model.fields.flatMap((field) => {
|
|
11761
|
-
const fieldName = goFieldName(field.name);
|
|
11826
|
+
const fieldName = names.get(field.name) ?? goFieldName(field.name);
|
|
11762
11827
|
const idExpr = field.kind === "scalar" && field.type === "String" && !field.isList ? generatedIdExpr$1(field) : null;
|
|
11763
11828
|
if (idExpr !== null) return field.isRequired ? [
|
|
11764
11829
|
`\tif m.${fieldName} == "" {`,
|
|
@@ -11794,7 +11859,7 @@ function generateBeforeCreateHook(model) {
|
|
|
11794
11859
|
"}"
|
|
11795
11860
|
];
|
|
11796
11861
|
}
|
|
11797
|
-
function generateBeforeUpdateHook(model) {
|
|
11862
|
+
function generateBeforeUpdateHook(model, names) {
|
|
11798
11863
|
const stamped = model.fields.filter((field) => isDateTimeScalar(field) && field.isUpdatedAt);
|
|
11799
11864
|
if (stamped.length === 0) return [];
|
|
11800
11865
|
return [
|
|
@@ -11802,7 +11867,7 @@ function generateBeforeUpdateHook(model) {
|
|
|
11802
11867
|
`func (*${goModelName(model.name)}) BeforeUpdate(tx *gorm.DB) error {`,
|
|
11803
11868
|
" now := tx.NowFunc().UTC().Truncate(time.Millisecond)",
|
|
11804
11869
|
...stamped.flatMap((field) => {
|
|
11805
|
-
const fieldName = goFieldName(field.name);
|
|
11870
|
+
const fieldName = names.get(field.name) ?? goFieldName(field.name);
|
|
11806
11871
|
return [
|
|
11807
11872
|
`\tif !tx.Statement.Changed("${fieldName}") {`,
|
|
11808
11873
|
`\t\ttx.Statement.SetColumn("${fieldName}", ${field.isRequired ? "" : "&"}${dateTimeGoType(field)}{Time: now})`,
|
|
@@ -11813,8 +11878,7 @@ function generateBeforeUpdateHook(model) {
|
|
|
11813
11878
|
"}"
|
|
11814
11879
|
];
|
|
11815
11880
|
}
|
|
11816
|
-
function generateStructField(field, isPk, isCompositePk, compositeIndexTags, enums, provider) {
|
|
11817
|
-
const fieldName = goFieldName(field.name);
|
|
11881
|
+
function generateStructField(field, fieldName, isPk, isCompositePk, compositeIndexTags, enums, provider) {
|
|
11818
11882
|
const def = field.default;
|
|
11819
11883
|
const hasNonZeroDefault = field.kind === "scalar" && !isPk && [
|
|
11820
11884
|
"Int",
|
|
@@ -11828,7 +11892,7 @@ function generateStructField(field, isPk, isCompositePk, compositeIndexTags, enu
|
|
|
11828
11892
|
return [
|
|
11829
11893
|
fieldName,
|
|
11830
11894
|
isDateTimeList(field) ? dateTimeGoType(field) : field.isList ? `[]${field.kind === "enum" ? "string" : prismaTypeToGoType(field.type, true)}` : isDateTimeScalar(field) ? `${field.isRequired ? "" : "*"}${dateTimeGoType(field)}` : scalarType,
|
|
11831
|
-
buildGormTags(field, isPk, isCompositePk, compositeIndexTags, enums, provider)
|
|
11895
|
+
buildGormTags(field, isPk, isCompositePk, compositeIndexTags, enums, provider, fieldName)
|
|
11832
11896
|
];
|
|
11833
11897
|
}
|
|
11834
11898
|
function needsReferencesTag(references) {
|
|
@@ -11848,11 +11912,13 @@ function constraintTag(onDelete, onUpdate) {
|
|
|
11848
11912
|
function buildRelationTag(parts) {
|
|
11849
11913
|
return `\`gorm:"${parts.join(";")}"\``;
|
|
11850
11914
|
}
|
|
11851
|
-
function generateRelationFields(associations) {
|
|
11915
|
+
function generateRelationFields(model, allModels, associations) {
|
|
11916
|
+
const of = (modelName) => (name) => goFieldNameIn(allModels, modelName, name);
|
|
11917
|
+
const own = of(model.name);
|
|
11852
11918
|
const belongsToLines = associations.belongsTo.map((assoc) => {
|
|
11853
|
-
const fieldName =
|
|
11854
|
-
const fkFieldName = assoc.foreignKeys.map(
|
|
11855
|
-
const refsFieldName = assoc.referencesList.map(
|
|
11919
|
+
const fieldName = own(assoc.name);
|
|
11920
|
+
const fkFieldName = assoc.foreignKeys.map(own).join(",");
|
|
11921
|
+
const refsFieldName = assoc.referencesList.map(of(assoc.targetModel)).join(",");
|
|
11856
11922
|
const isComposite = assoc.foreignKeys.length > 1;
|
|
11857
11923
|
const isAmbiguous = fieldName !== goModelName(assoc.targetModel) || associations.belongsTo.filter((a) => a.targetModel === assoc.targetModel).length > 1;
|
|
11858
11924
|
const isGuessed = fkFieldName === `${fieldName}${refsFieldName}`;
|
|
@@ -11866,35 +11932,35 @@ function generateRelationFields(associations) {
|
|
|
11866
11932
|
});
|
|
11867
11933
|
const hasManyLines = associations.hasMany.map((assoc) => {
|
|
11868
11934
|
const tagParts = [
|
|
11869
|
-
`foreignKey:${assoc.foreignKeys.map(
|
|
11870
|
-
...assoc.foreignKeys.length > 1 || needsReferencesTag(assoc.references) ? [`references:${assoc.referencesList.map(
|
|
11935
|
+
`foreignKey:${assoc.foreignKeys.map(of(assoc.targetModel)).join(",")}`,
|
|
11936
|
+
...assoc.foreignKeys.length > 1 || needsReferencesTag(assoc.references) ? [`references:${assoc.referencesList.map(own).join(",")}`] : [],
|
|
11871
11937
|
...[constraintTag(assoc.onDelete, assoc.onUpdate)].filter((c) => c !== null)
|
|
11872
11938
|
];
|
|
11873
11939
|
return [
|
|
11874
|
-
|
|
11940
|
+
own(assoc.name),
|
|
11875
11941
|
`[]${goModelName(assoc.targetModel)}`,
|
|
11876
11942
|
buildRelationTag(tagParts)
|
|
11877
11943
|
];
|
|
11878
11944
|
});
|
|
11879
11945
|
const hasOneLines = associations.hasOne.map((assoc) => {
|
|
11880
11946
|
const tagParts = [
|
|
11881
|
-
`foreignKey:${assoc.foreignKeys.map(
|
|
11882
|
-
...assoc.foreignKeys.length > 1 || needsReferencesTag(assoc.references) ? [`references:${assoc.referencesList.map(
|
|
11947
|
+
`foreignKey:${assoc.foreignKeys.map(of(assoc.targetModel)).join(",")}`,
|
|
11948
|
+
...assoc.foreignKeys.length > 1 || needsReferencesTag(assoc.references) ? [`references:${assoc.referencesList.map(own).join(",")}`] : [],
|
|
11883
11949
|
...[constraintTag(assoc.onDelete, assoc.onUpdate)].filter((c) => c !== null)
|
|
11884
11950
|
];
|
|
11885
11951
|
return [
|
|
11886
|
-
|
|
11952
|
+
own(assoc.name),
|
|
11887
11953
|
`*${goModelName(assoc.targetModel)}`,
|
|
11888
11954
|
buildRelationTag(tagParts)
|
|
11889
11955
|
];
|
|
11890
11956
|
});
|
|
11891
11957
|
const manyToManyLines = associations.manyToMany.map((assoc) => {
|
|
11892
11958
|
const joinTable = `_${assoc.relationName}`;
|
|
11893
|
-
const [
|
|
11959
|
+
const [ownColumn, related] = assoc.isSideA ? ["A", "B"] : ["B", "A"];
|
|
11894
11960
|
return [
|
|
11895
|
-
|
|
11961
|
+
own(assoc.name),
|
|
11896
11962
|
`[]${goModelName(assoc.targetModel)}`,
|
|
11897
|
-
`\`gorm:"many2many:${joinTable};joinForeignKey:${
|
|
11963
|
+
`\`gorm:"many2many:${joinTable};joinForeignKey:${ownColumn};joinReferences:${related}"\``
|
|
11898
11964
|
];
|
|
11899
11965
|
});
|
|
11900
11966
|
return [
|
|
@@ -11942,12 +12008,14 @@ function generateModelStruct(model, allModels, enums, indexes, provider) {
|
|
|
11942
12008
|
const associations = getAssociations$2(model, allModels);
|
|
11943
12009
|
const compositeTagMap = collectCompositeIndexTags(model, indexes);
|
|
11944
12010
|
const tableName = model.dbName ?? model.name;
|
|
11945
|
-
const
|
|
12011
|
+
const scalarFields = model.fields.filter((f) => f.kind !== "object");
|
|
12012
|
+
const names = goFieldNames(model);
|
|
12013
|
+
const fieldLines = scalarFields.map((field) => {
|
|
11946
12014
|
const isPk = field.isId || compositePkFieldNames.has(field.name);
|
|
11947
12015
|
const fieldIndexTags = compositeTagMap.get(field.name) ?? [];
|
|
11948
|
-
return generateStructField(field, isPk, isCompositePk, fieldIndexTags, enums, provider);
|
|
12016
|
+
return generateStructField(field, names.get(field.name) ?? goFieldName(field.name), isPk, isCompositePk, fieldIndexTags, enums, provider);
|
|
11949
12017
|
});
|
|
11950
|
-
const relationLines = generateRelationFields(associations);
|
|
12018
|
+
const relationLines = generateRelationFields(model, allModels, associations);
|
|
11951
12019
|
const tableNameMethod = [
|
|
11952
12020
|
"",
|
|
11953
12021
|
`func (${goModelName(model.name)}) TableName() string {`,
|
|
@@ -11959,8 +12027,8 @@ function generateModelStruct(model, allModels, enums, indexes, provider) {
|
|
|
11959
12027
|
...alignFields([...fieldLines, ...relationLines]),
|
|
11960
12028
|
"}",
|
|
11961
12029
|
...tableNameMethod,
|
|
11962
|
-
...generateBeforeCreateHook(model),
|
|
11963
|
-
...generateBeforeUpdateHook(model)
|
|
12030
|
+
...generateBeforeCreateHook(model, names),
|
|
12031
|
+
...generateBeforeUpdateHook(model, names)
|
|
11964
12032
|
].join("\n");
|
|
11965
12033
|
}
|
|
11966
12034
|
function hasImplicitManyToMany(models) {
|
|
@@ -13817,7 +13885,7 @@ function generatePreludeRs(models) {
|
|
|
13817
13885
|
return `${models.map((m) => ({
|
|
13818
13886
|
module: rustModule(m.name).ident,
|
|
13819
13887
|
name: m.name
|
|
13820
|
-
})).toSorted((a, b) => a.module < b.module ? -1 : a.module > b.module ? 1 : 0).map(({ module, name }) => `pub use super::${module}::Entity as ${name};`).join("\n")}\n`;
|
|
13888
|
+
})).toSorted((a, b) => a.module < b.module ? -1 : a.module > b.module ? 1 : 0).map(({ module, name }) => name === "Entity" ? `pub use super::${module}::Entity;` : `pub use super::${module}::Entity as ${name};`).join("\n")}\n`;
|
|
13821
13889
|
}
|
|
13822
13890
|
function collectM2MPairs(models) {
|
|
13823
13891
|
const pairs = models.flatMap((model) => getAssociations$1(model, models).manyToMany.map((assoc) => {
|
|
@@ -14258,6 +14326,7 @@ function getAssociations(model, allModels) {
|
|
|
14258
14326
|
if (end.kind === "hasMany") hasMany.push({
|
|
14259
14327
|
name: field.name,
|
|
14260
14328
|
targetModel: field.type,
|
|
14329
|
+
inverse: fkField,
|
|
14261
14330
|
foreignKey,
|
|
14262
14331
|
foreignKeys,
|
|
14263
14332
|
isList: true,
|
|
@@ -14266,6 +14335,7 @@ function getAssociations(model, allModels) {
|
|
|
14266
14335
|
else hasOne.push({
|
|
14267
14336
|
name: field.name,
|
|
14268
14337
|
targetModel: field.type,
|
|
14338
|
+
inverse: fkField,
|
|
14269
14339
|
foreignKey,
|
|
14270
14340
|
foreignKeys,
|
|
14271
14341
|
isList: false,
|
|
@@ -14456,6 +14526,26 @@ function findM2MBackPopulates(targetModelName, sourceModelName, sourceFieldName,
|
|
|
14456
14526
|
const backField = targetModel?.fields.find((f) => f.kind === "object" && f.type === sourceModelName && f.relationName === relationName && !(targetModelName === sourceModelName && f.name === sourceFieldName));
|
|
14457
14527
|
return backField ? attributeName(targetModel, backField.name) : pythonAttrName(makeSnakeCase(sourceModelName));
|
|
14458
14528
|
}
|
|
14529
|
+
/**
|
|
14530
|
+
* The `overlaps=` of a relationship that writes the foreign key `relation` holds (the many-to-one
|
|
14531
|
+
* itself or the collection on the other side): every other relationship that writes one of the
|
|
14532
|
+
* same columns. A composite key that shares a column with another relation
|
|
14533
|
+
* (`[projectId, environment]` beside `projectId`) has two relationships copying the column, which
|
|
14534
|
+
* SQLAlchemy warns of unless each names the others. The pair of a back_populates is left out, as
|
|
14535
|
+
* SQLAlchemy leaves it.
|
|
14536
|
+
*
|
|
14537
|
+
* @example
|
|
14538
|
+
* ```python
|
|
14539
|
+
* environment_ref: Mapped[Optional["ProjectEnvironment"]] = relationship(overlaps="database_configs,project", back_populates="database_configs")
|
|
14540
|
+
* ```
|
|
14541
|
+
*/
|
|
14542
|
+
function overlapsClause(holdingModelName, relation, allModels) {
|
|
14543
|
+
const holding = allModels.find((m) => m.name === holdingModelName);
|
|
14544
|
+
const keys = relation.relationFromFields ?? [];
|
|
14545
|
+
const names = (holding?.fields ?? []).flatMap((f) => f.kind === "object" && f.name !== relation.name && (f.relationFromFields ?? []).some((key) => keys.includes(key)) ? [attributeName(holding, f.name), findBackPopulates(f.type, holdingModelName, f.relationFromFields?.[0] ?? "", allModels, f.name)] : []);
|
|
14546
|
+
const sorted = [...new Set(names)].toSorted();
|
|
14547
|
+
return sorted.length > 0 ? `overlaps="${sorted.join(",")}", ` : "";
|
|
14548
|
+
}
|
|
14459
14549
|
function generateColumn(field, isPk, isFk, associations, allModels, enumMap, provider, model, isMapperKey) {
|
|
14460
14550
|
const tableName = model.dbName ?? model.name;
|
|
14461
14551
|
const columnName = field.dbName ?? field.name;
|
|
@@ -14566,7 +14656,9 @@ function generateBelongsToRelationships(associations, model, allModels) {
|
|
|
14566
14656
|
const identity = primaryKeyFields$1(model);
|
|
14567
14657
|
const remoteClause = assoc.targetModel === model.name && identity.length > 0 ? `remote_side=[${identity.map((name) => attributeName(model, name)).join(", ")}], ` : "";
|
|
14568
14658
|
const target = className(assoc.targetModel);
|
|
14569
|
-
|
|
14659
|
+
const mappedType = assoc.optional ? `Optional["${target}"]` : `"${target}"`;
|
|
14660
|
+
const relation = model.fields.find((f) => f.name === assoc.name);
|
|
14661
|
+
return ` ${snakeName}: Mapped[${mappedType}] = relationship(${remoteClause}${fkClause}${relation ? overlapsClause(model.name, relation, allModels) : ""}back_populates="${backPop}")`;
|
|
14570
14662
|
});
|
|
14571
14663
|
}
|
|
14572
14664
|
function inverseForeignKeys(targetModelName, foreignKeys, allModels) {
|
|
@@ -14579,7 +14671,8 @@ function generateHasManyRelationships(associations, model, allModels) {
|
|
|
14579
14671
|
const snakeName = attributeName(model, assoc.name);
|
|
14580
14672
|
const backPop = findBackPopulates(assoc.targetModel, model.name, assoc.foreignKey, allModels, assoc.name);
|
|
14581
14673
|
const fkClause = needsForeignKeysParam(model.name, assoc.targetModel, allModels) ? `foreign_keys=${inverseForeignKeys(assoc.targetModel, assoc.foreignKeys, allModels)}, ` : "";
|
|
14582
|
-
|
|
14674
|
+
const overlaps = overlapsClause(assoc.targetModel, assoc.inverse, allModels);
|
|
14675
|
+
return ` ${snakeName}: Mapped[list["${className(assoc.targetModel)}"]] = relationship(${fkClause}${deleteClause(assoc.onDelete)}${overlaps}back_populates="${backPop}")`;
|
|
14583
14676
|
});
|
|
14584
14677
|
}
|
|
14585
14678
|
function generateHasOneRelationships(associations, model, allModels) {
|
|
@@ -14588,7 +14681,9 @@ function generateHasOneRelationships(associations, model, allModels) {
|
|
|
14588
14681
|
const backPop = findBackPopulates(assoc.targetModel, model.name, assoc.foreignKey, allModels, assoc.name);
|
|
14589
14682
|
const fkClause = needsForeignKeysParam(model.name, assoc.targetModel, allModels) ? `foreign_keys=${inverseForeignKeys(assoc.targetModel, assoc.foreignKeys, allModels)}, ` : "";
|
|
14590
14683
|
const target = className(assoc.targetModel);
|
|
14591
|
-
|
|
14684
|
+
const mappedType = assoc.optional ? `Optional["${target}"]` : `"${target}"`;
|
|
14685
|
+
const overlaps = overlapsClause(assoc.targetModel, assoc.inverse, allModels);
|
|
14686
|
+
return ` ${snakeName}: Mapped[${mappedType}] = relationship(${fkClause}${deleteClause(assoc.onDelete)}${overlaps}back_populates="${backPop}")`;
|
|
14592
14687
|
});
|
|
14593
14688
|
}
|
|
14594
14689
|
function generateManyToManyRelationships(associations, model, allModels, m2mTables) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hekireki",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.43.0",
|
|
4
4
|
"description": "Hekireki is a code generator from Prisma schema to validation schemas, ORM models, OpenAPI and ER diagrams, with a Studio for your schema and its data",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"activerecord",
|