81 lines
2 KiB
TypeScript
81 lines
2 KiB
TypeScript
import { check_type } from "../util/type_check";
|
|
import type {
|
|
DocumentBody,
|
|
Document,
|
|
QueryListOption
|
|
} from "dbtype/api";
|
|
|
|
export const MetaContentBody = {
|
|
title: "string",
|
|
content_type: "string",
|
|
basepath: "string",
|
|
filename: "string",
|
|
content_hash: "string",
|
|
additional: "object",
|
|
tags: "string[]",
|
|
};
|
|
|
|
export const isDocBody = (c: unknown): c is DocumentBody => {
|
|
return check_type<DocumentBody>(c, MetaContentBody);
|
|
};
|
|
|
|
export const isDoc = (c: unknown): c is Document => {
|
|
if (typeof c !== "object" || c === null) return false;
|
|
if ("id" in c && typeof c.id === "number") {
|
|
const { id, ...rest } = c;
|
|
return isDocBody(rest);
|
|
}
|
|
return false;
|
|
};
|
|
|
|
export interface DocumentAccessor {
|
|
/**
|
|
* find list by option
|
|
* @returns documents list
|
|
*/
|
|
findList: (option?: QueryListOption) => Promise<Document[]>;
|
|
/**
|
|
* @returns document if exist, otherwise undefined
|
|
*/
|
|
findById: (id: number, tagload?: boolean) => Promise<Document | undefined>;
|
|
/**
|
|
* find by base path and filename.
|
|
* if you call this function with filename, its return array length is 0 or 1.
|
|
*/
|
|
findByPath: (basepath: string, filename?: string) => Promise<Document[]>;
|
|
/**
|
|
* find deleted content
|
|
*/
|
|
findDeleted: (content_type: string) => Promise<Document[]>;
|
|
/**
|
|
* search by in document
|
|
*/
|
|
search: (search_word: string) => Promise<Document[]>;
|
|
/**
|
|
* update document except tag.
|
|
*/
|
|
update: (c: Partial<Document> & { id: number }) => Promise<boolean>;
|
|
/**
|
|
* add document
|
|
*/
|
|
add: (c: DocumentBody) => Promise<number>;
|
|
/**
|
|
* add document list
|
|
*/
|
|
addList: (content_list: DocumentBody[]) => Promise<number[]>;
|
|
/**
|
|
* delete document
|
|
* @returns if it exists, return true.
|
|
*/
|
|
del: (id: number) => Promise<boolean>;
|
|
/**
|
|
* @param c Valid Document
|
|
* @param tagname tag name to add
|
|
* @returns if success, return true
|
|
*/
|
|
addTag: (c: Document, tag_name: string) => Promise<boolean>;
|
|
/**
|
|
* @returns if success, return true
|
|
*/
|
|
delTag: (c: Document, tag_name: string) => Promise<boolean>;
|
|
}
|