34 lines
975 B
JavaScript
34 lines
975 B
JavaScript
import { pool } from '../pool.js';
|
|
|
|
export async function create(from_type, from_id, to_type, to_id, relation = 'attached') {
|
|
const { rows: [r] } = await pool.query(
|
|
`INSERT INTO entity_links(from_type, from_id, to_type, to_id, relation)
|
|
VALUES($1,$2,$3,$4,$5)
|
|
ON CONFLICT (from_type, from_id, to_type, to_id, relation)
|
|
DO UPDATE SET relation=EXCLUDED.relation
|
|
RETURNING *`,
|
|
[from_type, from_id, to_type, to_id, relation]
|
|
);
|
|
return r;
|
|
}
|
|
|
|
export async function listFrom(from_type, from_id) {
|
|
const { rows } = await pool.query(
|
|
`SELECT * FROM entity_links WHERE from_type=$1 AND from_id=$2`,
|
|
[from_type, from_id]
|
|
);
|
|
return rows;
|
|
}
|
|
|
|
export async function listTo(to_type, to_id) {
|
|
const { rows } = await pool.query(
|
|
`SELECT * FROM entity_links WHERE to_type=$1 AND to_id=$2`,
|
|
[to_type, to_id]
|
|
);
|
|
return rows;
|
|
}
|
|
|
|
export async function remove(id) {
|
|
await pool.query(`DELETE FROM entity_links WHERE id=$1`, [id]);
|
|
}
|