Necesito cambiar el formato de mis datos que obtuve de db. Los datos tienen este formato:
[ { "id": 2, "report_id": null, "parking_lot_id": null, "message": "test2", "date": "2022-03-11", "seen": false, "category": "report", "user_id": 3 }, { "id": 3, "report_id": null, "parking_lot_id": null, "message": "test3", "date": "2022-03-11", "seen": false, "category": "report", "user_id": 3 }, { "id": 1, "report_id": null, "parking_lot_id": null, "message": "test", "date": "2022-03-03", "seen": true, "category": "report", "user_id": 3 }, ... ]Formato que necesito obtener. (Todas las notificaciones agrupadas por misma fecha)
[ { date: new Date(), notifications: [ { id: 0, message: 'Foo', date: new Date(), category: 'Web app', seen: true }, { id: 1, message: 'Test', date: new Date(), category: 'Web app', seen: false } ] }, ...¿Hay alguna manera fácil de hacer esto? Será mejor obtener datos en este formato de la base de datos, pero creo que eso no es posible...
La forma en que obtengo los datos de db se ve así (estoy usando el marco knex):
export async function getAllUserNotifications(request) { const userNotifications: any[] = await getUserNotifications(request.params.userId, queryBuilder(request)); return userNotifications.length > 0 ? userNotifications : [] } export async function getUserNotifications(userId: string, knex: Knex<any, unknown[]>,limit?:number) { return await knex('notification') .select('*') .where({ 'user_id': userId }) .limit(limit ? limit : 10) .orderBy('date', 'desc') }