Skip to content

Commit

Permalink
Merge pull request #8 from itsjxck/feature/support-multiple-generators
Browse files Browse the repository at this point in the history
Add support for multiple generators. 
- When using multiple generators, the `name` property is required. Otherwise, it is optional and will default to `'client'`.
  • Loading branch information
ridafkih authored Aug 2, 2022
2 parents d1cd1fa + bea1b3f commit 25924c3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
6 changes: 6 additions & 0 deletions lib/@types/prisma-generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export interface PrismaGeneratorOptions {
provider: string;
name?: string;
output?: string;
previewFeatures?: string[];
engineType?: "library" | "binary";
binaryTargets?: string[];
[key: string]: string | string[] | undefined;
}

export type PrismaMultiGeneratorOptions = Array<
Omit<PrismaGeneratorOptions, "name"> & { name: string }
>;
7 changes: 5 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { PrismaSchema } from "@/modules/PrismaSchema";

import { PrismaDataSourceOptions } from "@/@types/prisma-datasource";
import { PrismaGeneratorOptions } from "@/@types/prisma-generator";
import {
PrismaGeneratorOptions,
PrismaMultiGeneratorOptions,
} from "@/@types/prisma-generator";

interface CreateSchemaOptions {
datasource: PrismaDataSourceOptions;
generator: PrismaGeneratorOptions;
generator: PrismaGeneratorOptions | PrismaMultiGeneratorOptions;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/PrismaScalarField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class PrismaScalarField {
switch (typeof defaultValue) {
case "object":
const [prismaFunc] =
Object.entries(defaultValue).find(([_key, value]) => value) || [];
Object.entries(defaultValue).find(([, value]) => value) || [];
setDefaultValue(`${prismaFunc}()`);
break;
case "string":
Expand Down
27 changes: 20 additions & 7 deletions lib/modules/PrismaSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import { exportSchema } from "@/util/export";
import { parseKeyValueBlock } from "@/util/blocks";

import { PrismaDataSourceOptions } from "@/@types/prisma-datasource";
import { PrismaGeneratorOptions } from "@/@types/prisma-generator";
import {
PrismaGeneratorOptions,
PrismaMultiGeneratorOptions,
} from "@/@types/prisma-generator";

export class PrismaSchema {
private enums: Map<string, PrismaEnum> = new Map();
private models: Map<string, PrismaModel> = new Map();

constructor(
private readonly datasource: PrismaDataSourceOptions,
private readonly generator: PrismaGeneratorOptions
private readonly generator:
| PrismaGeneratorOptions
| PrismaMultiGeneratorOptions
) {}

/**
Expand All @@ -35,11 +40,19 @@ export class PrismaSchema {
* @returns A string representing the generator block.
*/
private parseGenerator() {
return parseKeyValueBlock(
"generator",
"client",
Object.entries(this.generator)
);
const generators = Array.isArray(this.generator)
? this.generator
: [this.generator];

return generators
.map(({ name = "client", ...generator }) =>
parseKeyValueBlock(
"generator",
name,
Object.entries(generator) as [string, string | string[]][]
)
)
.join("\n\n");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/util/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export const parseKeyValueBlock = (
keyword: string,
name: string,
entries: [string, string | { env: string }][]
entries: [string, string | string[] | { env: string }][]
) => {
const tokenPadding = Math.max(...entries.map(([key]) => key.length));
const body = entries
Expand Down

0 comments on commit 25924c3

Please sign in to comment.