Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Project "a" {
database_type: '''aaa\'hehe'''
}

Enum "ecommerce"."products_status" {
"out_of_stock"
"in_stock"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,36 @@ describe('@dbml/core - DbmlExporter flags', () => {
});
});

describe('@dbml/core - DbmlExporter project', () => {
const DBML_WITH_PROJECT = `
Project "project_name" {
database_type: 'PostgreSQL'
Note: 'Description of the project'
}

Table "users" {
"id" integer [pk]
}`.trim();

test('exports the Project block', () => {
const database = (new Parser()).parse(DBML_WITH_PROJECT, 'dbmlv2');
const res = DbmlExporter.export(database.normalize(), { includeRecords: true });
expect(res.trim()).toBe(DBML_WITH_PROJECT);
});

test('exports a Project with only a note', () => {
const database = (new Parser()).parse(`Project {\n Note: 'It\\'s a note'\n}`, 'dbmlv2');
const res = DbmlExporter.export(database.normalize(), { includeRecords: true });
expect(res.trim()).toBe(`Project {\n Note: '''It\\'s a note'''\n}`);
});

test('exports nothing when there is no Project', () => {
const database = (new Parser()).parse('Table "users" {\n "id" integer\n}', 'dbmlv2');
const res = DbmlExporter.export(database.normalize(), { includeRecords: true });
expect(res).not.toMatch(/^Project\b/m);
});
});

describe('@dbml/core - ModelExporter table groups', () => {
test('does not schema-qualify TableGroup names', () => {
const database = (new Parser()).parse(
Expand Down
14 changes: 14 additions & 0 deletions packages/dbml-core/src/export/DbmlExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,25 @@ class DbmlExporter {
return blocks.join('\n');
}

static exportProject (model: NormalizedModel): string {
const { name, databaseType, note } = model.database['1'];
if (!name && !databaseType && !note) return '';

const projectName = name ? ` "${name}"` : '';
const databaseTypeStr = databaseType ? ` database_type: ${DbmlExporter.escapeNote(databaseType)}\n` : '';
const noteStr = note ? ` Note: ${DbmlExporter.escapeNote(note)}\n` : '';

return `Project${projectName} {\n${databaseTypeStr}${noteStr}}\n`;
}

static export (model: NormalizedModel, options: DbmlExporterOptions): string {
const elementStrs: string[] = [];
const database = model.database['1'];
const { includeRecords } = options;

const projectStr = DbmlExporter.exportProject(model);
if (projectStr) elementStrs.push(projectStr);

database.schemaIds.forEach((schemaId) => {
const {
enumIds, tableIds, tableGroupIds, refIds, depIds,
Expand Down
Loading