Update tag associations without recreating document text assets
waterfall-wiki CI and deployment / verify-and-deploy (push) Has been cancelled
waterfall-wiki CI and deployment / verify-and-deploy (push) Has been cancelled
This commit is contained in:
@@ -57,3 +57,24 @@ export async function saveNote(id:string,input:z.infer<typeof noteEditInput>,rea
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export const noteTagsInput=z.object({tags:z.array(z.uuid()).max(50),revision:z.string().regex(/^[0-9a-f]{64}$/)}).strict();
|
||||
/** A tag edit never replaces a text asset, cover or media association. */
|
||||
export async function saveNoteTags(id:string,input:z.infer<typeof noteTagsInput>,read:(client:PoolClient)=>Promise<ExploreEntry|null>){
|
||||
await transaction(async client=>{
|
||||
await client.query('SELECT pg_advisory_xact_lock(hashtextextended($1,0))',['note-edit:'+id]);
|
||||
const stored=(await client.query(`SELECT id FROM posts WHERE ${/^[0-9a-f]{24}$/.test(id)?'public_id':'id'}=$1 FOR UPDATE`,[id])).rows[0];
|
||||
const current=await read(client);if(!current)throw Error('Note not found');
|
||||
if(current.revision!==input.revision)throw new NoteEditConflict('这篇笔记已被修改,请重新打开编辑后再保存。');
|
||||
const ids=[...new Set(input.tags)];if((await client.query('SELECT id FROM tags WHERE id=ANY($1::uuid[])',[ids])).rowCount!==ids.length)throw Error('Tag not found');
|
||||
if(stored){
|
||||
await client.query('UPDATE posts SET updated_at=now(),edited_at=now() WHERE id=$1',[stored.id]);
|
||||
await client.query('DELETE FROM post_tags WHERE post_id=$1',[stored.id]);
|
||||
for(const tagId of ids)await client.query('INSERT INTO post_tags(post_id,tag_id) VALUES($1,$2)',[stored.id,tagId]);
|
||||
}else{
|
||||
await client.query(`INSERT INTO note_edits(page_id,title,description,markdown) VALUES($1,$2,$3,$4) ON CONFLICT(page_id) DO UPDATE SET updated_at=now()`,[id,current.title,current.description,current.editableMarkdown]);
|
||||
await client.query('DELETE FROM note_edit_tags WHERE page_id=$1',[id]);
|
||||
for(const tagId of ids)await client.query('INSERT INTO note_edit_tags(page_id,tag_id) VALUES($1,$2)',[id,tagId]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user