feat: objects table with M2M words/pictures/pairs

- objects: status enum, JSONB selections, notes, blocked_topic, auto-timestamps
- pairs placeholder table for future use
- Junction tables: object_words, object_pictures, object_pairs
- Full CRUD + link/unlink endpoints for all three relations
- README updated

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-21 09:51:34 +02:00
parent 8bd4240ea9
commit dac991c861
4 changed files with 317 additions and 0 deletions

View File

@@ -116,6 +116,72 @@ async function migrate() {
)
`);
// pairs placeholder (Felder folgen später)
await query(`
CREATE TABLE IF NOT EXISTS pairs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)
`);
await query(`
DROP TRIGGER IF EXISTS pairs_updated_at ON pairs;
CREATE TRIGGER pairs_updated_at
BEFORE UPDATE ON pairs
FOR EACH ROW EXECUTE FUNCTION update_updated_at()
`);
// objects
await query(`
CREATE TABLE IF NOT EXISTS objects (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
status VARCHAR(20) NOT NULL DEFAULT 'draft'
CHECK (status IN ('draft', 'blocked', 'published')),
selections JSONB,
notes TEXT,
blocked_topic TEXT,
published_at TIMESTAMPTZ,
blocked_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)
`);
await query(`
DROP TRIGGER IF EXISTS objects_updated_at ON objects;
CREATE TRIGGER objects_updated_at
BEFORE UPDATE ON objects
FOR EACH ROW EXECUTE FUNCTION update_updated_at()
`);
// M2M: objects <-> words
await query(`
CREATE TABLE IF NOT EXISTS object_words (
object_id UUID NOT NULL REFERENCES objects(id) ON DELETE CASCADE,
word_id UUID NOT NULL REFERENCES words(id) ON DELETE CASCADE,
PRIMARY KEY (object_id, word_id)
)
`);
// M2M: objects <-> pictures
await query(`
CREATE TABLE IF NOT EXISTS object_pictures (
object_id UUID NOT NULL REFERENCES objects(id) ON DELETE CASCADE,
picture_id UUID NOT NULL REFERENCES pictures(id) ON DELETE CASCADE,
PRIMARY KEY (object_id, picture_id)
)
`);
// M2M: objects <-> pairs (Platzhalter)
await query(`
CREATE TABLE IF NOT EXISTS object_pairs (
object_id UUID NOT NULL REFERENCES objects(id) ON DELETE CASCADE,
pair_id UUID NOT NULL REFERENCES pairs(id) ON DELETE CASCADE,
PRIMARY KEY (object_id, pair_id)
)
`);
console.log('Migration complete');
}