Skip to content

use todo for functional indexes for now #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-dbml-generator",
"version": "0.10.0",
"version": "0.10.1",
"description": "Convert your Drizzle ORM schema into DBML markup",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down
83 changes: 71 additions & 12 deletions src/generators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,58 @@ import type {
} from '~/symbols';
import type { AnyColumn, BuildQueryConfig } from 'drizzle-orm';
import type { AnyBuilder, AnySchema, AnyTable } from '~/types';
import { getTableName } from "drizzle-orm";

function getChunk(v: any) {
if (is(v, Column)) {
//console.log("COL1", v);
return v;
//return { name: "TODO1" };
}
else {
//console.log("RET", v);
return { name: "TODO" };
}

}


function getName(obj:any) {

if (typeof(obj) == "string") {
return obj
} else {

console.log("getName",typeof(obj), Object.keys(obj))
if(obj.name) {
return getName(obj.name)
}
else {
const text= getTableName(obj);
if (text) {
//console.log("HELP",text)
return text
} else {
//console.log("HELP2",obj)
return undefined
}
}
}
}

function getCols(entry: any) {
// console.log("INDEXENTRY", entry);
let ret = undefined;
if (is(entry, SQL)) {
ret = entry.queryChunks.map(getChunk)
//console.log("INDEXENTRY1", ret);
} else {
ret = (entry as Column);
//console.log("INDEXENTRY2", ret);
}

return ret;
}

export abstract class BaseGenerator<
Schema extends AnySchema = AnySchema,
Expand All @@ -76,7 +128,8 @@ export abstract class BaseGenerator<

constructor(schema: Schema, relational: boolean) {
this.schema = schema;
this.relational = relational;
this.relational = relational;

}

protected isIncremental(_column: Column) {
Expand Down Expand Up @@ -139,9 +192,9 @@ export abstract class BaseGenerator<
}

protected generateTable(table: AnyTable) {
if (!this.relational) {
if (!this.relational) {
this.generateForeignKeys(table[this.InlineForeignKeys as typeof AnyInlineForeignKeys]);
}
}

const dbml = new DBML().insert('table ');

Expand Down Expand Up @@ -185,11 +238,9 @@ export abstract class BaseGenerator<
for (const indexName in indexes) {
const index = indexes[indexName].build(table);
dbml.tab(2);

// console.log("INDEX", index);
if (is(index, PgIndex) || is(index, MySqlIndex) || is(index, SQLiteIndex)) {
const configColumns = index.config.columns.flatMap((entry) =>
is(entry, SQL) ? entry.queryChunks.filter((v) => is(v, Column)) : (entry as Column)
);
const configColumns = index.config.columns.flatMap(getCols);

const idxColumns = wrapColumns(
configColumns as AnyColumn[],
Expand Down Expand Up @@ -239,6 +290,12 @@ export abstract class BaseGenerator<
const sourceColumns = fks[i].reference().columns;
const foreignColumns = fks[i].reference().foreignColumns;

const keyname = `ref ${fks[i].getName()}: `;
//console.log("generateForeignKeys1",i,keyname )

// console.log("generateForeignKeys1",i,getName(sourceTable) )
//console.log("generateForeignKeys2",i,getName(foreignTable))
//console.log("generateForeignKeys3",i,keyname)
const dbml = new DBML().insert(`ref ${fks[i].getName()}: `);

if (sourceSchema) {
Expand Down Expand Up @@ -266,8 +323,10 @@ export abstract class BaseGenerator<
];
const actionsStr = ` [${formatList(actions, this.buildQueryConfig.escapeName)}]`;

dbml.insert(actionsStr);
this.generatedRefs.push(dbml.build());
dbml.insert(actionsStr);
const total = dbml.build();
//console.log("TOTAL",total)
this.generatedRefs.push(total);
}
}

Expand Down Expand Up @@ -298,9 +357,8 @@ export abstract class BaseGenerator<
(relations_[i].table as unknown as AnyTable)[TableName],
relation.referencedTableName
].sort();
const key = `${tableNames[0]}-${tableNames[1]}${
relation.relationName ? `-${relation.relationName}` : ''
}`;
const key = `${tableNames[0]}-${tableNames[1]}${relation.relationName ? `-${relation.relationName}` : ''
}`;

if ((is(relation, One) && relation.config?.references.length) || 0 > 0) {
left[key] = {
Expand Down Expand Up @@ -387,6 +445,7 @@ export abstract class BaseGenerator<
.concatAll(this.generatedRefs)
.build();

//console.log("DBML",dbml)
return dbml;
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ export function formatList(
}

export function wrapColumns(columns: AnyColumn[], escapeName: (name: string) => string) {
//console.log("COLUMNS", columns);
const formatted = formatList(
columns.map((column) => column.name),
columns.map((column) => column ? column.name : "MISSING_COLUMN"),
escapeName,
true
);
return columns.length === 1 ? columns[0].name : `(${formatted})`;
if (columns.length === 1) {
return columns[0] ? columns[0].name : "noname";
}
else {
return `(${formatted})`;
}

}

export function wrapColumnNames(columns: string[], escapeName: (name: string) => string) {
Expand Down