Skip to content

Commit

Permalink
feat(codegen): add wrapper types
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Oct 26, 2023
1 parent 516575b commit f8e712f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-planets-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kysely-migrate": patch
---

Added wrapper types to codegen
79 changes: 18 additions & 61 deletions src/utils/codegen/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,25 @@ export const kyselyGeneratedImportSpecifier = factory.createImportSpecifier(
kyselyGeneratedIdentifier,
)

const columnIdentifier = factory.createIdentifier('c')
const selectIdentifier = factory.createIdentifier('s')
const insertIdentifier = factory.createIdentifier('i')
const updateIdentifier = factory.createIdentifier('i')
export const kyselyInsertableIdentifier = factory.createIdentifier('Insertable')
export const kyselyInsertableImportSpecifier = factory.createImportSpecifier(
true,
undefined,
kyselyInsertableIdentifier,
)

export const generatedIdentifier = factory.createIdentifier('Generated')
export const generatedTypeAlias = factory.createTypeAliasDeclaration(
[factory.createToken(SyntaxKind.ExportKeyword)],
generatedIdentifier,
[
factory.createTypeParameterDeclaration(
undefined,
columnIdentifier,
undefined,
undefined,
),
],
factory.createConditionalTypeNode(
factory.createTypeReferenceNode(columnIdentifier, undefined),
factory.createTypeReferenceNode(kyselyColumnTypeIdentifier, [
factory.createInferTypeNode(
factory.createTypeParameterDeclaration(
undefined,
selectIdentifier,
undefined,
undefined,
),
),
factory.createInferTypeNode(
factory.createTypeParameterDeclaration(
undefined,
insertIdentifier,
undefined,
undefined,
),
),
factory.createInferTypeNode(
factory.createTypeParameterDeclaration(
undefined,
updateIdentifier,
undefined,
undefined,
),
),
]),
factory.createTypeReferenceNode(kyselyColumnTypeIdentifier, [
factory.createTypeReferenceNode(selectIdentifier, undefined),
factory.createUnionTypeNode([
factory.createTypeReferenceNode(insertIdentifier, undefined),
factory.createKeywordTypeNode(SyntaxKind.UndefinedKeyword),
]),
factory.createTypeReferenceNode(updateIdentifier, undefined),
]),
factory.createTypeReferenceNode(kyselyColumnTypeIdentifier, [
factory.createTypeReferenceNode(columnIdentifier, undefined),
factory.createUnionTypeNode([
factory.createTypeReferenceNode(columnIdentifier, undefined),
factory.createKeywordTypeNode(SyntaxKind.UndefinedKeyword),
]),
factory.createTypeReferenceNode(columnIdentifier, undefined),
]),
),
export const kyselySelectableIdentifier = factory.createIdentifier('Selectable')
export const kyselySelectableImportSpecifier = factory.createImportSpecifier(
true,
undefined,
kyselySelectableIdentifier,
)

export const kyselyUpdateableIdentifier = factory.createIdentifier('Updateable')
export const kyselyUpdateableImportSpecifier = factory.createImportSpecifier(
true,
undefined,
kyselyUpdateableIdentifier,
)

export const jsonIdentifier = factory.createIdentifier('Json')
Expand Down
45 changes: 45 additions & 0 deletions src/utils/codegen/getTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { capitalCase } from 'change-case'
import { type TableMetadata } from 'kysely'
import {
EmitHint,
type Identifier,
type ImportSpecifier,
NewLineKind,
ScriptTarget,
Expand All @@ -16,6 +17,12 @@ import {
import {
kyselyGeneratedIdentifier,
kyselyGeneratedImportSpecifier,
kyselyInsertableIdentifier,
kyselyInsertableImportSpecifier,
kyselySelectableIdentifier,
kyselySelectableImportSpecifier,
kyselyUpdateableIdentifier,
kyselyUpdateableImportSpecifier,
} from './declarations.js'
import { mysqlDefinitions } from './definitions/mysql.js'
import { postgresDefinitions } from './definitions/postgres.js'
Expand Down Expand Up @@ -123,6 +130,44 @@ export function getTypes(
)
nodes.push(tableTypeAlias)

// Create `Selectable`, `Insertable` and `Updateable` wrappers for table type
if (importsMap.has('kysely')) {
const kyselyImports = importsMap.get('kysely')!
kyselyImports.add(kyselySelectableImportSpecifier)
kyselyImports.add(kyselyInsertableImportSpecifier)
kyselyImports.add(kyselyUpdateableImportSpecifier)
importsMap.set('kysely', kyselyImports)
} else
importsMap.set(
'kysely',
new Set([
kyselySelectableImportSpecifier,
kyselyInsertableImportSpecifier,
kyselyUpdateableImportSpecifier,
]),
)

function createWrapperTypeAlias(
type: 'insertable' | 'selectable' | 'updateable',
) {
let identifier: Identifier
if (type === 'insertable') identifier = kyselyInsertableIdentifier
else if (type === 'selectable') identifier = kyselySelectableIdentifier
else identifier = kyselyUpdateableIdentifier
return factory.createTypeAliasDeclaration(
[factory.createModifier(SyntaxKind.ExportKeyword)],
factory.createIdentifier(`${tableTypeName}${capitalCase(type)}`),
undefined,
factory.createTypeReferenceNode(identifier, [
factory.createTypeReferenceNode(tableTypeIdentifier, undefined),
]),
)
}
const insertableTypeAlias = createWrapperTypeAlias('insertable')
const selectableTypeAlias = createWrapperTypeAlias('selectable')
const updateableTypeAlias = createWrapperTypeAlias('updateable')
nodes.push(selectableTypeAlias, insertableTypeAlias, updateableTypeAlias)

// Create table type property for encompassing `DB` type
const tableDbTypeParameter = factory.createPropertySignature(
undefined,
Expand Down

0 comments on commit f8e712f

Please sign in to comment.