Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"db:rollback": "knex migrate:rollback",
"db:generate-schema": "docker-compose run schema-dump",
"db:export-checks": "node scripts/export-checks.js",
"db:export-checklists": "node scripts/export-checklists.js",
"db:seed": "knex seed:run"
},
"keywords": [],
Expand Down
53 changes: 53 additions & 0 deletions scripts/export-checklists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { writeFileSync } = require('fs')
const { getConfig } = require('../src/config')
const { dbSettings } = getConfig()
const knex = require('knex')(dbSettings)
const { join } = require('path');

(async () => {
try {
const query = `
SELECT
cl.id,
cl.author,
cl.title,
cl.description,
cl.code_name,
cl.url,
cl.created_at,
cl.updated_at,
COALESCE(json_agg(
json_build_object(
'id', ci.id,
'code_name', cc.code_name,
'title', cc.title,
'description', cc.description,
'priority_group', COALESCE(ci.priority_group, cc.default_priority_group),
'section_number', COALESCE(ci.section_number, cc.default_section_number),
'section_name', COALESCE(ci.section_name, cc.default_section_name),
'details_url', cc.details_url,
'created_at', cc.created_at,
'updated_at', cc.updated_at
)
) FILTER (WHERE ci.id IS NOT NULL), '[]') AS checks
FROM public.compliance_checklists cl
LEFT JOIN public.checklist_items ci ON cl.id = ci.checklist_id
LEFT JOIN public.compliance_checks cc ON ci.compliance_check_id = cc.id
GROUP BY cl.id
ORDER BY cl.id;
`

// Execute the raw query
const result = await knex.raw(query)

// Write the result to a JSON file
const outputPath = join(process.cwd(), 'output', 'checklists.json')
writeFileSync(outputPath, JSON.stringify(result.rows, null, 2))

console.log(`Data exported to ${outputPath}`)
} catch (error) {
console.error('Error exporting data:', error.message)
} finally {
await knex.destroy()
}
})()
Loading