add knex table type and fix bug
This commit is contained in:
parent
47ffe6e80f
commit
f5464b1507
@ -29,7 +29,7 @@ export async function up(knex:Knex) {
|
||||
b.primary(["content_id","tag_name"]);
|
||||
});
|
||||
await knex.schema.createTable("permissions",b=>{
|
||||
b.integer('username').unsigned().notNullable();
|
||||
b.string('username').notNullable();
|
||||
b.string("name").notNullable();
|
||||
b.primary(["username","name"]);
|
||||
b.foreign('username').references('users.username');
|
||||
|
@ -40,18 +40,21 @@ class KnexContentsAccessor implements ContentAccessor{
|
||||
}
|
||||
return false;
|
||||
};
|
||||
async findById(id:number,tagload?:boolean){
|
||||
const s:Content[] = await this.knex.select("*").from("contents").where({id:id});
|
||||
async findById(id:number,tagload?:boolean): Promise<Content|undefined>{
|
||||
const s = await this.knex.select("*").from("contents").where({id:id});
|
||||
if(s.length === 0) return undefined;
|
||||
const first = s[0];
|
||||
first.additional = JSON.parse((first.additional as unknown) as string)
|
||||
first['tags'] = [];
|
||||
let ret_tags:string[] = []
|
||||
if(tagload === true){
|
||||
const tags : DBTagContentRelation[] = await this.knex.select("*")
|
||||
.from("content_tag_relation").where({content_id:first.id});
|
||||
first.tags = tags.map(x=>x.tag_name);
|
||||
ret_tags = tags.map(x=>x.tag_name);
|
||||
}
|
||||
return first;
|
||||
return {
|
||||
tags:ret_tags,
|
||||
additional: JSON.parse(first.additional || "{}"),
|
||||
...first
|
||||
};
|
||||
};
|
||||
async findList(option?:QueryListOption){
|
||||
option = option || {};
|
||||
@ -115,11 +118,12 @@ class KnexContentsAccessor implements ContentAccessor{
|
||||
return result;
|
||||
};
|
||||
async findListByBasePath(path:string):Promise<Content[]>{
|
||||
let results:Content[] = await this.knex.select("*").from("contents").where({basepath:path});
|
||||
results.forEach(e => {
|
||||
e.additional = JSON.parse((e.additional as unknown) as string);
|
||||
});
|
||||
return results;
|
||||
let results = await this.knex.select("*").from("contents").where({basepath:path});
|
||||
return results.map(x=>({
|
||||
additional:JSON.parse(x.additional || "{}"),
|
||||
tags:[],
|
||||
...x
|
||||
}));
|
||||
}
|
||||
async update(c:Partial<Content> & { id:number }){
|
||||
const {id,tags,...rest} = c;
|
||||
|
@ -5,8 +5,9 @@ type DBTags = {
|
||||
name: string,
|
||||
description?: string
|
||||
}
|
||||
|
||||
class KnexTagAccessor implements TagAccessor{
|
||||
knex:Knex
|
||||
knex:Knex<DBTags>
|
||||
constructor(knex:Knex){
|
||||
this.knex = knex;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ export interface ContentContent{
|
||||
content_type : string,
|
||||
basepath : string,
|
||||
filename : string,
|
||||
hash? : string,
|
||||
content_hash? : string,
|
||||
additional : JSONMap,
|
||||
tags : string[],//eager loading
|
||||
}
|
||||
@ -21,7 +21,7 @@ export const MetaContentContent = {
|
||||
content_type : "string",
|
||||
basepath : "string",
|
||||
filename : "string",
|
||||
hash : "string",
|
||||
content_hash : "string",
|
||||
additional : "object",
|
||||
tags : "string[]",
|
||||
}
|
||||
|
34
src/types/db.d.ts
vendored
Normal file
34
src/types/db.d.ts
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
import Knex from "knex";
|
||||
|
||||
declare module "knex" {
|
||||
interface Tables {
|
||||
tags: {
|
||||
name: string;
|
||||
description?: string;
|
||||
};
|
||||
users: {
|
||||
username: string;
|
||||
password_hash: string;
|
||||
password_salt: string;
|
||||
};
|
||||
contents: {
|
||||
id: number;
|
||||
title: string;
|
||||
content_type: string;
|
||||
basepath: string;
|
||||
filename: string;
|
||||
content_hash?: string;
|
||||
additional?: string; //eager loading
|
||||
};
|
||||
content_tag_relation: {
|
||||
content_id: number;
|
||||
tag_name: string;
|
||||
};
|
||||
permissions: {
|
||||
username: string;
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
namespace Knex {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user