Skip to content
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

9.0.1 #8

Merged
merged 3 commits into from
Apr 29, 2023
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eojuk",
"version": "0.9.0",
"version": "0.9.1",
"description": "",
"keywords": [
"mysql",
Expand Down
117 changes: 117 additions & 0 deletions src/output/dapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import Column from "../types/column";
import { IOption } from "../types/option";
import { IEmmiter } from "../interfaces/emitter";
import Source from "../types/source";
import Table from "../types/table";
import { convertNameCaseByOption } from "../util.ts/name";
import { TAB } from "../util.ts/tab";
import { escapeDoubleQuote } from "../util.ts/escape";

const importTemplate = `// If under EE 9, change jakarta to javax
import jakarta.annotation.*;
import jakarta.persistence.*;
import jakarta.persistence.Table;

import org.hibernate.annotations.*;
import java.time.LocalDateTime;
`;

export class DapperEmitter implements IEmmiter {
private option: IOption;

// 컬럼 필드 코드 생성
private generateColumn(column: Column) {
const columnFieldName = convertNameCaseByOption(
this.option.outputFieldNameCase,
column.name
);

const hasCreatedAt = column.name == this.option.autoAddCreatedAt;
const hasUpdatedAt = column.name == this.option.autoAddUpdatedAt;
// const hasDeletedAt = column.name == this.option.autoAddDeletedAt

// PrimaryKey 강제 추가 옵션
if (column.name == this.option.autoAddPrimaryKey) {
column.isPrimaryKey = true;
}

const createdAt = hasCreatedAt ? `\n${TAB}@CreationTimestamp` : "";
const updatedAt = hasUpdatedAt ? `\n${TAB}@UpdateTimestamp` : "";
const deletedAt = ""; // hibernate에는 해당 기능이 없음

const primaryKey = column.isPrimaryKey ? `\n${TAB}@Id` : "";

const autoIncrement = column.isAutoIncrement
? `\n${TAB}@GeneratedValue(strategy = GenerationType.IDENTITY)`
: "";

const defaultValue = column.default
? `\n${TAB}@ColumnDefault("${escapeDoubleQuote(column.default)}")`
: "";

const comment = column.comment
? `\n${TAB}@Comment("${escapeDoubleQuote(column.comment)}")`
: "";

//const dataType = this.dbTypeToDataType(column.dbType);

const columnAnnotation = `\n${TAB}@Column(name = "${escapeDoubleQuote(
column.name
)}")`;

const notNullAnnotaion = column.isNotNull
? `\n${TAB}@Nonnull`
: `\n${TAB}@Nullable`;

return `${primaryKey}${autoIncrement}${columnAnnotation}${notNullAnnotaion}${comment}${defaultValue}${createdAt}${updatedAt}${deletedAt}
${TAB}${column.javaType} ${columnFieldName};`;
}

// 테이블 클래스 코드 생성
private generateTableCode(table: Table) {
const hasDatabaseName = this.option.databaseName != null;

const tableClassName = convertNameCaseByOption(
this.option.outputClassNameCase,
table.tableName
);

const schema = hasDatabaseName
? `, schema = "\\"${this.option.databaseName}\\""`
: "";

return `@Entity()
@Table(name = "\\"${table.tableName}\\""${schema})
public class ${tableClassName} {
${table.columns.map((column) => this.generateColumn(column)).join("\n")}
}`;
}

emit(
tables: Table[],
option: IOption = {
sourceSplit: true,
outputClassNameCase: "PASCAL",
outputFieldNameCase: "CAMEL",
}
): Source[] {
this.option = option;

if (option?.sourceSplit) {
return tables.map((table) => ({
sourceName: table.tableName,
source: importTemplate + "\n" + this.generateTableCode(table),
}));
} else {
return [
{
sourceName: "all",
source:
importTemplate +
"\n" +
tables.map((table) => this.generateTableCode(table)).join("\n\n"),
},
];
}
}
}
3 changes: 1 addition & 2 deletions src/output/jpa-java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { convertNameCaseByOption } from "../util.ts/name";
import { TAB } from "../util.ts/tab";
import { escapeDoubleQuote } from "../util.ts/escape";

const importTemplate = `
// If under EE 9, change jakarta to javax
const importTemplate = `// If under EE 9, change jakarta to javax
import jakarta.annotation.*;
import jakarta.persistence.*;
import jakarta.persistence.Table;
Expand Down
3 changes: 1 addition & 2 deletions src/output/jpa-kotlin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { convertNameCaseByOption } from "../util.ts/name";
import { TAB } from "../util.ts/tab";
import { escapeDoubleQuote } from "../util.ts/escape";

const importTemplate = `
// If under EE 9, change jakarta to javax
const importTemplate = `// If under EE 9, change jakarta to javax
import jakarta.annotation.*
import jakarta.persistence.*
import jakarta.persistence.Table
Expand Down
198 changes: 100 additions & 98 deletions src/output/sequelize-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import { IEmmiter } from "../interfaces/emitter";
import Source from "../types/source";
import Table from "../types/table";
import {
convertNameCaseByOption,
toCamelCase,
toSnakeCase,
convertNameCaseByOption,
toCamelCase,
toSnakeCase,
} from "../util.ts/name";
import { TAB } from "../util.ts/tab";

const importTemplate = `
import { literal } from 'sequelize';
const importTemplate = `import { literal } from 'sequelize';
import {
Model,
Table,
Expand All @@ -37,127 +36,130 @@ import {
`;

export class SequelizeTypescriptEmitter implements IEmmiter {
private option: IOption;

private dbTypeToDataType(dbtype: string): string {
if (["varchar", "text", "char"].includes(dbtype.toLowerCase())) {
return "DataType.STRING";
} else if (["bool", "boolean"].includes(dbtype.toLowerCase())) {
return "DataType.BOOLEAN";
} else if (["uuid"].includes(dbtype.toLowerCase())) {
return "DataType.UUID";
} else if (
["int", "int2", "int4", "int8", "bigint"].includes(
dbtype.toLowerCase()
)
) {
return "DataType.INTEGER";
} else {
return `'${dbtype}'`;
}
private option: IOption;

private dbTypeToDataType(dbtype: string): string {
if (["varchar", "text", "char"].includes(dbtype.toLowerCase())) {
return "DataType.STRING";
} else if (["bool", "boolean"].includes(dbtype.toLowerCase())) {
return "DataType.BOOLEAN";
} else if (["uuid"].includes(dbtype.toLowerCase())) {
return "DataType.UUID";
} else if (
["int", "int2", "int4", "int8", "bigint"].includes(dbtype.toLowerCase())
) {
return "DataType.INTEGER";
} else {
return `'${dbtype}'`;
}
}

// 컬럼 필드 코드 생성
private generateColumn(column: Column) {
const columnFieldName = convertNameCaseByOption(
this.option.outputFieldNameCase,
column.name
);

const hasCreatedAt = column.name == this.option.autoAddCreatedAt;
const hasUpdatedAt = column.name == this.option.autoAddUpdatedAt;
const hasDeletedAt = column.name == this.option.autoAddDeletedAt;

// PrimaryKey 강제 추가 옵션
if (column.name == this.option.autoAddPrimaryKey) {
column.isPrimaryKey = true;
}

// 컬럼 필드 코드 생성
private generateColumn(column: Column) {
const columnFieldName = convertNameCaseByOption(
this.option.outputFieldNameCase,
column.name
);

const hasCreatedAt = column.name == this.option.autoAddCreatedAt
const hasUpdatedAt = column.name == this.option.autoAddUpdatedAt
const hasDeletedAt = column.name == this.option.autoAddDeletedAt

// PrimaryKey 강제 추가 옵션
if(column.name == this.option.autoAddPrimaryKey) {
column.isPrimaryKey = true;
}

const createdAt = hasCreatedAt ? `\n${TAB}@CreatedAt` : "";
const updatedAt = hasUpdatedAt ? `\n${TAB}@UpdatedAt` : "";
const deletedAt = hasDeletedAt ? `\n${TAB}@DeletedAt` : "";
const createdAt = hasCreatedAt ? `\n${TAB}@CreatedAt` : "";
const updatedAt = hasUpdatedAt ? `\n${TAB}@UpdatedAt` : "";
const deletedAt = hasDeletedAt ? `\n${TAB}@DeletedAt` : "";

const primaryKey = column.isPrimaryKey
? `primaryKey: true, \n${TAB}${TAB}`
: "";
const primaryKey = column.isPrimaryKey
? `primaryKey: true, \n${TAB}${TAB}`
: "";

const autoIncrement = column.isAutoIncrement
? `autoIncrement: true, \n${TAB}${TAB}`
: "";
const autoIncrement = column.isAutoIncrement
? `autoIncrement: true, \n${TAB}${TAB}`
: "";

const defaultValue = column.default
? `\n${TAB}${TAB}default: literal("${column.default.replace(
'"',
'\\"'
)}"),`
: "";
const defaultValue = column.default
? `\n${TAB}${TAB}default: literal("${column.default.replace(
'"',
'\\"'
)}"),`
: "";

const dataType = this.dbTypeToDataType(column.dbType);
const dataType = this.dbTypeToDataType(column.dbType);

return ` @Comment(\`${column.comment ?? ""}\`)${createdAt}${updatedAt}${deletedAt}
return ` @Comment(\`${
column.comment ?? ""
}\`)${createdAt}${updatedAt}${deletedAt}
@Column({
${primaryKey}${autoIncrement}field: '${column.name}',
type: ${dataType},
allowNull: ${!column.isNotNull},${defaultValue}
})
${columnFieldName}: ${column.tsType};`;
}
}

// 테이블 클래스 코드 생성
private generateTableCode(table: Table) {
const hasCreatedAt = table.columns.find(e=>e.name == this.option.autoAddCreatedAt) != null;
const hasUpdatedAt = table.columns.find(e=>e.name == this.option.autoAddUpdatedAt) != null;
const hasDeletedAt = table.columns.find(e=>e.name == this.option.autoAddDeletedAt) != null;
// 테이블 클래스 코드 생성
private generateTableCode(table: Table) {
const hasCreatedAt =
table.columns.find((e) => e.name == this.option.autoAddCreatedAt) != null;
const hasUpdatedAt =
table.columns.find((e) => e.name == this.option.autoAddUpdatedAt) != null;
const hasDeletedAt =
table.columns.find((e) => e.name == this.option.autoAddDeletedAt) != null;

const hasDatabaseName = this.option.databaseName != null
const hasDatabaseName = this.option.databaseName != null;

const tableClassName = convertNameCaseByOption(
this.option.outputClassNameCase,
table.tableName
);
const tableClassName = convertNameCaseByOption(
this.option.outputClassNameCase,
table.tableName
);

return `@Table({
return `@Table({
tableName: '${table.tableName}',
paranoid: ${hasDeletedAt},
freezeTableName: true,
timestamps: ${hasCreatedAt || hasCreatedAt || hasDeletedAt},
createdAt: ${hasCreatedAt},
updatedAt: ${hasUpdatedAt},
deletedAt: ${hasDeletedAt},
${hasDatabaseName ? '' : '// '}schema: '${this.option.databaseName ?? 'public'}',
${hasDatabaseName ? "" : "// "}schema: '${
this.option.databaseName ?? "public"
}',
})
export class ${tableClassName} extends Model {
${table.columns.map((column) => this.generateColumn(column)).join("\n\n")}
}`;
}

emit(
tables: Table[],
option: IOption = {
sourceSplit: true,
outputClassNameCase: "PASCAL",
outputFieldNameCase: "CAMEL",
}

emit(
tables: Table[],
option: IOption = {
sourceSplit: true,
outputClassNameCase: "PASCAL",
outputFieldNameCase: "CAMEL",
}
): Source[] {
this.option = option;

if (option?.sourceSplit) {
return tables.map((table) => ({
sourceName: table.tableName,
source: importTemplate + "\n" + this.generateTableCode(table),
}));
} else {
return [
{
sourceName: "all",
source:
importTemplate +
"\n" +
tables
.map((table) => this.generateTableCode(table))
.join("\n\n"),
},
];
}
): Source[] {
this.option = option;

if (option?.sourceSplit) {
return tables.map((table) => ({
sourceName: table.tableName,
source: importTemplate + "\n" + this.generateTableCode(table),
}));
} else {
return [
{
sourceName: "all",
source:
importTemplate +
"\n" +
tables.map((table) => this.generateTableCode(table)).join("\n\n"),
},
];
}
}
}
3 changes: 1 addition & 2 deletions src/output/sqlalchemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { convertNameCaseByOption } from "../util.ts/name";
import { TAB } from "../util.ts/tab";
import { escapeDoubleQuote } from "../util.ts/escape";

const importTemplate = `
from sqlalchemy import Column
const importTemplate = `from sqlalchemy import Column
from sqlalchemy import String, Integer, Double, Float, BigInteger, DateTime, Boolean
from sqlalchemy.orm import declarative_base

Expand Down
Loading