Skip to content

Commit

Permalink
Update schema for Prisma 4 + fix breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Soro committed Jul 9, 2022
1 parent 4e56fcf commit 94f3fb3
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 90 deletions.
155 changes: 84 additions & 71 deletions prisma/schema.prisma

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions src/services/entities/classe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
getSqlSorting,
isEntityReadOnly,
queryParametersToFindAllEntities,
ReadonlyStatus
ReadonlyStatus,
transformQueryRawResultsBigIntsToNumbers
} from "./entities-utils";

export const findClasseOfEspeceId = async (
Expand Down Expand Up @@ -123,8 +124,10 @@ export const findPaginatedClasses = async (
`;

classeEntities = await prisma.$queryRaw<
(Classe & { nbEspeces: number; nbDonnees: number })[]
>`${donneesPerClasseIdRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`;
(Classe & { nbEspeces: bigint; nbDonnees: bigint })[]
>`${donneesPerClasseIdRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`.then(
transformQueryRawResultsBigIntsToNumbers
);
} else {
let orderBy: Prisma.Enumerable<Prisma.ClasseOrderByWithRelationInput> | undefined = undefined;
if (sortOrder) {
Expand Down
9 changes: 6 additions & 3 deletions src/services/entities/commune-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
getSqlSorting,
isEntityReadOnly,
queryParametersToFindAllEntities,
ReadonlyStatus
ReadonlyStatus,
transformQueryRawResultsBigIntsToNumbers
} from "./entities-utils";

export const findCommune = async (
Expand Down Expand Up @@ -218,8 +219,10 @@ export const findPaginatedCommunes = async (
`;

const nbDonneesForFilteredCommunes = await prisma.$queryRaw<
{ id: number; nbLieuxDits: number; nbDonnees: number }[]
>`${donneesPerCommuneRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`;
{ id: number; nbLieuxDits: bigint; nbDonnees: bigint }[]
>`${donneesPerCommuneRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`.then(
transformQueryRawResultsBigIntsToNumbers
);

const communesRq = await prisma.commune.findMany({
where: {
Expand Down
9 changes: 6 additions & 3 deletions src/services/entities/departement-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
getSqlSorting,
isEntityReadOnly,
queryParametersToFindAllEntities,
ReadonlyStatus
ReadonlyStatus,
transformQueryRawResultsBigIntsToNumbers
} from "./entities-utils";

export const getFilterClauseDepartement = (q: string | null | undefined): Prisma.DepartementWhereInput => {
Expand Down Expand Up @@ -142,8 +143,10 @@ export const findPaginatedDepartements = async (
`;

const nbDonneesForFilteredDepartements = await prisma.$queryRaw<
{ id: number; ownerId: string; nbLieuxDits: number; nbDonnees: number }[]
>`${donneesPerDepartementRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`;
{ id: number; ownerId: string; nbLieuxDits: bigint; nbDonnees: bigint }[]
>`${donneesPerDepartementRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`.then(
transformQueryRawResultsBigIntsToNumbers
);

const departementsRq = await prisma.departement.findMany({
include: {
Expand Down
100 changes: 99 additions & 1 deletion src/services/entities/entities-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { DatabaseRole, Prisma } from "@prisma/client";
import { mock } from "jest-mock-extended";
import { LoggedUser } from "../../types/LoggedUser";
import { getSqlPagination, getSqlSorting, isEntityReadOnly } from "./entities-utils";
import {
getSqlPagination,
getSqlSorting,
isEntityReadOnly,
transformQueryRawBigIntsToNumbers,
transformQueryRawResultsBigIntsToNumbers
} from "./entities-utils";

test("should return correct readonly status for non logged user", () => {
const entity = mock<{ ownerId: string }>();
Expand Down Expand Up @@ -153,3 +159,95 @@ test("should return correct SQL sorting with all fields in input", () => {
}).text
).toEqual("ORDER BY toto desc");
});

describe("Methods that transform bigints returned by a query raw to numbers", () => {
test("should properly return a positive value for a single result", () => {
const result = {
nbDonnees: BigInt(13n),
toto: "tutu",
test: 75
};

expect(transformQueryRawBigIntsToNumbers(result)).toEqual({
nbDonnees: 13,
toto: "tutu",
test: 75
});
});

test("should properly return a zero value for a single result", () => {
const result = {
nbDonnees: BigInt(0n),
toto: "tutu",
test: 75
};

expect(transformQueryRawBigIntsToNumbers(result)).toEqual({
nbDonnees: 0,
toto: "tutu",
test: 75
});
});

test("should handle multiple bigints", () => {
const result = {
nbDonnees: BigInt(13n),
toto: "tutu",
test: 75,
nbEspeces: BigInt(7n)
};

expect(transformQueryRawBigIntsToNumbers(result)).toEqual({
nbDonnees: 13,
toto: "tutu",
test: 75,
nbEspeces: 7
});
});

test("should handle an array of results", () => {
const results = [
{
nbDonnees: BigInt(0n),
toto: "tutu",
test: 75
},
{
nbDonnees: BigInt(13n),
toto: "tutu",
test: 75,
nbEspeces: BigInt(32n)
},
{
somethingRandom: BigInt(17n)
},
{
otherRandom: "hello there"
}
];

expect(transformQueryRawResultsBigIntsToNumbers(results)).toEqual([
{
nbDonnees: 0,
toto: "tutu",
test: 75
},
{
nbDonnees: 13,
toto: "tutu",
test: 75,
nbEspeces: 32
},
{
somethingRandom: 17
},
{
otherRandom: "hello there"
}
]);
});

test("should handle an empty array of results as input", () => {
expect(transformQueryRawResultsBigIntsToNumbers([]).map((result) => result.nbDonnees)).toEqual([]);
});
});
17 changes: 17 additions & 0 deletions src/services/entities/entities-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DatabaseRole, Prisma } from ".prisma/client";
import { ConditionalPick } from "type-fest";
import { SortOrder } from "../../graphql/generated/graphql-types";
import { LoggedUser } from "../../types/LoggedUser";

Expand Down Expand Up @@ -74,3 +75,19 @@ export const queryParametersToFindAllEntities = (
}
return {};
};

export const transformQueryRawBigIntsToNumbers = <
T extends Record<string, unknown>,
U extends ConditionalPick<T, bigint>
>(
result: T
): Omit<T, keyof U> & Record<keyof U, number> => {
return Object.entries(result).reduce((obj, [key, value]) => {
obj[key] = typeof value === "bigint" ? Number(value) : value;
return obj;
}, {} as Record<string, unknown>) as Omit<T, keyof U> & Record<keyof U, number>;
};

export const transformQueryRawResultsBigIntsToNumbers = <T extends Record<string, unknown>>(results: T[]) => {
return results.map(transformQueryRawBigIntsToNumbers);
};
9 changes: 6 additions & 3 deletions src/services/entities/lieu-dit-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
getSqlSorting,
isEntityReadOnly,
queryParametersToFindAllEntities,
ReadonlyStatus
ReadonlyStatus,
transformQueryRawResultsBigIntsToNumbers
} from "./entities-utils";

export type LieuDitWithCoordinatesAsNumber<T extends Lieudit = Lieudit> = Omit<T, "latitude" | "longitude"> & {
Expand Down Expand Up @@ -217,8 +218,10 @@ export const findPaginatedLieuxDits = async (
`;

const nbDonneesForFilteredLieuxDits = await prisma.$queryRaw<
{ id: number; nbDonnees: number }[]
>`${donneesPerLieuDitRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`;
{ id: number; nbDonnees: bigint }[]
>`${donneesPerLieuDitRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`.then(
transformQueryRawResultsBigIntsToNumbers
);

const lieuxDitsRq = await prisma.lieudit.findMany({
include: {
Expand Down
9 changes: 6 additions & 3 deletions src/services/entities/meteo-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
getSqlSorting,
isEntityReadOnly,
queryParametersToFindAllEntities,
ReadonlyStatus
ReadonlyStatus,
transformQueryRawResultsBigIntsToNumbers
} from "./entities-utils";

export const findMeteo = async (
Expand Down Expand Up @@ -126,8 +127,10 @@ export const findPaginatedMeteos = async (
`;

meteoEntities = await prisma.$queryRaw<
(Meteo & { nbDonnees: number })[]
>`${donneesPerObservateurIdRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`;
(Meteo & { nbDonnees: bigint })[]
>`${donneesPerObservateurIdRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`.then(
transformQueryRawResultsBigIntsToNumbers
);
} else {
const orderBy = orderByField ? { [orderByField]: sortOrder } : {};

Expand Down
9 changes: 6 additions & 3 deletions src/services/entities/observateur-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
getSqlSorting,
isEntityReadOnly,
queryParametersToFindAllEntities,
ReadonlyStatus
ReadonlyStatus,
transformQueryRawResultsBigIntsToNumbers
} from "./entities-utils";

export const findObservateur = async (
Expand Down Expand Up @@ -122,8 +123,10 @@ export const findPaginatedObservateurs = async (
`;

observateurEntities = await prisma.$queryRaw<
(Observateur & { nbDonnees: number })[]
>`${donneesPerObservateurIdRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`;
(Observateur & { nbDonnees: bigint })[]
>`${donneesPerObservateurIdRequest} ${getSqlSorting(options)} ${getSqlPagination(searchParams)}`.then(
transformQueryRawResultsBigIntsToNumbers
);
} else {
const orderBy = orderByField ? { [orderByField]: sortOrder } : {};

Expand Down

0 comments on commit 94f3fb3

Please sign in to comment.