From f5464b15079ccca56b32d844db304fd4fb07a6c9 Mon Sep 17 00:00:00 2001 From: monoid Date: Sun, 10 Jan 2021 04:27:50 +0900 Subject: [PATCH] add knex table type and fix bug --- migrations/initial.ts | 2 +- src/db/contents.ts | 26 +++++++++++++++----------- src/db/tag.ts | 3 ++- src/db/user.ts | 2 +- src/model/contents.ts | 4 ++-- src/types/db.d.ts | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 src/types/db.d.ts diff --git a/migrations/initial.ts b/migrations/initial.ts index eb630d9..6a8b075 100644 --- a/migrations/initial.ts +++ b/migrations/initial.ts @@ -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'); diff --git a/src/db/contents.ts b/src/db/contents.ts index 95ae9f4..67c92d5 100644 --- a/src/db/contents.ts +++ b/src/db/contents.ts @@ -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{ + 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{ - 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 & { id:number }){ const {id,tags,...rest} = c; diff --git a/src/db/tag.ts b/src/db/tag.ts index e992c9c..8e24c70 100644 --- a/src/db/tag.ts +++ b/src/db/tag.ts @@ -5,8 +5,9 @@ type DBTags = { name: string, description?: string } + class KnexTagAccessor implements TagAccessor{ - knex:Knex + knex:Knex constructor(knex:Knex){ this.knex = knex; } diff --git a/src/db/user.ts b/src/db/user.ts index 1a2e529..a5df975 100644 --- a/src/db/user.ts +++ b/src/db/user.ts @@ -1,7 +1,7 @@ import Knex from 'knex'; import {IUser,UserCreateInput, UserAccessor, Password} from '../model/user'; -type PermissionTable={ +type PermissionTable = { username:string, name:string }; diff --git a/src/model/contents.ts b/src/model/contents.ts index ceca139..dd380ed 100644 --- a/src/model/contents.ts +++ b/src/model/contents.ts @@ -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[]", } diff --git a/src/types/db.d.ts b/src/types/db.d.ts new file mode 100644 index 0000000..bd1b36f --- /dev/null +++ b/src/types/db.d.ts @@ -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 { + } +}