feat(repos): tags, polymorphic entity_links, attachments
This commit is contained in:
27
lib/db/repos/attachments.js
Normal file
27
lib/db/repos/attachments.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { pool } from '../pool.js';
|
||||
|
||||
export async function create({ entity_type, entity_id, filename, mime_type, size_bytes, blob_path, checksum }) {
|
||||
const { rows: [r] } = await pool.query(
|
||||
`INSERT INTO attachments(entity_type, entity_id, filename, mime_type, size_bytes, blob_path, checksum)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7) RETURNING *`,
|
||||
[entity_type, entity_id, filename, mime_type || null, size_bytes || null, blob_path, checksum || null]
|
||||
);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function listForEntity(entity_type, entity_id) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT * FROM attachments WHERE entity_type=$1 AND entity_id=$2 ORDER BY uploaded_at DESC`,
|
||||
[entity_type, entity_id]
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
|
||||
export async function getById(id) {
|
||||
const { rows: [r] } = await pool.query(`SELECT * FROM attachments WHERE id=$1`, [id]);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function del(id) {
|
||||
await pool.query(`DELETE FROM attachments WHERE id=$1`, [id]);
|
||||
}
|
||||
33
lib/db/repos/links.js
Normal file
33
lib/db/repos/links.js
Normal file
@@ -0,0 +1,33 @@
|
||||
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]);
|
||||
}
|
||||
43
lib/db/repos/tags.js
Normal file
43
lib/db/repos/tags.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import { pool } from '../pool.js';
|
||||
|
||||
export async function upsert(name, { description, color } = {}) {
|
||||
const { rows: [r] } = await pool.query(
|
||||
`INSERT INTO tags(name, description, color)
|
||||
VALUES($1,$2,$3)
|
||||
ON CONFLICT(name) DO UPDATE SET
|
||||
description=COALESCE(EXCLUDED.description, tags.description),
|
||||
color=COALESCE(EXCLUDED.color, tags.color)
|
||||
RETURNING *`,
|
||||
[name, description || null, color || null]
|
||||
);
|
||||
return r;
|
||||
}
|
||||
|
||||
export async function list() {
|
||||
const { rows } = await pool.query(`SELECT * FROM tags ORDER BY name`);
|
||||
return rows;
|
||||
}
|
||||
|
||||
export async function attach(entity_type, entity_id, tag_id) {
|
||||
await pool.query(
|
||||
`INSERT INTO entity_tags(entity_type, entity_id, tag_id)
|
||||
VALUES($1,$2,$3) ON CONFLICT DO NOTHING`,
|
||||
[entity_type, entity_id, tag_id]
|
||||
);
|
||||
}
|
||||
|
||||
export async function detach(entity_type, entity_id, tag_id) {
|
||||
await pool.query(
|
||||
`DELETE FROM entity_tags WHERE entity_type=$1 AND entity_id=$2 AND tag_id=$3`,
|
||||
[entity_type, entity_id, tag_id]
|
||||
);
|
||||
}
|
||||
|
||||
export async function listForEntity(entity_type, entity_id) {
|
||||
const { rows } = await pool.query(
|
||||
`SELECT t.* FROM tags t JOIN entity_tags et ON et.tag_id=t.id
|
||||
WHERE et.entity_type=$1 AND et.entity_id=$2 ORDER BY t.name`,
|
||||
[entity_type, entity_id]
|
||||
);
|
||||
return rows;
|
||||
}
|
||||
Reference in New Issue
Block a user