From 925929ee6f0b4e901bf9fac4f32e20356d5c5568 Mon Sep 17 00:00:00 2001 From: Hyun Jin Kim Date: Wed, 29 Jun 2022 15:01:40 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Database=20=EC=97=90=EC=84=9C=20retriev?= =?UTF-8?q?e=20=ED=9B=84=EC=9D=98=20ROW=EB=8A=94=20Required=20=EC=97=AC?= =?UTF-8?q?=EC=95=BC=20=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/crud-operations.ts | 24 ++++++++++++++++-------- src/weaver.ts | 6 +++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/crud-operations.ts b/src/crud-operations.ts index b30dbc8..5468681 100644 --- a/src/crud-operations.ts +++ b/src/crud-operations.ts @@ -38,19 +38,23 @@ export interface CrudOperationsOpts { - select(filter?: CrudFilter, sorts?: Array, relations?: Array): Promise>; + select(filter?: CrudFilter, sorts?: Array, relations?: Array): Promise>>; count(filter?: CrudFilter): Promise; - selectFirst(filter?: CrudFilter, sorts?: Array, relations?: Array): Promise; + selectFirst( + filter?: CrudFilter, + sorts?: Array, + relations?: Array + ): Promise | undefined>; exist(filter?: CrudFilter): Promise; - selectById(id: ID, relations?: Array): Promise; + selectById(id: ID, relations?: Array): Promise | undefined>; } export interface InsertOperations { - insert(data: ROW | Array): Promise; + insert(data: ROW | Array): Promise>; } export interface UpdateOperations { @@ -107,7 +111,11 @@ export class CrudOperations, sorts?: Array, relations?: Array): Promise> { + async select( + filter?: CrudFilter, + sorts?: Array, + relations?: Array + ): Promise>> { const query = this.knexReplica(this.table).modify((queryBuilder) => { this.applyFilter(queryBuilder, filter); this.applySort(queryBuilder, sorts); @@ -139,7 +147,7 @@ export class CrudOperations, sorts?: Array, relations?: Array - ): Promise { + ): Promise | undefined> { const rows = await this.select({ ...filter, limit: 1 }, sorts, relations); return rows[0]; } @@ -149,7 +157,7 @@ export class CrudOperations): Promise { + async selectById(id: ID, relations?: Array): Promise | undefined> { const include = { [this.idColumn]: id } as CrudFilterColumns; return this.selectFirst({ include }, undefined, relations); } @@ -157,7 +165,7 @@ export class CrudOperations): Promise { + async insert(data: ROW | Array): Promise> { // result is varying on dialect // mysql: the first one, sqlite3: the last one, ... // see http://knexjs.org/#Builder-insert diff --git a/src/weaver.ts b/src/weaver.ts index c7cef24..b663a04 100644 --- a/src/weaver.ts +++ b/src/weaver.ts @@ -26,7 +26,7 @@ export class Weaver { this.logger = LoggerFactory.getLogger('fastdao:weaver'); } - async weave(rows?: Array, relations?: Array): Promise> { + async weave(rows?: Array>, relations?: Array): Promise>> { if (!rows || rows.length === 0 || !relations || relations.length === 0) { // nothing to weave return []; @@ -64,9 +64,9 @@ export class Weaver { return rows; } - async selectRelationByIds(relation: Relation, ids: Array): Promise> { + async selectRelationByIds(relation: Relation, ids: Array): Promise>> { const missedIds: Array = []; - const hitRows: Array = []; + const hitRows: Array> = []; if (this.cache) { const cached: Array = ids && ids.length ? await this.cache.getAll(ids.map((id) => relation.table + ':' + id)) : [];