arocapi 2.1.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/README.md +70 -26
  2. package/dist/app.d.ts +3 -9
  3. package/dist/app.js +20 -34
  4. package/dist/app.js.map +1 -1
  5. package/dist/app.test.js +5 -40
  6. package/dist/app.test.js.map +1 -1
  7. package/dist/index.dev.d.ts +1 -8
  8. package/dist/index.dev.js +12 -2
  9. package/dist/index.dev.js.map +1 -1
  10. package/dist/routes/crate.d.ts +2 -0
  11. package/dist/routes/crate.js +8 -27
  12. package/dist/routes/crate.js.map +1 -1
  13. package/dist/routes/crate.test.js +21 -23
  14. package/dist/routes/crate.test.js.map +1 -1
  15. package/dist/routes/entities.d.ts +2 -0
  16. package/dist/routes/entities.js +16 -20
  17. package/dist/routes/entities.js.map +1 -1
  18. package/dist/routes/entities.test.js +20 -19
  19. package/dist/routes/entities.test.js.map +1 -1
  20. package/dist/routes/entity.d.ts +2 -0
  21. package/dist/routes/entity.js +7 -14
  22. package/dist/routes/entity.js.map +1 -1
  23. package/dist/routes/entity.test.js +13 -17
  24. package/dist/routes/entity.test.js.map +1 -1
  25. package/dist/routes/file.d.ts +2 -0
  26. package/dist/routes/file.js +8 -27
  27. package/dist/routes/file.js.map +1 -1
  28. package/dist/routes/file.test.js +19 -23
  29. package/dist/routes/file.test.js.map +1 -1
  30. package/dist/routes/files.d.ts +2 -0
  31. package/dist/routes/files.js +15 -20
  32. package/dist/routes/files.js.map +1 -1
  33. package/dist/routes/files.test.js +13 -35
  34. package/dist/routes/files.test.js.map +1 -1
  35. package/dist/routes/search.d.ts +4 -0
  36. package/dist/routes/search.js +21 -24
  37. package/dist/routes/search.js.map +1 -1
  38. package/dist/routes/search.test.js +56 -24
  39. package/dist/routes/search.test.js.map +1 -1
  40. package/dist/test/helpers/fastify.js +0 -6
  41. package/dist/test/helpers/fastify.js.map +1 -1
  42. package/dist/test/integration.setup.d.ts +2 -4
  43. package/dist/test/integration.setup.js +41 -21
  44. package/dist/test/integration.setup.js.map +1 -1
  45. package/dist/test/integration.test.js +59 -0
  46. package/dist/test/integration.test.js.map +1 -1
  47. package/dist/transformers/default.d.ts +11 -18
  48. package/dist/transformers/default.js +5 -18
  49. package/dist/transformers/default.js.map +1 -1
  50. package/dist/transformers/default.test.js +6 -15
  51. package/dist/transformers/default.test.js.map +1 -1
  52. package/dist/transformers/transformer.test.js +2 -6
  53. package/dist/transformers/transformer.test.js.map +1 -1
  54. package/dist/utils/errors.d.ts +7 -1
  55. package/dist/utils/errors.js +2 -1
  56. package/dist/utils/errors.js.map +1 -1
  57. package/dist/utils/headers.d.ts +7 -0
  58. package/dist/utils/headers.js +11 -0
  59. package/dist/utils/headers.js.map +1 -0
  60. package/dist/utils/queryBuilder.d.ts +2 -2
  61. package/dist/utils/queryBuilder.js +2 -3
  62. package/dist/utils/queryBuilder.js.map +1 -1
  63. package/package.json +24 -24
  64. package/prisma/models/entity.prisma +17 -13
  65. package/prisma/models/file.prisma +9 -13
  66. package/prisma.config.ts +0 -2
  67. package/src/app.test.ts +5 -44
  68. package/src/app.ts +25 -47
  69. package/src/index.dev.ts +13 -9
  70. package/src/routes/__snapshots__/search.test.ts.snap +2 -2
  71. package/src/routes/crate.test.ts +21 -23
  72. package/src/routes/crate.ts +11 -33
  73. package/src/routes/entities.test.ts +23 -19
  74. package/src/routes/entities.ts +19 -23
  75. package/src/routes/entity.test.ts +15 -17
  76. package/src/routes/entity.ts +9 -14
  77. package/src/routes/file.test.ts +19 -23
  78. package/src/routes/file.ts +11 -33
  79. package/src/routes/files.test.ts +14 -39
  80. package/src/routes/files.ts +18 -23
  81. package/src/routes/search.test.ts +65 -26
  82. package/src/routes/search.ts +30 -23
  83. package/src/test/helpers/fastify.ts +0 -12
  84. package/src/test/integration.setup.ts +44 -21
  85. package/src/test/integration.test.ts +72 -1
  86. package/src/transformers/default.test.ts +10 -20
  87. package/src/transformers/default.ts +14 -27
  88. package/src/transformers/transformer.test.ts +4 -6
  89. package/src/utils/errors.ts +10 -6
  90. package/src/utils/headers.ts +16 -0
  91. package/src/utils/queryBuilder.ts +3 -5
  92. package/prisma/migrations/20250515034400_entities/migration.sql +0 -14
  93. package/prisma/migrations/20250515050009_extend_entity_fields/migration.sql +0 -7
  94. package/prisma/migrations/20250804225438_add_record_type/migration.sql +0 -8
  95. package/prisma/migrations/20250805040110_add_rocrate_field/migration.sql +0 -2
  96. package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +0 -10
  97. package/prisma/migrations/20250923032753_api_refactor/migration.sql +0 -39
  98. package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +0 -8
  99. package/prisma/migrations/20251020140329_add_meta_column/migration.sql +0 -2
  100. package/prisma/migrations/20251027010713_add_file_table/migration.sql +0 -17
  101. package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +0 -2
  102. package/prisma/migrations/20251211025510_remove_rocrate_column/migration.sql +0 -8
  103. package/prisma/migrations/migration_lock.toml +0 -3
  104. package/prisma/schema.prisma +0 -17
  105. package/src/__snapshots__/app.test.ts.snap +0 -8
package/src/index.dev.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createReadStream, existsSync, readFileSync, statSync } from 'node:fs';
2
2
  import { Readable } from 'node:stream';
3
3
  import { Client } from '@opensearch-project/opensearch';
4
+ import { PrismaMariaDb } from '@prisma/adapter-mariadb';
4
5
  import Fastify from 'fastify';
5
6
 
6
7
  import arocapi, {
@@ -13,14 +14,17 @@ import arocapi, {
13
14
  import type { Entity, File } from './generated/prisma/client.js';
14
15
  import { PrismaClient } from './generated/prisma/client.js';
15
16
 
16
- declare module 'fastify' {
17
- interface FastifyInstance {
18
- prisma: PrismaClient;
19
- opensearch: Client;
20
- }
21
- }
22
-
23
- const prisma = new PrismaClient();
17
+ const adapter = new PrismaMariaDb({
18
+ host: 'localhost',
19
+ user: 'root',
20
+ password: 'password',
21
+ database: 'catalog',
22
+ port: 3306,
23
+ connectionLimit: 5,
24
+ // NOTE: This is required when using the default MariaDB server configuration with SSL disabled. In production, you should properly configure SSL and remove this option.
25
+ allowPublicKeyRetrieval: true,
26
+ });
27
+ const prisma = new PrismaClient({ adapter });
24
28
 
25
29
  if (!process.env.OPENSEARCH_URL) {
26
30
  throw new Error('OPENSEARCH_URL environment variable is not set');
@@ -160,7 +164,7 @@ const roCrateHandler: RoCrateHandler = {
160
164
  },
161
165
  };
162
166
 
163
- const oniTransformer: EntityTransformer = (entity, { request: _r, fastify: _f }) => {
167
+ const oniTransformer: EntityTransformer = (entity) => {
164
168
  return {
165
169
  ...entity,
166
170
  accessControl: 'Public',
@@ -28,7 +28,7 @@ exports[`Search Route > POST /search > should apply custom entity transformers 1
28
28
  }
29
29
  `;
30
30
 
31
- exports[`Search Route > POST /search > should handle opensearch errors 1`] = `
31
+ exports[`Search Route > POST /search > should handle id explicitly set to undefined 1`] = `
32
32
  {
33
33
  "error": {
34
34
  "code": "INTERNAL_ERROR",
@@ -37,7 +37,7 @@ exports[`Search Route > POST /search > should handle opensearch errors 1`] = `
37
37
  }
38
38
  `;
39
39
 
40
- exports[`Search Route > POST /search > should handle rocrateId explicitly set to undefined 1`] = `
40
+ exports[`Search Route > POST /search > should handle opensearch errors 1`] = `
41
41
  {
42
42
  "error": {
43
43
  "code": "INTERNAL_ERROR",
@@ -18,7 +18,7 @@ describe('Crate Route', () => {
18
18
 
19
19
  beforeEach(async () => {
20
20
  await fastifyBefore();
21
- await fastify.register(crateRoute, { roCrateHandler: mockRoCrateHandler });
21
+ await fastify.register(crateRoute, { prisma, roCrateHandler: mockRoCrateHandler });
22
22
  vi.clearAllMocks();
23
23
  });
24
24
 
@@ -27,12 +27,10 @@ describe('Crate Route', () => {
27
27
  });
28
28
 
29
29
  const mockFileEntity = {
30
- id: 1,
31
- rocrateId: 'http://example.com/entity/file.wav',
30
+ id: 'http://example.com/entity/file.wav',
32
31
  name: 'test.wav',
33
32
  description: 'A test file',
34
33
  entityType: 'http://schema.org/MediaObject',
35
- fileId: null,
36
34
  memberOf: 'http://example.com/collection',
37
35
  rootCollection: 'http://example.com/collection',
38
36
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -44,7 +42,7 @@ describe('Crate Route', () => {
44
42
 
45
43
  const mockCollectionEntity = {
46
44
  ...mockFileEntity,
47
- rocrateId: 'http://example.com/collection',
45
+ id: 'http://example.com/collection',
48
46
  name: 'Test Collection',
49
47
  entityType: 'http://pcdm.org/models#Collection',
50
48
  memberOf: null,
@@ -53,7 +51,7 @@ describe('Crate Route', () => {
53
51
 
54
52
  const mockObjectEntity = {
55
53
  ...mockFileEntity,
56
- rocrateId: 'http://example.com/object',
54
+ id: 'http://example.com/object',
57
55
  name: 'Test Object',
58
56
  entityType: 'http://pcdm.org/models#Object',
59
57
  memberOf: 'http://example.com/collection',
@@ -62,7 +60,7 @@ describe('Crate Route', () => {
62
60
 
63
61
  describe('GET /entity/:id/rocrate', () => {
64
62
  it('should stream RO-Crate metadata for File entity', async () => {
65
- prisma.entity.findFirst.mockResolvedValue(mockFileEntity);
63
+ prisma.entity.findUnique.mockResolvedValue(mockFileEntity);
66
64
 
67
65
  const mockStream = Readable.from(['{"@context": "https://w3id.org/ro/crate/1.1/context"}']);
68
66
  const mockResult: FileResult = {
@@ -98,7 +96,7 @@ describe('Crate Route', () => {
98
96
  });
99
97
 
100
98
  it('should stream RO-Crate metadata for Collection entity', async () => {
101
- prisma.entity.findFirst.mockResolvedValue(mockCollectionEntity);
99
+ prisma.entity.findUnique.mockResolvedValue(mockCollectionEntity);
102
100
 
103
101
  const mockStream = Readable.from(['{"@context": "https://w3id.org/ro/crate/1.1/context"}']);
104
102
  const mockResult: FileResult = {
@@ -122,7 +120,7 @@ describe('Crate Route', () => {
122
120
  });
123
121
 
124
122
  it('should stream RO-Crate metadata for Object entity', async () => {
125
- prisma.entity.findFirst.mockResolvedValue(mockObjectEntity);
123
+ prisma.entity.findUnique.mockResolvedValue(mockObjectEntity);
126
124
 
127
125
  const mockStream = Readable.from(['{"@context": "https://w3id.org/ro/crate/1.1/context"}']);
128
126
  const mockResult: FileResult = {
@@ -146,7 +144,7 @@ describe('Crate Route', () => {
146
144
  });
147
145
 
148
146
  it('should handle redirect response', async () => {
149
- prisma.entity.findFirst.mockResolvedValue(mockFileEntity);
147
+ prisma.entity.findUnique.mockResolvedValue(mockFileEntity);
150
148
 
151
149
  const mockResult: FileResult = {
152
150
  type: 'redirect',
@@ -164,7 +162,7 @@ describe('Crate Route', () => {
164
162
  });
165
163
 
166
164
  it('should handle file path response without nginx X-Accel-Redirect', async () => {
167
- prisma.entity.findFirst.mockResolvedValue(mockCollectionEntity);
165
+ prisma.entity.findUnique.mockResolvedValue(mockCollectionEntity);
168
166
 
169
167
  const mockStream = Readable.from(['rocrate content']);
170
168
  vi.mocked(createReadStream).mockReturnValue(mockStream as never);
@@ -192,7 +190,7 @@ describe('Crate Route', () => {
192
190
  });
193
191
 
194
192
  it('should handle file path response with nginx X-Accel-Redirect', async () => {
195
- prisma.entity.findFirst.mockResolvedValue(mockFileEntity);
193
+ prisma.entity.findUnique.mockResolvedValue(mockFileEntity);
196
194
 
197
195
  const mockResult: FileResult = {
198
196
  type: 'file',
@@ -217,7 +215,7 @@ describe('Crate Route', () => {
217
215
  });
218
216
 
219
217
  it('should return 404 when entity not found', async () => {
220
- prisma.entity.findFirst.mockResolvedValue(null);
218
+ prisma.entity.findUnique.mockResolvedValue(null);
221
219
 
222
220
  const response = await fastify.inject({
223
221
  method: 'GET',
@@ -232,7 +230,7 @@ describe('Crate Route', () => {
232
230
  });
233
231
 
234
232
  it('should return 404 when handler returns false', async () => {
235
- prisma.entity.findFirst.mockResolvedValue(mockFileEntity);
233
+ prisma.entity.findUnique.mockResolvedValue(mockFileEntity);
236
234
  vi.mocked(mockRoCrateHandler.get).mockResolvedValue(false);
237
235
 
238
236
  const response = await fastify.inject({
@@ -247,7 +245,7 @@ describe('Crate Route', () => {
247
245
  });
248
246
 
249
247
  it('should return 500 when database error occurs', async () => {
250
- prisma.entity.findFirst.mockRejectedValue(new Error('Database connection failed'));
248
+ prisma.entity.findUnique.mockRejectedValue(new Error('Database connection failed'));
251
249
 
252
250
  const response = await fastify.inject({
253
251
  method: 'GET',
@@ -260,7 +258,7 @@ describe('Crate Route', () => {
260
258
  });
261
259
 
262
260
  it('should return 500 when roCrateHandler throws error', async () => {
263
- prisma.entity.findFirst.mockResolvedValue(mockFileEntity);
261
+ prisma.entity.findUnique.mockResolvedValue(mockFileEntity);
264
262
  vi.mocked(mockRoCrateHandler.get).mockRejectedValue(new Error('RO-Crate error'));
265
263
 
266
264
  const response = await fastify.inject({
@@ -287,7 +285,7 @@ describe('Crate Route', () => {
287
285
 
288
286
  describe('HEAD /entity/:id/rocrate', () => {
289
287
  it('should return RO-Crate metadata headers for File entity', async () => {
290
- prisma.entity.findFirst.mockResolvedValue(mockFileEntity);
288
+ prisma.entity.findUnique.mockResolvedValue(mockFileEntity);
291
289
 
292
290
  const mockMetadata = {
293
291
  contentType: 'application/ld+json',
@@ -318,7 +316,7 @@ describe('Crate Route', () => {
318
316
  });
319
317
 
320
318
  it('should return RO-Crate metadata headers for Collection entity', async () => {
321
- prisma.entity.findFirst.mockResolvedValue(mockCollectionEntity);
319
+ prisma.entity.findUnique.mockResolvedValue(mockCollectionEntity);
322
320
 
323
321
  const mockMetadata = {
324
322
  contentType: 'text/plain',
@@ -338,7 +336,7 @@ describe('Crate Route', () => {
338
336
  });
339
337
 
340
338
  it('should return RO-Crate metadata headers for Object entity', async () => {
341
- prisma.entity.findFirst.mockResolvedValue(mockObjectEntity);
339
+ prisma.entity.findUnique.mockResolvedValue(mockObjectEntity);
342
340
 
343
341
  const mockMetadata = {
344
342
  contentType: 'application/json',
@@ -358,7 +356,7 @@ describe('Crate Route', () => {
358
356
  });
359
357
 
360
358
  it('should return 404 when entity not found', async () => {
361
- prisma.entity.findFirst.mockResolvedValue(null);
359
+ prisma.entity.findUnique.mockResolvedValue(null);
362
360
 
363
361
  const response = await fastify.inject({
364
362
  method: 'HEAD',
@@ -372,7 +370,7 @@ describe('Crate Route', () => {
372
370
  });
373
371
 
374
372
  it('should return 404 when head handler returns false', async () => {
375
- prisma.entity.findFirst.mockResolvedValue(mockFileEntity);
373
+ prisma.entity.findUnique.mockResolvedValue(mockFileEntity);
376
374
  vi.mocked(mockRoCrateHandler.head).mockResolvedValue(false);
377
375
 
378
376
  const response = await fastify.inject({
@@ -387,7 +385,7 @@ describe('Crate Route', () => {
387
385
  });
388
386
 
389
387
  it('should return 500 when head handler throws error', async () => {
390
- prisma.entity.findFirst.mockResolvedValue(mockFileEntity);
388
+ prisma.entity.findUnique.mockResolvedValue(mockFileEntity);
391
389
  vi.mocked(mockRoCrateHandler.head).mockRejectedValue(new Error('Metadata error'));
392
390
 
393
391
  const response = await fastify.inject({
@@ -403,7 +401,7 @@ describe('Crate Route', () => {
403
401
 
404
402
  describe('GET /entity/:id/rocrate - exhaustiveness check', () => {
405
403
  it('should handle unexpected RO-Crate result type', async () => {
406
- prisma.entity.findFirst.mockResolvedValue(mockFileEntity);
404
+ prisma.entity.findUnique.mockResolvedValue(mockFileEntity);
407
405
 
408
406
  // Mock an invalid result type to test exhaustiveness check
409
407
  const invalidResult = {
@@ -1,35 +1,23 @@
1
1
  import { createReadStream } from 'node:fs';
2
- import type { FastifyPluginAsync, FastifyReply } from 'fastify';
2
+ import type { FastifyPluginAsync } from 'fastify';
3
3
  import type { ZodTypeProvider } from 'fastify-type-provider-zod';
4
4
  import { z } from 'zod/v4';
5
+ import type { PrismaClient } from '../generated/prisma/client.js';
5
6
  import type { FileMetadata, RoCrateHandler } from '../types/fileHandlers.js';
6
7
  import { createInternalError, createNotFoundError } from '../utils/errors.js';
8
+ import { setFileHeaders } from '../utils/headers.js';
7
9
 
8
10
  const paramsSchema = z.object({
9
11
  id: z.url(),
10
12
  });
11
13
 
12
14
  type CrateRouteOptions = {
15
+ prisma: PrismaClient;
13
16
  roCrateHandler: RoCrateHandler;
14
17
  };
15
18
 
16
- const setFileHeaders = (
17
- reply: FastifyReply,
18
- metadata: { contentType: string; contentLength: number; etag?: string; lastModified?: Date },
19
- ) => {
20
- reply.header('Content-Type', metadata.contentType);
21
- reply.header('Content-Length', metadata.contentLength.toString());
22
-
23
- if (metadata.etag) {
24
- reply.header('ETag', metadata.etag);
25
- }
26
- if (metadata.lastModified) {
27
- reply.header('Last-Modified', metadata.lastModified.toUTCString());
28
- }
29
- };
30
-
31
19
  const crate: FastifyPluginAsync<CrateRouteOptions> = async (fastify, opts) => {
32
- const { roCrateHandler } = opts;
20
+ const { prisma, roCrateHandler } = opts;
33
21
 
34
22
  fastify.withTypeProvider<ZodTypeProvider>().head(
35
23
  '/entity/:id/rocrate',
@@ -42,20 +30,15 @@ const crate: FastifyPluginAsync<CrateRouteOptions> = async (fastify, opts) => {
42
30
  const { id } = request.params;
43
31
 
44
32
  try {
45
- const entity = await fastify.prisma.entity.findFirst({
46
- where: {
47
- rocrateId: id,
48
- },
33
+ const entity = await prisma.entity.findUnique({
34
+ where: { id },
49
35
  });
50
36
 
51
37
  if (!entity) {
52
38
  return reply.code(404).send(createNotFoundError('The requested entity was not found', id));
53
39
  }
54
40
 
55
- const metadata: FileMetadata | false = await roCrateHandler.head(entity, {
56
- request,
57
- fastify,
58
- });
41
+ const metadata: FileMetadata | false = await roCrateHandler.head(entity, { request, fastify });
59
42
 
60
43
  if (!metadata) {
61
44
  return reply.code(404).send(createNotFoundError('The requested RO-Crate metadata was not found', id));
@@ -84,20 +67,15 @@ const crate: FastifyPluginAsync<CrateRouteOptions> = async (fastify, opts) => {
84
67
  const { id } = request.params;
85
68
 
86
69
  try {
87
- const entity = await fastify.prisma.entity.findFirst({
88
- where: {
89
- rocrateId: id,
90
- },
70
+ const entity = await prisma.entity.findUnique({
71
+ where: { id },
91
72
  });
92
73
 
93
74
  if (!entity) {
94
75
  return reply.code(404).send(createNotFoundError('The requested entity was not found', id));
95
76
  }
96
77
 
97
- const result = await roCrateHandler.get(entity, {
98
- request,
99
- fastify,
100
- });
78
+ const result = await roCrateHandler.get(entity, { request, fastify });
101
79
 
102
80
  if (!result) {
103
81
  return reply.code(404).send(createNotFoundError('The requested RO-Crate could not be retrieved', id));
@@ -7,7 +7,7 @@ import entitiesRoute from './entities.js';
7
7
  describe('Entities Route', () => {
8
8
  beforeEach(async () => {
9
9
  await fastifyBefore();
10
- await fastify.register(entitiesRoute, { accessTransformer: AllPublicAccessTransformer });
10
+ await fastify.register(entitiesRoute, { prisma, accessTransformer: AllPublicAccessTransformer });
11
11
  });
12
12
 
13
13
  afterEach(async () => {
@@ -18,12 +18,11 @@ describe('Entities Route', () => {
18
18
  it('should return entities with default pagination', async () => {
19
19
  const mockEntities = [
20
20
  {
21
- id: 1,
22
- rocrateId: 'http://example.com/entity/1',
21
+ id: 'http://example.com/entity/1',
23
22
  name: 'Test Entity 1',
24
23
  description: 'First test entity',
25
24
  entityType: 'http://pcdm.org/models#Collection',
26
- fileId: null,
25
+
27
26
  memberOf: null,
28
27
  rootCollection: null,
29
28
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -33,12 +32,11 @@ describe('Entities Route', () => {
33
32
  meta: {},
34
33
  },
35
34
  {
36
- id: 2,
37
- rocrateId: 'http://example.com/entity/2',
35
+ id: 'http://example.com/entity/2',
38
36
  name: 'Test Entity 2',
39
37
  description: 'Second test entity',
40
38
  entityType: 'http://pcdm.org/models#Object',
41
- fileId: null,
39
+
42
40
  memberOf: 'http://example.com/entity/1',
43
41
  rootCollection: 'http://example.com/entity/1',
44
42
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -62,7 +60,8 @@ describe('Entities Route', () => {
62
60
  expect(body).toMatchSnapshot();
63
61
  expect(prisma.entity.findMany).toHaveBeenCalledWith({
64
62
  where: {},
65
- orderBy: { rocrateId: 'asc' },
63
+ include: { file: { select: { id: true } } },
64
+ orderBy: { id: 'asc' },
66
65
  skip: 0,
67
66
  take: 100,
68
67
  });
@@ -83,7 +82,8 @@ describe('Entities Route', () => {
83
82
  expect(response.statusCode).toBe(200);
84
83
  expect(prisma.entity.findMany).toHaveBeenCalledWith({
85
84
  where: { memberOf: 'http://example.com/collection/1' },
86
- orderBy: { rocrateId: 'asc' },
85
+ include: { file: { select: { id: true } } },
86
+ orderBy: { id: 'asc' },
87
87
  skip: 0,
88
88
  take: 100,
89
89
  });
@@ -104,7 +104,8 @@ describe('Entities Route', () => {
104
104
  expect(response.statusCode).toBe(200);
105
105
  expect(prisma.entity.findMany).toHaveBeenCalledWith({
106
106
  where: { entityType: { in: ['http://pcdm.org/models#Collection'] } },
107
- orderBy: { rocrateId: 'asc' },
107
+ include: { file: { select: { id: true } } },
108
+ orderBy: { id: 'asc' },
108
109
  skip: 0,
109
110
  take: 100,
110
111
  });
@@ -129,7 +130,8 @@ describe('Entities Route', () => {
129
130
  in: ['http://pcdm.org/models#Collection', 'http://pcdm.org/models#Object'],
130
131
  },
131
132
  },
132
- orderBy: { rocrateId: 'asc' },
133
+ include: { file: { select: { id: true } } },
134
+ orderBy: { id: 'asc' },
133
135
  skip: 0,
134
136
  take: 100,
135
137
  });
@@ -151,7 +153,8 @@ describe('Entities Route', () => {
151
153
  expect(response.statusCode).toBe(200);
152
154
  expect(prisma.entity.findMany).toHaveBeenCalledWith({
153
155
  where: {},
154
- orderBy: { rocrateId: 'asc' },
156
+ include: { file: { select: { id: true } } },
157
+ orderBy: { id: 'asc' },
155
158
  skip: 10,
156
159
  take: 50,
157
160
  });
@@ -173,13 +176,14 @@ describe('Entities Route', () => {
173
176
  expect(response.statusCode).toBe(200);
174
177
  expect(prisma.entity.findMany).toHaveBeenCalledWith({
175
178
  where: {},
179
+ include: { file: { select: { id: true } } },
176
180
  orderBy: { name: 'desc' },
177
181
  skip: 0,
178
182
  take: 100,
179
183
  });
180
184
  });
181
185
 
182
- it('should map id sort to rocrateId field', async () => {
186
+ it('should sort by id field directly', async () => {
183
187
  prisma.entity.findMany.mockResolvedValue([]);
184
188
  prisma.entity.count.mockResolvedValue(0);
185
189
 
@@ -194,7 +198,8 @@ describe('Entities Route', () => {
194
198
  expect(response.statusCode).toBe(200);
195
199
  expect(prisma.entity.findMany).toHaveBeenCalledWith({
196
200
  where: {},
197
- orderBy: { rocrateId: 'asc' },
201
+ include: { file: { select: { id: true } } },
202
+ orderBy: { id: 'asc' },
198
203
  skip: 0,
199
204
  take: 100,
200
205
  });
@@ -246,14 +251,14 @@ describe('Entities Route', () => {
246
251
 
247
252
  await fastifyBefore();
248
253
  await fastify.register(entitiesRoute, {
254
+ prisma,
249
255
  accessTransformer: AllPublicAccessTransformer,
250
256
  entityTransformers: [customTransformer],
251
257
  });
252
258
 
253
259
  const mockEntities = [
254
260
  {
255
- id: 1,
256
- rocrateId: 'http://example.com/entity/1',
261
+ id: 'http://example.com/entity/1',
257
262
  name: 'Test Entity 1',
258
263
  description: 'First test entity',
259
264
  entityType: 'http://pcdm.org/models#Collection',
@@ -282,12 +287,11 @@ describe('Entities Route', () => {
282
287
  it('should return null for memberOf/rootCollection when parent entity not found', async () => {
283
288
  const mockEntities = [
284
289
  {
285
- id: 1,
286
- rocrateId: 'http://example.com/entity/1',
290
+ id: 'http://example.com/entity/1',
287
291
  name: 'Test Entity 1',
288
292
  description: 'Entity with missing parent',
289
293
  entityType: 'http://pcdm.org/models#Object',
290
- fileId: null,
294
+
291
295
  memberOf: 'http://example.com/entity/deleted',
292
296
  rootCollection: 'http://example.com/entity/deleted',
293
297
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -1,6 +1,7 @@
1
1
  import type { FastifyPluginAsync } from 'fastify';
2
2
  import type { ZodTypeProvider } from 'fastify-type-provider-zod';
3
3
  import { z } from 'zod/v4';
4
+ import type { PrismaClient } from '../generated/prisma/client.js';
4
5
  import { baseEntityTransformer, resolveEntityReferences } from '../transformers/default.js';
5
6
  import type { AccessTransformer, EntityTransformer } from '../types/transformers.js';
6
7
  import { createInternalError } from '../utils/errors.js';
@@ -23,12 +24,13 @@ const querySchema = z.object({
23
24
  });
24
25
 
25
26
  type EntitiesRouteOptions = {
27
+ prisma: PrismaClient;
26
28
  accessTransformer: AccessTransformer;
27
29
  entityTransformers?: EntityTransformer[];
28
30
  };
29
31
 
30
32
  const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts) => {
31
- const { accessTransformer, entityTransformers = [] } = opts;
33
+ const { prisma, accessTransformer, entityTransformers = [] } = opts;
32
34
  fastify.withTypeProvider<ZodTypeProvider>().get(
33
35
  '/entities',
34
36
  {
@@ -40,7 +42,7 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
40
42
  const { memberOf, entityType, limit, offset, sort, order } = request.query;
41
43
 
42
44
  try {
43
- const where: NonNullable<Parameters<typeof fastify.prisma.entity.findMany>[0]>['where'] = {};
45
+ const where: NonNullable<Parameters<typeof prisma.entity.findMany>[0]>['where'] = {};
44
46
 
45
47
  if (memberOf) {
46
48
  where.memberOf = memberOf;
@@ -52,21 +54,21 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
52
54
  };
53
55
  }
54
56
 
55
- const sortField = sort === 'id' ? 'rocrateId' : sort;
56
-
57
- const dbEntities = await fastify.prisma.entity.findMany({
58
- where,
59
- orderBy: {
60
- [sortField]: order,
61
- },
62
- skip: offset,
63
- take: limit,
64
- });
65
-
66
- const total = await fastify.prisma.entity.count({ where });
57
+ const [dbEntities, total] = await Promise.all([
58
+ prisma.entity.findMany({
59
+ where,
60
+ include: { file: { select: { id: true } } },
61
+ orderBy: {
62
+ [sort]: order,
63
+ },
64
+ skip: offset,
65
+ take: limit,
66
+ }),
67
+ prisma.entity.count({ where }),
68
+ ]);
67
69
 
68
70
  // Resolve memberOf and rootCollection references
69
- const refMap = await resolveEntityReferences(dbEntities, fastify.prisma);
71
+ const refMap = await resolveEntityReferences(dbEntities, prisma);
70
72
 
71
73
  // Apply transformers to each entity: base -> access -> additional
72
74
  const entities = await Promise.all(
@@ -77,17 +79,11 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
77
79
  memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
78
80
  rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
79
81
  };
80
- const authorisedEntity = await accessTransformer(standardEntity, {
81
- request,
82
- fastify,
83
- });
82
+ const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
84
83
 
85
84
  let result = authorisedEntity;
86
85
  for (const transformer of entityTransformers) {
87
- result = await transformer(result, {
88
- request,
89
- fastify,
90
- });
86
+ result = await transformer(result, { request, fastify });
91
87
  }
92
88
 
93
89
  return result;