Skip to content

Commit

Permalink
support prisma 5
Browse files Browse the repository at this point in the history
  • Loading branch information
marcjulian committed Feb 15, 2024
1 parent c96b10b commit f782719
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 85 deletions.
8 changes: 3 additions & 5 deletions __tests__/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ describe('Project', () => {
test('generate no project block', async () => {
const { generators } = await generateConfig(datamodelUnnamedProject);
const projectOptions = await getProjectOptions(generators[0].config);
const project = generateProject(projectOptions);

expect(project.length).toEqual(0);
expect(projectOptions).toBeUndefined();
});

test('generate a project block with note', async () => {
const { generators } = await generateConfig(datamodelProjectWithNote);
const projectOptions = await getProjectOptions(generators[0].config);
const project = generateProject(projectOptions);
const project = generateProject(projectOptions!);

const expected =
'Project "Test Project" {\n' +
Expand All @@ -33,7 +31,7 @@ describe('Project', () => {
test('generate a project block with noteMd', async () => {
const { generators } = await generateConfig(datamodelProjectWithNoteMd);
const projectOptions = await getProjectOptions(generators[0].config);
const project = generateProject(projectOptions);
const project = generateProject(projectOptions!);

const expected =
'Project "Test Project" {\n' +
Expand Down
134 changes: 76 additions & 58 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@
"url": "https://github.com/notiz-dev/prisma-dbml-generator/issues"
},
"dependencies": {
"@prisma/generator-helper": "4.16.1",
"@prisma/internals": "4.16.1"
"@prisma/generator-helper": "5.0.0",
"@prisma/internals": "5.0.0"
},
"devDependencies": {
"@prisma/client": "4.16.1",
"@prisma/client": "5.0.0",
"@types/jest": "^29.2.4",
"@types/node": "18.11.0",
"jest": "29.7.0",
"prettier": "3.2.5",
"prisma": "4.16.1",
"prisma": "5.0.0",
"ts-jest": "29.1.2",
"ts-toolbelt": "^9.6.0",
"typescript": "5.3.3"
Expand Down
6 changes: 5 additions & 1 deletion src/cli/dbml-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ export const defaultDBMLFileName = 'schema.dbml';
export async function generate(options: GeneratorOptions) {
const { output, config } = options.generator;
const outputDir = parseEnvValue(output!);
const dbmlFileName = config.outputName || defaultDBMLFileName;

const dbmlFileName =
typeof config.outputName === 'string'
? config.outputName
: defaultDBMLFileName;
const allowManyToMany = config.manyToMany === 'false' ? false : true;
const mapToDbSchema = config.mapToDbSchema === 'false' ? false : true;
const includeRelationFields =
Expand Down
42 changes: 25 additions & 17 deletions src/generator/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,32 @@ export async function getProjectOptions({
projectDatabaseType,
projectNote,
projectNotePath,
}: GeneratorConfig['config']): Promise<ProjectOptions> {
let projectNoteMd = '';
}: GeneratorConfig['config']): Promise<ProjectOptions | undefined> {
if (typeof projectName === 'string') {
let projectNoteMd = '';

if (projectNotePath) {
const fullPath = `${process.cwd()}/${projectNotePath}`;
try {
projectNoteMd = await readFile(fullPath, 'utf-8');
} catch (e) {
console.log(
`❌ Error: project note markdown file not found: ${fullPath}`,
);
if (projectNotePath) {
const fullPath = `${process.cwd()}/${projectNotePath}`;
try {
projectNoteMd = await readFile(fullPath, 'utf-8');
} catch (e) {
console.log(
`❌ Error: project note markdown file not found: ${fullPath}`,
);
}
}
}

return {
name: projectName && `"${projectName}"`,
databaseType: projectDatabaseType || '',
note: projectNoteMd || projectNote || '', // noteMd takes precedence
isMd: projectNoteMd !== '',
};
return {
name: projectName && `"${projectName}"`,
databaseType:
typeof projectDatabaseType === 'string' ? projectDatabaseType : '',
note: projectNoteMd
? projectNoteMd
: typeof projectNote === 'string'
? projectNote
: '', // noteMd takes precedence
isMd: projectNoteMd !== '',
};
}
return undefined;
}

0 comments on commit f782719

Please sign in to comment.