or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

cache.mddatabase.mddebug.mdentities.mdhelper-api.mdindex.mdmigrations.mdschema.mdseeders.md

entities.mddocs/

0

# Entity Commands

1

2

MikroORM CLI entity management commands for generating TypeScript entity classes from existing database schemas. This reverse-engineering functionality helps migrate existing databases to MikroORM.

3

4

## Capabilities

5

6

### Generate Entities

7

8

Generates TypeScript entity classes based on the current database schema. Supports both file output and console display for review.

9

10

```typescript { .api }

11

/**

12

* Generate entities from database schema command

13

*/

14

command: "generate-entities"

15

16

interface GenerateEntitiesOptions extends BaseArgs {

17

save?: boolean; // Save entities to filesystem (--save / -s)

18

dump?: boolean; // Output entities to console (--dump / -d)

19

path?: string; // Directory path where to save entities (--path / -p)

20

schema?: string; // Generate entities only for specific schema (--schema)

21

}

22

```

23

24

**Usage Examples:**

25

26

```bash

27

# Generate and display entities in console

28

mikro-orm generate-entities --dump

29

30

# Generate and save entities to filesystem

31

mikro-orm generate-entities --save

32

33

# Save entities to specific directory

34

mikro-orm generate-entities --save --path ./src/entities

35

36

# Generate entities for specific schema only

37

mikro-orm generate-entities --save --schema public

38

39

# Generate with custom configuration

40

mikro-orm generate-entities --dump --config ./orm.config.js

41

```

42

43

## Command Options

44

45

### Required Options

46

47

At least one of these options must be provided:

48

- `--save` / `-s`: Save generated entities to filesystem

49

- `--dump` / `-d`: Display generated entities in console

50

51

### Optional Options

52

53

- `--path` / `-p`: Set directory path where entities will be saved (used with --save)

54

- `--schema`: Generate entities only for the specified database schema

55

- `--config`: Path to ORM configuration file(s)

56

- `--contextName` / `--context`: Configuration context name

57

58

## Generated Entity Features

59

60

The entity generator creates TypeScript classes with:

61

62

- **Entity Decorators**: Proper `@Entity()` decorators with table names

63

- **Property Decorators**: Column mappings with `@Property()`, `@PrimaryKey()`, etc.

64

- **Type Definitions**: Correct TypeScript types based on database column types

65

- **Relationships**: Foreign key relationships as entity references

66

- **Indexes**: Database indexes represented as entity metadata

67

- **Constraints**: Unique constraints and other database constraints

68

69

## Error Handling

70

71

### Common Errors

72

73

- **Missing required options**: Command fails if neither `--save` nor `--dump` is provided

74

- **Database connection errors**: Cannot generate entities without valid database connection

75

- **Schema not found**: Specified schema must exist in the database

76

- **Permission errors**: File system write permissions required when using `--save`

77

- **Path errors**: Target directory must exist or be creatable when using `--path`

78

79

### Entity Generation Issues

80

81

- **Unsupported column types**: Some database-specific types may not map perfectly to TypeScript

82

- **Complex relationships**: Circular dependencies may require manual adjustment

83

- **Naming conflicts**: Generated class names may conflict with TypeScript reserved words

84

- **Case sensitivity**: Database naming conventions may require adjustment for TypeScript

85

86

## Database Support

87

88

Entity generation works with:

89

- **PostgreSQL**: Full support including schemas, custom types, and complex relationships

90

- **MySQL/MariaDB**: Complete support with proper type mapping

91

- **SQLite**: Basic support with standard SQL types

92

- **MongoDB**: Not applicable (document-based, not schema-based)

93

94

## Generated Code Example

95

96

Example generated entity:

97

98

```typescript

99

import { Entity, PrimaryKey, Property, ManyToOne } from '@mikro-orm/core';

100

import { User } from './User';

101

102

@Entity()

103

export class Post {

104

105

@PrimaryKey()

106

id!: number;

107

108

@Property()

109

title!: string;

110

111

@Property({ type: 'text' })

112

content!: string;

113

114

@Property()

115

createdAt: Date = new Date();

116

117

@ManyToOne(() => User)

118

author!: User;

119

120

}

121

```

122

123

## Configuration

124

125

Entity generation uses these configuration options:

126

127

```typescript { .api }

128

interface EntityGeneratorConfiguration {

129

discovery?: {

130

warnWhenNoEntities?: boolean; // Suppress entity warnings during generation

131

};

132

baseDir?: string; // Base directory for resolving paths

133

entities?: string[]; // Entity paths (not used during generation)

134

entitiesTs?: string[]; // TypeScript entity paths (not used during generation)

135

}

136

```