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"]);
|
b.primary(["content_id","tag_name"]);
|
||||||
});
|
});
|
||||||
await knex.schema.createTable("permissions",b=>{
|
await knex.schema.createTable("permissions",b=>{
|
||||||
b.integer('username').unsigned().notNullable();
|
b.string('username').notNullable();
|
||||||
b.string("name").notNullable();
|
b.string("name").notNullable();
|
||||||
b.primary(["username","name"]);
|
b.primary(["username","name"]);
|
||||||
b.foreign('username').references('users.username');
|
b.foreign('username').references('users.username');
|
||||||
|
@ -40,18 +40,21 @@ class KnexContentsAccessor implements ContentAccessor{
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
async findById(id:number,tagload?:boolean){
|
async findById(id:number,tagload?:boolean): Promise<Content|undefined>{
|
||||||
const s:Content[] = await this.knex.select("*").from("contents").where({id:id});
|
const s = await this.knex.select("*").from("contents").where({id:id});
|
||||||
if(s.length === 0) return undefined;
|
if(s.length === 0) return undefined;
|
||||||
const first = s[0];
|
const first = s[0];
|
||||||
first.additional = JSON.parse((first.additional as unknown) as string)
|
let ret_tags:string[] = []
|
||||||
first['tags'] = [];
|
|
||||||
if(tagload === true){
|
if(tagload === true){
|
||||||
const tags : DBTagContentRelation[] = await this.knex.select("*")
|
const tags : DBTagContentRelation[] = await this.knex.select("*")
|
||||||
.from("content_tag_relation").where({content_id:first.id});
|
.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){
|
async findList(option?:QueryListOption){
|
||||||
option = option || {};
|
option = option || {};
|
||||||
@ -115,11 +118,12 @@ class KnexContentsAccessor implements ContentAccessor{
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
async findListByBasePath(path:string):Promise<Content[]>{
|
async findListByBasePath(path:string):Promise<Content[]>{
|
||||||
let results:Content[] = await this.knex.select("*").from("contents").where({basepath:path});
|
let results = await this.knex.select("*").from("contents").where({basepath:path});
|
||||||
results.forEach(e => {
|
return results.map(x=>({
|
||||||
e.additional = JSON.parse((e.additional as unknown) as string);
|
additional:JSON.parse(x.additional || "{}"),
|
||||||
});
|
tags:[],
|
||||||
return results;
|
...x
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
async update(c:Partial<Content> & { id:number }){
|
async update(c:Partial<Content> & { id:number }){
|
||||||
const {id,tags,...rest} = c;
|
const {id,tags,...rest} = c;
|
||||||
|
@ -5,8 +5,9 @@ type DBTags = {
|
|||||||
name: string,
|
name: string,
|
||||||
description?: string
|
description?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
class KnexTagAccessor implements TagAccessor{
|
class KnexTagAccessor implements TagAccessor{
|
||||||
knex:Knex
|
knex:Knex<DBTags>
|
||||||
constructor(knex:Knex){
|
constructor(knex:Knex){
|
||||||
this.knex = knex;
|
this.knex = knex;
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ export interface ContentContent{
|
|||||||
content_type : string,
|
content_type : string,
|
||||||
basepath : string,
|
basepath : string,
|
||||||
filename : string,
|
filename : string,
|
||||||
hash? : string,
|
content_hash? : string,
|
||||||
additional : JSONMap,
|
additional : JSONMap,
|
||||||
tags : string[],//eager loading
|
tags : string[],//eager loading
|
||||||
}
|
}
|
||||||
@ -21,7 +21,7 @@ export const MetaContentContent = {
|
|||||||
content_type : "string",
|
content_type : "string",
|
||||||
basepath : "string",
|
basepath : "string",
|
||||||
filename : "string",
|
filename : "string",
|
||||||
hash : "string",
|
content_hash : "string",
|
||||||
additional : "object",
|
additional : "object",
|
||||||
tags : "string[]",
|
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