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
6 changes: 3 additions & 3 deletions dbml-playground/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dbml/playground",
"version": "10.1.0",
"version": "10.1.1-alpha.0",
"description": "Interactive playground for debugging and visualizing the DBML parser pipeline",
"author": "Holistics <dev@holistics.io>",
"license": "Apache-2.0",
Expand All @@ -26,8 +26,8 @@
"format": "prettier --write src/"
},
"dependencies": {
"@dbml/core": "^10.1.0",
"@dbml/parse": "^10.1.0",
"@dbml/core": "^10.1.1-alpha.0",
"@dbml/parse": "^10.1.1-alpha.0",
"@phosphor-icons/vue": "^2.2.0",
"dompurify": "^3.4.13",
"floating-vue": "^5.2.2",
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "10.1.0",
"version": "10.1.1-alpha.0",
"npmClient": "yarn",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
8 changes: 4 additions & 4 deletions packages/dbml-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "@dbml/cli",
"version": "10.1.0",
"version": "10.1.1-alpha.0",
"description": "",
"main": "lib/index.js",
"license": "Apache-2.0",
Expand Down Expand Up @@ -33,9 +33,9 @@
],
"dependencies": {
"@babel/cli": "^7.21.0",
"@dbml/connector": "^10.1.0",
"@dbml/core": "^10.1.0",
"@dbml/parse": "^10.1.0",
"@dbml/connector": "^10.1.1-alpha.0",
"@dbml/core": "^10.1.1-alpha.0",
"@dbml/parse": "^10.1.1-alpha.0",
"bluebird": "^3.5.5",
"chalk": "^2.4.2",
"commander": "^2.20.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/dbml-connector/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "@dbml/connector",
"version": "10.1.0",
"version": "10.1.1-alpha.0",
"description": "This package was created to fetch the schema JSON from many kind of databases.",
"author": "huy.phung.sw@gmail.com",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions packages/dbml-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "@dbml/core",
"version": "10.1.0",
"version": "10.1.1-alpha.0",
"description": "> TODO: description",
"author": "Holistics <dev@holistics.io>",
"license": "Apache-2.0",
Expand Down Expand Up @@ -47,7 +47,7 @@
"lint:fix": "eslint --fix ."
},
"dependencies": {
"@dbml/parse": "^10.1.0",
"@dbml/parse": "^10.1.1-alpha.0",
"antlr4": "^4.13.1",
"lodash": "^4.18.1",
"lodash-es": "^4.18.1",
Expand Down
3 changes: 0 additions & 3 deletions packages/dbml-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
syncDiagramView,
findDiagramViewBlocks,
syncDep,
findDepBlocks,
} from './transform';
import { VERSION } from './utils/version';

Expand All @@ -23,7 +22,6 @@ export {
syncDiagramView,
findDiagramViewBlocks,
syncDep,
findDepBlocks,
ModelExporter,
CompilerError,
Parser,
Expand Down Expand Up @@ -80,7 +78,6 @@ export type {
DepSyncOperation,
DepSyncEdge,
DepEndpointRef,
DepBlock,
TextEdit,
ElementIdentifier,
SchemaIdentifier,
Expand Down
7 changes: 3 additions & 4 deletions packages/dbml-core/src/transform/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Compiler, DEFAULT_ENTRY, MemoryProjectLayout } from '@dbml/parse';
import type {
DiagramViewSyncOperation, DiagramViewBlock, TextEdit,
DepSyncOperation, DepBlock, ElementIdentifier, TableIdentifier,
DepSyncOperation, ElementIdentifier, TableIdentifier,
} from '@dbml/parse';

export { findDiagramViewBlocks, findDepBlocks } from '@dbml/parse';
export { findDiagramViewBlocks } from '@dbml/parse';

export function renameTable (oldName: string | TableIdentifier, newName: string | TableIdentifier, dbmlCode: string): string {
const layout = new MemoryProjectLayout();
Expand All @@ -28,12 +28,11 @@ export function syncDiagramView (
export function syncDep (
dbmlCode: string,
operations: DepSyncOperation[],
blocks?: DepBlock[],
): { newDbml: string; edits: TextEdit[] } {
const layout = new MemoryProjectLayout();
layout.setSource(DEFAULT_ENTRY, dbmlCode);
const compiler = new Compiler(layout);
return compiler.syncDep(DEFAULT_ENTRY, operations, blocks);
return compiler.syncDep(DEFAULT_ENTRY, operations);
}

export function updateElementSettingEdit (
Expand Down
3 changes: 0 additions & 3 deletions packages/dbml-core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
syncDiagramView,
findDiagramViewBlocks,
syncDep,
findDepBlocks,
} from './transform';
export {
renameTable,
Expand All @@ -18,7 +17,6 @@ export {
syncDiagramView,
findDiagramViewBlocks,
syncDep,
findDepBlocks,
importer,
exporter,
ModelExporter,
Expand Down Expand Up @@ -78,7 +76,6 @@ export type {
DepSyncOperation,
DepSyncEdge,
DepEndpointRef,
DepBlock,
TextEdit,
ElementIdentifier,
SchemaIdentifier,
Expand Down
7 changes: 1 addition & 6 deletions packages/dbml-core/types/transform/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
DiagramViewSyncOperation, DiagramViewBlock, TextEdit,
DepSyncOperation, DepBlock, ElementIdentifier, TableIdentifier,
DepSyncOperation, ElementIdentifier, TableIdentifier,
} from '@dbml/parse';

export function renameTable(
Expand Down Expand Up @@ -36,9 +36,4 @@ export function findDiagramViewBlocks(
export function syncDep(
dbmlCode: string,
operations: DepSyncOperation[],
blocks?: DepBlock[],
): { newDbml: string; edits: TextEdit[] };

export function findDepBlocks(
dbmlCode: string,
): DepBlock[];
101 changes: 87 additions & 14 deletions packages/dbml-parse/__tests__/examples/compiler/syncDep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,38 +127,111 @@ Dep {
expect(db.deps[0].color).toBe('#abcabc');
});

it('matches a table-level edge to a column-level block (implicit table dep)', () => {
it('leaves a column-level block alone when the target is table-level', () => {
const dbml = `${PRELUDE}
Dep {
a.id -> b.id
}`;
const { newDbml } = syncDep(dbml, [
const { newDbml, edits } = syncDep(dbml, [
{ operation: 'update', edge: tableEdge('a', 'b'), color: '#1abc9c' },
]);
expect(newDbml).toContain('[color: #1abc9c]');
// Should update in place, not create a duplicate
expect(newDbml.match(/Dep/g)?.length).toBe(1);
const db = interpret(newDbml).getValue()!;
expect(db.deps).toHaveLength(1);
expect(db.deps[0].color).toBe('#1abc9c');
expect(edits).toHaveLength(0);
expect(newDbml).toBe(dbml);
});
});

describe('syncDep - create with table-level edge matching column-level block', () => {
it('treats an existing column-level block as an update when creating with a table-level edge', () => {
describe('syncDep - create with a table-level edge beside a column-level block', () => {
it('creates the direct block rather than colouring the column-level one', () => {
const dbml = `${PRELUDE}
Dep {
a.id -> b.id
}`;
const { newDbml } = syncDep(dbml, [
{ operation: 'create', edge: tableEdge('a', 'b'), color: '#ff5733' },
]);
// Should update existing block, not create a new one
expect(newDbml.match(/Dep/g)?.length).toBe(1);
expect(newDbml).toContain('[color: #ff5733]');
expect(newDbml).toContain('Dep [color: #ff5733] {');
expect(newDbml).toContain('a -> b');
const db = interpret(newDbml).getValue()!;
expect(db.deps).toHaveLength(2);
const direct = db.deps.find((d) => d.edges[0].upstream.fieldNames.length === 0);
const columnLevel = db.deps.find((d) => d.edges[0].upstream.fieldNames.length === 1);
expect(direct?.color).toBe('#ff5733');
expect(columnLevel?.color).toBeUndefined();
});
});

describe('syncDep - mixed-level edges', () => {
const mixedEdge = (): DepSyncOperation['edge'] => ({
upstream: { tableName: 'a', fieldNames: ['id'] },
downstream: { tableName: 'b', fieldNames: [] },
});

const MIXED = `${PRELUDE}
Dep {
a.id -> b
}`;

it('colors the block carrying the edge', () => {
const { newDbml } = syncDep(MIXED, [
{ operation: 'create', edge: mixedEdge(), color: '#0055ff' },
]);
const db = interpret(newDbml).getValue()!;
expect(db.deps).toHaveLength(1);
expect(db.deps[0].color).toBe('#0055ff');
});

it('notes the block carrying the edge', () => {
const { newDbml } = syncDep(MIXED, [
{ operation: 'update', edge: mixedEdge(), note: 'from etl' },
]);
const db = interpret(newDbml).getValue()!;
expect(db.deps).toHaveLength(1);
expect(db.deps[0].color).toBe('#ff5733');
expect(db.deps[0].note?.value).toBe('from etl');
});

it('leaves one block after repeated picks', () => {
const first = syncDep(MIXED, [
{ operation: 'create', edge: mixedEdge(), color: '#0055ff' },
]).newDbml;
const second = syncDep(first, [
{ operation: 'create', edge: mixedEdge(), color: '#ff0055' },
]).newDbml;
const db = interpret(second).getValue()!;
expect(db.deps).toHaveLength(1);
expect(db.deps[0].color).toBe('#ff0055');
});

it('tells a schema-qualified table apart from a table column', () => {
const dbml = `
Table s.a { id int }
Table a { id int }
Table b { id int }

Dep {
s.a -> b
}

Dep {
a.id -> b
}`;
const { newDbml } = syncDep(dbml, [
{
operation: 'create',
edge: { upstream: { schemaName: 's', tableName: 'a', fieldNames: [] }, downstream: { tableName: 'b', fieldNames: [] } },
color: '#111111',
},
{
operation: 'create',
edge: mixedEdge(),
color: '#222222',
},
]);
const db = interpret(newDbml).getValue()!;
expect(db.deps).toHaveLength(2);
const schemaLevel = db.deps.find((d) => d.edges[0].upstream.fieldNames.length === 0);
const columnLevel = db.deps.find((d) => d.edges[0].upstream.fieldNames.length === 1);
expect(schemaLevel?.color).toBe('#111111');
expect(columnLevel?.color).toBe('#222222');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,33 +289,38 @@ Table b {
});

describe('updateElementSetting - inline dep', () => {
const dep = (up: string, down: string): ElementIdentifier => ({
// An inline `[dep: -> b.id]` on a.id is the edge a.id -> b.id, so its identity carries both fields
const dep = (upTable: string, upField: string, downTable: string, downField: string): ElementIdentifier => ({
kind: MetadataKind.Dep,
upstream: { tableName: up },
downstream: { tableName: down },
upstream: { tableName: upTable, fieldNames: [upField] },
downstream: { tableName: downTable, fieldNames: [downField] },
});

it('extracts inline dep to standalone when adding a setting', () => {
const dbml = `Table a {
id int [dep: -> b]
const INLINE = `Table a {
id int [dep: -> b.id]
}
Table b {
id int
}`;
const result = update(dbml, dep('a', 'b'), 'color', '#FF0000');
expect(result).not.toContain('[dep: -> b]');

it('extracts inline dep to standalone when adding a setting', () => {
const result = update(INLINE, dep('a', 'id', 'b', 'id'), 'color', '#FF0000');
expect(result).not.toContain('[dep: -> b.id]');
expect(result).toContain('color: #FF0000');
expect(result).toContain('a -> b');
expect(result).toContain('a.id -> b.id');
});

it('does nothing when removing a setting from an inline dep', () => {
const dbml = `Table a {
id int [dep: -> b]
}
Table b {
id int
}`;
const result = update(dbml, dep('a', 'b'), 'color', null);
expect(result).toBe(dbml);
const result = update(INLINE, dep('a', 'id', 'b', 'id'), 'color', null);
expect(result).toBe(INLINE);
});

it('does nothing when the target is table-level and the inline dep is not', () => {
const result = update(INLINE, {
kind: MetadataKind.Dep,
upstream: { tableName: 'a' },
downstream: { tableName: 'b' },
} as ElementIdentifier, 'color', '#FF0000');
expect(result).toBe(INLINE);
});
});
2 changes: 1 addition & 1 deletion packages/dbml-parse/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "@dbml/parse",
"version": "10.1.0",
"version": "10.1.1-alpha.0",
"description": "DBML parser v2",
"author": "Holistics <dev@holistics.io>",
"license": "Apache-2.0",
Expand Down
2 changes: 0 additions & 2 deletions packages/dbml-parse/src/compiler/queries/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ export {
} from './syncDiagramView';
export {
syncDep,
findDepBlocks,
generateDepBlock,
type DepSyncOperation,
type DepSyncEdge,
type DepEndpointRef,
type DepBlock,
} from './syncDep';
export { applyTextEdits, type TextEdit } from './applyTextEdits';
export type {
Expand Down
Loading
Loading