Skip to content

Commit 68b47f8

Browse files
authored
Merge pull request #9 from SalemC/resolve-generated-relations-not-respecting-nullable-column
Resolve generated relations not respecting nullable column
2 parents 49c4483 + c8b9062 commit 68b47f8

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/TypeScriptifyModel.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,16 @@ private function getTypeScriptType(stdClass $columnSchema): string {
274274
// We generate new interfaces for any relational attributes.
275275
// That means we can recursively instantiate the current class to generate
276276
// as many interface definitions for relational attributes as we need.
277-
return (new self($fullyQualifiedRelatedModelName))->generate();
277+
$mappedType = (new self($fullyQualifiedRelatedModelName))->generate();
278+
} else {
279+
// If the attribute is natively casted, we'll want to perform native cast checking
280+
// to generate the correct TypeScript type. If it's not natively casted, we can
281+
// simply map the database type to a TypeScript type.
282+
$mappedType = $this->isAttributeNativelyCasted($columnSchema->Field)
283+
? $this->mapNativeCastToTypeScriptType($columnSchema->Field)
284+
: $this->mapDatabaseTypeToTypeScriptType($columnType);
278285
}
279286

280-
// If the attribute is natively casted, we'll want to perform native cast checking
281-
// to generate the correct TypeScript type. If it's not natively casted, we can
282-
// simply map the database type to a TypeScript type.
283-
$mappedType = $this->isAttributeNativelyCasted($columnSchema->Field)
284-
? $this->mapNativeCastToTypeScriptType($columnSchema->Field)
285-
: $this->mapDatabaseTypeToTypeScriptType($columnType);
286-
287287
// We can't do much with an unknown type.
288288
if ($mappedType === 'unknown') return $mappedType;
289289

@@ -313,19 +313,23 @@ private function generateInterface(): string {
313313

314314
$outputBuffer = collect([
315315
// The output buffer always needs to start with the first `interface X {` line.
316-
'interface ' . (Str::of($this->fullyQualifiedModelName)->afterLast('\\')) . " {"
316+
sprintf('interface %s {', Str::of($this->fullyQualifiedModelName)->afterLast('\\')),
317317
]);
318318

319319
$tableColumns->each(function ($column) use ($outputBuffer) {
320320
// If this attribute is hidden and we're not including hidden, we'll skip it.
321321
if (!$this->includeHidden && $this->isAttributeHidden($column->Field)) return;
322322

323323
if ($this->isAttributeRelation($column->Field)) {
324-
// The foreign model name will be the name of the interface we'll generate for this relation.
325-
$foreignModelName = Str::of($this->convertForeignKeyToFullyQualifiedModelName($column->Field))->afterLast('\\');
326324
$relationName = $this->convertForeignKeyToPredictedRelationName($column->Field);
325+
$generatedTypeScriptType = Str::of($this->getTypeScriptType($column));
326+
327+
$generatedTypeName = $generatedTypeScriptType
328+
->after('interface ')
329+
->before(' {')
330+
->append($generatedTypeScriptType->afterLast('}'));
327331

328-
$outputBuffer->push(sprintf(' %s: %s;', $relationName, $foreignModelName));
332+
$outputBuffer->push(sprintf(' %s: %s;', $relationName, $generatedTypeName));
329333

330334
// Add an empty line so related interfaces aren't directly after each other.
331335
$outputBuffer->prepend('');
@@ -335,7 +339,7 @@ private function generateInterface(): string {
335339
// Once we've got the interface, we'll want to explode it, then prepend
336340
// each piece of the interface to the output buffer.
337341
// We reverse the exploded string because we prepend, otherwise we'd prepend backwards.
338-
Str::of($this->getTypeScriptType($column))
342+
$generatedTypeScriptType
339343
->explode("\n")
340344
->reverse()
341345
->each(fn ($str) => $outputBuffer->prepend($str));

0 commit comments

Comments
 (0)