diff --git a/package.json b/package.json index 6638c37..e4e3772 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "mysql-to-zod": "tsx ./src/main.ts", "dev": "tsx ./src/main.ts", "build": "zx ./build.mjs && zx ./addShebang.mjs", - "debugBuild": "esbuild ./src/main.ts --bundle --sourcemap --platform=node --outfile=./dist/main.js", + "debugBuild": "esbuild ./src/main.ts --bundle --sourcemap --platform=node --outfile=./dist/main.js --external:prettier", "test": "vitest", "type": "tsc --noEmit", "lint": "pnpm biome check --apply ./src", @@ -68,4 +68,4 @@ "node": "18.17.0", "npm": "9.8.1" } -} +} \ No newline at end of file diff --git a/src/features/sync/utils/syncUtil.spec.ts b/src/features/sync/utils/syncUtil.spec.ts index 90f045d..3c92b44 100644 --- a/src/features/sync/utils/syncUtil.spec.ts +++ b/src/features/sync/utils/syncUtil.spec.ts @@ -20,7 +20,7 @@ describe("schemaInformationToText", () => { }); describe("mergeSchemaTextWithOldInformation 完成したschemaTextと以前のschemaInformationを合体する関数", () => { - it("case1", () => { + it("case1", async () => { const schemaName = "aaaSchema"; const schemaInformation: SchemaInformation = { tableName: "aaaSchema", @@ -32,7 +32,7 @@ describe("mergeSchemaTextWithOldInformation 完成したschemaTextと以前のsc const result = `export const aaaSchema = z.object({ DB_ID: z.number().optional(), });`; - const ex = mergeSchemaTextWithOldInformation({ + const ex = await mergeSchemaTextWithOldInformation({ schemaInformation, schemaName, schemaText, @@ -89,7 +89,7 @@ describe("getSchemaInformation", () => { }); describe("formatByPrettier", () => { - it("case1", () => { + it("case1", async () => { const str = "export const myTodoListSchema = z.object({ id: z.number(), status: z.string(), task: z.string(), description: z.string().nullish(), due_date: z.date().nullish(), created_at: z.date().nullish(), updated_at: z.date().nullish(),});"; const result = `export const myTodoListSchema = z.object({ @@ -102,6 +102,6 @@ describe("formatByPrettier", () => { updated_at: z.date().nullish(), }); `; - expect(formatByPrettier(str)).toBe(result); + expect(await formatByPrettier(str)).toBe(result); }); }); diff --git a/src/process/buildSchemaText/buildSchemaText.ts b/src/process/buildSchemaText/buildSchemaText.ts index f0aa0ce..f195af9 100644 --- a/src/process/buildSchemaText/buildSchemaText.ts +++ b/src/process/buildSchemaText/buildSchemaText.ts @@ -1,4 +1,4 @@ -import { A, O, R, pipe } from "@mobily/ts-belt"; +import { R } from "@mobily/ts-belt"; import { produce } from "immer"; import type { SchemaInformation } from "../../features/sync/types/syncType"; import type { MysqlToZodOption } from "../../options/options"; @@ -29,46 +29,29 @@ export const buildSchemaText = async ({ draft.push("import { globalSchema } from './globalSchema';"); }).join("\n"); - const loop = async ( - restTables: readonly string[], - result: SchemaResult, - ): Promise> => { - if (A.isEmpty(restTables)) return R.Ok(result); - /* isEmptyで調べているのでgetExnを使用する */ - const headTable = pipe(restTables, A.head, O.getExn); - const tailTables = pipe(restTables, A.tail, O.getExn); + const result: SchemaResult = { + schema: "", + columns: [], + }; - const tableDefinition = await getTableDefinition( - headTable, + for (const table of tables) { + const definition = await getTableDefinition( + table, // biome-ignore lint/suspicious/noExplicitAny: option.dbConnection as any, ); - const schemaTextEither = createSchemaFile( - tableDefinition, + const schema = await createSchemaFile( + definition, option, schemaInformationList, ); + if (R.isOk(schema)) { + result.schema += R.getExn(schema).schema; + result.columns.push(...R.getExn(schema).columns); + } + } - return R.match( - schemaTextEither, - (ok) => { - const nextResult = produce(result, (draft) => { - draft.schema += ok.schema; - draft.columns.push(...ok.columns); - }); - return loop(tailTables, nextResult); - }, - async (err) => { - return await R.Error(err); - }, - ); - }; - const schemaTexts = await loop(tables, { - schema: "", - columns: [], - }); - - return R.flatMap(schemaTexts, (x) => { + return R.flatMap(R.Ok(result), (x) => { const text = strListToStrLf([importDeclaration, x.schema]); return R.Ok({ text, columns: x.columns, option }); }); diff --git a/src/process/buildSchemaText/utils/createSchema.spec.ts b/src/process/buildSchemaText/utils/createSchema.spec.ts index e8f44d3..123bd77 100644 --- a/src/process/buildSchemaText/utils/createSchema.spec.ts +++ b/src/process/buildSchemaText/utils/createSchema.spec.ts @@ -3,7 +3,7 @@ import type { Column } from "../types/buildSchemaTextType"; import { createSchema } from "./createSchema"; describe("createSchema", () => { - it("case 1 separate = false", () => { + it("case 1 separate = false", async () => { const tableName = "todo"; const columns: Column[] = [ { @@ -32,7 +32,7 @@ export type Todo = z.infer;`; }; expect( - createSchema({ + await createSchema({ tableName, tableComment: undefined, columns, @@ -43,7 +43,7 @@ export type Todo = z.infer;`; ).toStrictEqual(result); }); - it("case2 separate = true", () => { + it("case2 separate = true", async () => { const tableName = "todo"; const columns: Column[] = [ { @@ -75,7 +75,7 @@ export type InsertTodo = z.infer;`; }; expect( - createSchema({ + await createSchema({ tableName, tableComment: undefined, columns, @@ -86,7 +86,7 @@ export type InsertTodo = z.infer;`; ).toStrictEqual(result); }); - it("case3 separate = true, suffix", () => { + it("case3 separate = true, suffix", async () => { const tableName = "todo"; const columns: Column[] = [ { @@ -119,7 +119,7 @@ export type TodoInsert = z.infer;`; }; expect( - createSchema({ + await createSchema({ tableName, tableComment: undefined, columns, diff --git a/src/process/buildSchemaText/utils/createSchema.ts b/src/process/buildSchemaText/utils/createSchema.ts index a98dbaf..ccb4b29 100644 --- a/src/process/buildSchemaText/utils/createSchema.ts +++ b/src/process/buildSchemaText/utils/createSchema.ts @@ -22,16 +22,15 @@ type UpdateSchemaTextProps = { schemaInformation: SchemaInformation; }; -export const mergeSchemaTextWithOldInformation = ({ +export const mergeSchemaTextWithOldInformation = async ({ schemaName, schemaInformation, schemaText, }: UpdateSchemaTextProps) => { /* 完成したテキストからschemaInformationをつくる */ - + const formatted = await formatByPrettier(schemaText); const nextSchemaInformation = pipe( - schemaText, - formatByPrettier, + formatted, parse, (x) => x[0] as SchemaInformation, (x) => { @@ -66,7 +65,7 @@ export const mergeSchemaTextWithOldInformation = ({ const rawNextSchemaText = schemaInformationToText( replacedSchemaInformation, ).join(""); - const formattedSchemaText = formatByPrettier(rawNextSchemaText); + const formattedSchemaText = await formatByPrettier(rawNextSchemaText); return formattedSchemaText.trim(); }; @@ -78,14 +77,14 @@ type CreateSchemaProps = { schemaInformationList: readonly SchemaInformation[]; mode: CreateSchemaModeUnion; }; -export const createSchema = ({ +export const createSchema = async ({ tableName, columns, options, tableComment, schemaInformationList, mode, -}: CreateSchemaProps): SchemaResult => { +}: CreateSchemaProps): Promise => { const schemaString = columns .map((x) => composeColumnStringList({ column: x, option: options, mode }).join("\n"), @@ -114,7 +113,7 @@ export const createSchema = ({ const merged = G.isNullable(thisSchemaInformation) ? schemaText - : mergeSchemaTextWithOldInformation({ + : await mergeSchemaTextWithOldInformation({ schemaName, schemaText, schemaInformation: { @@ -146,16 +145,14 @@ export const createSchema = ({ /* isSeparateのとき、関数をinsert modeで再実行する */ const separeteInsertSchema = separateSchema.isSeparate && mode === "select" - ? `\n${ - createSchema({ - tableName, - columns, - options, - tableComment, - schemaInformationList, - mode: "insert", - }).schema - }` + ? `\n${await createSchema({ + tableName, + columns, + options, + tableComment, + schemaInformationList, + mode: "insert", + }).then((x) => x.schema)}` : ""; return { diff --git a/src/process/buildSchemaText/utils/createSchemaFile.ts b/src/process/buildSchemaText/utils/createSchemaFile.ts index 5a8c274..94bf765 100644 --- a/src/process/buildSchemaText/utils/createSchemaFile.ts +++ b/src/process/buildSchemaText/utils/createSchemaFile.ts @@ -28,11 +28,11 @@ export const convertToColumn = (ast: any) => { export const isCreate = (ast: AST): ast is Create => "create_definitions" in ast; -export const createSchemaFile = ( +export const createSchemaFile = async ( tableDefinition: string[], // 0がテーブルネーム、1がテーブル定義 options: MysqlToZodOption, schemaInformationList: readonly SchemaInformation[], -): R.Result => { +): Promise> => { const parser = new Parser(); const [tableName, tableDefinitionString] = tableDefinition; if (G.isNullable(tableName) || G.isNullable(tableDefinitionString)) @@ -56,7 +56,7 @@ export const createSchemaFile = ( optionCommentsTable: options?.comments?.table, tableName, }); - const { schema } = createSchema({ + const { schema } = await createSchema({ tableName, columns, options, diff --git a/src/process/formatByPrettier.ts b/src/process/formatByPrettier.ts index bf7d6e3..b76f1d5 100644 --- a/src/process/formatByPrettier.ts +++ b/src/process/formatByPrettier.ts @@ -1,6 +1,6 @@ -import prettier from "@prettier/sync"; +import prettier from "prettier"; -export const formatByPrettier = (str: string): string => +export const formatByPrettier = async (str: string): Promise => prettier.format(str, { parser: "babel-ts", }); diff --git a/src/process/outputToFile.ts b/src/process/outputToFile.ts index 88c5c9a..2cd0bea 100644 --- a/src/process/outputToFile.ts +++ b/src/process/outputToFile.ts @@ -15,7 +15,7 @@ export const outputToFile = async ({ output, globalSchema, }: OutputParams) => { - const formatted = formatByPrettier(schemaRawText); + const formatted = await formatByPrettier(schemaRawText); const { fileName, outDir } = pipe( output, @@ -33,7 +33,7 @@ export const outputToFile = async ({ /* globalSchema */ if (G.isNullable(globalSchema)) return; - const globalSchemaFormatted = formatByPrettier(globalSchema); + const globalSchemaFormatted = await formatByPrettier(globalSchema); const globalSchemaSavePath = join(process.cwd(), outDir, "globalSchema.ts"); writeFileSync(globalSchemaSavePath, globalSchemaFormatted); console.info("\nglobalSchema file created!"); diff --git a/test/config/rawTest2Config.js b/test/config/rawTest2Config.js index 98f3472..cd5a07b 100644 --- a/test/config/rawTest2Config.js +++ b/test/config/rawTest2Config.js @@ -1,11 +1,13 @@ const options = { - dbConnection: { - database: "test", - host: "localhost", - password: "test", - port: 33202, - user: "test", + "dbConnection": { + "database": "test", + "host": "localhost", + "password": "test", + "port": 32789, + "user": "test" }, - tableNames: ["posts"], + "tableNames": [ + "posts" + ] }; -module.exports = options; + module.exports = options; \ No newline at end of file