Skip to content

Commit

Permalink
refactor: deduplicate entity seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Dec 4, 2023
1 parent 8ced91b commit 74f8c39
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker.test.command.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

- name: Create entities
run: |
docker compose exec api /usr/src/app/create-entities.js 40
docker compose exec api /usr/src/app/seeder.js 40
- name: Run updater
run: |
Expand Down
26 changes: 6 additions & 20 deletions seeder/create-entities.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
#!/usr/bin/env node

const wbEdit = require('wikibase-edit')(require('./wikibase-edit.config'));

const numEntities = parseInt(process.argv[2], 10)
if (!Number.isFinite(numEntities)) {
throw new Error(`Expected numeric argument to be given, got ${process.argv[2]}`)
}

;(async function () {
module.exports = async function (numEntities) {
const result = []
for (let i = 1; i <= numEntities; i++) {
await wbEdit.entity.create({
const { entity } = await wbEdit.entity.create({
type: 'item',
labels: {
'en': new Date().toISOString()
Expand All @@ -18,15 +12,7 @@ if (!Number.isFinite(numEntities)) {
'en': new Date().toDateString() + new Date().toISOString()
}
})
result.push(entity)
}

return `Sucessfully created ${numEntities} entities`
})()
.then((result) => {
console.log(result)
})
.catch((err) => {
console.error("Failed to run command.")
console.error(err)
process.exit(1)
})
return result
}
21 changes: 21 additions & 0 deletions seeder/seeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

const createEntities = require('./create-entities');

const numEntities = parseInt(process.argv[2], 10)
if (!Number.isFinite(numEntities)) {
throw new Error(`Expected numeric argument to be given, got ${process.argv[2]}`)
}

;(async function () {
const result = await createEntities(numEntities)
return `Sucessfully created ${result.length} entities`
})()
.then((result) => {
console.log(result)
})
.catch((err) => {
console.error("Failed to run command.")
console.error(err)
process.exit(1)
})
21 changes: 3 additions & 18 deletions seeder/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const http = require('http');
const assert = require('assert');
const wbEdit = require( 'wikibase-edit' )( require( './wikibase-edit.config' ) );
const createEntities = require('./create-entities');

let batchId = 1;

Expand Down Expand Up @@ -50,26 +50,11 @@ http.createServer(function (req, res) {
return Promise.reject(err);
}
const numEntities = 20;
const entities = [];

for (let i = 1; i <= numEntities; ++i) {
const { entity } = await wbEdit.entity.create({
type: 'item',
labels: {
'en': new Date().toISOString()
},
descriptions: {
'en': new Date().toDateString() + new Date().toISOString()
}
});

console.log('created item id', entity.id)
entities.push(entity.id)
}
const entities = await createEntities(numEntities);

responseObject = {
'id': batchId++,
'entityIds': entities.join(','),
'entityIds': entities.map(e => e.id).join(','),
'wiki': {
'domain': process.env.API_WIKIBASE_DOMAIN,
'wiki_queryservice_namespace': {
Expand Down

0 comments on commit 74f8c39

Please sign in to comment.