suppa-mcp-2 1.1.4 → 1.2.1
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/LICENSE +21 -21
- package/README.md +387 -342
- package/dist/auth.js +29 -25
- package/dist/server.js +93 -50
- package/dist/tools/entity.js +343 -40
- package/dist/tools/migrate.js +148 -11
- package/package.json +50 -50
- package/skills/suppa-entity-code/SKILL.md +133 -0
- package/skills/suppa-entity-code/references/entities.md +292 -0
- package/skills/suppa-entity-code/references/registration.md +224 -0
- package/skills/suppa-entity-code/references/seeds.md +136 -0
- package/skills/suppa-entity-code/references/tabular-parts.md +146 -0
- package/skills/suppa-entity-code/references/v1-mapping.md +78 -0
- package/skills/suppa-entity-code/scripts/validate-suppa-schema.mjs +1063 -0
- package/skills/suppa-entity-code/templates/entity.template.ts +79 -0
- package/skills/suppa-entity-code/templates/seed.template.ts +41 -0
- package/skills/suppa-entity-code/templates/tabular-part.template.ts +93 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
# Entity declaration — verbatim rules
|
|
2
|
+
|
|
3
|
+
Transcribed from docs.modern-expo.com/backend/application-developing/migrations.html (captured 2026-08-03). If an option is not in these tables, it does not exist — stop and ask.
|
|
4
|
+
|
|
5
|
+
## A Minimal Entity
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Column, Entity, FieldTypeEnum, SystemBaseEntity } from '@suppa/sdk';
|
|
9
|
+
|
|
10
|
+
@Entity({
|
|
11
|
+
name: 'Workflows',
|
|
12
|
+
title: { en: 'Workflows', uk: 'Робочі процеси' },
|
|
13
|
+
importKeyFields: ['name'],
|
|
14
|
+
representativeFieldName: 'name',
|
|
15
|
+
})
|
|
16
|
+
export class Workflows extends SystemBaseEntity {
|
|
17
|
+
@Column({ name: 'name', type: FieldTypeEnum.Text, nullable: false })
|
|
18
|
+
name: string;
|
|
19
|
+
|
|
20
|
+
@Column({ name: 'description', type: FieldTypeEnum.Text })
|
|
21
|
+
description: string;
|
|
22
|
+
|
|
23
|
+
@Column({
|
|
24
|
+
name: 'private',
|
|
25
|
+
type: FieldTypeEnum.Boolean,
|
|
26
|
+
default: 'false',
|
|
27
|
+
})
|
|
28
|
+
private: boolean;
|
|
29
|
+
|
|
30
|
+
@Column({ name: 'externalId', type: FieldTypeEnum.UUID })
|
|
31
|
+
externalId: string;
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## SystemBaseEntity
|
|
36
|
+
|
|
37
|
+
Every entity must extend SystemBaseEntity. It contributes the standard columns, so you never declare them yourself:
|
|
38
|
+
|
|
39
|
+
| Field | Type | Description |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| id | serial | Auto-increment primary key |
|
|
42
|
+
| createdAt | timestamp | Creation timestamp |
|
|
43
|
+
| updatedAt | timestamp | Last update timestamp |
|
|
44
|
+
| deletedAt | timestamp | Soft-delete timestamp |
|
|
45
|
+
| createdBy | ManyToOne(Users) | Who created the record |
|
|
46
|
+
| removedBy | ManyToOne(Users) | Who soft-deleted it |
|
|
47
|
+
|
|
48
|
+
## @Entity() Options
|
|
49
|
+
|
|
50
|
+
| Option | Type | Description |
|
|
51
|
+
|---|---|---|
|
|
52
|
+
| name | string | Entity and table name. Defaults to the class name. |
|
|
53
|
+
| title | Record<string, string> | Localized display name. |
|
|
54
|
+
| comment | string | Description shown in metadata. |
|
|
55
|
+
| representativeFieldName | string | Field used to display a record. Defaults to id. |
|
|
56
|
+
| importKeyFields | string[] | Business key used by seeds, import and relation references. |
|
|
57
|
+
| key | string | Stable identifier that survives renaming the entity. |
|
|
58
|
+
| icon | Record<string, any> | Icon configuration. |
|
|
59
|
+
| type | string | Entity kind. Omit for a standalone entity, or set 'tabular-part' — see Tabular Parts. |
|
|
60
|
+
| relationEntityName | string | Name of the entity this one belongs to. Required for a tabular part. |
|
|
61
|
+
| options | Record<string, any> | Platform features to enable or disable for the entity (see below). |
|
|
62
|
+
|
|
63
|
+
See [references/tabular-parts.md](tabular-parts.md) for the full triad required to declare a tabular part (`type: 'tabular-part'` + `relationEntityName` + an `owner` relation).
|
|
64
|
+
|
|
65
|
+
options toggles the built-in per-entity features. Disable what the entity does not need — each enabled feature adds side tables and processing:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
@Entity({
|
|
69
|
+
name: 'Workflows',
|
|
70
|
+
importKeyFields: ['name'],
|
|
71
|
+
options: {
|
|
72
|
+
comments: false,
|
|
73
|
+
favorites: false,
|
|
74
|
+
approvals: false,
|
|
75
|
+
timeTracking: false,
|
|
76
|
+
browsingHistory: false,
|
|
77
|
+
trackChangeHistory: false,
|
|
78
|
+
globalSearch: false,
|
|
79
|
+
notificationMutes: false,
|
|
80
|
+
reminders: false,
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
export class Workflows extends SystemBaseEntity {}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## @Column()
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { Column, FieldTypeEnum } from '@suppa/sdk';
|
|
90
|
+
|
|
91
|
+
@Column({
|
|
92
|
+
name: 'title',
|
|
93
|
+
type: FieldTypeEnum.Text,
|
|
94
|
+
nullable: false,
|
|
95
|
+
length: 255,
|
|
96
|
+
})
|
|
97
|
+
title: string;
|
|
98
|
+
|
|
99
|
+
@Column({
|
|
100
|
+
name: 'estimatedDuration',
|
|
101
|
+
type: FieldTypeEnum.Numeric,
|
|
102
|
+
decimalPlaces: 2,
|
|
103
|
+
})
|
|
104
|
+
estimatedDuration: number;
|
|
105
|
+
|
|
106
|
+
@Column({ name: 'isActive', type: FieldTypeEnum.Boolean, default: 'false' })
|
|
107
|
+
isActive: boolean;
|
|
108
|
+
|
|
109
|
+
@Column({ name: 'deadline', type: FieldTypeEnum.Timestamp })
|
|
110
|
+
deadline: string;
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
| Option | Type | Description |
|
|
114
|
+
|---|---|---|
|
|
115
|
+
| type | FieldTypeEnum | Column type. Take the value from FieldTypeEnum (see below). |
|
|
116
|
+
| name | string | Column name. Defaults to the property name. |
|
|
117
|
+
| nullable | boolean | Allow NULL. |
|
|
118
|
+
| primary | boolean | Mark as primary key. |
|
|
119
|
+
| unique | boolean | Unique constraint. |
|
|
120
|
+
| default | string | Default value as a SQL expression string. |
|
|
121
|
+
| length | number | Maximum length. |
|
|
122
|
+
| title | Record<string, string> | Localized field name. |
|
|
123
|
+
| comment | string | Description. |
|
|
124
|
+
| key | string | Stable identifier that survives renaming. |
|
|
125
|
+
| minLength | number | Validation: minimum length. |
|
|
126
|
+
| minValue | number | Validation: minimum value. |
|
|
127
|
+
| maxValue | number | Validation: maximum value. |
|
|
128
|
+
| decimalPlaces | number | Decimal precision. |
|
|
129
|
+
| isOnlyPositive | boolean | Positive numbers only. |
|
|
130
|
+
| mask | string | Input mask. |
|
|
131
|
+
|
|
132
|
+
> **WARNING**
|
|
133
|
+
> `default` is a string containing a SQL expression, not a JavaScript value. Write `default: 'false'`, not `default: false`, and `default: "'draft'"` for a string literal.
|
|
134
|
+
|
|
135
|
+
## FieldTypeEnum — Column Types
|
|
136
|
+
|
|
137
|
+
Column types come from the FieldTypeEnum enum exported by @suppa/sdk:
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
import { FieldTypeEnum } from '@suppa/sdk';
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
`type` is declared as string, so a raw literal such as `type: 'text'` still compiles — but a misspelled one (`'txet'`) compiles too and only fails when the migration runs. Referencing the enum turns that into an editor error, and gives you the full list of valid types on autocomplete.
|
|
144
|
+
|
|
145
|
+
| Member | Value | Use for |
|
|
146
|
+
|---|---|---|
|
|
147
|
+
| Text | text | Strings of any length. Add length to cap it. |
|
|
148
|
+
| Numeric | numeric | Decimal numbers. Pair with decimalPlaces. |
|
|
149
|
+
| Integer | integer | Whole numbers. |
|
|
150
|
+
| Serial | serial | Auto-increment integer. Already provided by SystemBaseEntity.id. |
|
|
151
|
+
| Boolean | boolean | true / false. |
|
|
152
|
+
| Timestamp | timestamp | Dates and date-times. |
|
|
153
|
+
| JSON | json | Arbitrary structured payloads. |
|
|
154
|
+
| UUID | uuid | External identifiers. |
|
|
155
|
+
| MultiLang | multi-language | Per-language string values stored on the record. |
|
|
156
|
+
| Bytea | bytea | Raw binary. Prefer @File for attachments. |
|
|
157
|
+
| TsVector | tsvector | Full-text search vectors. |
|
|
158
|
+
| MultiType | multi-type | Composite field built from sub-fields. |
|
|
159
|
+
| Icon | icon | Icon reference. |
|
|
160
|
+
|
|
161
|
+
> **TIP**
|
|
162
|
+
> FieldTypeEnum covers scalar columns only. Relations, enums and files are declared with their own decorators — @ManyToOne, @ManyToMany, @OneToMany, @Enum, @MultiEnum, @File, @MultiFile — and never with @Column({ type: ... }). The SDK also exports RelationTypeEnum with those relation kinds, but you do not pass it manually; each decorator sets it for you.
|
|
163
|
+
|
|
164
|
+
## Relations
|
|
165
|
+
|
|
166
|
+
Relation decorators take a thunk returning the target class — `() => Workflows` — which keeps circular imports working:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
import {
|
|
170
|
+
Entity,
|
|
171
|
+
ManyToOne,
|
|
172
|
+
ManyToMany,
|
|
173
|
+
OneToMany,
|
|
174
|
+
SystemBaseEntity,
|
|
175
|
+
IconsEntity,
|
|
176
|
+
} from '@suppa/sdk';
|
|
177
|
+
import { Workflows } from './workflows.entity';
|
|
178
|
+
|
|
179
|
+
@Entity({ name: 'StageWorkflows', importKeyFields: ['name'] })
|
|
180
|
+
export class StageWorkflows extends SystemBaseEntity {
|
|
181
|
+
@ManyToOne(() => Workflows, { nullable: false })
|
|
182
|
+
workflow: Workflows;
|
|
183
|
+
|
|
184
|
+
@ManyToOne(() => IconsEntity)
|
|
185
|
+
icon: any;
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
| Decorator | Signature | Use |
|
|
190
|
+
|---|---|---|
|
|
191
|
+
| @ManyToOne | (target, options?) or (target, inverse, options?) | Foreign key on this entity. |
|
|
192
|
+
| @ManyToMany | (target, options?) or (target, inverse, options?) | Pivot table between both entities. |
|
|
193
|
+
| @ManyToManyBackRef | (target, inverse, options?) | Reverse side of a ManyToMany. |
|
|
194
|
+
| @OneToMany | (target, inverse, options?) | Reverse side of a ManyToOne. |
|
|
195
|
+
|
|
196
|
+
`inverse` is a property selector on the target entity, for example `(task) => task.project`.
|
|
197
|
+
|
|
198
|
+
Platform entities are importable from the SDK when you need to point at them — for example `IconsEntity`.
|
|
199
|
+
|
|
200
|
+
## Enums
|
|
201
|
+
|
|
202
|
+
@Enum and @MultiEnum accept a TypeScript enum with string values. Give the enum a global name (first argument) so it can be referenced from seeds and shared across entities:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
export enum StageStatusEnum {
|
|
206
|
+
Active = 'active',
|
|
207
|
+
InProgress = 'inprogress',
|
|
208
|
+
Deferred = 'deferred',
|
|
209
|
+
Completed = 'completed',
|
|
210
|
+
Canceled = 'cancelled',
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export const StageStatus = {
|
|
214
|
+
[StageStatusEnum.Active]: { title: { en: 'Active', uk: 'Активний' } },
|
|
215
|
+
[StageStatusEnum.InProgress]: {
|
|
216
|
+
title: { en: 'In Progress', uk: 'В процесі' },
|
|
217
|
+
},
|
|
218
|
+
[StageStatusEnum.Deferred]: { title: { en: 'Deferred', uk: 'Відкладений' } },
|
|
219
|
+
[StageStatusEnum.Completed]: {
|
|
220
|
+
title: { en: 'Completed', uk: 'Виконаний' },
|
|
221
|
+
},
|
|
222
|
+
[StageStatusEnum.Canceled]: { title: { en: 'Canceled', uk: 'Скасований' } },
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
@Enum('StageWorkflows.status', StageStatusEnum, StageStatus, {
|
|
226
|
+
nullable: false,
|
|
227
|
+
})
|
|
228
|
+
status: StageStatusEnum;
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
The second map is optional and adds a localized title, icon and order per value.
|
|
232
|
+
|
|
233
|
+
Use @MultiEnum with the same signatures for multi-select.
|
|
234
|
+
|
|
235
|
+
## Files
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
@File({ title: { en: 'Cover', uk: 'Обкладинка' } })
|
|
239
|
+
cover: any;
|
|
240
|
+
|
|
241
|
+
@MultiFile({ title: { en: 'Attachments', uk: 'Вкладення' } })
|
|
242
|
+
attachments: any[];
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## Indexes and Checks
|
|
246
|
+
|
|
247
|
+
@Index() and @Check() are class decorators:
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
@Entity({ name: 'Tasks', importKeyFields: ['externalId'] })
|
|
251
|
+
@Index('idx_tasks_project', ['project'])
|
|
252
|
+
@Index('idx_tasks_alive', ['deadline'], { where: '"deletedAt" IS NULL' })
|
|
253
|
+
@Check(
|
|
254
|
+
'chk_tasks_positive_duration',
|
|
255
|
+
['estimatedDuration'],
|
|
256
|
+
'"estimatedDuration" > 0',
|
|
257
|
+
)
|
|
258
|
+
export class Tasks extends SystemBaseEntity {}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
| @Index() option | Type | Description |
|
|
262
|
+
|---|---|---|
|
|
263
|
+
| unique | boolean | Unique index. |
|
|
264
|
+
| method | string | Index method, e.g. btree, gin. |
|
|
265
|
+
| where | string | Partial index predicate. |
|
|
266
|
+
| expression | string | Expression index. |
|
|
267
|
+
| comment | string | Description. |
|
|
268
|
+
|
|
269
|
+
## importKeyFields — the Business Key
|
|
270
|
+
|
|
271
|
+
importKeyFields names the fields that identify a record independently of its numeric id. It is the backbone of seeds, export/import and cross-schema references, because id values differ between tenants.
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
@Entity({ name: 'Workflows', importKeyFields: ['name'] })
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Pick fields that are stable and unique in practice. Without importKeyFields, seeds have nothing to match on and cannot stay idempotent.
|
|
278
|
+
|
|
279
|
+
> **TIP**
|
|
280
|
+
> Use the key option on @Entity() and @Column() when you expect to rename things later. The key is the stable identity used to recognize an existing entity or field, so a rename becomes a rename instead of a drop-and-create. Without it the key defaults to the name — see Removing and Re-Adding a Field.
|
|
281
|
+
|
|
282
|
+
## About title
|
|
283
|
+
|
|
284
|
+
title is not stored as a column. During migration the platform creates a Localizations record holding the per-language strings and links the entity or field metadata to it. You write the object inline and the platform handles the rest:
|
|
285
|
+
|
|
286
|
+
```ts
|
|
287
|
+
@Column({
|
|
288
|
+
type: FieldTypeEnum.Text,
|
|
289
|
+
title: { en: 'Email', uk: 'Електронна пошта' },
|
|
290
|
+
})
|
|
291
|
+
email: string;
|
|
292
|
+
```
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# Registration — verbatim rules
|
|
2
|
+
|
|
3
|
+
Transcribed from docs.modern-expo.com/backend/application-developing/migrations.html (captured 2026-08-03). If an option is not in these tables, it does not exist — stop and ask.
|
|
4
|
+
|
|
5
|
+
## EntityModule.forFeature
|
|
6
|
+
|
|
7
|
+
Everything goes through EntityModule from @suppa/sdk. Import it with forFeature() in the module that owns the entities — usually AppModule.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Module } from '@nestjs/common';
|
|
11
|
+
import { EntityModule } from '@suppa/sdk';
|
|
12
|
+
import { Workflows, StageWorkflows } from '../common/database/entities';
|
|
13
|
+
import { workflowsSeed, stageWorkflowsSeed } from '../common/database/seeds';
|
|
14
|
+
|
|
15
|
+
@Module({
|
|
16
|
+
imports: [
|
|
17
|
+
EntityModule.forFeature([Workflows, StageWorkflows], {
|
|
18
|
+
seeds: [workflowsSeed, stageWorkflowsSeed],
|
|
19
|
+
}),
|
|
20
|
+
],
|
|
21
|
+
})
|
|
22
|
+
export class AppModule {}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
EntityModule.forFeature() is what makes the platform aware of your entities and seeds. An entity class that is never passed to forFeature() is invisible — no table is created for it.
|
|
26
|
+
|
|
27
|
+
## Call Shapes
|
|
28
|
+
|
|
29
|
+
There are two supported shapes:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// 1. Explicit classes, with optional options
|
|
33
|
+
EntityModule.forFeature([Workflows, StageWorkflows], {
|
|
34
|
+
seeds: [workflowsSeed],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// 2. Options only — entities and seeds are autoloaded from paths
|
|
38
|
+
EntityModule.forFeature({
|
|
39
|
+
entitiesPath: 'dist/common/database/entities',
|
|
40
|
+
seedsPath: 'dist/common/database/seeds',
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Options
|
|
45
|
+
|
|
46
|
+
| Option | Type | Description |
|
|
47
|
+
|---|---|---|
|
|
48
|
+
| seeds | EntitySeedDefinition[] | Seeds created with createSeed(). |
|
|
49
|
+
| entitiesPath | string \| string[] | Directory or file to autoload entity classes from. |
|
|
50
|
+
| seedsPath | string \| string[] | Directory or file to autoload seeds from. |
|
|
51
|
+
|
|
52
|
+
Resolution order:
|
|
53
|
+
|
|
54
|
+
1. If the entities array is passed and not empty, it is used and entitiesPath is ignored.
|
|
55
|
+
2. Otherwise entities are loaded from entitiesPath.
|
|
56
|
+
3. If seeds is passed and not empty, it is used and seedsPath is ignored.
|
|
57
|
+
4. Otherwise seeds are loaded from seedsPath.
|
|
58
|
+
|
|
59
|
+
## Recommended Project Structure
|
|
60
|
+
|
|
61
|
+
Keep entities and seeds side by side, separate from business logic:
|
|
62
|
+
|
|
63
|
+
```txt
|
|
64
|
+
src/
|
|
65
|
+
index.ts
|
|
66
|
+
app/
|
|
67
|
+
app.module.ts
|
|
68
|
+
common/
|
|
69
|
+
database/
|
|
70
|
+
index.ts
|
|
71
|
+
entities/
|
|
72
|
+
index.ts
|
|
73
|
+
workflows.entity.ts
|
|
74
|
+
stage-workflows.entity.ts
|
|
75
|
+
seeds/
|
|
76
|
+
index.ts
|
|
77
|
+
workflows.seed.ts
|
|
78
|
+
stage-workflows.seed.ts
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Full Registration Example
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { Module } from '@nestjs/common';
|
|
85
|
+
import { EntityModule } from '@suppa/sdk';
|
|
86
|
+
import {
|
|
87
|
+
StageWorkflows,
|
|
88
|
+
Tasks,
|
|
89
|
+
Workflows,
|
|
90
|
+
} from '../common/database/entities';
|
|
91
|
+
import {
|
|
92
|
+
stageWorkflowsSeed,
|
|
93
|
+
workflowsSeed,
|
|
94
|
+
} from '../common/database/seeds';
|
|
95
|
+
|
|
96
|
+
@Module({
|
|
97
|
+
imports: [
|
|
98
|
+
EntityModule.forFeature([Workflows, StageWorkflows, Tasks], {
|
|
99
|
+
seeds: [workflowsSeed, stageWorkflowsSeed],
|
|
100
|
+
}),
|
|
101
|
+
],
|
|
102
|
+
})
|
|
103
|
+
export class AppModule {}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Two rules to remember:
|
|
107
|
+
|
|
108
|
+
- Every entity class must appear in the entities array.
|
|
109
|
+
- Every seed must appear in the seeds array, ordered so that dependencies come first.
|
|
110
|
+
|
|
111
|
+
## How Migrations Are Applied
|
|
112
|
+
|
|
113
|
+
You never run a migration command. The platform applies your schema as part of the application lifecycle:
|
|
114
|
+
|
|
115
|
+
1. The runner starts your application — on creation of the Applications record, on reload, or when a tenant is provisioned.
|
|
116
|
+
2. The SDK reads the decorator metadata of everything registered in EntityModule.forFeature() and generates two migration files: one for entity metadata, one for seeds.
|
|
117
|
+
3. The files are applied to the tenant's schema: tables, columns, relations, enums, indexes and checks are created or updated, then seeds are inserted or updated.
|
|
118
|
+
4. The result is recorded. If nothing in your entities or seeds changed since the last start, the step is skipped.
|
|
119
|
+
|
|
120
|
+
Two details worth knowing:
|
|
121
|
+
|
|
122
|
+
Entities and seeds behave differently on conflict. Entity metadata is overridden so the schema always converges to your class definitions. Seed rows are only inserted when missing, so data edited by users in the workspace is not overwritten on the next start.
|
|
123
|
+
|
|
124
|
+
It runs per tenant. Each tenant schema is migrated separately, and a newly provisioned tenant gets your entities and seeds when the application first boots for it.
|
|
125
|
+
|
|
126
|
+
## Removing and Re-Adding a Field
|
|
127
|
+
|
|
128
|
+
Deleting a property from an entity class and later adding it back with the same name is the one change whose outcome surprises people. Nothing is destroyed, and nothing starts empty: the field comes back with its old data.
|
|
129
|
+
|
|
130
|
+
The reason is that the sync matches your declarations against existing metadata by key, and the key of a field defaults to `<EntityName>.<fieldName>`. Same entity plus same field name means the same key, whether or not you ever set key yourself.
|
|
131
|
+
|
|
132
|
+
### Removing a Field
|
|
133
|
+
|
|
134
|
+
Delete the property, push, reload — on the next start the sync sees a key that exists in metadata but is no longer declared, and tears the field down:
|
|
135
|
+
|
|
136
|
+
| Step | Result |
|
|
137
|
+
|---|---|
|
|
138
|
+
| Field metadata | Soft-deleted: deletedAt is set and removedBy points at the system user. The row itself stays. |
|
|
139
|
+
| Column | Renamed, not dropped: title becomes title_1730457600000, where the suffix is deletedAt in epoch milliseconds. |
|
|
140
|
+
| Data | Untouched. Every value is still in the renamed column. |
|
|
141
|
+
| Relation fields | The relation is torn down the same way, through a soft removal. |
|
|
142
|
+
| Indexes and checks on the field | Their metadata is removed together with the field. |
|
|
143
|
+
| API and UI | The field is gone from metadata, from selects, and from forms. |
|
|
144
|
+
|
|
145
|
+
```sql
|
|
146
|
+
-- what the platform actually runs
|
|
147
|
+
ALTER TABLE "Tasks" RENAME COLUMN "title" TO "title_1730457600000";
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
> **WARNING**
|
|
151
|
+
> The renamed column is never cleaned up. It stays in the table with all its rows, invisible to the platform, until someone drops it manually. Removing a field is therefore not a way to delete data, and repeated remove/re-add cycles leave one leftover column each.
|
|
152
|
+
|
|
153
|
+
### Adding the Same Field Back
|
|
154
|
+
|
|
155
|
+
Re-declare the property with the same name on the same entity, and the sync resolves the key to the soft-deleted field instead of creating a new one:
|
|
156
|
+
|
|
157
|
+
| Step | Result |
|
|
158
|
+
|---|---|
|
|
159
|
+
| Field metadata | The same row is restored — deletedAt is cleared. No duplicate field, and the field id does not change. |
|
|
160
|
+
| Column | title_1730457600000 is renamed back to title. |
|
|
161
|
+
| Data | The old values are back, including everything written before the removal. |
|
|
162
|
+
|
|
163
|
+
```sql
|
|
164
|
+
ALTER TABLE "Tasks" RENAME COLUMN "title_1730457600000" TO "title";
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
> **WARNING**
|
|
168
|
+
> Re-adding a field is a restore, not a fresh start. If you removed the field in order to clear it, the data returns the moment you declare it again. There is no flag that turns this off — to get a genuinely empty field, the new field must have a different key.
|
|
169
|
+
|
|
170
|
+
## The Development Loop
|
|
171
|
+
|
|
172
|
+
```txt
|
|
173
|
+
edit entity / seed → push → reload the application → verify
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Reload with the application record id:
|
|
177
|
+
|
|
178
|
+
```http
|
|
179
|
+
POST /core/applications/:appId/reload
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
(From Run application:) appId is the id of the record in the Applications table. The runner rebuilds the app from that record and restarts the running instance.
|
|
183
|
+
|
|
184
|
+
See Run application for the full application lifecycle.
|
|
185
|
+
|
|
186
|
+
> **TIP**
|
|
187
|
+
> For a faster loop, suppa-dev start --watch runs your application locally through a tunnel so hooks and controllers reload on save.
|
|
188
|
+
|
|
189
|
+
## Common Mistakes
|
|
190
|
+
|
|
191
|
+
| Symptom | Cause |
|
|
192
|
+
|---|---|
|
|
193
|
+
| No table is created for an entity | The class was not passed to EntityModule.forFeature(). |
|
|
194
|
+
| Autoloading finds nothing | entitiesPath / seedsPath points at src/ instead of compiled dist/. |
|
|
195
|
+
| Duplicate seed rows after a restart | Record is missing one or more importKeyFields, so nothing matched. |
|
|
196
|
+
| Seed relation is empty or fails to resolve | Relation referenced by numeric id, or the target seed runs later. |
|
|
197
|
+
| Back reference can't insert | A reverse @OneToMany field is present in a seed record, possibly as []. |
|
|
198
|
+
| A renamed entity or field appears as a new one | No key option was set, so the rename was not recognized as a rename. |
|
|
199
|
+
| default value is rejected | A JavaScript value was passed instead of a SQL expression string. |
|
|
200
|
+
| A re-added field already contains old data | Same name means the same key, so the soft-deleted field was restored with its column. |
|
|
201
|
+
| A removed field is still present after a reload | The removal pass was skipped because the system user could not be resolved. |
|
|
202
|
+
| The table has columns such as title_1730457600000 | Leftovers from removed fields. Renaming is how the platform removes a column; nothing cleans them up. |
|
|
203
|
+
| A tabular part has no ownerId column and its tab stays empty | relationEntityName was set, but the owner @ManyToOne was not declared. |
|
|
204
|
+
| A tabular part is missing from the parent's tabs | type: 'tabular-part' or relationEntityName is missing, so it is listed under /related instead. |
|
|
205
|
+
| Duplicate tabular-part rows across different parents | owner is missing from importKeyFields, so the key is matched globally. |
|
|
206
|
+
|
|
207
|
+
## Requirements
|
|
208
|
+
|
|
209
|
+
Use the latest version of @suppa/sdk:
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
pnpm add @suppa/sdk@latest
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Entities rely on decorator metadata, so the application tsconfig.json must enable both decorator flags:
|
|
216
|
+
|
|
217
|
+
```json
|
|
218
|
+
{
|
|
219
|
+
"compilerOptions": {
|
|
220
|
+
"experimentalDecorators": true,
|
|
221
|
+
"emitDecoratorMetadata": true
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# Seed declaration — verbatim rules
|
|
2
|
+
|
|
3
|
+
Transcribed from docs.modern-expo.com/backend/application-developing/migrations.html (captured 2026-08-03). If an option is not in these tables, it does not exist — stop and ask.
|
|
4
|
+
|
|
5
|
+
## createSeed
|
|
6
|
+
|
|
7
|
+
A seed is created with createSeed(entityName, data, options?) and exported from a *.seed.ts file.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { createSeed } from '@suppa/sdk';
|
|
11
|
+
import { EntityNamesEnum } from '../../modules';
|
|
12
|
+
|
|
13
|
+
export const workflowsSeed = createSeed(EntityNamesEnum.Workflows, [
|
|
14
|
+
{
|
|
15
|
+
name: 'Default',
|
|
16
|
+
description: 'Default set of statuses',
|
|
17
|
+
private: false,
|
|
18
|
+
externalId: null,
|
|
19
|
+
},
|
|
20
|
+
]);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The first argument is the entity name as a string — the same value as `name` in `@Entity()`. Keeping those names in a local enum (`EntityNamesEnum`) avoids typos across seeds.
|
|
24
|
+
|
|
25
|
+
## Idempotency
|
|
26
|
+
|
|
27
|
+
Seeds run on every application start, for every tenant. They must therefore be idempotent: a seed either inserts a record or updates the existing one, but never duplicates it.
|
|
28
|
+
|
|
29
|
+
Matching is done on the target entity's importKeyFields. So:
|
|
30
|
+
|
|
31
|
+
- Always provide all importKeyFields in every record.
|
|
32
|
+
- If the natural key does not fit your data, override identity per record with `$key`.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
export const localizationsSeed = createSeed('Localizations', [
|
|
36
|
+
{
|
|
37
|
+
$key: { key: 'task-manager.greeting' },
|
|
38
|
+
key: 'task-manager.greeting',
|
|
39
|
+
en: 'Hello',
|
|
40
|
+
uk: 'Привіт',
|
|
41
|
+
},
|
|
42
|
+
]);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Referencing Relations
|
|
46
|
+
|
|
47
|
+
Reference a related record through the target's importKeyFields, as an object — never by numeric id:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { createSeed } from '@suppa/sdk';
|
|
51
|
+
import { StageStatusEnum } from '../entities';
|
|
52
|
+
import { EntityNamesEnum } from '../../modules';
|
|
53
|
+
|
|
54
|
+
export const stageWorkflowsSeed = createSeed(EntityNamesEnum.StageWorkflows, [
|
|
55
|
+
{
|
|
56
|
+
name: 'To Do',
|
|
57
|
+
color: '#3069FE',
|
|
58
|
+
order: 1000,
|
|
59
|
+
default: true,
|
|
60
|
+
status: {
|
|
61
|
+
name: 'StageWorkflows.status',
|
|
62
|
+
value: StageStatusEnum.Active,
|
|
63
|
+
},
|
|
64
|
+
workflow: { name: 'Default' },
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: 'In Progress',
|
|
68
|
+
color: '#47D1E2',
|
|
69
|
+
order: 2000,
|
|
70
|
+
status: {
|
|
71
|
+
name: 'StageWorkflows.status',
|
|
72
|
+
value: StageStatusEnum.InProgress,
|
|
73
|
+
},
|
|
74
|
+
workflow: { name: 'Default' },
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: 'Done',
|
|
78
|
+
color: '#75D900',
|
|
79
|
+
order: 5000,
|
|
80
|
+
status: {
|
|
81
|
+
name: 'StageWorkflows.status',
|
|
82
|
+
value: StageStatusEnum.Completed,
|
|
83
|
+
},
|
|
84
|
+
workflow: { name: 'Default' },
|
|
85
|
+
},
|
|
86
|
+
]);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Two reference shapes appear here:
|
|
90
|
+
|
|
91
|
+
| Field type | Reference shape |
|
|
92
|
+
|---|---|
|
|
93
|
+
| @ManyToOne | { <importKeyField>: value } — e.g. { name: 'Default' } |
|
|
94
|
+
| @Enum | { name: '<global enum name>', value: '<enum value>' } |
|
|
95
|
+
| @ManyToMany | An array of reference objects. |
|
|
96
|
+
|
|
97
|
+
Because `workflow: { name: 'Default' }` resolves by name, the seed works in every tenant regardless of what id the Default workflow received there.
|
|
98
|
+
|
|
99
|
+
> **WARNING**
|
|
100
|
+
> Seed order matters for relations. Register a seed after the seed that creates the records it points at — workflowsSeed before stageWorkflowsSeed.
|
|
101
|
+
|
|
102
|
+
## What Not to Put in a Seed
|
|
103
|
+
|
|
104
|
+
| Never include | Why |
|
|
105
|
+
|---|---|
|
|
106
|
+
| id, createdAt, updatedAt, deletedAt, createdBy, removedBy | System columns. They differ per tenant and corrupt matching. |
|
|
107
|
+
| $readAccess, $updateAccess, $instanceAccess | Access metadata, not data. |
|
|
108
|
+
| Reverse @OneToMany fields — even as [] | Back references cannot be inserted; the import throws. |
|
|
109
|
+
| Numeric relation ids | They do not match across tenants. Use import-key references. |
|
|
110
|
+
|
|
111
|
+
Seeding a tabular part's owner is the same @ManyToOne reference shape as any other relation — see [references/tabular-parts.md](tabular-parts.md) for the owner-referenced seed example.
|
|
112
|
+
|
|
113
|
+
> **TIP**
|
|
114
|
+
> When you build a seed from an exported record, strip those keys before committing. Seed a child entity with its own seed instead of nesting it in the parent.
|
|
115
|
+
|
|
116
|
+
## Keeping Seed Files Thin
|
|
117
|
+
|
|
118
|
+
When a record carries a large payload, move the data to a static file and import it:
|
|
119
|
+
|
|
120
|
+
`src/common/database/static/currencies.ts`
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
export const CURRENCIES = [
|
|
124
|
+
{ code: 'USD', symbol: '$' },
|
|
125
|
+
{ code: 'EUR', symbol: '€' },
|
|
126
|
+
];
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`src/common/database/seeds/currencies.seed.ts`
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { createSeed } from '@suppa/sdk';
|
|
133
|
+
import { CURRENCIES } from '../static/currencies';
|
|
134
|
+
|
|
135
|
+
export const currenciesSeed = createSeed('Currencies', CURRENCIES);
|
|
136
|
+
```
|