diff --git a/src/Library/Service/WetlandService.ts b/src/Library/Service/WetlandService.ts index 0b2d311..deb2965 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): Promise { + return this.getRepository(Entity).findOne(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): Promise { + 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): Promise { + 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): Promise { + 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/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' ] } }, }; 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); }; });