Skip to content

Commit

Permalink
(test) : début ajout de feature pour l'aggréation dans l'API.
Browse files Browse the repository at this point in the history
  • Loading branch information
mamarmite committed Sep 8, 2023
1 parent addbd58 commit f1d8eb0
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/Abstract/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@ abstract class AbstractModel {
abstract dataTransfertObject(document: any):any;

abstract documentation():Promise<any>;

}
export default AbstractModel;
28 changes: 28 additions & 0 deletions src/Database/QueryBuilder/ApiQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default class ApiQuery {
public sections:Array<any>;
private _transmuted:any;
private _options:any;
private _lookup:any;
private _matches:any;
private _projections:any;

/**
Expand Down Expand Up @@ -57,6 +59,7 @@ export default class ApiQuery {
return this._transmuted;
}


public set options(values) {
this._options = values;
}
Expand All @@ -70,6 +73,31 @@ export default class ApiQuery {
return this._options;
}


public set matches(values:any) {
this._matches = values;
}

public get matches() {
return this._matches;
}


public set lookup(values:any) {
/*
from: Collection,
localField: current Model field to match the value of ForeignField,
foreignField: The field to match the from collection,
as: Name of the properties to push the data.,
*/
this._lookup = values;
}

public get lookup() {
return this._lookup;
}


public get projections() {
return {};
}
Expand Down
7 changes: 6 additions & 1 deletion src/Database/Search/SearchRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class SearchRoutes extends AbstractRoute {
*/
public setupPublicRoutes(): express.Router {

this.routerInstance.get('/', [this.fullSearchHandler.bind(this), this.routeSendResponse.bind(this)])
this.routerInstance.get('/', [this.fullSearchHandler.bind(this), this.routeSendResponse.bind(this)]);
this.routerInstance.get('/all', [this.aggregateAllHandler.bind(this), this.routeSendResponse.bind(this)]);
this.routerInstance.get('/regex', [this.textSearchSuggestionsHandler.bind(this), this.routeSendResponse.bind(this)]);
this.routerInstance.get('/text', [this.textSearchResultsHandler.bind(this), this.routeSendResponse.bind(this)]);
this.routerInstance.get('/nearestTaxonomy', [this.nearTaxonomyToSearchIndex.bind(this), this.routeSendResponse.bind(this)]);
Expand Down Expand Up @@ -126,6 +127,10 @@ class SearchRoutes extends AbstractRoute {
return next();
}

public async aggregateAllHandler(req:Request, res:Response, next: NextFunction):Promise<any> {
return next();
}


}

Expand Down
2 changes: 1 addition & 1 deletion src/Database/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export abstract class Service {
const items = await this.model.find(query.transmuted, query.projections, query.options);//.sort(sort).skip(skip).limit(limit);
const returnItems = items.map((doc: any) => {
return this.appModel.dataTransfertObject(doc);
})
});

//populate
return SuccessResponse.create(
Expand Down
23 changes: 23 additions & 0 deletions src/Database/ServiceAggregate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import AbstractModel from "@core/Model";


export default class ServiceAggregate {
// take the model - mongooseModel

public model:AbstractModel;

constructor(model:AbstractModel) {
this.model = model;
}

public async lookupFor($query:any, $lookupQUery:any):Promise<any> {
let aggregateArray:Array<any> = [];
aggregateArray.push($query);
aggregateArray.push($lookupQUery);
return this.model.mongooseModel.aggregate([
{$match: $query},
{$lookup: $lookupQUery}
]).exec();
}

}
45 changes: 43 additions & 2 deletions src/Persons/Controllers/PersonsController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import Person from "../Models/Person"
import PersonsService from "../Services/PersonsService";
import AbstractController from "../../Abstract/Controller"
import {ApiResponseContract} from "@src/Http/Responses/ApiResponse";
import {ReasonPhrases, StatusCodes} from "http-status-codes";
import {SuccessResponse} from "@src/Http/Responses/SuccessResponse";
import ServiceAggregate from "@database/ServiceAggregate";
import {objectIdModifier} from "@database/QueryBuilder/PropertyModifier";

class PersonsController extends AbstractController {

/** @private @static Singleton instance */
private static _instance:AbstractController;
private static _instance:AbstractController|PersonsController;

/** @public PersonsService */
service:PersonsService;
Expand All @@ -23,11 +28,47 @@ class PersonsController extends AbstractController {
* @public @static @method getInstance Create the singleton instance if not existing
* @return {PersonsController} Controller singleton constructor
*/
public static getInstance():AbstractController {
public static getInstance():AbstractController|PersonsController {
if (PersonsController._instance === undefined) {
PersonsController._instance = new PersonsController();
}
return PersonsController._instance;
}

public async aggregateOrgs(id:any): Promise<ApiResponseContract> {

const PersonModel:any = Person.getInstance();
/*const results:any = await PersonModel.aggregate([
{
$lookup: {
from: 'organisations',
localField: '_id',
foreignField: 'team.member',
as: 'org'
}
}
]).exec();*/

const aggregateService = new ServiceAggregate(PersonModel);
const results:any = await aggregateService.lookupFor(
{
_id: objectIdModifier(id)
},
{
from: 'organisations',
localField: '_id',
foreignField: 'team.member',
as: 'organisations'
}
);

console.log(results);

return SuccessResponse.create(
results,
StatusCodes.OK,
ReasonPhrases.OK
);
}
}
export default PersonsController;
15 changes: 9 additions & 6 deletions src/Persons/Models/Person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class Person extends AbstractModel {
service: PersonsService;
mongooseModel: mongoose.Model<any>;

public aggregations:Array<any> = [{
from: 'organisations',
localField: '_id',
foreignField: 'team.member',
as: 'organisations'
}];

/** @public Database schema */
schema: Schema =
new Schema<PersonSchema>({
Expand Down Expand Up @@ -270,27 +277,23 @@ class Person extends AbstractModel {
}

public registerEvents():void {

this.schema.pre('find', function() {
taxonomyPopulate(this, 'occupations.skills');
taxonomyPopulate(this, 'domains.domain');
middlewarePopulateProperty(this, "mainImage");

populateUser(this, "status.requestedBy", User.getInstance().mongooseModel);
populateUser(this, "status.lastModifiedBy", User.getInstance().mongooseModel);

//populateUser(this, "occupations.occupation.status.requestedBy", User.getInstance().mongooseModel);
//populateUser(this, "occupations.occupation.status.lastModifiedBy", User.getInstance().mongooseModel);
});

this.schema.pre('findOne', function() {
taxonomyPopulate(this, 'occupations.skills');
taxonomyPopulate(this, 'domains.domain');
middlewarePopulateProperty(this, 'mainImage');

populateUser(this, "status.requestedBy", User.getInstance().mongooseModel);
populateUser(this, "status.lastModifiedBy", User.getInstance().mongooseModel);

//populateUser(this, "occupations.occupation.status.requestedBy", User.getInstance().mongooseModel);
//populateUser(this, "occupations.occupation.status.lastModifiedBy", User.getInstance().mongooseModel);
});
}
}
Expand Down
22 changes: 21 additions & 1 deletion src/Persons/Routes/PersonsRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import express from "express";
import express, {NextFunction, Request, Response} from "express";
import PersonsController from "@src/Persons/Controllers/PersonsController";
import AbstractController from "@core/Controller";
import CrudRoute from "@core/CrudRoute";
Expand Down Expand Up @@ -38,5 +38,25 @@ class PersonsRoutes extends CrudRoute {
getinfo: [],
getdoc: [],
}

public setupAdditionnalPublicRoutes(router: express.Router):express.Router {
// Set the /:slug handler at the end of other route, to allow the routes sets in setupAdditionnalPublicRoutes to be 1 in priority.
this.routerInstance.get('/agg/:id', [
this.getPersonsOrganisationsHandler.bind(this),
this.routeSendResponse.bind(this),
]);
return router;
}

public async getPersonsOrganisationsHandler(req: Request, res: Response, next: NextFunction): Promise<any> {

if (req.params.id) {
const personsController:PersonsController = PersonsController.getInstance() as PersonsController;
res.serviceResponse = await personsController.aggregateOrgs(req.params.id);
}

return next();
}

}
export {PersonsRoutes};
12 changes: 12 additions & 0 deletions src/Persons/Schemas/withOrganisations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


const withOrganiations = function() {
this.aggregate([{
from: 'organisations',
localField: '_id',
foreignField: 'team.member',
as: 'organisations'
}])
}

export default withOrganiations;

0 comments on commit f1d8eb0

Please sign in to comment.