Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit ef984b3

Browse files
Update dev data insertion and deletion scripts to also insert and delete into elasticsearch
1 parent 6c2e4df commit ef984b3

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

scripts/constants.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* This module contains es resources configuration.
3+
* Identical to the one from ES processor, but updated to work with the api
4+
*/
5+
6+
const config = require('config')
7+
8+
const topResources = {
9+
achievementprovider: {
10+
index: config.get('ES.DOCUMENTS.achievementprovider.index'),
11+
type: config.get('ES.DOCUMENTS.achievementprovider.type')
12+
},
13+
attribute: {
14+
index: config.get('ES.DOCUMENTS.attribute.index'),
15+
type: config.get('ES.DOCUMENTS.attribute.type')
16+
},
17+
attributegroup: {
18+
index: config.get('ES.DOCUMENTS.attributegroup.index'),
19+
type: config.get('ES.DOCUMENTS.attributegroup.type')
20+
},
21+
organization: {
22+
index: config.get('ES.DOCUMENTS.organization.index'),
23+
type: config.get('ES.DOCUMENTS.organization.type')
24+
},
25+
role: {
26+
index: config.get('ES.DOCUMENTS.role.index'),
27+
type: config.get('ES.DOCUMENTS.role.type')
28+
},
29+
skill: {
30+
index: config.get('ES.DOCUMENTS.skill.index'),
31+
type: config.get('ES.DOCUMENTS.skill.type')
32+
},
33+
skillprovider: {
34+
index: config.get('ES.DOCUMENTS.skillprovider.index'),
35+
type: config.get('ES.DOCUMENTS.skillprovider.type')
36+
},
37+
user: {
38+
index: config.get('ES.DOCUMENTS.user.index'),
39+
type: config.get('ES.DOCUMENTS.user.type')
40+
}
41+
}
42+
43+
const userResources = {
44+
achievement: {
45+
propertyName: config.get('ES.DOCUMENTS.achievement.userField'),
46+
relateKey: 'achievementsProviderId'
47+
},
48+
externalprofile: {
49+
propertyName: config.get('ES.DOCUMENTS.externalprofile.userField'),
50+
relateKey: 'organizationId'
51+
},
52+
userattribute: {
53+
propertyName: config.get('ES.DOCUMENTS.userattribute.userField'),
54+
relateKey: 'attributeId'
55+
},
56+
userrole: {
57+
propertyName: config.get('ES.DOCUMENTS.userrole.userField'),
58+
relateKey: 'roleId'
59+
},
60+
userskill: {
61+
propertyName: config.get('ES.DOCUMENTS.userskill.userField'),
62+
relateKey: 'skillId'
63+
}
64+
}
65+
66+
const modelToESIndexMapping = {
67+
User: 'user',
68+
Role: 'role',
69+
SkillsProvider: 'skillprovider',
70+
Organization: 'organization',
71+
Skill: 'skill',
72+
UsersRole: 'userrole',
73+
UsersSkill: 'userskill',
74+
Achievement: 'achievement',
75+
ExternalProfile: 'externalprofile',
76+
AchievementsProvider: 'achievementprovider',
77+
AttributeGroup: 'attributegroup',
78+
Attribute: 'attribute',
79+
UserAttribute: 'userattribute'
80+
}
81+
82+
module.exports = {
83+
topResources, userResources, modelToESIndexMapping
84+
}
File renamed without changes.

scripts/db/dropAll.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,24 @@
33
*/
44
const models = require('../../src/models')
55
const logger = require('../../src/common/logger')
6+
const { topResources, modelToESIndexMapping } = require('../constants')
7+
const { getESClient } = require('../../src/common/es-client')
68

79
async function main () {
10+
const client = getESClient()
811
const keys = Object.keys(models)
912
for (let i = 0; i < keys.length; i++) {
1013
const key = keys[i]
1114
if (models[key].tableName) {
15+
const esResourceName = modelToESIndexMapping[key]
1216
try {
1317
await models.DBHelper.drop(models[key])
18+
19+
if (_.includes(_.keys(topResources), esResourceName)) {
20+
await client.indices.delete({
21+
index: topResources[esResourceName].index
22+
})
23+
}
1424
} catch (e) {
1525
console.error(e)
1626
logger.warn(`drop table ${key} failed`)

scripts/db/genData.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,77 @@
1+
const _ = require('lodash')
12
const models = require('../../src/models')
23
const logger = require('../../src/common/logger')
4+
const { getESClient } = require('../../src/common/es-client')
5+
const { topResources, userResources, modelToESIndexMapping } = require('../constants')
6+
7+
async function insertIntoES (modelName, body) {
8+
const esResourceName = modelToESIndexMapping[modelName]
9+
10+
if (!esResourceName) {
11+
logger.error(`Cannot insert data into model ${modelName}. No equivalent elasticsearch index found`)
12+
13+
return
14+
}
15+
16+
const client = getESClient()
17+
18+
if (_.includes(_.keys(topResources), esResourceName)) {
19+
await client.create({
20+
index: topResources[esResourceName].index,
21+
type: topResources[esResourceName].type,
22+
id: body.id,
23+
body,
24+
refresh: 'true'
25+
})
26+
} else if (_.includes(_.keys(userResources), esResourceName)) {
27+
const userResource = userResources[esResourceName]
28+
29+
const user = await client.getSource({
30+
index: topResources.user.index,
31+
type: topResources.user.type,
32+
id: body.userId
33+
})
34+
35+
const relateId = body[userResource.relateKey]
36+
37+
if (!user[userResource.propertyName]) {
38+
user[userResource.propertyName] = []
39+
}
40+
41+
if (_.some(user[userResource.propertyName], [userResource.relateKey, relateId])) {
42+
logger.error(`Can't create existing ${esResourceName} with the ${userResource.relateKey}: ${relateId}, userId: ${body.userId}`)
43+
} else {
44+
user[userResource.propertyName].push(body)
45+
await client.update({
46+
index: topResources.user.index,
47+
type: topResources.user.type,
48+
id: body.userId,
49+
body: { doc: user },
50+
refresh: 'true'
51+
})
52+
}
53+
}
54+
}
355

456
/**
557
* import test data
658
* @return {Promise<void>}
759
*/
860
async function main () {
961
await models.init()
10-
const keys = Object.keys(models)
62+
63+
let keys = Object.keys(models)
64+
keys = _.orderBy(keys, k => {
65+
const esResourceName = modelToESIndexMapping[k]
66+
67+
// Create parent data first
68+
if (_.includes(_.keys(topResources), esResourceName)) {
69+
return -1
70+
}
71+
72+
return 1
73+
})
74+
1175
for (let i = 0; i < keys.length; i++) {
1276
const key = keys[i]
1377
if (models[key].tableName) {
@@ -16,6 +80,7 @@ async function main () {
1680
await models.DBHelper.clear(models[key])
1781
for (let i = 0; i < data.length; i++) {
1882
await models.DBHelper.save(models[key], new models[key]().from(data[i]), true)
83+
await insertIntoES(key, data[i])
1984
}
2085
logger.info('import data for ' + key + ' done')
2186
} catch (e) {

0 commit comments

Comments
 (0)