Skip to content

Commit

Permalink
Add support for both GENERATED ALWAYS and GENERATED BY DEFAULT in kan…
Browse files Browse the repository at this point in the history
…el and kanel-kysely (#452)
  • Loading branch information
mjwwit authored Aug 19, 2023
1 parent 5a3a619 commit 1409a20
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
9 changes: 6 additions & 3 deletions packages/kanel-kysely/src/processFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,21 @@ const processFile = (
}

let initializerType = 'never';
if (canInitialize) {
if (canInitialize && column.generated !== 'ALWAYS') {
if (baseType === 'Date') {
baseType += ' | string';
}
initializerType =
column.isNullable || column.defaultValue || column.isIdentity
column.isNullable ||
column.defaultValue ||
column.isIdentity ||
column.generated === 'BY DEFAULT'
? `${baseType} | null`
: baseType;
}

let mutatorType = 'never';
if (canMutate) {
if (canMutate && column.generated !== 'ALWAYS') {
mutatorType = `${baseType} | null`;
}

Expand Down
13 changes: 8 additions & 5 deletions packages/kanel/src/generators/generateProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ const generateProperties = <D extends CompositeDetails>(
? R.sort(config.propertySortFunction, ps as any)
: ps;

const result: InterfacePropertyDeclaration[] = sortedPs.map(
(p: CompositeProperty): InterfacePropertyDeclaration => {
const result: InterfacePropertyDeclaration[] = sortedPs
.filter((p) => generateFor === 'selector' || p.generated !== 'ALWAYS')
.map((p: CompositeProperty): InterfacePropertyDeclaration => {
// If this is a (materialized or not) view column, we need to check
// the source table to see if the column is nullable.
if ((p as ViewColumn | MaterializedViewColumn).source) {
Expand Down Expand Up @@ -58,7 +59,10 @@ const generateProperties = <D extends CompositeDetails>(
} = config.getPropertyMetadata(p, details, generateFor, config);

const canBeOptional: boolean =
p.isNullable || p.defaultValue || p.isIdentity;
p.isNullable ||
p.defaultValue ||
p.isIdentity ||
p.generated === 'BY DEFAULT';

const t = typeOverride ?? resolveType(p, details, config);

Expand Down Expand Up @@ -107,8 +111,7 @@ const generateProperties = <D extends CompositeDetails>(
typeName,
typeImports,
};
},
);
});
return result;
};

Expand Down

0 comments on commit 1409a20

Please sign in to comment.