Skip to content

Commit

Permalink
begin fix for valtyr#107
Browse files Browse the repository at this point in the history
  • Loading branch information
alii committed Sep 28, 2024
1 parent 0a5be44 commit 27cbafa
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/helpers/generateFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export const generateFile = (
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>;
export type Timestamp = ColumnType<Date, Date | string, Date | string>;`;
export type Timestamp = ColumnType<Date, Date | string, Date | string>;
export type EnumArrayInner<T extends string, All extends string> = T extends infer Single extends string ? T | \`\${Single},\${Exclude<All, Single>}\` : never
export type EnumArray<T extends string> = \`{\${EnumArrayInner<T, T>}}\`;`;

if (withEnumImport) {
const enumImportStatement = `import type { ${withEnumImport.names.join(
Expand Down
39 changes: 39 additions & 0 deletions src/helpers/generateModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,42 @@ test("it respects camelCase option", () => {
userName: string | null;
};`);
});

test("it respects enum array values", () => {
const model = generateModel(
{
name: "User",
fields: [
{
name: "permissions",
isId: false,
isGenerated: false,
kind: "enum",
type: "UserPermissions",
hasDefaultValue: true,
isList: true,
isReadOnly: false,
isRequired: true,
isUnique: false,
},
],
primaryKey: null,
uniqueFields: [],
uniqueIndexes: [],
dbName: null,
},
{
databaseProvider: "postgresql",
fileName: "env(DATABASE_URL)",
enumFileName: "",
camelCase: true,
readOnlyIds: false,
}
);

const source = stringifyTsNode(model.definition);

expect(source).toEqual(`export type User = {
permissions: EnumArray<UserPermissions>;
};`);
});
18 changes: 14 additions & 4 deletions src/helpers/generateModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ export const generateModel = (model: DMMF.Model, config: Config) => {
return generateField({
isId: field.isId,
name: normalizeCase(dbName || field.name, config),
type: ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(field.type),
undefined
),
type: field.isList
? ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier("EnumArray"),
[
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(field.type),
undefined
),
]
)
: ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(field.type),
undefined
),
nullable: !field.isRequired,
generated: isGenerated,
list: field.isList,
Expand Down

0 comments on commit 27cbafa

Please sign in to comment.