Skip to content

Commit

Permalink
add count feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Jan 30, 2023
1 parent 498479d commit 2bc77c1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ composer require kiwilan/laravel-typescriptable
- [x] Generate TypeScript types for [Eloquent relations](https://laravel.com/docs/9.x/eloquent-relationships) (except `morphTo`)
- [x] Generate TypeScript types for `casts` (include native `enum` support)
- [x] Generate TypeScript types for `dates`
- [x] Generate TypeScript types for `appends` (partial for [Attribute Casting](https://laravel.com/docs/9.x/eloquent-mutators))
- [x] Generate TypeScript types for `appends` (partial for [`Casts\Attribute`](https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor), you can use old way to define `get*Attribute` methods)
- [x] Generate TypeScript types `counts`

## Usage
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kiwilan/laravel-typescriptable",
"description": "Laravel package to type Eloquent models with autogenerated TypeScript, ready for Inertia.",
"version": "0.1.0",
"version": "0.1.10",
"keywords": [
"kiwilan",
"laravel",
Expand Down
20 changes: 20 additions & 0 deletions src/Services/TypescriptableService/TypescriptableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(
public ?string $mediable = null,
/** @var TypescriptableProperty[] */
public array $properties = [],
public array $counts = [],
public ?string $tsString = null,
public ?string $phpString = null,
) {
Expand All @@ -50,6 +51,7 @@ public static function make(TypescriptableClass $class): self

$parser->setAppends();
$parser->setRelations();
$parser->setCounts();

if ($parser->class->command?->fakeTeam && $parser->class->name === 'User') {
$parser->setFakeTeam();
Expand Down Expand Up @@ -85,6 +87,15 @@ public static function fake(TypescriptableClass $class, array $properties): self
return $model;
}

private function setCounts()
{
foreach ($this->relations as $field => $relation) {
if ($relation->isArray) {
$this->counts[$field] = $relation->type;
}
}
}

private function convertToPhp(): string
{
$php[] = '<?php';
Expand Down Expand Up @@ -154,6 +165,15 @@ private function setProperties()
);
}

foreach ($this->counts as $field => $type) {
$this->properties["{$field}_count"] = TypescriptableProperty::make(
table: $this->class->table,
dbColumn: new TypescriptableDbColumn("{$field}_count", 'number'),
overrideTsType: true,
isCount: true,
);
}

if ($this->mediable) {
$this->properties['mediable'] = TypescriptableProperty::make(
table: $this->table,
Expand Down
3 changes: 3 additions & 0 deletions src/Services/TypescriptableService/TypescriptableProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(
public bool $isRelation = false,
public bool $isArray = false,
public bool $isAppend = false,
public bool $isCount = false,
public bool $overrideTsType = false,
public ?string $tsType = null,
public ?string $tsString = null,
Expand All @@ -39,6 +40,7 @@ public static function make(
bool $isRelation = false,
bool $isAppend = false,
bool $isArray = false,
bool $isCount = false,
): self {
$property = new self(
table: $table,
Expand All @@ -58,6 +60,7 @@ public static function make(
$property->isRelation = $isRelation;
$property->isAppend = $isAppend;
$property->isArray = $isArray;
$property->isCount = $isCount;
$property->setPhpString();

return $property;
Expand Down

0 comments on commit 2bc77c1

Please sign in to comment.