Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Mario564 committed Dec 10, 2024
1 parent 98e7948 commit 53d612a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 22 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-dbml-generator",
"version": "0.9.0",
"version": "0.10.0",
"description": "Convert your Drizzle ORM schema into DBML markup",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand Down Expand Up @@ -30,7 +30,7 @@
"@types/node": "^20.5.6",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"drizzle-orm": "^0.32.0",
"drizzle-orm": "^0.38.0",
"eslint": "^8.47.0",
"eslint-config-prettier": "^9.0.0",
"prettier": "^3.0.2",
Expand All @@ -39,6 +39,6 @@
"vitest": "^0.34.3"
},
"peerDependencies": {
"drizzle-orm": ">=0.32.0"
"drizzle-orm": ">=0.36.0"
}
}
23 changes: 13 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/dbml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class DBML {
* Escapes characters that aren't allowed in DBML surrounding the input with double quotes
*/
public escapeSpaces(str: string) {
this.built += /\W/.test(str) ? `"${str}"` : str;
this.built += /\W/.test(str) || /^[0-9_]/.test(str) ? `"${str}"` : str;
return this;
}

Expand Down
21 changes: 16 additions & 5 deletions src/generators/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@ import {
PrimaryKey as PgPrimaryKey,
UniqueConstraint as PgUniqueConstraint,
isPgEnum,
PgTable
PgTable,
Check as PgCheck
} from 'drizzle-orm/pg-core';
import {
ForeignKey as MySqlForeignKey,
Index as MySqlIndex,
PrimaryKey as MySqlPrimaryKey,
MySqlTable,
UniqueConstraint as MySqlUniqueConstraint
UniqueConstraint as MySqlUniqueConstraint,
Check as MySqlCheck
} from 'drizzle-orm/mysql-core';
import {
ForeignKey as SQLiteForeignKey,
Index as SQLiteIndex,
PrimaryKey as SQLitePrimaryKey,
SQLiteTable,
UniqueConstraint as SQLiteUniqueConstraint
UniqueConstraint as SQLiteUniqueConstraint,
Check as SQLiteCheck
} from 'drizzle-orm/sqlite-core';
import { CasingCache } from 'drizzle-orm/casing';
import { writeFileSync } from 'fs';
import { resolve } from 'path';
import type {
Expand All @@ -66,7 +70,8 @@ export abstract class BaseGenerator<
protected buildQueryConfig: BuildQueryConfig = {
escapeName: () => '',
escapeParam: () => '',
escapeString: () => ''
escapeString: () => '',
casing: new CasingCache()
};

constructor(schema: Schema, relational: boolean) {
Expand Down Expand Up @@ -156,7 +161,13 @@ export abstract class BaseGenerator<
const extraConfigColumns = table[ExtraConfigColumns];
const extraConfig = extraConfigBuilder?.(extraConfigColumns ?? {});

const builtIndexes = Object.values(extraConfig ?? {}).map((b: AnyBuilder) => b.build(table));
const builtIndexes = (
Array.isArray(extraConfig) ? extraConfig : Object.values(extraConfig ?? {})
)
.map((b: AnyBuilder) => b?.build(table))
.filter((b) => b !== undefined)
// The DBML markup language doesn't support check constraints
.filter((index) => !(is(index, PgCheck) || is(index, MySqlCheck) || is(index, SQLiteCheck)));
const fks = builtIndexes.filter(
(index) =>
is(index, PgForeignKey) || is(index, MySqlForeignKey) || is(index, SQLiteForeignKey)
Expand Down
4 changes: 3 additions & 1 deletion src/generators/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BaseGenerator, writeDBMLFile } from './common';
import { is } from 'drizzle-orm';
import { MySqlColumnWithAutoIncrement } from 'drizzle-orm/mysql-core';
import { MySqlInlineForeignKeys } from '~/symbols';
import { CasingCache } from 'drizzle-orm/casing';
import type { BuildQueryConfig } from 'drizzle-orm';
import type { AnyMySqlColumn } from 'drizzle-orm/mysql-core';
import type { MySqlSchema, Options } from '~/types';
Expand All @@ -11,7 +12,8 @@ class MySqlGenerator extends BaseGenerator<MySqlSchema, AnyMySqlColumn> {
protected override buildQueryConfig: BuildQueryConfig = {
escapeName: (name) => `\`${name}\``,
escapeParam: (_num) => '?',
escapeString: (str) => `'${str.replace(/'/g, "''")}'`
escapeString: (str) => `'${str.replace(/'/g, "''")}'`,
casing: new CasingCache()
};

protected override isIncremental(column: AnyMySqlColumn) {
Expand Down
4 changes: 3 additions & 1 deletion src/generators/pg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DBML } from '~/dbml';
import { BaseGenerator, writeDBMLFile } from './common';
import { PgInlineForeignKeys } from '~/symbols';
import { CasingCache } from 'drizzle-orm/casing';
import type { BuildQueryConfig } from 'drizzle-orm';
import type { AnyPgColumn, PgEnum } from 'drizzle-orm/pg-core';
import type { PgSchema, Options } from '~/types';
Expand All @@ -10,7 +11,8 @@ class PgGenerator extends BaseGenerator<PgSchema, AnyPgColumn> {
protected override buildQueryConfig: BuildQueryConfig = {
escapeName: (name) => `"${name}"`,
escapeParam: (num) => `$${num + 1}`,
escapeString: (str) => `'${str.replace(/'/g, "''")}'`
escapeString: (str) => `'${str.replace(/'/g, "''")}'`,
casing: new CasingCache()
};

protected override isIncremental(column: AnyPgColumn) {
Expand Down
4 changes: 3 additions & 1 deletion src/generators/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BaseGenerator, writeDBMLFile } from './common';
import { SQLiteInlineForeignKeys } from '~/symbols';
import { is, SQL } from 'drizzle-orm';
import { SQLiteBaseInteger } from 'drizzle-orm/sqlite-core';
import { CasingCache } from 'drizzle-orm/casing';
import type { BuildQueryConfig } from 'drizzle-orm';
import type { AnySQLiteColumn } from 'drizzle-orm/sqlite-core';
import type { SQLiteSchema, Options } from '~/types';
Expand All @@ -11,7 +12,8 @@ class SQLiteGenerator extends BaseGenerator<SQLiteSchema, AnySQLiteColumn> {
protected override buildQueryConfig: BuildQueryConfig = {
escapeName: (name) => `"${name}"`,
escapeParam: (_num) => '?',
escapeString: (str) => `'${str.replace(/'/g, "''")}'`
escapeString: (str) => `'${str.replace(/'/g, "''")}'`,
casing: new CasingCache()
};

protected override isIncremental(column: AnySQLiteColumn) {
Expand Down

0 comments on commit 53d612a

Please sign in to comment.