Skip to content

Instantly share code, notes, and snippets.

@rococodogs
Last active September 19, 2017 18:51
Show Gist options
  • Save rococodogs/02faada16a37805ae7281692a8bfbd82 to your computer and use it in GitHub Desktop.
Save rococodogs/02faada16a37805ae7281692a8bfbd82 to your computer and use it in GitHub Desktop.
{
"user": "metadb",
"host": "localhost",
"database": "metadb",
"password": ""
}
const fs = require('fs')
const path = require('path')
const { Client } = require('pg')
const config = require('./config.json')
const client = new Client(config)
const OUTPUT_DIR = path.join(__dirname, 'authorities')
client.connect()
const query = "SELECT * FROM controlled_vocab WHERE vocab_name != '' AND contents != ''"
client.query(query, (err, res) => {
client.end()
// get all of the vocabularies w/ values
const vocabs = res.rows
for (let i = 0; i < vocabs.length; i++) {
vocab = vocabs[i]
const name = vocab['vocab_name'].toLowerCase().replace(/\./g, '_')
let terms = vocab['contents'].split(';')
if (name.indexOf('subject_ocm') > -1) {
terms = terms.sort((a, b) => {
const anum = parseOcmNumber(a)
const bnum = parseOcmNumber(b)
return anum < bnum ? -1 : 1
})
}
const body = terms.reduce((str, term) => {
let t = term
if (/[#|:]/g.test(t) && t.indexOf('"')) {
t = '"' + t + '"'
}
str += ' - ' + t + '\n'
return str
}, '')
const outputPath = path.join(OUTPUT_DIR, name + '.yml')
fs.writeFileSync(outputPath, '# ' + vocab['vocab_name'] + '\nterms:\n' + body)
}
})
function parseOcmNumber (str) {
const match = str.match(/^\d+/g)
if (!match) {
return str
}
return parseInt(match[0], 10)
}
{
"name": "metadb-authorities-to-hyrax-yaml",
"version": "0.0.0",
"description": "",
"main": "convert.js",
"keywords": [],
"author": "Adam Malantonio <[email protected]>",
"license": "GPL-3.0",
"dependencies": {
"pg": "^7.3.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment