Replies: 3 comments 4 replies
-
Hi @kkkasio, you discover the solution? I have same problem... |
Beta Was this translation helpful? Give feedback.
-
Here's my solution: First I create an enum with all possible values: // ./contracts/enums.ts
export enum PostStatus {
DRAFT = 'draft',
PUBLIC = 'public',
PRIVATE = 'private',
DELETED = 'deleted',
} Next, I import the enum into migration file and use // ./database/migrations/1601605294173_create_posts_table.ts
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
import { PostStatus } from 'Contracts/enums'
export default class extends BaseSchema {
protected tableName = 'posts'
public async up () {
this.schema.createTable(this.tableName, (table) => {
table.increments('id')
table.enum('status', Object.values(PostStatus))
.defaultTo(PostStatus.DRAFT)
.notNullable()
// ...
})
}
public async down () {
this.schema.dropTable(this.tableName)
}
} Then, I import the enum into model file and use it to typing the column: // ./app/Models/Post.ts
import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'
import { PostStatus } from 'Contracts/enums'
export default class Post extends BaseModel {
@column({ isPrimary: true })
public id: number
@column()
public status: PostStatus
// ...
} Now, you only need to update the list in one place. It's more organized, for me. The only "downside" is that u will have to use this enum each time u create a post, update "status" field or write a query. // ./app/Controllers/Http/PostsController.ts
import Post from 'App/Models/Post'
import { PostStatus } from 'Contracts/enums'
export default class PostsController {
public async index() {
const posts = await Post.query()
.where('status', PostStatus.PUBLIC)
return posts
}
public async create() {
const post = await Post.create({
title: 'lorem ipsum dolor...',
status: PostStatus.PRIVATE,
})
return post
}
public async update({ params }) {
const post = await Post.findOrFail(params.id)
post.status = PostStatus.PUBLIC
await post.save()
return post
}
} |
Beta Was this translation helpful? Give feedback.
-
Hey! 👋 You can use String Union or a TypeScript Enum. Via String Union: @column()
public type: 'Rodada' | 'Mensal' // ... Via Enum: enum LeagueType {
Rodada = 'Rodada',
Mensal = 'Mensal',
// ...
}
//
@column()
public type: LeagueType |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello dear friends!
I have a question when defining the columns within my model
In my database I want to have an enum type field that contains some values.
When I am going to map this to my model, I know that it should logically pass as a "listing" so she can know what value to accept (as if she were typing a list)
my question is if i just pass string and keep happy, or if there really is a possibility to type the values.
there is nothing said in the Columns documentation on the subject.
https://preview.adonisjs.com/guides/models/columns#defining-columns
Thanks so much :)
Beta Was this translation helpful? Give feedback.
All reactions