0

I am trying to retrieve data from a postgresql database, transform it to a view model array and return the data to the client, ideally as a single object in this case rather than an array. I am getting the following error for the code I have listed beneath the error message. I am new to the functional programming space and am confused on how to utilize the data transformation concepts?

TS2345: Argument of type ‘(department: {}) => (department: QueryResult) => Department’ is not assignable to parameter of type '(a: {}) => TaskEither<DBError, {}>'.   Type ‘(department: QueryResult) => Department’ is not assignable to type 'TaskEither<DBError, {}>'.

type DBError = MultipleResultsError | NoRecordsFoundError | Error;

const retrieveDepartments = async ({departmentId}: {departmentId: number}): Promise<TE.TaskEither<DBError, Department[]>> => {
    const client = await pool.connect();

    try {
        const query = 'SELECT * FROM department where departmentId = ?';
        const res: QueryResult<Department> = await client.query(query, [departmentId]);

        return pipe(
            TE.right(res.rows),
            TE.chain(
                TE.traverseArray(department => mapDepartmentViewModel)
            )
        );
    } catch (e) {
        return TE.left(e);
    } finally {
        client.release();
    }
}

export type Department = {
    departmentId: number,
    departmentName: string,
    departmentSupervisorId: number,
    departmentSupervisorFirstName: string,
    departmentSupervisorLastName: string
}

export const mapDepartmentViewModel = (department: QueryResult): Department => ({
    departmentId: department.departmentId,
    departmentName: department.departmentName,
    departmentSupervisorId: department.departmentSupervisorId,
    departmentSupervisorFirstName: department.departmentSupervisorFirstName,
    departmentSupervisorLastName: department.departmentSupervisorLastName
});
user1790300
  • 2,143
  • 10
  • 54
  • 123

0 Answers0