From 42bd10214554e432782ebb3810691a57319f833c Mon Sep 17 00:00:00 2001 From: RWOverdijk Date: Wed, 21 Nov 2018 14:31:04 +0100 Subject: [PATCH 1/3] feat(WetlandService): simple crud methods --- src/Library/Service/WetlandService.ts | 67 ++++++++++++++++++++++++- src/decorators/actions/createAction.ts | 25 +-------- src/decorators/actions/destroyAction.ts | 5 +- src/decorators/actions/findAction.ts | 2 +- src/decorators/actions/findOneAction.ts | 2 +- src/decorators/actions/modifyAction.ts | 17 ++----- 6 files changed, 73 insertions(+), 45 deletions(-) diff --git a/src/Library/Service/WetlandService.ts b/src/Library/Service/WetlandService.ts index 0b2d311..25742cd 100644 --- a/src/Library/Service/WetlandService.ts +++ b/src/Library/Service/WetlandService.ts @@ -1,6 +1,8 @@ import { EntityCtor, EntityManager, EntityRepository, Mapping, Populate, Scope, Wetland } from 'wetland'; import { WetlandConfigType } from '../WetlandConfigType'; -import { createDebugLogger } from 'stix'; +import { createDebugLogger, ServiceManager } from 'stix'; +import { FindOptions } from 'wetland/dist/src/EntityRepository'; +import { BodyParamType, IdParamType } from '../../decorators/actions'; const debug = createDebugLogger('wetland'); @@ -52,4 +54,67 @@ export class WetlandService { public getMapping (Entity: T): Mapping { return this.getEntityManager().getMapping(Entity); } + + public findOne (Entity: EntityCtor, criteria: {} | number | string, options?: FindOptions) { + return this.getRepository(Entity).findOne(criteria, options); + } + + public find (Entity: EntityCtor, criteria?: {} | number | string, options?: FindOptions) { + return this.getRepository(Entity).find(criteria, options); + } + + public async destroy (Entity: EntityCtor, criteria?: {} | number | string) { + const manager = this.getManager(); + const result = await manager.getRepository(Entity).findOne(criteria); + + if (result) { + await manager.remove(result).flush(); + } + + return result; + } + + public async create (Entity: EntityCtor, data: object | object[], recursive?: boolean | number) { + const manager = await this.getManager(); + const populator = this.getPopulator(manager); + const persistedEntries = (() => { + const persistEntry = (entry: Object) => { + const newRecord = populator.assign(Entity, entry, null, recursive); + + manager.persist(newRecord); + + return newRecord; + }; + + if (!Array.isArray(data)) { + return persistEntry(data); + } + + return data.map(persistEntry); + })(); + + await manager.flush(); + + return persistedEntries; + } + + public async modify (Entity: EntityCtor, pkValue: number | string, data: object | object[], recursive?: boolean | number) { + const manager = await this.getManager(); + const pk = manager.getMapping(Entity).getPrimaryKey(); + const populator = this.getPopulator(manager); + const safeData = { ...data, [pk]: pkValue }; + const base = await populator.findDataForUpdate(pkValue, Entity as any, safeData); + + if (!base) { + return null; + } + + // Assign values to fetched base. + populator.assign(Entity, safeData, base, recursive); + + // Apply changes. + await manager.flush(); + + return base; + } } diff --git a/src/decorators/actions/createAction.ts b/src/decorators/actions/createAction.ts index fa6fc59..3b3ca54 100644 --- a/src/decorators/actions/createAction.ts +++ b/src/decorators/actions/createAction.ts @@ -6,29 +6,6 @@ import { patchAction } from '../patchAction'; export const createAction = patchAction('create', (Entity: EntityCtor, sm: ServiceManager, recursive?: boolean | number) => { return async function create ({ request: { body } }: BodyParamType) { - const service = sm.get(WetlandService); - const manager = await service.getManager(); - const populator = service.getPopulator(manager); - const persistedEntries = (() => { - if (!Array.isArray(body)) { - const newRecord = populator.assign(Entity, body, null, recursive); - - manager.persist(newRecord); - - return newRecord; - } - - return body.map((entry: Object) => { - const newRecord = populator.assign(Entity, entry, null, recursive); - - manager.persist(newRecord); - - return newRecord; - }); - })(); - - await manager.flush(); - - return this.createdResponse(persistedEntries); + return this.createdResponse(await sm.get(WetlandService).create(Entity, body, recursive)); }; }); diff --git a/src/decorators/actions/destroyAction.ts b/src/decorators/actions/destroyAction.ts index a9ccc9a..5becd5d 100644 --- a/src/decorators/actions/destroyAction.ts +++ b/src/decorators/actions/destroyAction.ts @@ -6,15 +6,12 @@ import { patchAction } from '../patchAction'; export const destroyAction = patchAction('destroy', (Entity: EntityCtor, sm: ServiceManager) => { return async function destroy ({ state: { params: { id } } }: IdParamType) { - const manager = await sm.get(WetlandService).getManager(); - const result = await manager.getRepository(Entity).findOne(id); + const result = await sm.get(WetlandService).destroy(Entity, id); if (!result) { return this.notFoundResponse(); } - await manager.remove(result).flush(); - return this.okResponse(result); }; }); diff --git a/src/decorators/actions/findAction.ts b/src/decorators/actions/findAction.ts index 0931b5d..f9b7e8d 100644 --- a/src/decorators/actions/findAction.ts +++ b/src/decorators/actions/findAction.ts @@ -5,7 +5,7 @@ import { patchAction } from '../patchAction'; export const findAction = patchAction('find', (Entity: EntityCtor, sm: ServiceManager) => { return async function find ({ state: { query: { criteria, options } } }: any) { - const results = await sm.get(WetlandService).getRepository(Entity).find(criteria, options); + const results = await sm.get(WetlandService).find(Entity, criteria, options); return this.okResponse(results || []); }; diff --git a/src/decorators/actions/findOneAction.ts b/src/decorators/actions/findOneAction.ts index b6d2d6e..d02a6f6 100644 --- a/src/decorators/actions/findOneAction.ts +++ b/src/decorators/actions/findOneAction.ts @@ -6,7 +6,7 @@ import { patchAction } from '../patchAction'; export const findOneAction = patchAction('findOne', (Entity: EntityCtor, sm: ServiceManager) => { return async function findOne ({ state: { params: { id } } }: IdParamType) { - const results = await sm.get(WetlandService).getRepository(Entity).findOne(id); + const results = await sm.get(WetlandService).find(Entity, id); if (!results) { return this.notFoundResponse(); diff --git a/src/decorators/actions/modifyAction.ts b/src/decorators/actions/modifyAction.ts index ebf04a4..25a2835 100644 --- a/src/decorators/actions/modifyAction.ts +++ b/src/decorators/actions/modifyAction.ts @@ -6,23 +6,12 @@ import { patchAction } from '../patchAction'; export const modifyAction = patchAction('modify', (Entity: EntityCtor, sm: ServiceManager, recursive = 1) => { return async function modify ({ state, request: { body } }: BodyParamType) { - const service = sm.get(WetlandService); - const manager = await service.getManager(); - const pk = manager.getMapping(Entity).getPrimaryKey(); - const { [pk]: x, ...data } = state.data || body; - const populator = service.getPopulator(manager); - const base = await populator.findDataForUpdate(state.params.id, Entity as any, data); + const result = await sm.get(WetlandService).modify(Entity, state.params.id, state.data || body, recursive); - if (!base) { + if (!result) { return this.notFoundResponse(); } - // Assign values to fetched base. - populator.assign(Entity, data, base, recursive); - - // Apply changes. - await manager.flush(); - - return this.okResponse(base); + return this.okResponse(result); }; }); From 37e675ded5ecd9e0c8587b066dbd55fe76ed2fb2 Mon Sep 17 00:00:00 2001 From: RWOverdijk Date: Thu, 22 Nov 2018 14:18:34 +0100 Subject: [PATCH 2/3] feat(WetlandService): more verbose and proper typing --- src/Library/Service/WetlandService.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Library/Service/WetlandService.ts b/src/Library/Service/WetlandService.ts index 25742cd..deb2965 100644 --- a/src/Library/Service/WetlandService.ts +++ b/src/Library/Service/WetlandService.ts @@ -55,17 +55,17 @@ export class WetlandService { return this.getEntityManager().getMapping(Entity); } - public findOne (Entity: EntityCtor, criteria: {} | number | string, options?: FindOptions) { - return this.getRepository(Entity).findOne(criteria, options); + public findOne (Entity: EntityCtor, criteria: {} | number | string, options?: FindOptions): Promise { + return this.getRepository(Entity).findOne(criteria, options); } - public find (Entity: EntityCtor, criteria?: {} | number | string, options?: FindOptions) { - return this.getRepository(Entity).find(criteria, options); + public find (Entity: EntityCtor, criteria?: {} | number | string, options?: FindOptions): Promise { + return this.getRepository(Entity).find(criteria, options); } - public async destroy (Entity: EntityCtor, criteria?: {} | number | string) { + public async destroy (Entity: EntityCtor, criteria?: {} | number | string): Promise { const manager = this.getManager(); - const result = await manager.getRepository(Entity).findOne(criteria); + const result = await manager.getRepository(Entity).findOne(criteria); if (result) { await manager.remove(result).flush(); @@ -74,7 +74,7 @@ export class WetlandService { return result; } - public async create (Entity: EntityCtor, data: object | object[], recursive?: boolean | number) { + public async create (Entity: EntityCtor, data: object | object[], recursive?: boolean | number): Promise { const manager = await this.getManager(); const populator = this.getPopulator(manager); const persistedEntries = (() => { @@ -98,7 +98,7 @@ export class WetlandService { return persistedEntries; } - public async modify (Entity: EntityCtor, pkValue: number | string, data: object | object[], recursive?: boolean | number) { + public async modify (Entity: EntityCtor, pkValue: number | string, data: object | object[], recursive?: boolean | number): Promise { const manager = await this.getManager(); const pk = manager.getMapping(Entity).getPrimaryKey(); const populator = this.getPopulator(manager); From 0668b89c3fa0f44031d9ad0b241295707db322af Mon Sep 17 00:00:00 2001 From: RWOverdijk Date: Thu, 22 Nov 2018 14:19:02 +0100 Subject: [PATCH 3/3] fix(config): more accurate env var reading --- src/config/wetland.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/wetland.ts b/src/config/wetland.ts index 48a3340..db84c40 100644 --- a/src/config/wetland.ts +++ b/src/config/wetland.ts @@ -1,6 +1,6 @@ const { env } = process; export const wetland = { - devMigrations: env.NODE_ENV !== 'production' || env.WETLAND_DEV_MIGRATIONS, + devMigrations: env.NODE_ENV !== 'production' || env.WETLAND_DEV_MIGRATIONS === 'true', mapping: { defaultNamesToUnderscore: true, defaults: { cascades: [ 'persist' ] } }, };