Skip to main content

Change the data model

Marblism generates all the data models for your entities you need. But as your app evolves, you may need to add more attributes or more entities to your app.

Change the attributes of an entity

Let's say we want to add a new string hashtag attribute on our Tweet entity.

1. Change the API model

/server/src/modules/tweet/domain/tweet.model.ts

@Entity()
export class Tweet {
@PrimaryGeneratedColumn('uuid')
id: string

@Column({})
content: string

@Column({})
userId: string

@Column({})
hashtag: string

@ManyToOne(() => User, parent => parent.tweets)
user?: User

@OneToMany(() => Comment, child => child.tweet)
comments?: Comment[]
}
info

TypeORM automatically takes the types of each attribute to create the database tables. To check all the supported types, read this.

When you make a change to the API data model, the database is automatically updated on the dev environment. To update the database on your production read this.

2. Change the DTOs

Add the newly created field to the DTO (if it's a field needed to create or update the entity)

/server/src/modules/tweet/application/tweet.dto.ts

export class TweetCreateDto {
@IsString()
@IsNotEmpty() // to make it mandatory when you create a new tweet
hashtag: string
}

3. Change the client model

/web/src/domain/tweet/tweet.model.ts

export class Tweet {
id: string

content: string

userId: string

hashtag: string

user?: User

comments?: Comment[]
}

Add a new entity

Let's say we want to add a new entity like to record each like on the tweets.

1. Copy-paste a module in the API

In api/src/modules, take any simple module and copy paste to create a new module called 'like'.

Rename all the files and all the code inside the file, update the model.

2. Import the new module and domain

In api/src/modules/app.application.modules.ts, import the new module.

@Module({
imports: [
LikeApplicationModule,
],
controllers: [],
providers: [],
})

In api/src/modules/app.domain.modules.ts, import the new module.

@Module({
imports: [
LikeDomainModule,
],
controllers: [],
providers: [],
})

3. Copy-paste a domain in the Client

In api/src/domain, take any simple domain and copy paste to create a new domain called 'like'.

Rename all the files and all the code inside the file, update the model, put the correct API routes pointing to the back-end.