Render Markdown lists and edit note contents and tags in place
wiki CI and deployment / verify-and-deploy (push) Successful in 22m1s
wiki CI and deployment / verify-and-deploy (push) Successful in 22m1s
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import type {PoolClient} from 'pg';
|
||||
import { db } from "./db";
|
||||
import { decodeCursor, encodeCursor, type FeedData, type Post, type PostKind, type Tag } from "./model";
|
||||
|
||||
export async function tags(): Promise<Tag[]> { return (await db().query('SELECT *,slug AS path FROM tags ORDER BY name,id')).rows; }
|
||||
export async function tags(client?:PoolClient): Promise<Tag[]> { return (await (client||db()).query('SELECT *,slug AS path FROM tags ORDER BY name,id')).rows; }
|
||||
const expand = `SELECT p.*,
|
||||
COALESCE((SELECT jsonb_agg(ae ORDER BY ae.position) FROM (SELECT a.*,pa.position,pa.role,COALESCE((SELECT jsonb_agg(v.derivation) FROM assets v WHERE v.source_asset_id=a.id),'[]') AS variants FROM post_assets pa JOIN assets a ON a.id=pa.asset_id WHERE pa.post_id=p.id) ae),'[]') AS assets,
|
||||
COALESCE((SELECT jsonb_agg(t) FROM tags t JOIN post_tags pt ON pt.tag_id=t.id WHERE pt.post_id=p.id),'[]') AS tags FROM posts p`;
|
||||
@@ -12,15 +13,15 @@ export async function getPost(slug: string): Promise<Post | null> {
|
||||
const result = await db().query(`${expand} WHERE p.slug=$1`,[slug]);
|
||||
return serialize(result.rows,await tags())[0] || null;
|
||||
}
|
||||
export async function getPostById(id:string):Promise<Post|null>{
|
||||
const result=await db().query(`${expand} WHERE p.id=$1`,[id]);
|
||||
return serialize(result.rows,await tags())[0]||null;
|
||||
export async function getPostById(id:string,client?:PoolClient):Promise<Post|null>{
|
||||
const result=await (client||db()).query(`${expand} WHERE p.id=$1`,[id]);
|
||||
return serialize(result.rows,await tags(client))[0]||null;
|
||||
}
|
||||
export async function feed(options: {kind?: PostKind; path?: string; cursor?: string; q?: string} = {}): Promise<FeedData> {
|
||||
const allTags=await tags(); const target=options.path ? allTags.find(t=>t.path===options.path) : undefined;
|
||||
if(options.path&&!target) throw new Error("Tag not found");
|
||||
const cursor=decodeCursor(options.cursor); const values: unknown[]=[];
|
||||
const where=[`EXISTS(SELECT 1 FROM post_assets pa JOIN assets a ON a.id=pa.asset_id WHERE pa.post_id=p.id AND pa.role IN ('content','caption') AND ((a.storage_key IS NOT NULL AND a.byte_size>0) OR (a.kind='text' AND length(a.body)>0)))`];
|
||||
const where=[`EXISTS(SELECT 1 FROM post_assets pa JOIN assets a ON a.id=pa.asset_id WHERE pa.post_id=p.id AND pa.role IN ('content','caption') AND ((a.storage_key IS NOT NULL AND a.byte_size>0) OR (a.kind='text' AND a.body IS NOT NULL)))`];
|
||||
if(options.kind){values.push(options.kind);where.push(`p.kind=$${values.length}`);}
|
||||
if(target){values.push([target.id]);where.push(`EXISTS(SELECT 1 FROM post_tags WHERE post_id=p.id AND tag_id=ANY($${values.length}::uuid[]))`);}
|
||||
if(options.q){values.push(`%${options.q.replace(/[\\%_]/g,"\\$&")}%`);where.push(`p.title ILIKE $${values.length}`);}
|
||||
|
||||
Reference in New Issue
Block a user