You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/techniques/sql.md
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -358,6 +358,63 @@ With that option specified, every entity registered through the `forFeature()` m
358
358
359
359
> warning **Warning** Note that entities that aren't registered through the `forFeature()` method, but are only referenced from the entity (via a relationship), won't be included by way of the `autoLoadEntities` setting.
360
360
361
+
#### Separating entity definition
362
+
363
+
You can define an entity and its columns right in the model, using decorators. But some people prefer to define entities and their columns inside separate files using the ["entity schemas"](https://typeorm.io/#/separating-entity-definition).
364
+
365
+
```typescript
366
+
import { EntitySchema } from'typeorm';
367
+
import { User } from'./user.entity';
368
+
369
+
exportconst UserSchema =newEntitySchema<User>({
370
+
name: 'User',
371
+
target: User,
372
+
columns: {
373
+
id: {
374
+
type: Number,
375
+
primary: true,
376
+
generated: true,
377
+
},
378
+
firstName: {
379
+
type: String,
380
+
},
381
+
lastName: {
382
+
type: String,
383
+
},
384
+
isActive: {
385
+
type: Boolean,
386
+
default: true,
387
+
},
388
+
},
389
+
relations: {
390
+
photos: {
391
+
type: 'one-to-many',
392
+
target: 'Photo', // the name of the PhotoSchema
393
+
},
394
+
},
395
+
});
396
+
```
397
+
398
+
> warning error **Warning** If you provide the `target` option, the `name` option value has to be the same as the name of the target class.
399
+
> If you do not provide the `target` you can use any name.
400
+
401
+
Nest allows you to use an `EntitySchema` instance wherever an `Entity` is expected, for example:
A database transaction symbolizes a unit of work performed within a database management system against a database, and treated in a coherent and reliable way independent of other transactions. A transaction generally represents any change in a database ([learn more](https://en.wikipedia.org/wiki/Database_transaction)).
0 commit comments