Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(WetlandService): simple crud methods #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion src/Library/Service/WetlandService.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -52,4 +54,67 @@ export class WetlandService {
public getMapping<T> (Entity: T): Mapping<T> {
return this.getEntityManager().getMapping(Entity);
}

public findOne<T> (Entity: EntityCtor<T>, criteria: {} | number | string, options?: FindOptions): Promise<T> {
return this.getRepository<T>(Entity).findOne(criteria, options);
}

public find<T> (Entity: EntityCtor<T>, criteria?: {} | number | string, options?: FindOptions): Promise<T[]> {
return this.getRepository<T>(Entity).find(criteria, options);
}

public async destroy<T> (Entity: EntityCtor<T>, criteria?: {} | number | string): Promise<T> {
const manager = this.getManager();
const result = await manager.getRepository<T>(Entity).findOne(criteria);

if (result) {
await manager.remove(result).flush();
}

return result;
}

public async create<T> (Entity: EntityCtor<T>, data: object | object[], recursive?: boolean | number): Promise<T|T[]> {
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<T> (Entity: EntityCtor<T>, pkValue: number | string, data: object | object[], recursive?: boolean | number): Promise<T|T[]> {
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;
}
}
2 changes: 1 addition & 1 deletion src/config/wetland.ts
Original file line number Diff line number Diff line change
@@ -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' ] } },
};
25 changes: 1 addition & 24 deletions src/decorators/actions/createAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@ import { patchAction } from '../patchAction';

export const createAction = patchAction('create', (Entity: EntityCtor<any>, 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));
};
});
5 changes: 1 addition & 4 deletions src/decorators/actions/destroyAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ import { patchAction } from '../patchAction';

export const destroyAction = patchAction('destroy', (Entity: EntityCtor<any>, 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);
};
});
2 changes: 1 addition & 1 deletion src/decorators/actions/findAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { patchAction } from '../patchAction';

export const findAction = patchAction('find', (Entity: EntityCtor<any>, 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 || []);
};
Expand Down
2 changes: 1 addition & 1 deletion src/decorators/actions/findOneAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { patchAction } from '../patchAction';

export const findOneAction = patchAction('findOne', (Entity: EntityCtor<any>, 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();
Expand Down
17 changes: 3 additions & 14 deletions src/decorators/actions/modifyAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,12 @@ import { patchAction } from '../patchAction';

export const modifyAction = patchAction('modify', (Entity: EntityCtor<any>, 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);
};
});