Skip to content

Commit

Permalink
fix: improve seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
masb0ymas committed Sep 17, 2023
1 parent 637cc00 commit 42a64b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
30 changes: 14 additions & 16 deletions src/database/seeders/20220814112942-RoleSeeder.ts
Original file line number Diff line number Diff line change
@@ -1,5 1,3 @@
'use strict'

import { isEmpty } from 'lodash'
import { DataTypes, QueryInterface } from 'sequelize'
import { default as ConstRole } from '~/core/constants/ConstRole'
Expand All @@ -19,24 17,24 @@ const data = [
},
]

const formData: any[] = []

if (!isEmpty(data)) {
for (let i = 0; i < data.length; i = 1) {
const item = data[i]

formData.push({
...item,
created_at: new Date(),
updated_at: new Date(),
})
}
}

export async function up(
queryInterface: QueryInterface,
Sequelize: typeof DataTypes
) {
const formData: any[] = []

if (!isEmpty(data)) {
for (let i = 0; i < data.length; i = 1) {
const item = data[i]

formData.push({
...item,
created_at: new Date(),
updated_at: new Date(),
})
}
}

await queryInterface.bulkInsert('role', formData)
}

Expand Down
41 changes: 20 additions & 21 deletions src/database/seeders/20220814113111-UserSeeder.ts
Original file line number Diff line number Diff line change
@@ -1,14 1,11 @@
'use strict'

import { hashSync } from 'bcrypt'
import { green } from 'colorette'
import _ from 'lodash'
import { DataTypes, QueryInterface } from 'sequelize'
import { v4 as uuidv4 } from 'uuid'
import { hashing } from '~/config/hashing'
import { logger } from '~/config/pino'
import { default as ConstRole } from '~/core/constants/ConstRole'

const salt = 10
const defaultPassword = 'Padang123'

logger.info(`Seed - your default password: ${green(defaultPassword)}`)
Expand All @@ -31,27 28,29 @@ const data = [
},
]

const formData: any[] = []

if (!_.isEmpty(data)) {
for (let i = 0; i < data.length; i = 1) {
const item = data[i]

formData.push({
...item,
id: uuidv4(),
is_active: true,
password: hashSync(defaultPassword, salt),
created_at: new Date(),
updated_at: new Date(),
})
}
}

export async function up(
queryInterface: QueryInterface,
Sequelize: typeof DataTypes
) {
const password = await hashing.hash(defaultPassword)

const formData: any[] = []

if (!_.isEmpty(data)) {
for (let i = 0; i < data.length; i = 1) {
const item = data[i]

formData.push({
...item,
id: uuidv4(),
is_active: true,
password,
created_at: new Date(),
updated_at: new Date(),
})
}
}

await queryInterface.bulkInsert('user', formData)
}

Expand Down

0 comments on commit 42a64b5

Please sign in to comment.