prisma-laravel-migrate 0.0.26 → 0.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/dist/cli/cli.js +1 -1
- package/dist/generator/modeler/generator.js +19 -1
- package/dist/utils/build.js +15 -0
- package/package.json +1 -1
- package/stubs/model.stub +6 -3
package/README.md
CHANGED
|
@@ -573,6 +573,7 @@ your Eloquent model.
|
|
|
573
573
|
| `@ignore` | Relation field | Skips generating the relationship method |
|
|
574
574
|
| `@with` (no args) | Relation field | Adds that single relation to `$with` |
|
|
575
575
|
| `@with(rel1,rel2,…)` | Model only | Adds listed relations to `$with` |
|
|
576
|
+
| **NEW** `@extends:Full\Namespace\MyExtension` | Model only | Adds `extends MyExtension;` |
|
|
576
577
|
| **NEW** `@trait:Full\Namespace\MyTrait` | Model only | Adds `use MyTrait;` inside the class |
|
|
577
578
|
| **NEW** `@implements:Full\Interface as Alias`| Model only | Adds the interface (with alias) to the class’s `implements` list |
|
|
578
579
|
| **NEW** `@observer:App\Observers\FooObserver`| Model only | Generates a `boot()` method that calls `static::observe(FooObserver::class);` |
|
|
@@ -654,6 +655,7 @@ Combine multiple inline directives; they’re processed left‑to‑right.
|
|
|
654
655
|
/// @hidden{secretToken}
|
|
655
656
|
/// @guarded{password,apiToken}
|
|
656
657
|
/// @trait:Illuminate\Auth\Authenticatable
|
|
658
|
+
/// @extend:Illuminate\Auth\User
|
|
657
659
|
/// @implements:Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract
|
|
658
660
|
/// @observer:App\Observers\UserObserver
|
|
659
661
|
/// @factory:UserFactory
|
|
@@ -675,11 +677,12 @@ model User {
|
|
|
675
677
|
|
|
676
678
|
```php
|
|
677
679
|
use Illuminate\Auth\Authenticatable;
|
|
680
|
+
use Illuminate\Auth\User;
|
|
678
681
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
679
682
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
680
683
|
use App\Observers\UserObserver;
|
|
681
684
|
|
|
682
|
-
class User extends
|
|
685
|
+
class User extends User implements AuthenticatableContract
|
|
683
686
|
{
|
|
684
687
|
use HasFactory, Authenticatable;
|
|
685
688
|
|
package/dist/cli/cli.js
CHANGED
|
@@ -37,7 +37,7 @@ cli
|
|
|
37
37
|
const schemaPath = path.resolve(process.cwd(), opts.schema);
|
|
38
38
|
const schemaDir = path.dirname(schemaPath); // prisma/
|
|
39
39
|
const userStubs = path.join(schemaDir, "stubs"); // prisma/stubs
|
|
40
|
-
const stubDirRel = "./" + path.relative(
|
|
40
|
+
const stubDirRel = "./" + path.relative(process.cwd(), userStubs).replace(/\\/g, "/"); // "./stubs"
|
|
41
41
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
42
42
|
const pkgStubs = path.resolve(__dirname, "../../stubs"); // bundled stubs
|
|
43
43
|
/* 2. Load schema.prisma ---------------------------------------- */
|
|
@@ -133,6 +133,22 @@ export class PrismaToLaravelModelGenerator {
|
|
|
133
133
|
addUse(traitUses, m[1], m[2]);
|
|
134
134
|
traits.push(shortName(m[1], m[2]));
|
|
135
135
|
}
|
|
136
|
+
/* ---------------- NEW: @extend ----------------------------------------- */
|
|
137
|
+
// Syntax: /// @extend:App\BaseClasses\SoftModel
|
|
138
|
+
// Optional alias just like traits:
|
|
139
|
+
/*
|
|
140
|
+
/// @extend:App\Foo\Bar as BaseBar
|
|
141
|
+
→ use App\Foo\Bar as BaseBar;
|
|
142
|
+
→ extends "BaseBar"
|
|
143
|
+
*/
|
|
144
|
+
const extendRE = /@extend:([^\s]+)(?:\s+as\s+(\w+))?/;
|
|
145
|
+
let parentClass = "Model"; // default
|
|
146
|
+
let parentUse; // for imports[]
|
|
147
|
+
const extMatch = extendRE.exec(modelDoc);
|
|
148
|
+
if (extMatch) {
|
|
149
|
+
parentClass = shortName(extMatch[1], extMatch[2]); // "Bar" | "BaseBar"
|
|
150
|
+
parentUse = { fqcn: extMatch[1], alias: extMatch[2] };
|
|
151
|
+
}
|
|
136
152
|
// collect implements ------------------------------------------------------
|
|
137
153
|
const implUses = [];
|
|
138
154
|
const implementsArr = [];
|
|
@@ -152,6 +168,7 @@ export class PrismaToLaravelModelGenerator {
|
|
|
152
168
|
const appends = appendsRE.exec(modelDoc)?.[1]?.split(',').map(s => s.trim()).filter(Boolean) ?? [];
|
|
153
169
|
// final imports list ------------------------------------------------------
|
|
154
170
|
const imports = [
|
|
171
|
+
...(parentUse ? [parentUse] : []),
|
|
155
172
|
...traitUses,
|
|
156
173
|
...implUses,
|
|
157
174
|
...(observerUse ? [observerUse] : []),
|
|
@@ -173,7 +190,8 @@ export class PrismaToLaravelModelGenerator {
|
|
|
173
190
|
observer,
|
|
174
191
|
touches,
|
|
175
192
|
traits,
|
|
176
|
-
imports
|
|
193
|
+
imports,
|
|
194
|
+
extends: parentClass !== 'Model' ? parentClass : undefined
|
|
177
195
|
};
|
|
178
196
|
});
|
|
179
197
|
return { models, enums };
|
package/dist/utils/build.js
CHANGED
|
@@ -79,6 +79,21 @@ export function buildModelContent(model) {
|
|
|
79
79
|
}`);
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
|
+
//--- handle relationships -----------------------------------------
|
|
83
|
+
model.relations.forEach(rel => {
|
|
84
|
+
// For morphTo* the first param is the relation name (string),
|
|
85
|
+
// otherwise it’s the related model class.
|
|
86
|
+
const isMorph = rel.type.startsWith('morph');
|
|
87
|
+
const args = [
|
|
88
|
+
isMorph ? `'${rel.morphType ?? rel.name}'` : rel.modelClass,
|
|
89
|
+
rel.foreignKey ? `'${rel.foreignKey}'` : null,
|
|
90
|
+
rel.localKey ? `'${rel.localKey}'` : null,
|
|
91
|
+
]
|
|
92
|
+
.filter(Boolean)
|
|
93
|
+
.join(', ');
|
|
94
|
+
out.push(`public function ${rel.name}()`, '{', ` return $this->${rel.type}(${args});` +
|
|
95
|
+
(rel.pivotTable ? ` // pivot: ${rel.pivotTable}` : ''), '}', '');
|
|
96
|
+
});
|
|
82
97
|
//---
|
|
83
98
|
out.push(...buildAppendAccessors(model.appends));
|
|
84
99
|
/* ---- done --------------------------------------------------------- */
|
package/package.json
CHANGED
package/stubs/model.stub
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
namespace App\\Models;
|
|
4
4
|
|
|
5
|
-
${model.imports.join('\n')}
|
|
6
|
-
|
|
5
|
+
${model.imports.join('\n')}${model.extends && model.extends !== 'Illuminate\Database\Eloquent\Model'
|
|
6
|
+
? ''
|
|
7
|
+
: '\nuse Illuminate\\Database\\Eloquent\\Model;'}
|
|
7
8
|
|
|
8
|
-
class ${model.className} extends
|
|
9
|
+
class ${model.className} extends ${model.extends ?? 'Model'}${model.implements.length
|
|
10
|
+
? ' implements ' + model.implements.join(', ')
|
|
11
|
+
: ''}
|
|
9
12
|
{
|
|
10
13
|
protected $table = '${model.tableName}';
|
|
11
14
|
|