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

feat: Add new type generation #6

Draft
wants to merge 2 commits into
base: feat/refactor-project
Choose a base branch
from
Draft
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
376 changes: 222 additions & 154 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"license": "ISC",
"dependencies": {
"pg-schema-dump": "^2.0.1",
"prettier": "^3.5.1",
"ts-morph": "^25.0.1",
"yargs": "^17.7.2"
},
"peerDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getConnectionStringFromEnv } from "pg-connection-from-env"
import { Context } from "./get-project-context"
import { generateSchema } from "./lib/generate-schema"
import { generateStructure } from "./lib/generate-structure"
import { generateTypes } from "./lib/generate-types"
import { generateGetDbClient } from "./lib/generate-get-db-client"

export const generate = async (
Expand All @@ -21,6 +22,11 @@ export const generate = async (
schemas,
})

await generateTypes({
zapatos_dir: `${zapatosDir ?? dbDir}/zapatos`,
output_dir: `${dbDir}/types`,
})

await generateStructure({
database_name: defaultDatabase,
output_dir: dbDir,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { Config, Schema, Table } from "../../../types"
import { pascalCase } from "../../../utils"
import {
StructureKind,
type Project,
type SourceFile,
type StatementedNodeStructure,
} from "ts-morph"

export const createCustomTypesFile = (
args: {
project: Project
schema: Schema
table: Table
},
config: Config
): SourceFile => {
const { project, schema, table } = args
const { output_dir } = config

const pascal_table_name = pascalCase(table.name)

const statements: StatementedNodeStructure["statements"] = [
{
kind: StructureKind.ImportDeclaration,
namedImports: ["SubsetOf"],
moduleSpecifier: "../../generated/utils",
isTypeOnly: true,
},
{
kind: StructureKind.ImportDeclaration,
namedImports: [`Selectable${pascal_table_name}`],
moduleSpecifier: `../../generated/${schema.name}/${pascal_table_name}QueryTypes`,
isTypeOnly: true,
},
(writer) => writer.blankLine(),
{
kind: StructureKind.TypeAlias,
name: `${pascal_table_name}CustomTypes`,
type: `SubsetOf<Selectable${pascal_table_name}, {}>`,
isExported: true,
},
(writer) => writer.blankLine(),
{
kind: StructureKind.ExportAssignment,
isExportEquals: false,
expression: `${pascal_table_name}CustomTypes`,
},
]

return project.createSourceFile(
`${output_dir}/custom/${schema.name}/${pascal_table_name}CustomTypes.ts`,
{
statements,
},
{
overwrite: false,
}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type { Config, Schema, Table } from "../../../types"
import { pascalCase } from "../../../utils"
import {
StructureKind,
type Project,
type SourceFile,
type StatementedNodeStructure,
} from "ts-morph"

export const createResultingTypeFile = (
args: {
project: Project
schema: Schema
table: Table
},
config: Config
): SourceFile => {
const { project, schema, table } = args
const { output_dir } = config

const pascal_table_name = pascalCase(table.name)

const statements: StatementedNodeStructure["statements"] = [
{
kind: StructureKind.ImportDeclaration,
moduleSpecifier: `./${pascal_table_name}QueryTypes`,
namedImports: [
`Selectable${pascal_table_name}`,
`Insertable${pascal_table_name}`,
],
isTypeOnly: true,
},
]

if (table.is_affected_by_pgtui_bugs) {
statements.push({
kind: StructureKind.ImportDeclaration,
moduleSpecifier: "../utils",
namedImports: ["ReproducePgtuiBugs"],
isTypeOnly: true,
})
}

if (table.is_customizable) {
statements.push(
{
kind: StructureKind.ImportDeclaration,
moduleSpecifier: "../utils",
namedImports: ["CustomizeDbType", "CustomizeDbTypeInitializer"],
isTypeOnly: true,
},
{
kind: StructureKind.ImportDeclaration,
moduleSpecifier: `../../custom/${schema.name}/${pascal_table_name}CustomTypes`,
defaultImport: `${pascal_table_name}CustomTypes`,
isTypeOnly: true,
}
)
}

statements.push(
(writer) => writer.newLine(),
{
kind: StructureKind.TypeAlias,
isExported: true,
name: pascal_table_name,
type: table.is_customizable
? `CustomizeDbType<Selectable${pascal_table_name}, ${pascal_table_name}CustomTypes>`
: `Selectable${pascal_table_name}`,
},
{
kind: StructureKind.TypeAlias,
isExported: true,
name: `${pascal_table_name}Initializer`,
type: table.is_customizable
? `CustomizeDbTypeInitializer<Insertable${pascal_table_name}, ${pascal_table_name}CustomTypes>`
: `Insertable${pascal_table_name}`,
}
)

if (table.is_affected_by_pgtui_bugs) {
statements.push({
kind: StructureKind.TypeAlias,
isExported: true,
name: `${pascal_table_name}WithPgtuiBugs`,
type: `ReproducePgtuiBugs<${pascal_table_name}>`,
})
}

return project.createSourceFile(
`${output_dir}/generated/${schema.name}/${pascal_table_name}.ts`,
{ statements },
{ overwrite: true }
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import type { Config, Schema, Table } from "../../../types"
import { pascalCase } from "../../../utils"
import {
type SourceFile,
StructureKind,
type Project,
type StatementedNodeStructure,
} from "ts-morph"

export const createQueryTypesFile = (
args: {
project: Project
schema: Schema
table: Table
},
config: Config
): SourceFile => {
const { project, schema, table } = args
const { output_dir } = config

const base_type_name = pascalCase(table.name)

const statements: StatementedNodeStructure["statements"] = []

if (table.uses_zapatos_column_type) {
statements.push({
kind: StructureKind.ImportDeclaration,
moduleSpecifier: "zapatos/db",
namespaceImport: "db",
isTypeOnly: true,
})
}

statements.push(
(writer) => writer.newLine(),
{
kind: StructureKind.TypeAlias,
isExported: true,
name: `Selectable${base_type_name}`,
type: (writer) =>
writer.inlineBlock(() => {
for (const column of Object.values(table.selectable_columns)) {
writer.writeLine(
`${column.name}${column.is_optional ? "?" : ""}: ${column.type};`
)
}
}),
},
{
kind: StructureKind.TypeAlias,
isExported: true,
name: `Insertable${base_type_name}`,
type: (writer) =>
writer.inlineBlock(() => {
for (const column of Object.values(table.insertable_columns)) {
writer.writeLine(
`${column.name}${column.is_optional ? "?" : ""}: ${column.type};`
)
}
}),
}
)

return project.createSourceFile(
`${output_dir}/generated/${schema.name}/${base_type_name}QueryTypes.ts`,
{
statements,
},
{ overwrite: true }
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import type { Config, Schema } from "../../../types"
import { pascalCase } from "../../../utils"
import {
StructureKind,
type Project,
type SourceFile,
type StatementedNodeStructure,
} from "ts-morph"

export const createSchemaGeneratedTypeIndexFile = (
args: {
project: Project
schema: Schema
},
config: Config,
): SourceFile => {
const { project, schema } = args
const { output_dir } = config

const pascale_schema_name = pascalCase(schema.name)

const tables = Object.values(schema.tables)
const pascal_table_names = tables
.map((table) => table.name)
.reduce<Record<string, string>>((acc, table_name) => {
acc[table_name] = pascalCase(table_name)
return acc
}, {})

const statements: StatementedNodeStructure["statements"] = [
{
kind: StructureKind.ImportDeclaration,
moduleSpecifier: "../utils",
namedImports: ["KyselyTable"],
isTypeOnly: true,
},
]

for (const table of tables) {
statements.push({
kind: StructureKind.ImportDeclaration,
isTypeOnly: true,
moduleSpecifier: `./${pascal_table_names[table.name]}`,
namedImports: [
pascal_table_names[table.name],
...(table.is_affected_by_pgtui_bugs
? [`${pascal_table_names[table.name]}WithPgtuiBugs`]
: []),
`${pascal_table_names[table.name]}Initializer`,
],
})
}

statements.push(
(writer) => writer.newLine(),
{
kind: StructureKind.TypeAlias,
isExported: true,
name: `${pascale_schema_name}Tables`,
type: `[ ${tables
.map((table) => `"${schema.name}.${table.name}"`)
.join(", ")} ]`,
},
{
kind: StructureKind.TypeAlias,
isExported: true,
name: `${pascale_schema_name}Table`,
type: `${pascale_schema_name}Tables[number]`,
},
(writer) => writer.newLine(),
)

if (config.generate_knex_types) {
statements.push({
kind: StructureKind.Interface,
isExported: true,
name: "KnexSchemaTypeMap",
properties: tables.map((table) => ({
name:
schema.name === "seam"
? table.name
: `"${schema.name}.${table.name}"`,
type: table.is_affected_by_pgtui_bugs
? `${pascal_table_names[table.name]}WithPgtuiBugs`
: pascal_table_names[table.name],
})),
})
}

statements.push(
{
kind: StructureKind.TypeAlias,
isExported: true,
name: "KyselySchemaTypeMap",
type: `{ ${tables
.map(
(table) =>
`"${schema.name}.${table.name}": KyselyTable<${
pascal_table_names[table.name]
}, ${pascal_table_names[table.name]}Initializer>;`,
)
.join(" ")} }`,
},
(writer) => writer.newLine(),
{
kind: StructureKind.ExportDeclaration,
isTypeOnly: true,
namedExports: tables.flatMap((table) => [
pascal_table_names[table.name],
...(table.is_affected_by_pgtui_bugs
? [`${pascal_table_names[table.name]}WithPgtuiBugs`]
: []),
`${pascal_table_names[table.name]}Initializer`,
]),
},
)

return project.createSourceFile(
`${output_dir}/generated/${schema.name}/index.ts`,
{
statements,
},
{ overwrite: true },
)
}
Loading