- Add database/queries/{sources,rules,mappings,records}.sql — one file per
route, all business logic in PostgreSQL functions
- Replace parameterized queries in all four route files with lit()/jsonLit()
literal interpolation for debuggability
- Add api/lib/sql.js with lit(), jsonLit(), arr() helpers
- Fix get_view_data to use json_agg (preserves column order) with subquery
(guarantees sort order is respected before aggregation)
- Fix jsonLit() for JSONB params so plain strings become valid JSON
- Update manage.py option 3 to deploy database/queries/ instead of functions.sql
- Add SPEC.md covering architecture, philosophy, and manage.py
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
75 lines
2.5 KiB
JavaScript
75 lines
2.5 KiB
JavaScript
/**
|
|
* Records Routes
|
|
* Query and manage imported records
|
|
*/
|
|
|
|
const express = require('express');
|
|
const { lit } = require('../lib/sql');
|
|
|
|
module.exports = (pool) => {
|
|
const router = express.Router();
|
|
|
|
// List records for a source
|
|
router.get('/source/:source_name', async (req, res, next) => {
|
|
try {
|
|
const { limit = 100, offset = 0, transformed_only } = req.query;
|
|
const result = await pool.query(
|
|
`SELECT * FROM list_records(${lit(req.params.source_name)}, ${lit(parseInt(limit))}, ${lit(parseInt(offset))}, ${lit(transformed_only === 'true')})`
|
|
);
|
|
res.json(result.rows);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
// Get single record
|
|
router.get('/:id', async (req, res, next) => {
|
|
try {
|
|
const result = await pool.query(`SELECT * FROM get_record(${lit(parseInt(req.params.id))})`);
|
|
if (result.rows.length === 0) return res.status(404).json({ error: 'Record not found' });
|
|
res.json(result.rows[0]);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
// Search records
|
|
router.post('/search', async (req, res, next) => {
|
|
try {
|
|
const { source_name, query, limit = 100 } = req.body;
|
|
if (!source_name || !query) {
|
|
return res.status(400).json({ error: 'Missing required fields: source_name, query' });
|
|
}
|
|
const result = await pool.query(
|
|
`SELECT * FROM search_records(${lit(source_name)}, ${lit(query)}, ${lit(parseInt(limit))})`
|
|
);
|
|
res.json(result.rows);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
// Delete record
|
|
router.delete('/:id', async (req, res, next) => {
|
|
try {
|
|
const result = await pool.query(`SELECT * FROM delete_record(${lit(parseInt(req.params.id))})`);
|
|
if (result.rows.length === 0) return res.status(404).json({ error: 'Record not found' });
|
|
res.json({ success: true, deleted: result.rows[0].id });
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
// Delete all records for a source
|
|
router.delete('/source/:source_name/all', async (req, res, next) => {
|
|
try {
|
|
const result = await pool.query(`SELECT * FROM delete_source_records(${lit(req.params.source_name)})`);
|
|
res.json({ success: true, deleted_count: result.rows[0].deleted_count });
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
return router;
|
|
};
|