arocapi 2.0.4 → 3.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 (89) hide show
  1. package/README.md +81 -9
  2. package/dist/app.d.ts +6 -0
  3. package/dist/app.js +9 -3
  4. package/dist/app.js.map +1 -1
  5. package/dist/index.dev.js +11 -1
  6. package/dist/index.dev.js.map +1 -1
  7. package/dist/routes/crate.js +5 -18
  8. package/dist/routes/crate.js.map +1 -1
  9. package/dist/routes/crate.test.js +20 -22
  10. package/dist/routes/crate.test.js.map +1 -1
  11. package/dist/routes/entities.js +12 -10
  12. package/dist/routes/entities.js.map +1 -1
  13. package/dist/routes/entities.test.js +18 -18
  14. package/dist/routes/entities.test.js.map +1 -1
  15. package/dist/routes/entity.js +3 -4
  16. package/dist/routes/entity.js.map +1 -1
  17. package/dist/routes/entity.test.js +11 -16
  18. package/dist/routes/entity.test.js.map +1 -1
  19. package/dist/routes/file.js +5 -18
  20. package/dist/routes/file.js.map +1 -1
  21. package/dist/routes/file.test.js +18 -22
  22. package/dist/routes/file.test.js.map +1 -1
  23. package/dist/routes/files.js +12 -11
  24. package/dist/routes/files.js.map +1 -1
  25. package/dist/routes/files.test.js +5 -34
  26. package/dist/routes/files.test.js.map +1 -1
  27. package/dist/routes/search.d.ts +3 -0
  28. package/dist/routes/search.js +22 -128
  29. package/dist/routes/search.js.map +1 -1
  30. package/dist/routes/search.test.js +53 -23
  31. package/dist/routes/search.test.js.map +1 -1
  32. package/dist/test/integration.setup.d.ts +2 -4
  33. package/dist/test/integration.setup.js +18 -21
  34. package/dist/test/integration.setup.js.map +1 -1
  35. package/dist/transformers/default.d.ts +12 -19
  36. package/dist/transformers/default.js +5 -18
  37. package/dist/transformers/default.js.map +1 -1
  38. package/dist/transformers/default.test.js +6 -15
  39. package/dist/transformers/default.test.js.map +1 -1
  40. package/dist/transformers/transformer.test.js +2 -6
  41. package/dist/transformers/transformer.test.js.map +1 -1
  42. package/dist/utils/errors.d.ts +7 -1
  43. package/dist/utils/errors.js +2 -1
  44. package/dist/utils/errors.js.map +1 -1
  45. package/dist/utils/headers.d.ts +7 -0
  46. package/dist/utils/headers.js +11 -0
  47. package/dist/utils/headers.js.map +1 -0
  48. package/dist/utils/queryBuilder.d.ts +35 -0
  49. package/dist/utils/queryBuilder.js +98 -0
  50. package/dist/utils/queryBuilder.js.map +1 -0
  51. package/package.json +27 -23
  52. package/prisma/models/entity.prisma +17 -13
  53. package/prisma/models/file.prisma +9 -13
  54. package/prisma.config.ts +0 -2
  55. package/src/app.ts +15 -2
  56. package/src/index.dev.ts +12 -1
  57. package/src/routes/__snapshots__/search.test.ts.snap +2 -2
  58. package/src/routes/crate.test.ts +20 -22
  59. package/src/routes/crate.ts +6 -24
  60. package/src/routes/entities.test.ts +21 -18
  61. package/src/routes/entities.ts +12 -12
  62. package/src/routes/entity.test.ts +13 -16
  63. package/src/routes/entity.ts +3 -4
  64. package/src/routes/file.test.ts +18 -22
  65. package/src/routes/file.ts +6 -24
  66. package/src/routes/files.test.ts +5 -37
  67. package/src/routes/files.ts +12 -13
  68. package/src/routes/search.test.ts +62 -25
  69. package/src/routes/search.ts +34 -153
  70. package/src/test/integration.setup.ts +19 -21
  71. package/src/transformers/default.test.ts +10 -20
  72. package/src/transformers/default.ts +15 -28
  73. package/src/transformers/transformer.test.ts +4 -6
  74. package/src/utils/errors.ts +10 -6
  75. package/src/utils/headers.ts +16 -0
  76. package/src/utils/queryBuilder.ts +132 -0
  77. package/prisma/migrations/20250515034400_entities/migration.sql +0 -14
  78. package/prisma/migrations/20250515050009_extend_entity_fields/migration.sql +0 -7
  79. package/prisma/migrations/20250804225438_add_record_type/migration.sql +0 -8
  80. package/prisma/migrations/20250805040110_add_rocrate_field/migration.sql +0 -2
  81. package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +0 -10
  82. package/prisma/migrations/20250923032753_api_refactor/migration.sql +0 -39
  83. package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +0 -8
  84. package/prisma/migrations/20251020140329_add_meta_column/migration.sql +0 -2
  85. package/prisma/migrations/20251027010713_add_file_table/migration.sql +0 -17
  86. package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +0 -2
  87. package/prisma/migrations/20251211025510_remove_rocrate_column/migration.sql +0 -8
  88. package/prisma/migrations/migration_lock.toml +0 -3
  89. package/prisma/schema.prisma +0 -17
@@ -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",
@@ -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,9 +1,10 @@
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
5
  import type { FileMetadata, RoCrateHandler } from '../types/fileHandlers.js';
6
6
  import { createInternalError, createNotFoundError } from '../utils/errors.js';
7
+ import { setFileHeaders } from '../utils/headers.js';
7
8
 
8
9
  const paramsSchema = z.object({
9
10
  id: z.url(),
@@ -13,21 +14,6 @@ type CrateRouteOptions = {
13
14
  roCrateHandler: RoCrateHandler;
14
15
  };
15
16
 
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
17
  const crate: FastifyPluginAsync<CrateRouteOptions> = async (fastify, opts) => {
32
18
  const { roCrateHandler } = opts;
33
19
 
@@ -42,10 +28,8 @@ const crate: FastifyPluginAsync<CrateRouteOptions> = async (fastify, opts) => {
42
28
  const { id } = request.params;
43
29
 
44
30
  try {
45
- const entity = await fastify.prisma.entity.findFirst({
46
- where: {
47
- rocrateId: id,
48
- },
31
+ const entity = await fastify.prisma.entity.findUnique({
32
+ where: { id },
49
33
  });
50
34
 
51
35
  if (!entity) {
@@ -84,10 +68,8 @@ const crate: FastifyPluginAsync<CrateRouteOptions> = async (fastify, opts) => {
84
68
  const { id } = request.params;
85
69
 
86
70
  try {
87
- const entity = await fastify.prisma.entity.findFirst({
88
- where: {
89
- rocrateId: id,
90
- },
71
+ const entity = await fastify.prisma.entity.findUnique({
72
+ where: { id },
91
73
  });
92
74
 
93
75
  if (!entity) {
@@ -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
  });
@@ -252,8 +257,7 @@ describe('Entities Route', () => {
252
257
 
253
258
  const mockEntities = [
254
259
  {
255
- id: 1,
256
- rocrateId: 'http://example.com/entity/1',
260
+ id: 'http://example.com/entity/1',
257
261
  name: 'Test Entity 1',
258
262
  description: 'First test entity',
259
263
  entityType: 'http://pcdm.org/models#Collection',
@@ -282,12 +286,11 @@ describe('Entities Route', () => {
282
286
  it('should return null for memberOf/rootCollection when parent entity not found', async () => {
283
287
  const mockEntities = [
284
288
  {
285
- id: 1,
286
- rocrateId: 'http://example.com/entity/1',
289
+ id: 'http://example.com/entity/1',
287
290
  name: 'Test Entity 1',
288
291
  description: 'Entity with missing parent',
289
292
  entityType: 'http://pcdm.org/models#Object',
290
- fileId: null,
293
+
291
294
  memberOf: 'http://example.com/entity/deleted',
292
295
  rootCollection: 'http://example.com/entity/deleted',
293
296
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -52,18 +52,18 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
52
52
  };
53
53
  }
54
54
 
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 });
55
+ const [dbEntities, total] = await Promise.all([
56
+ fastify.prisma.entity.findMany({
57
+ where,
58
+ include: { file: { select: { id: true } } },
59
+ orderBy: {
60
+ [sort]: order,
61
+ },
62
+ skip: offset,
63
+ take: limit,
64
+ }),
65
+ fastify.prisma.entity.count({ where }),
66
+ ]);
67
67
 
68
68
  // Resolve memberOf and rootCollection references
69
69
  const refMap = await resolveEntityReferences(dbEntities, fastify.prisma);
@@ -18,12 +18,11 @@ describe('Entity Route', () => {
18
18
  describe('GET /entity/:id', () => {
19
19
  it('should return entity when found', async () => {
20
20
  const mockEntity = {
21
- id: 1,
22
- rocrateId: 'http://example.com/entity/123',
21
+ id: 'http://example.com/entity/123',
23
22
  name: 'Test Entity',
24
23
  description: 'A test entity',
25
24
  entityType: 'http://schema.org/Person',
26
- fileId: null,
25
+
27
26
  memberOf: null,
28
27
  rootCollection: null,
29
28
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -33,7 +32,7 @@ describe('Entity Route', () => {
33
32
  meta: {},
34
33
  };
35
34
 
36
- prisma.entity.findFirst.mockResolvedValue(mockEntity);
35
+ prisma.entity.findUnique.mockResolvedValue(mockEntity);
37
36
 
38
37
  const response = await fastify.inject({
39
38
  method: 'GET',
@@ -43,16 +42,16 @@ describe('Entity Route', () => {
43
42
 
44
43
  expect(response.statusCode).toBe(200);
45
44
  expect(body).toMatchSnapshot();
46
- expect(prisma.entity.findFirst).toHaveBeenCalledWith({
45
+ expect(prisma.entity.findUnique).toHaveBeenCalledWith({
47
46
  where: {
48
- rocrateId: 'http://example.com/entity/123',
47
+ id: 'http://example.com/entity/123',
49
48
  },
49
+ include: { file: { select: { id: true } } },
50
50
  });
51
51
  });
52
52
 
53
53
  it('should return 404 when entity not found', async () => {
54
- console.log('🪚 ♊');
55
- prisma.entity.findFirst.mockResolvedValue(null);
54
+ prisma.entity.findUnique.mockResolvedValue(null);
56
55
 
57
56
  const response = await fastify.inject({
58
57
  method: 'GET',
@@ -65,7 +64,7 @@ describe('Entity Route', () => {
65
64
  });
66
65
 
67
66
  it('should return 500 when database error occurs', async () => {
68
- prisma.entity.findFirst.mockRejectedValue(new Error('Database connection failed'));
67
+ prisma.entity.findUnique.mockRejectedValue(new Error('Database connection failed'));
69
68
 
70
69
  const response = await fastify.inject({
71
70
  method: 'GET',
@@ -102,8 +101,7 @@ describe('Entity Route', () => {
102
101
  });
103
102
 
104
103
  const mockEntity = {
105
- id: 1,
106
- rocrateId: 'http://example.com/entity/123',
104
+ id: 'http://example.com/entity/123',
107
105
  name: 'Test Entity',
108
106
  description: 'A test entity',
109
107
  entityType: 'http://pcdm.org/models#Collection',
@@ -116,7 +114,7 @@ describe('Entity Route', () => {
116
114
  };
117
115
 
118
116
  // @ts-expect-error TS is looking at the wronf function signature
119
- prisma.entity.findFirst.mockResolvedValue(mockEntity);
117
+ prisma.entity.findUnique.mockResolvedValue(mockEntity);
120
118
 
121
119
  const response = await fastify.inject({
122
120
  method: 'GET',
@@ -130,12 +128,11 @@ describe('Entity Route', () => {
130
128
 
131
129
  it('should return null for memberOf/rootCollection when parent entity not found', async () => {
132
130
  const mockEntity = {
133
- id: 1,
134
- rocrateId: 'http://example.com/entity/123',
131
+ id: 'http://example.com/entity/123',
135
132
  name: 'Test Entity',
136
133
  description: 'A test entity',
137
134
  entityType: 'http://pcdm.org/models#Object',
138
- fileId: null,
135
+
139
136
  memberOf: 'http://example.com/entity/deleted',
140
137
  rootCollection: 'http://example.com/entity/deleted',
141
138
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -146,7 +143,7 @@ describe('Entity Route', () => {
146
143
  };
147
144
 
148
145
  // First call returns the entity, second call (for reference resolution) returns empty
149
- prisma.entity.findFirst.mockResolvedValue(mockEntity);
146
+ prisma.entity.findUnique.mockResolvedValue(mockEntity);
150
147
  prisma.entity.findMany.mockResolvedValue([]);
151
148
 
152
149
  const response = await fastify.inject({
@@ -27,10 +27,9 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
27
27
  const { id } = request.params;
28
28
 
29
29
  try {
30
- const entity = await fastify.prisma.entity.findFirst({
31
- where: {
32
- rocrateId: id,
33
- },
30
+ const entity = await fastify.prisma.entity.findUnique({
31
+ where: { id },
32
+ include: { file: { select: { id: true } } },
34
33
  });
35
34
 
36
35
  if (!entity) {