Skip to content

Commit

Permalink
Merge pull request #47 from kiwilan/develop
Browse files Browse the repository at this point in the history
# v1.11.34
  • Loading branch information
ewilan-riviere authored Jan 11, 2024
2 parents b494931 + 602b470 commit 50d8697
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ DB_SQLSRV_USER=sa
DB_SQLSRV_PASSWORD=12345OHdf%e
DB_SQLSRV_DATABASE=testing

DB_PREFIX=ts
DB_PREFIX=ts_
DATABASE_TYPES=mysql,sqlite,pgsql,sqlsrv # mysql,sqlite,pgsql,sqlsrv
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
![Banner with printer shop picture in background and Typescriptable Laravel title](https://raw.githubusercontent.com/kiwilan/typescriptable-laravel/main/docs/banner.jpg)

[![php][php-version-src]][php-version-href]
[![laravel][laravel-src]][laravel-href]
[![version][version-src]][version-href]
[![tests][tests-src]][tests-href]

[![downloads][downloads-src]][downloads-href]
[![license][license-src]][license-href]
[![codecov][codecov-src]][codecov-href]
[![tests][tests-src]][tests-href]

[![laravel][laravel-src]][laravel-href]
[![npm][npm-version-src]][npm-version-href]

PHP package for Laravel **to type Eloquent models**, **routes**, [**Spatie Settings**](https://github.com/spatie/laravel-settings) with **autogenerated TypeScript**.
Expand Down Expand Up @@ -143,6 +143,24 @@ With options:

## Troubleshooting

### Database prefix

If you have a database prefix, you can add it in `config/typescriptable.php` file with `DB_PREFIX` env variable.

```php
return [
'database_prefix' => env('DB_PREFIX', ''),
];
```

Or you can use `DB_PREFIX` into `config/database.php` file.

```php
'prefix' => env('DB_PREFIX', ''),
```

Two configs works.

### Override models

`kiwilan/typescriptable-laravel` will cover many cases, but if you want to override some models, you can just create a type like `resources/js/types/index.ts` and extends `Model` type.
Expand Down
4 changes: 3 additions & 1 deletion src/Typed/Database/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ private function setColumns(): array
throw new \Exception("Database driver not supported: {$this->driver}");
}

if (! Schema::hasTable($this->name)) {
$schemaTables = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();

if (! in_array($this->name, $schemaTables)) {
return [];
}

Expand Down
50 changes: 31 additions & 19 deletions tests/Data/database/migrations/create_models_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Kiwilan\Typescriptable\Tests\Data\Enums\PublishStatusEnum;
use Kiwilan\Typescriptable\TypescriptableConfig;

return new class extends Migration
{
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
Schema::create($this->createTable('users'), function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
Expand All @@ -22,7 +23,7 @@ public function up(): void
$table->timestamps();
});

Schema::create('stories', function (Blueprint $table) {
Schema::create($this->createTable('stories'), function (Blueprint $table) {
$table->id();

$table->string('title');
Expand All @@ -39,7 +40,7 @@ public function up(): void
$table->timestamps();
});

Schema::create('authors', function (Blueprint $table) {
Schema::create($this->createTable('authors'), function (Blueprint $table) {
$table->id();

$table->string('name');
Expand All @@ -49,7 +50,7 @@ public function up(): void
$table->timestamps();
});

Schema::create('chapters', function (Blueprint $table) {
Schema::create($this->createTable('chapters'), function (Blueprint $table) {
$table->id();

$table->string('name')->nullable();
Expand All @@ -60,7 +61,7 @@ public function up(): void
$table->timestamps();
});

Schema::create('categories', function (Blueprint $table) {
Schema::create($this->createTable('categories'), function (Blueprint $table) {
$table->id();

$table->string('name');
Expand All @@ -71,29 +72,29 @@ public function up(): void
$table->timestamps();
});

Schema::table('stories', function (Blueprint $table) {
$table->foreignId('author_id')->nullable()->constrained('authors')->nullOnDelete();
$table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();
Schema::table($this->createTable('stories'), function (Blueprint $table) {
$table->foreignId('author_id')->nullable()->constrained($this->createTable('authors'))->nullOnDelete();
$table->foreignId('category_id')->nullable()->constrained($this->createTable('categories'))->nullOnDelete();
});

Schema::table('chapters', function (Blueprint $table) {
$table->foreignId('story_id')->nullable()->constrained('stories')->nullOnDelete();
Schema::table($this->createTable('chapters'), function (Blueprint $table) {
$table->foreignId('story_id')->nullable()->constrained($this->createTable('stories'))->nullOnDelete();
});

Schema::create('tags', function (Blueprint $table) {
Schema::create($this->createTable('tags'), function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug')->unique();
$table->timestamps();
});

Schema::create('story_tag', function (Blueprint $table) {
Schema::create($this->createTable('story_tag'), function (Blueprint $table) {
$table->id();
$table->foreignId('tag_id')->constrained();
$table->foreignId('story_id')->constrained();
$table->foreignId('tag_id')->constrained($this->createTable('tags'))->cascadeOnDelete();
$table->foreignId('story_id')->constrained($this->createTable('stories'))->cascadeOnDelete();
});

Schema::create('comments', function (Blueprint $table) {
Schema::create($this->createTable('comments'), function (Blueprint $table) {
$table->id();

$table->string('name')->nullable();
Expand All @@ -107,15 +108,15 @@ public function up(): void

$table->foreignId('comment_id')
->nullable()
->constrained()
->constrained($this->createTable('comments'))
->onDelete('no action');

$table->morphs('commentable');

$table->timestamps();
});

Schema::create('members', function (Blueprint $table) {
Schema::create($this->createTable('members'), function (Blueprint $table) {
$table->id();

$table->unsignedBigInteger('tmdb_id')->nullable();
Expand All @@ -132,7 +133,7 @@ public function up(): void
$table->timestamps();
});

Schema::create('memberables', function (Blueprint $table) {
Schema::create($this->createTable('memberables'), function (Blueprint $table) {
$table->foreignId('member_id')->index();
$table->string('character')->nullable();
$table->string('job')->nullable();
Expand All @@ -144,7 +145,7 @@ public function up(): void
$table->ulidMorphs('memberable');
});

Schema::create('movies', function (Blueprint $table) {
Schema::create($this->createTable('movies'), function (Blueprint $table) {
$table->ulid('id')->primary();

$table->integer('tmdb_id')->nullable();
Expand Down Expand Up @@ -187,4 +188,15 @@ public function up(): void
$table->timestamps();
});
}

private function createTable(string $name): string
{
$prefix = TypescriptableConfig::databasePrefix();

if ($prefix) {
$name = "{$prefix}{$name}";
}

return $name;
}
};
1 change: 0 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public static function setupDatabase(?string $type = null): void
config()->set('database.default', $driver->name);
config()->set("database.connections.{$driver->name}", [
'driver' => $driver->name,
'database' => $driver->database,
'url' => $driver->url,
'host' => $driver->host,
'port' => $driver->port,
Expand Down

0 comments on commit 50d8697

Please sign in to comment.