Skip to content

Commit

Permalink
fixes import issue with indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoalbanese committed Mar 6, 2024
1 parent 0fbaa43 commit 87de270
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/commands/generate/generators/model/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const generateImportStatement = (
.join(", ")
.concat(
`, ${mappings.tableFunc}`
)} } from "drizzle-orm/${dbType}-core";\nimport { createInsertSchema, createSelectSchema } from "drizzle-zod";\nimport { z } from "zod";\n${
)}${schema.index ? ", uniqueIndex" : ""} } from "drizzle-orm/${dbType}-core";\nimport { createInsertSchema, createSelectSchema } from "drizzle-zod";\nimport { z } from "zod";\n${
referenceImports.length > 0 ? referenceImports.join("\n") : ""
}${
belongsToUser && provider !== "planetscale" && authSubType !== "managed"
Expand Down Expand Up @@ -139,13 +139,14 @@ const generateFieldsForSchema = (fields: DBField[], mappings: TypeMap) => {

const generateIndex = (schema: Schema) => {
const { tableName, index } = schema;
const { tableNameCamelCase } = formatTableName(tableName);
const { tableNameCamelCase, tableNameSingularSnake } =
formatTableName(tableName);
return index
? `, (${tableNameCamelCase}) => {
return {
${toCamelCase(
index
)}Index: uniqueIndex('${index}_idx').on(${tableNameCamelCase}.${toCamelCase(
)}Index: uniqueIndex('${tableNameSingularSnake}_${index}_idx').on(${tableNameCamelCase}.${toCamelCase(
index
)}),
}
Expand Down
3 changes: 3 additions & 0 deletions src/commands/generate/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export const formatTableName = (tableName: string) => {
);
const tableNameKebabCase = snakeToKebab(tableName);

const tableNameSingularSnake = pluralize.singular(tableName);

return {
tableNameCamelCase,
tableNameSingular,
Expand All @@ -92,6 +94,7 @@ export const formatTableName = (tableName: string) => {
tableNameNormalEnglishLowerCase,
tableNameNormalEnglishSingularLowerCase,
tableNameKebabCase,
tableNameSingularSnake,
};
};

Expand Down

0 comments on commit 87de270

Please sign in to comment.