Skip to content

Commit 6466f3f

Browse files
committed
Release vintasend-prisma@0.8.2
1 parent 862c1e8 commit 6466f3f

3 files changed

Lines changed: 57 additions & 4 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vintasend-prisma",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"description": "VintaSend Backend implementation for Prisma",
55
"main": "dist/index.js",
66
"scripts": {
@@ -18,7 +18,7 @@
1818
"dependencies": {
1919
"@prisma/client": "^7.3.0",
2020
"prisma": "^7.3.0",
21-
"vintasend": "^0.8.1"
21+
"vintasend": "^0.8.2"
2222
},
2323
"devDependencies": {
2424
"@types/jest": "^30.0.0",

src/__tests__/prisma-notification-backend.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import type {
22
AnyDatabaseNotification,
33
DatabaseNotification,
44
} from 'vintasend/dist/types/notification';
5-
import type { NotificationFilter } from 'vintasend/dist/services/notification-backends/base-notification-backend';
5+
import type {
6+
NotificationFilter,
7+
NotificationOrderBy,
8+
} from 'vintasend/dist/services/notification-backends/base-notification-backend';
69
import { PrismaNotificationBackendFactory } from '../index';
710
import { NotificationStatusEnum, NotificationTypeEnum } from '../prisma-notification-backend';
811
import type {
@@ -1442,6 +1445,33 @@ describe('PrismaNotificationBackend', () => {
14421445
take: 10,
14431446
});
14441447
});
1448+
1449+
it.each([
1450+
{ field: 'sendAfter', direction: 'asc' },
1451+
{ field: 'sendAfter', direction: 'desc' },
1452+
{ field: 'sentAt', direction: 'asc' },
1453+
{ field: 'sentAt', direction: 'desc' },
1454+
{ field: 'readAt', direction: 'asc' },
1455+
{ field: 'readAt', direction: 'desc' },
1456+
{ field: 'createdAt', direction: 'asc' },
1457+
{ field: 'createdAt', direction: 'desc' },
1458+
{ field: 'updatedAt', direction: 'asc' },
1459+
{ field: 'updatedAt', direction: 'desc' },
1460+
] as NotificationOrderBy[])('should map orderBy $field $direction into prisma findMany', async (orderBy) => {
1461+
const findManyMock = mockPrismaClient.notification.findMany as jest.Mock;
1462+
findManyMock.mockResolvedValue([mockNotification]);
1463+
1464+
await backend.filterNotifications({}, 1, 10, orderBy);
1465+
1466+
expect(findManyMock).toHaveBeenCalledWith({
1467+
where: {},
1468+
orderBy: {
1469+
[orderBy.field]: orderBy.direction,
1470+
},
1471+
skip: 10,
1472+
take: 10,
1473+
});
1474+
});
14451475
});
14461476

14471477
describe('deserializeNotificationForUpdate', () => {

src/prisma-notification-backend.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { BaseNotificationBackend, NotificationFilter } from 'vintasend/dist/services/notification-backends/base-notification-backend';
1+
import type {
2+
BaseNotificationBackend,
3+
NotificationFilter,
4+
NotificationOrderBy,
5+
} from 'vintasend/dist/services/notification-backends/base-notification-backend';
26
import type { InputJsonValue, JsonValue } from 'vintasend/dist/types/json-values';
37
import type {
48
DatabaseNotification,
@@ -116,6 +120,14 @@ type PrismaStringFilter = {
116120
mode?: 'default' | 'insensitive';
117121
};
118122

123+
type PrismaOrderByField = 'sendAfter' | 'sentAt' | 'readAt' | 'createdAt' | 'updatedAt';
124+
125+
type PrismaOrderDirection = 'asc' | 'desc';
126+
127+
type PrismaOrderBy = {
128+
[key in PrismaOrderByField]?: PrismaOrderDirection;
129+
};
130+
119131
type PrismaNotificationWhereInput<NotificationIdType, UserIdType> = {
120132
AND?: PrismaNotificationWhereInput<NotificationIdType, UserIdType>[];
121133
OR?: PrismaNotificationWhereInput<NotificationIdType, UserIdType>[];
@@ -144,6 +156,7 @@ export interface NotificationPrismaClientInterface<NotificationIdType, UserIdTyp
144156
notification: {
145157
findMany(args?: {
146158
where?: PrismaNotificationWhereInput<NotificationIdType, UserIdType>;
159+
orderBy?: PrismaOrderBy;
147160
skip?: number;
148161
take?: number;
149162
include?: {
@@ -491,6 +504,12 @@ export class PrismaNotificationBackend<
491504
return where;
492505
}
493506

507+
private convertNotificationOrderByToPrismaOrderBy(orderBy: NotificationOrderBy): PrismaOrderBy {
508+
return {
509+
[orderBy.field]: orderBy.direction,
510+
};
511+
}
512+
494513
/**
495514
* Serialize a Prisma notification model to either DatabaseNotification or DatabaseOneOffNotification
496515
* based on whether it has a userId or not (internal implementation)
@@ -1503,10 +1522,14 @@ export class PrismaNotificationBackend<
15031522
filter: NotificationFilter<Config>,
15041523
page: number,
15051524
pageSize: number,
1525+
orderBy?: NotificationOrderBy,
15061526
): Promise<AnyDatabaseNotification<Config>[]> {
15071527
const where = this.convertNotificationFilterToPrismaWhere(filter);
15081528
const notifications = await this.prismaClient.notification.findMany({
15091529
where,
1530+
...(orderBy
1531+
? { orderBy: this.convertNotificationOrderByToPrismaOrderBy(orderBy) }
1532+
: {}),
15101533
skip: page * pageSize,
15111534
take: pageSize,
15121535
});

0 commit comments

Comments
 (0)