Skip to content

Backend: all database find operations should use .json to get a serializable version of the entities #227

@IzKuipers

Description

@IzKuipers

Right now we make database calls such as this:

const tokens = await Tokens.find<Token>({});

Which is fine, but as soon as we try to spread it, like so:

const x = tokens.map((t) => ({ ...t, value: undefined });

We end up with this weird mongoose object that contains the actual data in the _doc property. This is why we have so many _doc calls in ReArc, for instance:

const shares = await getUserShares(user._id.toString());

res.json(
  shares.map((s) => ({
    ...(s as any)._doc,
    passwordHash: undefined,
  }))
);

We need to change getUserShares in this case to return the JSON representation of the data:

export async function getUserShares(userId: string): Promise<SharedDrive[]> {
-  return await SharedDrives.find<SharedDrive>({ userId });
+  return (await SharedDrives.find({ userId })).map((share) => share.toJSON());
}

This has to be done for every database operation, just to clean up the codebase and to minimize confusion.

Metadata

Metadata

Assignees

Labels

ReArc BackendThis concerns either the backend itself or the client -> server connectionRefactoringThis issue refactors a part of the codebase

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions