l-mario564 / drizzle-dbml-generator Goto Github PK
View Code? Open in Web Editor NEWGenerate DBML markup from your schema defined with Drizzle ORM.
License: MIT License
Generate DBML markup from your schema defined with Drizzle ORM.
License: MIT License
I am getting this error and I am unsure if it's due to a custom SQL column ts_vector
column for full-text search.
TypeError: column.getSQLType is not a function
at PgGenerator.generateColumn (E:\Projects\denarius\node_modules\.pnpm\[email protected]_@[email protected][email protected]\node_modules\drizzle-dbml-generator\dist\index.cjs:139:91)
at PgGenerator.generateTable (E:\Projects\denarius\node_modules\.pnpm\[email protected]_@[email protected][email protected]\node_modules\drizzle-dbml-generator\dist\index.cjs:172:31)
at PgGenerator.generate (E:\Projects\denarius\node_modules\.pnpm\[email protected]_@[email protected][email protected]\node_modules\drizzle-dbml-generator\dist\index.cjs:284:35)
at pgGenerate (E:\Projects\denarius\node_modules\.pnpm\[email protected]_@[email protected][email protected]\node_modules\drizzle-dbml-generator\dist\index.cjs:328:68)
at <anonymous> (e:\Projects\denarius\bin\scripts\db\dbml.ts:10:1)
at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
at async loadESM (node:internal/process/esm_loader:34:7)
at async handleMainPromise (node:internal/modules/run_main:66:12)
Here is the function where it errs out and the column
parameter to the function is object
in some cases and function() { return new SQL([this])
in the instance where it crashes. (no getSQLType()
on that function)
generateColumn(column) {
const dbml = new DBML().tab().escapeSpaces(column.name).insert(" ").escapeType(column.getSQLType());
const constraints = [];
if (column.primary) {
constraints.push("pk");
}
if (column.notNull) {
constraints.push("not null");
}
if (column.isUnique) {
constraints.push("unique");
}
if (this.isIncremental(column)) {
constraints.push("increment");
}
if (column.default !== void 0) {
constraints.push(`default: ${this.mapDefaultValue(column.default)}`);
}
if (constraints.length > 0) {
dbml.insert(` [${formatList(constraints, this.buildQueryConfig.escapeName)}]`);
}
return dbml.build();
}
How does it work when schemas are not all in one schemas.ts file but broken down like this? :
๐ฆ
โ ๐ src
โ ๐ db
โ ๐ schema
โ ๐ users.ts
โ ๐ countries.ts
โ ๐ cities.ts
โ ๐ products.ts
โ ๐ clients.ts
โ ๐ enums.ts
โ ๐ etc.ts
With a table defined in the schema as so:
export const warehouseOffices = pgTable(
'warehouse_offices',
{
officeId: integer('office_id')
.notNull()
.references(() => offices.id),
warehouseId: integer('warehouse_id')
.notNull()
.references(() => warehouses.id),
},
(table) => {
return {
pk: primaryKey({ columns: [table.officeId, table.warehouseId] }),
};
}
);
The generated .dbml
doesn't include any refs
for this table:
table warehouse_offices {
office_id integer [not null]
warehouse_id integer [not null]
indexes {
(office_id, warehouse_id) [pk]
}
}
I need to add these lines to the resulting .dbml
:
ref: warehouse_offices.warehouse_id - warehouses.id
ref: warehouse_offices.office_id - offices.id
Is there a way to build a script that reverses the current project?
It could be a CLI that would generate a drizzle schema from DBML.
There is a similar package for prisma, but it would be nice to think about it for Drizzle.
hey ,
first of all thanks for this great lib ๐๐ป
i've encountered this issue when launching the script
FYI : tried with ts-node and tsx but without success.
{"level":50,"time":1717200292667,"pid":*****,"hostname":"********","msg":"๐ด Error generating DBML"}
Error generating DBML: SyntaxError: "undefined" is not valid JSON
at JSON.parse (<anonymous>)
at <anonymous> (******/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]__@neondatabase+_cm6mdk32q54q32ryyx46rir3wq/node_modules/src/pg-core/indexes.ts:122:27)
at Array.map (<anonymous>)
at IndexBuilderOn.on (******/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]__@neondatabase+_cm6mdk32q54q32ryyx46rir3wq/node_modules/src/pg-core/indexes.ts:116:12)
at PgTable.<anonymous> (******/src/lib/secure/db/schema/activities.ts:20:59)
at PgGenerator.generateTable (******/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]__@neo_diado5yhhvrhexhjdbgb7czlxa/node_modules/drizzle-dbml-generator/dist/index.cjs:178:67)
at PgGenerator.generate (******/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]__@neo_diado5yhhvrhexhjdbgb7czlxa/node_modules/drizzle-dbml-generator/dist/index.cjs:286:35)
at pgGenerate (******/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]__@neo_diado5yhhvrhexhjdbgb7czlxa/node_modules/drizzle-dbml-generator/dist/index.cjs:330:68)
at <anonymous> (******/src/lib/secure/db/dbml.ts:9:2)
at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
and here is my little script :
import { pgGenerate } from 'drizzle-dbml-generator';
import { logger } from '../logger';
import * as schema from './schema/';
logger.debug('๐ข Generating DBML');
console.log('Generating DBML...');
console.log(Object.keys(schema));
try{
pgGenerate({
schema,
out: './docs/schema.dbml',
relational: true
});
logger.info('๐ข DBML generated');
} catch (e) {
logger.error('๐ด Error generating DBML');
console.error('Error generating DBML:', e);
process.exit(1);
}
trying to generate the dbml using default configurations throws this error
SyntaxError: Cannot use import statement outside a module
relations between tables are missing when having a sqlite schema.
I hope this schema is correctly defined in drizzle
schema.ts
:
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
export const tokens = sqliteTable('tokens', {
pid: text('pid').primaryKey().notNull(),
assetName: text('asset_name').notNull(),
});
export const vaults = sqliteTable('vaults', {
name: text('name').primaryKey().notNull(),
description: text('description').notNull(),
});
export const vaultsTokens = sqliteTable('vaults_tokens', {
id: text('id').primaryKey().notNull(),
vaultName: integer('vault_name')
.notNull()
.references(() => vaults.name),
tokenPid: integer('token_pid')
.notNull()
.references(() => tokens.pid),
percentage: integer('percentage').notNull(),
});
dbml.ts
:
import {
generatedDrizzleSchemaPath,
generatedSchemaDiagramPath,
} from '@/constants/env';
import * as schema from './schema.ts';
import { sqliteGenerate } from 'drizzle-dbml-generator';
const out = generatedSchemaDiagramPath;
const relational = true;
sqliteGenerate({ schema, out, relational });
generated schema.dbml
:
table tokens {
pid text [pk, not null]
asset_name text [not null]
}
table vaults {
name text [pk, not null]
description text [not null]
}
table vaults_tokens {
id text [pk, not null]
vault_name integer [not null]
token_pid integer [not null]
percentage integer [not null]
}
I wanna emit dbml to just stdout, and redirect it to other place
Perhaps making it as default behavior is more sensible. And --file $path
as an option (which is useful some cases, but POSIX-style redirection is simpler)
If I run:
node --experimental-specifier-resolution=node --loader ts-node/esm ./db/generate-dbml.mts
with ES6
syntax, I get:
(node:452) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
If I run:
npx tsx ./db/generate-dbml.ts
with ES6
syntax, I get:
/Users/kevinschaich/repositories/arlo-quoting-tool/node_modules/drizzle-dbml-generator/dist/index.cjs:139
const dbml = new DBML().tab().escapeSpaces(column.name).insert(" ").escapeType(column.getSQLType());
TypeError: column.getSQLType is not a function
If I run:
node --experimental-specifier-resolution=node --loader ts-node/esm ./db/generate-dbml.mts
with node require
syntax, I get:
(node:99972) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
If I run:
npx tsx ./db/generate-dbml.ts
with node require
syntax, I get:
/Users/kevinschaich/repositories/arlo-quoting-tool/node_modules/drizzle-dbml-generator/dist/index.cjs:139
const dbml = new DBML().tab().escapeSpaces(column.name).insert(" ").escapeType(column.getSQLType());
TypeError: column.getSQLType is not a function
require
syntax code:
const schema = require('./schema')
const drizzleDbmlGenerator = require('drizzle-dbml-generator')
drizzleDbmlGenerator.pgGenerate({ schema, out: './db/schema.dbml', relational: true })
ES6
syntax code:
import * as schema from './schema'
import { pgGenerate } from 'drizzle-dbml-generator' // Using Postgres for this example
pgGenerate({ schema, out: './db/schema.dbml', relational: true })
#5 seems relevant for the TypeError: column.getSQLType is not a function
piece.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.