UNPKG

@jvhaile/loopback4-helper

Version:
42 lines 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeStampedRepository = void 0; const repository_1 = require("@loopback/repository"); class TimeStampedRepository extends repository_1.DefaultCrudRepository { constructor(entityClass, dataSource) { super(entityClass, dataSource); this.modelClass.observe('persist', async (ctx) => { ctx.data.modifiedAt = new Date(); }); } async aggregate(query) { const collection = this.dataSource.connector.collection(this.entityClass.modelName); return await collection.aggregate(deepMapObj(query)).get(); } } exports.TimeStampedRepository = TimeStampedRepository; const _ = require('lodash'); const ObjectId = require('mongodb').ObjectID; function deepMapObj(obj) { if (_.isArray(obj)) { return obj.map(deepMapObj); } if (_.isObject(obj)) { const newObj = {}; Object.keys(obj).forEach(key => { //@ts-ignore newObj[key] = deepMapObj(obj[key]); }); return newObj; } if (_.isString(obj) && obj.startsWith('oid')) { try { return ObjectId(obj.slice(3)); } catch (e) { return obj.slice(3); } } return obj; } //# sourceMappingURL=time.stamped.repository.js.map