Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
marcjulian committed Feb 15, 2024
1 parent 421d0e4 commit c96b10b
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 38 deletions.
6 changes: 3 additions & 3 deletions __tests__/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('Tables', () => {

test('generate a table with single composite unique index', async () => {
const dmmf = await generateDMMF(
datamodelTableWithSingleCompositeUniqueIndex
datamodelTableWithSingleCompositeUniqueIndex,
);

const expected = `Table A {
Expand All @@ -61,7 +61,7 @@ describe('Tables', () => {

test('generate a table with three fields as composite unique index', async () => {
const dmmf = await generateDMMF(
datamodelTableWithThreeFieldsCompositeUniqueIndex
datamodelTableWithThreeFieldsCompositeUniqueIndex,
);

const expected = `Table A {
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('Tables', () => {

test('generate a table with block id and composite unique index', async () => {
const dmmf = await generateDMMF(
datamodelTableWithBlockIdAndCompositeUnqiue
datamodelTableWithBlockIdAndCompositeUnqiue,
);

const expected = `Table User {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/dbml-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function generate(options: GeneratorOptions) {
allowManyToMany,
mapToDbSchema,
includeRelationFields,
projectOptions
projectOptions,
);

await writeFile(join(outputDir, dbmlFileName), dbmlSchema);
Expand Down
4 changes: 2 additions & 2 deletions src/generator/dbml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export function generateDBMLSchema(
allowManyToMany: boolean = true,
mapToDbSchema: boolean = false,
includeRelationFields: boolean = true,
projectOptions?: ProjectOptions
projectOptions?: ProjectOptions,
): string {
const tables = generateTables(
dmmf.datamodel.models,
mapToDbSchema,
includeRelationFields
includeRelationFields,
);
const manyToManyTables = allowManyToMany
? generateManyToManyTables(dmmf.datamodel.models, mapToDbSchema)
Expand Down
2 changes: 1 addition & 1 deletion src/generator/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DMMF } from '@prisma/generator-helper';

export function generateEnums(enums: DMMF.DatamodelEnum[]): string[] {
return enums.map(
(e) => `Enum ${e.name} {\n` + generateEnumValues(e.values) + '\n}'
(e) => `Enum ${e.name} {\n` + generateEnumValues(e.values) + '\n}',
);
}

Expand Down
28 changes: 14 additions & 14 deletions src/generator/many-to-many-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DMMF } from '@prisma/generator-helper';

export function generateManyToManyTables(
models: DMMF.Model[],
mapToDbSchema: boolean = false
mapToDbSchema: boolean = false,
): string[] {
const manyToManyFields = filterManyToManyRelationFields(models);
if (manyToManyFields.length === 0) {
Expand All @@ -16,7 +16,7 @@ function generateTables(
manyToManyFields: DMMF.Field[],
models: DMMF.Model[],
manyToManyTables: string[] = [],
mapToDbSchema: boolean = false
mapToDbSchema: boolean = false,
): string[] {
const manyFirst = manyToManyFields.shift();
if (!manyFirst) {
Expand All @@ -25,7 +25,7 @@ function generateTables(

// In the case of a manyMany, second field should be found
const manySecond = manyToManyFields.find(
(field) => field.relationName === manyFirst.relationName
(field) => field.relationName === manyFirst.relationName,
)!;

// If its a manyMany, generate the join table
Expand All @@ -36,26 +36,26 @@ function generateTables(
`${generateJoinFields(
[manyFirst, manySecond],
models,
mapToDbSchema
mapToDbSchema,
)}` +
'\n}'
'\n}',
);
}

return generateTables(
manyToManyFields.filter(
(field) => field.relationName !== manyFirst.relationName
(field) => field.relationName !== manyFirst.relationName,
),
models,
manyToManyTables,
mapToDbSchema
mapToDbSchema,
);
}

function generateJoinFields(
fields: [DMMF.Field, DMMF.Field],
models: DMMF.Model[],
mapToDbSchema: boolean = false
mapToDbSchema: boolean = false,
): string {
return fields
.map((field) => joinField(field, models, mapToDbSchema))
Expand All @@ -65,15 +65,15 @@ function generateJoinFields(
function joinField(
field: DMMF.Field,
models: DMMF.Model[],
mapToDbSchema: boolean = false
mapToDbSchema: boolean = false,
): string {
const fieldName = mapToDbSchema
? getModelByType(models, field.type)?.dbName || field.type
: field.type;

return ` ${field.name.toLowerCase()}Id ${getJoinIdType(
field,
models
models,
)} [ref: > ${fieldName}.${field.relationToFields![0]}]`;
}

Expand All @@ -83,8 +83,8 @@ function getJoinIdType(joinField: DMMF.Field, models: DMMF.Model[]): string {
.map(
(model) =>
model.fields.find(
(field) => field.name === joinField.relationToFields![0]
)!
(field) => field.name === joinField.relationToFields![0],
)!,
)[0];

return joinIdField.type;
Expand All @@ -100,15 +100,15 @@ function filterManyToManyRelationFields(models: DMMF.Model[]): DMMF.Field[] {
field.isList &&
field.relationFromFields?.length === 0 &&
// Updated this condition to match manyMany fields as defined in the new DMMF
field.relationToFields?.length === 0
field.relationToFields?.length === 0,
)
// As the relationToFields is not populated in the DMMF, we need to populate it manually
.map((field) => ({
...field,
relationToFields: model.fields
.filter((f) => f.isId)
.map((f) => f.name),
}))
})),
)
.flat();
}
2 changes: 1 addition & 1 deletion src/generator/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DMMF } from '@prisma/generator-helper';

export const getModelByType = (
models: DMMF.Model[],
type: string
type: string,
): DMMF.Model | undefined => {
return models.find((model) => model.name === type);
};
2 changes: 1 addition & 1 deletion src/generator/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function getProjectOptions({
projectNoteMd = await readFile(fullPath, 'utf-8');
} catch (e) {
console.log(
`❌ Error: project note markdown file not found: ${fullPath}`
`❌ Error: project note markdown file not found: ${fullPath}`,
);
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/generator/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const manyToOne = '>';

export function generateRelations(
models: DMMF.Model[],
mapToDbSchema: boolean = false
mapToDbSchema: boolean = false,
): string[] {
const refs: string[] = [];
models.forEach((model) => {
Expand All @@ -16,7 +16,7 @@ export function generateRelations(
(field) =>
field.relationName &&
field.relationToFields?.length &&
field.relationFromFields?.length
field.relationFromFields?.length,
)
.forEach((field) => {
const relationFrom = model.name;
Expand All @@ -25,7 +25,7 @@ export function generateRelations(
const relationOperator = getRelationOperator(
models,
relationFrom,
relationTo
relationTo,
);

const relationFormName =
Expand All @@ -36,15 +36,15 @@ export function generateRelations(
: relationTo;

const ref = `Ref: ${relationFormName}.${combineKeys(
field.relationFromFields!
field.relationFromFields!,
)} ${relationOperator} ${relationToName}.${combineKeys(
field.relationToFields!!
field.relationToFields!!,
)}`;

const referentialActions = getReferentialActions(
models,
relationFrom,
relationTo
relationTo,
);

refs.push(`${ref}${referentialActions}`);
Expand All @@ -56,7 +56,7 @@ export function generateRelations(
const getRelationOperator = (
models: DMMF.Model[],
from: string,
to: string
to: string,
): string => {
const model = models.find((model) => model.name === to);
const field = model?.fields.find((field) => field.type === from);
Expand All @@ -72,7 +72,7 @@ const combineKeys = (keys: string[]): string => {
const getReferentialActions = (
models: DMMF.Model[],
from: string,
to: string
to: string,
): string => {
const model = models.find((model) => model.name === from);
const field = model?.fields.find((field) => field.type === to);
Expand All @@ -83,7 +83,7 @@ const getReferentialActions = (
`delete: ${
referentialActionsMap.get(field.relationOnDelete) ||
field.relationOnDelete
}`
}`,
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/generator/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getModelByType } from './model';
export function generateTables(
models: DMMF.Model[],
mapToDbSchema: boolean = false,
includeRelationFields: boolean = true
includeRelationFields: boolean = true,
): string[] {
return models.map((model) => {
let modelName = model.name;
Expand All @@ -20,7 +20,7 @@ export function generateTables(
model.fields,
models,
mapToDbSchema,
includeRelationFields
includeRelationFields,
) +
generateTableIndexes(model) +
generateTableDocumentation(model) +
Expand Down Expand Up @@ -52,12 +52,12 @@ const generateTableBlockId = (primaryFields: string[] | undefined): string => {
};

const generateTableCompositeUniqueIndex = (
uniqueFields: string[][]
uniqueFields: string[][],
): string => {
return uniqueFields
.filter((composite) => composite.length > 1)
.map(
(composite) => ` (${composite.join(', ')}) [${DBMLKeywords.Unique}]`
(composite) => ` (${composite.join(', ')}) [${DBMLKeywords.Unique}]`,
)
.join('\n');
};
Expand All @@ -71,7 +71,7 @@ const generateFields = (
fields: DMMF.Field[],
models: DMMF.Model[],
mapToDbSchema: boolean = false,
includeRelationFields: boolean = true
includeRelationFields: boolean = true,
): string => {
if (!includeRelationFields) {
fields = fields.filter((field) => !field.relationName);
Expand Down Expand Up @@ -129,7 +129,7 @@ const generateColumnDefinition = (field: DMMF.Field): string => {

if (field.documentation) {
columnDefinition.push(
`${DBMLKeywords.Note}: '${field.documentation.replace(/'/g, "\\'")}'`
`${DBMLKeywords.Note}: '${field.documentation.replace(/'/g, "\\'")}'`,
);
}

Expand Down

0 comments on commit c96b10b

Please sign in to comment.