Skip to content

Commit 66ad717

Browse files
committed
creating keydata table
1 parent b0685cc commit 66ad717

File tree

6 files changed

+187
-3
lines changed

6 files changed

+187
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ scripts/my_username
1313
backups/*
1414
# Sentry Auth Token
1515
.env.sentry-build-plugin
16-
.idea/
16+
.idea/
17+
data.xlsx

package-lock.json

Lines changed: 105 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080
"vite-plugin-socket-io": "^1.0.2",
8181
"vite-plugin-svgr": "^4.2.0",
8282
"winston": "^3.3.3",
83-
"winston-gelf-transporter": "^1.0.2"
83+
"winston-gelf-transporter": "^1.0.2",
84+
"xlsx": "^0.18.5"
8485
},
8586
"devDependencies": {
8687
"@types/compression": "^1.7.5",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { INTEGER, JSONB } from 'sequelize'
2+
3+
export const up = ({ context: queryInterface }) => {
4+
return queryInterface.createTable('key_data', {
5+
id: {
6+
allowNull: false,
7+
autoIncrement: true,
8+
primaryKey: true,
9+
type: INTEGER,
10+
},
11+
data: {
12+
type: JSONB,
13+
},
14+
})
15+
}
16+
17+
export const down = ({ context: queryInterface }) => {
18+
return queryInterface.dropTable('key_data')
19+
}

server/models/keyData.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { InferAttributes, InferCreationAttributes, Model, CreationOptional, INTEGER, JSONB } from 'sequelize'
2+
import { sequelize } from '../database/connection.js'
3+
4+
class KeyData extends Model<InferAttributes<KeyData>, InferCreationAttributes<KeyData>> {
5+
declare id: CreationOptional<number>
6+
declare data: {}
7+
}
8+
9+
KeyData.init(
10+
{
11+
id: {
12+
type: INTEGER,
13+
autoIncrement: true,
14+
primaryKey: true,
15+
},
16+
data: {
17+
type: JSONB,
18+
allowNull: false,
19+
},
20+
},
21+
{
22+
sequelize,
23+
tableName: 'key_data',
24+
underscored: true,
25+
}
26+
)
27+
28+
export default KeyData

server/scripts/seed.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/* eslint-disable no-restricted-syntax */
22
/* eslint-disable no-await-in-loop */
3+
import xlsx from 'xlsx'
4+
import path from 'path'
35
import db from '../models/index.js'
6+
import KeyData from '../models/keyData.js'
47

58
import logger from '../util/logger.js'
69
import { data, facultyMap } from '../../config/data.js'
@@ -73,9 +76,37 @@ const seedFacultiesAndStudyprogrammes = async () => {
7376
}
7477
}
7578

79+
// for this you have to have a data.xlsx file in the root of the project
80+
const seedKeyData = async () => {
81+
const __dirname = path.dirname(new URL(import.meta.url).pathname)
82+
const workbook = xlsx.readFile(path.resolve(__dirname, '../../data.xlsx'))
83+
84+
const jsonSheet = {}
85+
86+
workbook.SheetNames.forEach((sheetName) => {
87+
const worksheet = workbook.Sheets[sheetName]
88+
const data = xlsx.utils.sheet_to_json(worksheet)
89+
console.log(JSON.stringify(data, null, 2))
90+
jsonSheet[sheetName] = data
91+
})
92+
93+
// logger.info(JSON.stringify(jsonSheet, null, 2))
94+
95+
await KeyData.destroy({ where: {} })
96+
97+
try {
98+
await KeyData.create({
99+
data: jsonSheet,
100+
})
101+
} catch (error) {
102+
logger.error(error)
103+
}
104+
}
105+
76106
const seed = async () => {
77107
logger.info('Seeding ...')
78108
await seedFacultiesAndStudyprogrammes()
109+
await seedKeyData()
79110
logger.info('Seeding completed')
80111
}
81112

0 commit comments

Comments
 (0)