Skip to content

Commit ffcd6ea

Browse files
committed
feat: add migration & seed
1 parent 32c131b commit ffcd6ea

File tree

6 files changed

+347
-0
lines changed

6 files changed

+347
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
import { DataTypes, QueryInterface } from 'sequelize'
4+
5+
/** @type {import('sequelize-cli').Migration} */
6+
module.exports = {
7+
async up(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
8+
await queryInterface.createTable('role', {
9+
id: {
10+
allowNull: false,
11+
primaryKey: true,
12+
type: Sequelize.UUID,
13+
defaultValue: Sequelize.UUIDV4,
14+
},
15+
created_at: {
16+
allowNull: false,
17+
type: Sequelize.DATE,
18+
},
19+
updated_at: {
20+
allowNull: false,
21+
type: Sequelize.DATE,
22+
},
23+
deleted_at: {
24+
type: Sequelize.DATE,
25+
},
26+
name: {
27+
allowNull: false,
28+
type: Sequelize.STRING,
29+
},
30+
})
31+
},
32+
33+
async down(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
34+
await queryInterface.dropTable('role')
35+
},
36+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict'
2+
3+
import { DataTypes, QueryInterface } from 'sequelize'
4+
5+
/** @type {import('sequelize-cli').Migration} */
6+
module.exports = {
7+
async up(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
8+
await queryInterface.createTable('user', {
9+
id: {
10+
allowNull: false,
11+
primaryKey: true,
12+
type: Sequelize.UUID,
13+
defaultValue: Sequelize.UUIDV4,
14+
},
15+
created_at: {
16+
allowNull: false,
17+
type: Sequelize.DATE,
18+
},
19+
updated_at: {
20+
allowNull: false,
21+
type: Sequelize.DATE,
22+
},
23+
deleted_at: {
24+
type: Sequelize.DATE,
25+
},
26+
fullname: {
27+
allowNull: false,
28+
type: Sequelize.STRING,
29+
},
30+
email: {
31+
allowNull: false,
32+
type: Sequelize.STRING,
33+
},
34+
password: {
35+
allowNull: false,
36+
type: Sequelize.STRING,
37+
},
38+
phone: {
39+
type: Sequelize.STRING('20'),
40+
},
41+
token_verify: {
42+
type: Sequelize.TEXT,
43+
},
44+
address: {
45+
type: Sequelize.TEXT,
46+
},
47+
is_active: {
48+
allowNull: false,
49+
defaultValue: false,
50+
type: Sequelize.BOOLEAN,
51+
},
52+
is_blocked: {
53+
allowNull: false,
54+
defaultValue: false,
55+
type: Sequelize.BOOLEAN,
56+
},
57+
role_id: {
58+
allowNull: false,
59+
type: Sequelize.UUID,
60+
defaultValue: Sequelize.UUIDV4,
61+
references: {
62+
model: 'role',
63+
key: 'id',
64+
},
65+
},
66+
upload_id: {
67+
allowNull: true,
68+
type: Sequelize.UUID,
69+
defaultValue: Sequelize.UUIDV4,
70+
references: {
71+
model: 'upload',
72+
key: 'id',
73+
},
74+
},
75+
})
76+
77+
await queryInterface.addConstraint('user', {
78+
type: 'unique',
79+
fields: ['email'],
80+
name: 'UNIQUE_USERS_EMAIL',
81+
})
82+
},
83+
84+
async down(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
85+
await queryInterface.dropTable('user')
86+
},
87+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict'
2+
3+
import { DataTypes, QueryInterface } from 'sequelize'
4+
5+
/** @type {import('sequelize-cli').Migration} */
6+
module.exports = {
7+
async up(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
8+
await queryInterface.createTable('upload', {
9+
id: {
10+
allowNull: false,
11+
primaryKey: true,
12+
type: Sequelize.UUID,
13+
defaultValue: Sequelize.UUIDV4,
14+
},
15+
created_at: {
16+
allowNull: false,
17+
type: Sequelize.DATE,
18+
},
19+
updated_at: {
20+
allowNull: false,
21+
type: Sequelize.DATE,
22+
},
23+
deleted_at: {
24+
type: Sequelize.DATE,
25+
},
26+
keyfile: {
27+
allowNull: false,
28+
type: Sequelize.STRING,
29+
},
30+
filename: {
31+
allowNull: false,
32+
type: Sequelize.STRING,
33+
},
34+
mimetype: {
35+
allowNull: false,
36+
type: Sequelize.STRING,
37+
},
38+
size: {
39+
allowNull: false,
40+
type: Sequelize.INTEGER,
41+
},
42+
signed_url: {
43+
allowNull: false,
44+
type: Sequelize.TEXT,
45+
},
46+
expiry_date_url: {
47+
allowNull: false,
48+
type: Sequelize.DATE,
49+
},
50+
})
51+
},
52+
53+
async down(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
54+
await queryInterface.dropTable('upload')
55+
},
56+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict'
2+
3+
import { DataTypes, QueryInterface } from 'sequelize'
4+
5+
/** @type {import('sequelize-cli').Migration} */
6+
module.exports = {
7+
async up(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
8+
await queryInterface.createTable('session', {
9+
id: {
10+
allowNull: false,
11+
primaryKey: true,
12+
type: Sequelize.UUID,
13+
defaultValue: Sequelize.UUIDV4,
14+
},
15+
created_at: {
16+
allowNull: false,
17+
type: Sequelize.DATE,
18+
},
19+
updated_at: {
20+
allowNull: false,
21+
type: Sequelize.DATE,
22+
},
23+
user_id: {
24+
allowNull: false,
25+
type: Sequelize.UUID,
26+
defaultValue: Sequelize.UUIDV4,
27+
references: {
28+
model: 'user',
29+
key: 'id',
30+
},
31+
},
32+
token: {
33+
allowNull: false,
34+
type: Sequelize.TEXT,
35+
},
36+
ip_address: {
37+
type: Sequelize.STRING,
38+
},
39+
device: {
40+
type: Sequelize.STRING,
41+
},
42+
platform: {
43+
type: Sequelize.STRING,
44+
},
45+
user_agent: {
46+
type: Sequelize.STRING,
47+
},
48+
latitude: {
49+
type: Sequelize.STRING,
50+
},
51+
longitude: {
52+
type: Sequelize.STRING,
53+
},
54+
})
55+
},
56+
57+
async down(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
58+
await queryInterface.dropTable('session')
59+
},
60+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
import { isEmpty } from 'lodash'
4+
import { DataTypes, QueryInterface } from 'sequelize'
5+
import { ConstRole } from '~/lib/constant/seed/role'
6+
7+
const data = [
8+
{
9+
id: ConstRole.ID_SUPER_ADMIN,
10+
name: 'Super Admin',
11+
},
12+
{
13+
id: ConstRole.ID_ADMIN,
14+
name: 'Admin',
15+
},
16+
{
17+
id: ConstRole.ID_USER,
18+
name: 'User',
19+
},
20+
]
21+
22+
/** @type {import('sequelize-cli').Migration} */
23+
module.exports = {
24+
async up(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
25+
const formData: any[] = []
26+
27+
if (!isEmpty(data)) {
28+
for (let i = 0; i < data.length; i += 1) {
29+
const item = data[i]
30+
31+
formData.push({
32+
...item,
33+
created_at: new Date(),
34+
updated_at: new Date(),
35+
})
36+
}
37+
}
38+
39+
await queryInterface.bulkInsert('role', formData)
40+
},
41+
42+
async down(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
43+
await queryInterface.bulkDelete('role', {})
44+
},
45+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
import { green } from 'colorette'
4+
import _ from 'lodash'
5+
import { DataTypes, QueryInterface } from 'sequelize'
6+
import { v4 as uuidv4 } from 'uuid'
7+
import { env } from '~/config/env'
8+
import Hashing from '~/config/hashing'
9+
import { logger } from '~/config/logger'
10+
import { ConstRole } from '~/lib/constant/seed/role'
11+
12+
const hashing = new Hashing()
13+
14+
const defaultPassword = env.APP_DEFAULT_PASS
15+
logger.info(`Seed - your default password: ${green(defaultPassword)}`)
16+
17+
const data = [
18+
{
19+
fullname: 'Super Admin',
20+
email: 'super.admin@example.com',
21+
role_id: ConstRole.ID_SUPER_ADMIN,
22+
},
23+
{
24+
fullname: 'Admin',
25+
email: 'admin@example.com',
26+
role_id: ConstRole.ID_ADMIN,
27+
},
28+
{
29+
fullname: 'User',
30+
email: 'user@example.com',
31+
role_id: ConstRole.ID_USER,
32+
},
33+
]
34+
35+
/** @type {import('sequelize-cli').Migration} */
36+
module.exports = {
37+
async up(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
38+
const password = await hashing.hash(defaultPassword)
39+
40+
const formData: any[] = []
41+
42+
if (!_.isEmpty(data)) {
43+
for (let i = 0; i < data.length; i += 1) {
44+
const item = data[i]
45+
46+
formData.push({
47+
...item,
48+
id: uuidv4(),
49+
is_active: true,
50+
password,
51+
created_at: new Date(),
52+
updated_at: new Date(),
53+
})
54+
}
55+
}
56+
57+
await queryInterface.bulkInsert('user', formData)
58+
},
59+
60+
async down(queryInterface: QueryInterface, Sequelize: typeof DataTypes) {
61+
await queryInterface.bulkDelete('user', {})
62+
},
63+
}

0 commit comments

Comments
 (0)