Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Sử dụng Seeds & Factories trong AdonisJs/Node.js
- Bài viết này được thực hiện trên AdonisJs v4.1 | Ngày 06/01/2019
# Các bước thực hiện
- #1. Factory
- #2. Seed
# Factory
Đôi khi chúng ta cần dữ liệu giả để test nhanh trong database, nhưng lại không muốn nhập liệu thủ công mà các CSDL(SQL Server, MySQl, ...) hỗ trợ hay viết code thêm dữ liệu mất thời gian. Các đơn giản nhất là dùng dữ liệu giả Factory mà AdonisJs hỗ trợ.
Ví dụ: để tạo các bản ghi trong
App/Models/User
vàApp/Models/Post
thì code sẽ như sau:
Các field được định nghĩa trong bảng users như sau:
'use strict'
/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')
class UserSchema extends Schema {
up () {
this.create('users', (table) => {
table.increments()
table.integer('country_id').unsigned().notNullable()
table.string('username', 80).notNullable().unique()
table.string('email', 254).notNullable().unique()
table.string('password', 60).notNullable()
table.timestamps()
})
}
down () {
this.drop('users')
}
}
module.exports = UserSchema
Các field trong bảng users sẽ được dùng để return trong factory như: (country_id, username, email, password)
'use strict'
/*
|--------------------------------------------------------------------------
| Factory
|--------------------------------------------------------------------------
|
| Factories are used to define blueprints for database tables or Lucid
| models. Later you can use these blueprints to seed your database
| with dummy data.
|
*/
/** @type {import('@adonisjs/lucid/src/Factory')} */
const Factory = use('Factory')
/** @type {import('@adonisjs/framework/src/Hash')} */
const Hash = use('Hash')
/**
*
* @param {Object} faker - Instance of chance @ref http://chancejs.com/
*/
Factory.blueprint('App/Models/User', async (faker) => {
return {
country_id: 2,
username: faker.username(),
email: faker.email({
domain: 'gmail.com'
}),
password: await Hash.make(faker.password())
}
})
Factory.blueprint('App/Models/Post', async (faker) => {
return {
user_id: 2,
post_title: faker.sentence({
words: 5
}),
post_body: faker.word({
length: 5
})
}
})
@param {Object} faker - là instance của @class Chance.js, tham khảo thêm tại http://chancejs.com/
Chance là bộ tạo ngẫu nhiên: string, number, email, ...
# Seed
Lệnh tạo Seed như sau:
$ adonis make:seed User
Mở file UserSeeder trong database/seeds/UserSeeder.js
'use strict'
/*
|--------------------------------------------------------------------------
| UserSeeder
|--------------------------------------------------------------------------
|
| Make use of the Factory instance to seed database with dummy data or
| make use of Lucid models directly.
|
*/
/** @type {import('@adonisjs/lucid/src/Factory')} */
const Factory = use('Factory')
class UserSeeder {
async run() {
// using that blueprint to generate dummy data from factory.js
await Factory
.model('App/Models/User')
.createMany(5)
}
}
module.exports = UserSeeder
@method .createMany(5) sẽ toạ ngẫu nhiên 5 bản ghi trong bảng users. Để chạy nó, đơn giản là dùng lệnh:
$ adonis seed
Nếu bạn có nhiều file seed, mà chỉ muốn chạy một số file trong đó thì sử dụng:
$ adonis seed --files='UserSeeder.js, PostSeeder.js'
Cảm ơn anh em nhiều.
Tham khảo:
[1] https://www.adonisjs.com/docs/4.1/seeds-and-factories







