pecunia-cli 0.2.6 → 0.2.7

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/api.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { a as generateKyselySchema, n as generateSchema, o as generateDrizzleSchema, r as generatePrismaSchema, t as adapters } from "./generators-BVZYl7Wb.mjs";
1
+ import { a as generateKyselySchema, n as generateSchema, o as generateDrizzleSchema, r as generatePrismaSchema, t as adapters } from "./generators-Cb42XRwg.mjs";
2
2
 
3
3
  export { adapters, generateDrizzleSchema, generateKyselySchema, generatePrismaSchema, generateSchema };
@@ -633,61 +633,97 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
633
633
  const modelName = getModelName(tableKey);
634
634
  const oneRelations = [];
635
635
  const manyRelations = [];
636
- if (table.relations) for (const [relationName, relationDef] of Object.entries(table.relations)) {
637
- const referencedModelName = getModelName(relationDef.model);
638
- const foreignKeyField = table.fields[relationDef.foreignKey];
639
- if (relationDef.kind === "one") {
640
- if (foreignKeyField?.references) {
641
- const fieldRef = `${modelName}.${getFieldName({
642
- model: tableKey,
643
- field: relationDef.foreignKey
644
- })}`;
645
- const referenceRef = `${referencedModelName}.${getFieldName({
646
- model: relationDef.model,
647
- field: foreignKeyField.references.field || "id"
648
- })}`;
649
- oneRelations.push({
650
- key: relationName,
651
- model: referencedModelName,
652
- type: "one",
653
- reference: {
654
- field: fieldRef,
655
- references: referenceRef,
656
- fieldName: relationDef.foreignKey
657
- }
658
- });
659
- }
660
- } else if (relationDef.kind === "many") {
661
- const referencedTable = tables[relationDef.model];
662
- if (referencedTable) {
663
- const fkField = Object.entries(referencedTable.fields).find(([_, field]) => field.references && (field.references.model === tableKey || field.references.model === getModelName(tableKey)));
664
- if (fkField) {
665
- const [fkFieldName] = fkField;
666
- const fieldRef = `${referencedModelName}.${getFieldName({
667
- model: relationDef.model,
668
- field: fkFieldName
669
- })}`;
670
- const referenceRef = `${modelName}.${getFieldName({
636
+ if (table.relations) {
637
+ for (const [relationName, relationDef] of Object.entries(table.relations)) {
638
+ const referencedModelName = getModelName(relationDef.model);
639
+ const foreignKeyField = table.fields[relationDef.foreignKey];
640
+ const isSelfReferential = relationDef.model === tableKey || referencedModelName === modelName;
641
+ const generateRelationName = (fkName) => {
642
+ let cleaned = convertToSnakeCase(fkName, adapter.options?.camelCase).replace(/_by_id$/, "").replace(/_id$/, "");
643
+ const participleToNoun = {
644
+ "reversed": "reversal",
645
+ "created": "creation",
646
+ "updated": "update"
647
+ };
648
+ if (participleToNoun[cleaned]) cleaned = participleToNoun[cleaned];
649
+ return `${convertToSnakeCase(modelName, adapter.options?.camelCase)}_${cleaned}`;
650
+ };
651
+ if (relationDef.kind === "one") {
652
+ if (foreignKeyField?.references) {
653
+ const fieldRef = `${modelName}.${getFieldName({
671
654
  model: tableKey,
672
- field: "id"
655
+ field: relationDef.foreignKey
673
656
  })}`;
674
- manyRelations.push({
657
+ const referenceRef = `${referencedModelName}.${getFieldName({
658
+ model: relationDef.model,
659
+ field: foreignKeyField.references.field || "id"
660
+ })}`;
661
+ oneRelations.push({
675
662
  key: relationName,
676
663
  model: referencedModelName,
677
- type: "many",
664
+ type: "one",
678
665
  reference: {
679
666
  field: fieldRef,
680
667
  references: referenceRef,
681
- fieldName: fkFieldName
668
+ fieldName: relationDef.foreignKey
669
+ },
670
+ relationName: isSelfReferential ? generateRelationName(relationDef.foreignKey) : void 0
671
+ });
672
+ }
673
+ } else if (relationDef.kind === "many") {
674
+ const referencedTable = tables[relationDef.model];
675
+ if (referencedTable) {
676
+ const fkField = Object.entries(referencedTable.fields).find(([_, field]) => field.references && (field.references.model === tableKey || field.references.model === getModelName(tableKey)));
677
+ if (fkField) {
678
+ const [fkFieldName] = fkField;
679
+ const fieldRef = `${referencedModelName}.${getFieldName({
680
+ model: relationDef.model,
681
+ field: fkFieldName
682
+ })}`;
683
+ const referenceRef = `${modelName}.${getFieldName({
684
+ model: tableKey,
685
+ field: "id"
686
+ })}`;
687
+ let relationNameForMany;
688
+ if (isSelfReferential) {
689
+ const matchingOne = oneRelations.find((rel) => rel.reference?.fieldName === fkFieldName && rel.model === referencedModelName);
690
+ if (matchingOne?.relationName) relationNameForMany = matchingOne.relationName;
691
+ else {
692
+ let cleaned = convertToSnakeCase(fkFieldName, adapter.options?.camelCase).replace(/_by_id$/, "").replace(/_id$/, "");
693
+ const participleToNoun = {
694
+ "reversed": "reversal",
695
+ "created": "creation",
696
+ "updated": "update"
697
+ };
698
+ if (participleToNoun[cleaned]) cleaned = participleToNoun[cleaned];
699
+ relationNameForMany = `${convertToSnakeCase(modelName, adapter.options?.camelCase)}_${cleaned}`;
700
+ }
682
701
  }
702
+ manyRelations.push({
703
+ key: relationName,
704
+ model: referencedModelName,
705
+ type: "many",
706
+ reference: {
707
+ field: fieldRef,
708
+ references: referenceRef,
709
+ fieldName: fkFieldName
710
+ },
711
+ relationName: relationNameForMany
712
+ });
713
+ } else manyRelations.push({
714
+ key: relationName,
715
+ model: referencedModelName,
716
+ type: "many"
683
717
  });
684
- } else manyRelations.push({
685
- key: relationName,
686
- model: referencedModelName,
687
- type: "many"
688
- });
718
+ }
689
719
  }
690
720
  }
721
+ for (const oneRel of oneRelations) {
722
+ if (!oneRel.relationName || !oneRel.reference) continue;
723
+ const oneRelFieldName = oneRel.reference.fieldName;
724
+ const matchingMany = manyRelations.find((manyRel) => manyRel.model === oneRel.model && manyRel.reference?.fieldName === oneRelFieldName);
725
+ if (matchingMany) matchingMany.relationName = oneRel.relationName;
726
+ }
691
727
  }
692
728
  const relationsByModel = /* @__PURE__ */ new Map();
693
729
  for (const relation of oneRelations) {
@@ -703,10 +739,12 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
703
739
  for (const relation of duplicateRelations) {
704
740
  if (!relation.reference) continue;
705
741
  const fieldName = relation.reference.fieldName;
706
- const tableRelation = `export const ${`${modelName}${fieldName.charAt(0).toUpperCase() + fieldName.slice(1)}Relations`} = relations(${modelName}, ({ one }) => ({
742
+ const relationExportName = `${modelName}${fieldName.charAt(0).toUpperCase() + fieldName.slice(1)}Relations`;
743
+ const relationNameParam = relation.relationName ? `,\n relationName: "${relation.relationName}"` : "";
744
+ const tableRelation = `export const ${relationExportName} = relations(${modelName}, ({ one }) => ({
707
745
  ${relation.key}: one(${relation.model}, {
708
746
  fields: [${relation.reference.field}],
709
- references: [${relation.reference.references}],
747
+ references: [${relation.reference.references}]${relationNameParam}
710
748
  })
711
749
  }))`;
712
750
  relationsString += `\n${tableRelation}\n`;
@@ -715,24 +753,36 @@ const generateDrizzleSchema = async ({ options, file, adapter }) => {
715
753
  const hasMany = manyRelations.length > 0;
716
754
  if (hasOne && hasMany) {
717
755
  const tableRelation = `export const ${modelName}Relations = relations(${modelName}, ({ one, many }) => ({
718
- ${singleRelations.map((relation) => relation.reference ? ` ${relation.key}: one(${relation.model}, {
756
+ ${singleRelations.map((relation) => {
757
+ if (!relation.reference) return "";
758
+ const relationNameParam = relation.relationName ? `,\n relationName: "${relation.relationName}"` : "";
759
+ return ` ${relation.key}: one(${relation.model}, {
719
760
  fields: [${relation.reference.field}],
720
- references: [${relation.reference.references}],
721
- })` : "").filter((x) => x !== "").join(",\n ")}${singleRelations.length > 0 && manyRelations.length > 0 ? "," : ""}
722
- ${manyRelations.map(({ key, model }) => ` ${key}: many(${model})`).join(",\n ")}
761
+ references: [${relation.reference.references}]${relationNameParam}
762
+ })`;
763
+ }).filter((x) => x !== "").join(",\n ")}${singleRelations.length > 0 && manyRelations.length > 0 ? "," : ""}
764
+ ${manyRelations.map(({ key, model, relationName }) => {
765
+ return ` ${key}: many(${model}${relationName ? `, { relationName: "${relationName}" }` : ""})`;
766
+ }).join(",\n ")}
723
767
  }))`;
724
768
  relationsString += `\n${tableRelation}\n`;
725
769
  } else if (hasOne) {
726
770
  const tableRelation = `export const ${modelName}Relations = relations(${modelName}, ({ one }) => ({
727
- ${singleRelations.map((relation) => relation.reference ? ` ${relation.key}: one(${relation.model}, {
771
+ ${singleRelations.map((relation) => {
772
+ if (!relation.reference) return "";
773
+ const relationNameParam = relation.relationName ? `,\n relationName: "${relation.relationName}"` : "";
774
+ return ` ${relation.key}: one(${relation.model}, {
728
775
  fields: [${relation.reference.field}],
729
- references: [${relation.reference.references}],
730
- })` : "").filter((x) => x !== "").join(",\n ")}
776
+ references: [${relation.reference.references}]${relationNameParam}
777
+ })`;
778
+ }).filter((x) => x !== "").join(",\n ")}
731
779
  }))`;
732
780
  relationsString += `\n${tableRelation}\n`;
733
781
  } else if (hasMany) {
734
782
  const tableRelation = `export const ${modelName}Relations = relations(${modelName}, ({ many }) => ({
735
- ${manyRelations.map(({ key, model }) => ` ${key}: many(${model})`).join(",\n ")}
783
+ ${manyRelations.map(({ key, model, relationName }) => {
784
+ return ` ${key}: many(${model}${relationName ? `, { relationName: "${relationName}" }` : ""})`;
785
+ }).join(",\n ")}
736
786
  }))`;
737
787
  relationsString += `\n${tableRelation}\n`;
738
788
  }
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { i as getPackageInfo, n as generateSchema } from "./generators-BVZYl7Wb.mjs";
2
+ import { i as getPackageInfo, n as generateSchema } from "./generators-Cb42XRwg.mjs";
3
3
  import { Command } from "commander";
4
4
  import fs, { existsSync, readFileSync } from "node:fs";
5
5
  import fs$1 from "node:fs/promises";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pecunia-cli",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "type": "module",
5
5
  "module": "dist/index.mjs",
6
6
  "main": "./dist/index.mjs",