Code Monkey home page Code Monkey logo

drizzle-dbml-generator's Introduction

Hi, I'm Mario564, a full-stack Javascript and Typescript web developer who likes to code both as a hobby and as a profession. I mainly work with modern techonologies such as Next.js, SvelteKit, tRPC, Prisma, Drizzle ORM, Tailwind CSS, and few other tools. I also make some spreadsheets with Google Sheets.

Mario564's GitHub stats

drizzle-dbml-generator's People

Contributors

cometkim avatar kevinschaich avatar l-mario564 avatar mendylanda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

drizzle-dbml-generator's Issues

Support stdout

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)

Can't get generate function to work with any combination of syntax/tools

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.

DBML to Drizzle ORM

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.

relations missing in sqlite

relations between tables are missing when having a sqlite schema.

I hope this schema is correctly defined in drizzle

Steps to reproduce:

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]
}

getting weird module issue

trying to generate the dbml using default configurations throws this error

SyntaxError: Cannot use import statement outside a module

TypeError: column.getSQLType is not a function

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();
  }

Does it work with multiple schema files?

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.