arocapi 1.1.0 → 1.2.2

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 (128) hide show
  1. package/LICENSE.md +5 -0
  2. package/README.md +459 -36
  3. package/dist/app.d.ts +19 -1
  4. package/dist/app.js +37 -29
  5. package/dist/app.js.map +1 -1
  6. package/dist/app.test.js +124 -0
  7. package/dist/app.test.js.map +1 -0
  8. package/dist/index.dev.js +146 -0
  9. package/dist/index.dev.js.map +1 -0
  10. package/dist/routes/crate.d.ts +7 -0
  11. package/dist/routes/crate.js +101 -0
  12. package/dist/routes/crate.js.map +1 -0
  13. package/dist/routes/crate.test.d.ts +1 -0
  14. package/dist/routes/crate.test.js +360 -0
  15. package/dist/routes/crate.test.js.map +1 -0
  16. package/dist/routes/entities.d.ts +6 -1
  17. package/dist/routes/entities.js +52 -39
  18. package/dist/routes/entities.js.map +1 -1
  19. package/dist/routes/entities.test.d.ts +1 -0
  20. package/dist/routes/entities.test.js +245 -0
  21. package/dist/routes/entities.test.js.map +1 -0
  22. package/dist/routes/entity.d.ts +6 -1
  23. package/dist/routes/entity.js +30 -8
  24. package/dist/routes/entity.js.map +1 -1
  25. package/dist/routes/entity.test.d.ts +1 -0
  26. package/dist/routes/entity.test.js +109 -0
  27. package/dist/routes/entity.test.js.map +1 -0
  28. package/dist/routes/file.d.ts +7 -0
  29. package/dist/routes/file.js +113 -0
  30. package/dist/routes/file.js.map +1 -0
  31. package/dist/routes/file.test.d.ts +1 -0
  32. package/dist/routes/file.test.js +319 -0
  33. package/dist/routes/file.test.js.map +1 -0
  34. package/dist/routes/files.d.ts +8 -0
  35. package/dist/routes/files.js +62 -0
  36. package/dist/routes/files.js.map +1 -0
  37. package/dist/routes/files.test.d.ts +1 -0
  38. package/dist/routes/files.test.js +244 -0
  39. package/dist/routes/files.test.js.map +1 -0
  40. package/dist/routes/search.d.ts +6 -1
  41. package/dist/routes/search.js +44 -19
  42. package/dist/routes/search.js.map +1 -1
  43. package/dist/routes/search.test.d.ts +1 -0
  44. package/dist/routes/search.test.js +698 -0
  45. package/dist/routes/search.test.js.map +1 -0
  46. package/dist/test/helpers/fastify.d.ts +9 -0
  47. package/dist/test/helpers/fastify.js +24 -0
  48. package/dist/test/helpers/fastify.js.map +1 -0
  49. package/dist/test/integration.setup.d.ts +58 -0
  50. package/dist/test/integration.setup.js +181 -0
  51. package/dist/test/integration.setup.js.map +1 -0
  52. package/dist/test/integration.test.d.ts +1 -0
  53. package/dist/test/integration.test.js +337 -0
  54. package/dist/test/integration.test.js.map +1 -0
  55. package/dist/transformers/default.d.ts +45 -0
  56. package/dist/transformers/default.js +46 -0
  57. package/dist/transformers/default.js.map +1 -0
  58. package/dist/transformers/default.test.d.ts +1 -0
  59. package/dist/transformers/default.test.js +177 -0
  60. package/dist/transformers/default.test.js.map +1 -0
  61. package/dist/transformers/transformer.test.d.ts +1 -0
  62. package/dist/transformers/transformer.test.js +176 -0
  63. package/dist/transformers/transformer.test.js.map +1 -0
  64. package/dist/types/fileHandlers.d.ts +41 -0
  65. package/dist/types/fileHandlers.js +2 -0
  66. package/dist/types/fileHandlers.js.map +1 -0
  67. package/dist/types/transformers.d.ts +10 -0
  68. package/dist/types/transformers.js +2 -0
  69. package/dist/types/transformers.js.map +1 -0
  70. package/dist/utils/errors.d.ts +23 -0
  71. package/dist/utils/errors.js +21 -0
  72. package/dist/utils/errors.js.map +1 -0
  73. package/dist/utils/errors.test.d.ts +1 -0
  74. package/dist/utils/errors.test.js +66 -0
  75. package/dist/utils/errors.test.js.map +1 -0
  76. package/package.json +35 -28
  77. package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +10 -0
  78. package/prisma/migrations/20250923032753_api_refactor/migration.sql +39 -0
  79. package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +8 -0
  80. package/prisma/migrations/20251020140329_add_meta_column/migration.sql +2 -0
  81. package/prisma/migrations/20251027010713_add_file_table/migration.sql +17 -0
  82. package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +2 -0
  83. package/prisma/models/entity.prisma +16 -12
  84. package/prisma/models/file.prisma +20 -0
  85. package/prisma.config.ts +6 -1
  86. package/src/__snapshots__/app.test.ts.snap +8 -0
  87. package/src/app.test.ts +158 -0
  88. package/src/app.ts +99 -36
  89. package/src/index.dev.ts +196 -0
  90. package/src/reset.d.ts +1 -0
  91. package/src/routes/__snapshots__/entities.test.ts.snap +68 -0
  92. package/src/routes/__snapshots__/entity.test.ts.snap +57 -0
  93. package/src/routes/__snapshots__/search.test.ts.snap +139 -0
  94. package/src/routes/crate.test.ts +441 -0
  95. package/src/routes/crate.ts +145 -0
  96. package/src/routes/entities.test.ts +284 -0
  97. package/src/routes/entities.ts +69 -48
  98. package/src/routes/entity.test.ts +132 -0
  99. package/src/routes/entity.ts +59 -22
  100. package/src/routes/file.test.ts +399 -0
  101. package/src/routes/file.ts +157 -0
  102. package/src/routes/files.test.ts +303 -0
  103. package/src/routes/files.ts +89 -0
  104. package/src/routes/search.test.ts +807 -0
  105. package/src/routes/search.ts +67 -25
  106. package/src/test/__snapshots__/integration.test.ts.snap +18 -0
  107. package/src/test/helpers/fastify.ts +39 -0
  108. package/src/test/integration.setup.ts +200 -0
  109. package/src/test/integration.test.ts +411 -0
  110. package/src/transformers/default.test.ts +199 -0
  111. package/src/transformers/default.ts +165 -0
  112. package/src/transformers/transformer.test.ts +233 -0
  113. package/src/types/fileHandlers.ts +65 -0
  114. package/src/types/transformers.ts +52 -0
  115. package/src/utils/errors.test.ts +75 -0
  116. package/src/utils/errors.ts +45 -0
  117. package/dist/index.js +0 -25
  118. package/dist/index.js.map +0 -1
  119. package/dist/scripts/loadEntities.js +0 -170
  120. package/dist/scripts/loadEntities.js.map +0 -1
  121. package/dist/type.d.ts +0 -6
  122. package/dist/type.js +0 -2
  123. package/dist/type.js.map +0 -1
  124. package/src/index.ts +0 -35
  125. package/src/scripts/loadEntities.ts +0 -223
  126. package/src/type.ts +0 -7
  127. /package/dist/{scripts/loadEntities.d.ts → app.test.d.ts} +0 -0
  128. /package/dist/{index.d.ts → index.dev.d.ts} +0 -0
@@ -4,6 +4,9 @@ import type { Search_Request, Search_RequestBody } from '@opensearch-project/ope
4
4
  import type { FastifyPluginAsync } from 'fastify';
5
5
  import type { ZodTypeProvider } from 'fastify-type-provider-zod';
6
6
  import { z } from 'zod/v4';
7
+ import { baseEntityTransformer } from '../transformers/default.js';
8
+ import type { AccessTransformer, EntityTransformer } from '../types/transformers.js';
9
+ import { createInternalError } from '../utils/errors.js';
7
10
 
8
11
  const boundingBoxSchema = z.object({
9
12
  topRight: z.object({
@@ -116,9 +119,9 @@ const buildAggregations = (
116
119
  size: 20,
117
120
  },
118
121
  },
119
- conformsTo: {
122
+ entityType: {
120
123
  terms: {
121
- field: 'conformsTo',
124
+ field: 'entityType',
122
125
  size: 20,
123
126
  },
124
127
  },
@@ -161,7 +164,13 @@ const buildSort = (sort: SearchParams['sort'], order: SearchParams['order']) =>
161
164
  return [{ [sortField]: order }];
162
165
  };
163
166
 
164
- const search: FastifyPluginAsync = async (fastify, _opts) => {
167
+ type SearchRouteOptions = {
168
+ accessTransformer: AccessTransformer;
169
+ entityTransformers?: EntityTransformer[];
170
+ };
171
+
172
+ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) => {
173
+ const { accessTransformer, entityTransformers = [] } = opts;
165
174
  fastify.withTypeProvider<ZodTypeProvider>().post(
166
175
  '/search',
167
176
  {
@@ -192,29 +201,62 @@ const search: FastifyPluginAsync = async (fastify, _opts) => {
192
201
 
193
202
  const response = await fastify.opensearch.search(opensearchQuery);
194
203
 
195
- // Transform response
204
+ if (!response.body?.hits?.hits) {
205
+ throw new Error('Invalid search response: missing hits data');
206
+ }
196
207
 
197
- const entities = response.body.hits.hits.map((hit) => {
198
- if (!hit._source) {
199
- throw new Error('Missing _source in search hit');
200
- }
201
- return {
202
- id: hit._source.rocrateId,
203
- name: hit._source.name,
204
- description: hit._source.description,
205
- conformsTo: hit._source.conformsTo,
206
- recordType: hit._source.recordType,
207
- memberOf: hit._source.memberOf,
208
- root: hit._source.root,
209
- createdAt: hit._source.createdAt,
210
- updatedAt: hit._source.updatedAt,
211
- searchExtra: {
212
- score: hit._score,
213
- highlight: hit.highlight,
208
+ const rocrateIds = response.body.hits.hits
209
+ .map((hit) => hit._source?.rocrateId as string | undefined)
210
+ .filter(Boolean);
211
+
212
+ const dbEntities = await fastify.prisma.entity.findMany({
213
+ where: {
214
+ rocrateId: {
215
+ in: rocrateIds,
214
216
  },
215
- };
217
+ },
216
218
  });
217
219
 
220
+ const entityMap = new Map(dbEntities.map((entity) => [entity.rocrateId, entity]));
221
+
222
+ const entities = await Promise.all(
223
+ response.body.hits.hits.map(async (hit) => {
224
+ if (!hit._source?.rocrateId) {
225
+ throw new Error('Missing rocrateId in search hit');
226
+ }
227
+
228
+ const dbEntity = entityMap.get(hit._source.rocrateId);
229
+ if (!dbEntity) {
230
+ fastify.log.warn(`Entity ${hit._source.rocrateId} found in OpenSearch but not in database`);
231
+
232
+ return null;
233
+ }
234
+
235
+ const standardEntity = baseEntityTransformer(dbEntity);
236
+ const authorisedEntity = await accessTransformer(standardEntity, {
237
+ request,
238
+ fastify,
239
+ });
240
+
241
+ let result = authorisedEntity;
242
+ for (const transformer of entityTransformers) {
243
+ result = await transformer(result, {
244
+ request,
245
+ fastify,
246
+ });
247
+ }
248
+
249
+ // Add search-specific metadata
250
+ return {
251
+ ...(result as Record<string, unknown>),
252
+ searchExtra: {
253
+ score: hit._score,
254
+ highlight: hit.highlight,
255
+ },
256
+ };
257
+ }),
258
+ ).then((results) => results.filter(Boolean));
259
+
218
260
  const facets: Record<string, Array<{ name: string; count: number }>> = {};
219
261
  if (response.body.aggregations) {
220
262
  Object.keys(response.body.aggregations).forEach((key) => {
@@ -241,6 +283,7 @@ const search: FastifyPluginAsync = async (fastify, _opts) => {
241
283
  }
242
284
  }
243
285
 
286
+ /* v8 ignore next 3 -- Not sure how to force opensearch to hit the other path -- @preserve */
244
287
  const total =
245
288
  typeof response.body.hits.total === 'number'
246
289
  ? response.body.hits.total
@@ -257,10 +300,9 @@ const search: FastifyPluginAsync = async (fastify, _opts) => {
257
300
  return result;
258
301
  } catch (error) {
259
302
  const err = error as Error;
260
- console.error(err);
261
- fastify.log.error('Search error:', err.message);
303
+ fastify.log.error(`Search error: ${err.message}`);
262
304
 
263
- return reply.code(500).send({ message: 'Search failed' });
305
+ return reply.code(500).send(createInternalError('Search failed'));
264
306
  }
265
307
  },
266
308
  );
@@ -0,0 +1,18 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`Integration Tests > GET /entity/:id > should return entity from database 1`] = `
4
+ {
5
+ "access": {
6
+ "content": true,
7
+ "metadata": true,
8
+ },
9
+ "contentLicenseId": "https://creativecommons.org/licenses/by/4.0/",
10
+ "description": "First test entity",
11
+ "entityType": "http://pcdm.org/models#Collection",
12
+ "id": "http://example.com/entity/1",
13
+ "memberOf": null,
14
+ "metadataLicenseId": "https://creativecommons.org/licenses/by/4.0/",
15
+ "name": "Test Collection",
16
+ "rootCollection": null,
17
+ }
18
+ `;
@@ -0,0 +1,39 @@
1
+ import type { Client } from '@opensearch-project/opensearch';
2
+ import type { FastifyInstance } from 'fastify';
3
+ import Fastify from 'fastify';
4
+ import { serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
5
+ import { mockDeep, mockReset } from 'vitest-mock-extended';
6
+
7
+ import type { PrismaClient } from '../../generated/prisma/client.js';
8
+
9
+ export let fastify: FastifyInstance;
10
+ export const prisma = mockDeep<PrismaClient>();
11
+ export const opensearch = mockDeep<Client>();
12
+
13
+ // @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
14
+ prisma.getter = undefined;
15
+ // @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
16
+ prisma.setter = undefined;
17
+
18
+ // @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
19
+ opensearch.getter = undefined;
20
+ // @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
21
+ opensearch.setter = undefined;
22
+
23
+ export const fastifyBefore = async () => {
24
+ mockReset(prisma);
25
+ mockReset(opensearch);
26
+
27
+ fastify = Fastify({ logger: false });
28
+ fastify.decorate('prisma', prisma);
29
+ fastify.decorate('opensearch', opensearch);
30
+
31
+ fastify.setValidatorCompiler(validatorCompiler);
32
+ fastify.setSerializerCompiler(serializerCompiler);
33
+
34
+ return fastify;
35
+ };
36
+
37
+ export const fastifyAfter = async () => {
38
+ await fastify.close();
39
+ };
@@ -0,0 +1,200 @@
1
+ import { Client } from '@opensearch-project/opensearch';
2
+ import type { FastifyInstance } from 'fastify';
3
+ import Fastify from 'fastify';
4
+ import app, { AllPublicAccessTransformer, AllPublicFileAccessTransformer } from '../app.js';
5
+ import { PrismaClient } from '../generated/prisma/client.js';
6
+
7
+ let fastify: FastifyInstance;
8
+ let prisma: PrismaClient;
9
+ let opensearch: Client;
10
+
11
+ export const getTestApp = () => fastify;
12
+
13
+ export async function setupIntegrationTests() {
14
+ process.env.DATABASE_URL = 'mysql://root:password@localhost:3307/catalog_test';
15
+ process.env.OPENSEARCH_URL = 'http://localhost:9201';
16
+ process.env.NODE_ENV = 'test';
17
+
18
+ prisma = new PrismaClient({
19
+ datasources: {
20
+ db: {
21
+ url: process.env.DATABASE_URL,
22
+ },
23
+ },
24
+ });
25
+
26
+ opensearch = new Client({
27
+ node: process.env.OPENSEARCH_URL,
28
+ });
29
+
30
+ fastify = Fastify({ logger: false });
31
+
32
+ await fastify.register(app, {
33
+ prisma,
34
+ opensearch,
35
+ disableCors: true,
36
+ accessTransformer: AllPublicAccessTransformer,
37
+ fileAccessTransformer: AllPublicFileAccessTransformer,
38
+ fileHandler: {
39
+ get: async () => {
40
+ throw new Error('File handler not implemented in integration tests');
41
+ },
42
+ head: async () => {
43
+ throw new Error('File handler not implemented in integration tests');
44
+ },
45
+ },
46
+ roCrateHandler: {
47
+ get: async () => {
48
+ throw new Error('RO-Crate handler not implemented in integration tests');
49
+ },
50
+ head: async () => {
51
+ throw new Error('RO-Crate head handler not implemented in integration tests');
52
+ },
53
+ },
54
+ });
55
+
56
+ await fastify.ready();
57
+ }
58
+
59
+ export async function teardownIntegrationTests() {
60
+ await cleanupTestData();
61
+
62
+ await fastify.close();
63
+ await prisma.$disconnect();
64
+ opensearch.close();
65
+ }
66
+
67
+ export async function cleanupTestData() {
68
+ await prisma.entity.deleteMany({});
69
+
70
+ await opensearch.indices.delete({
71
+ index: 'entities',
72
+ ignore_unavailable: true,
73
+ });
74
+ }
75
+
76
+ export async function seedTestData() {
77
+ await cleanupTestData();
78
+
79
+ const testEntities = [
80
+ {
81
+ id: 1,
82
+ rocrateId: 'http://example.com/entity/1',
83
+ name: 'Test Collection',
84
+ description: 'First test entity',
85
+ entityType: 'http://pcdm.org/models#Collection',
86
+ memberOf: null,
87
+ rootCollection: null,
88
+ metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
89
+ contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
90
+ createdAt: new Date(),
91
+ updatedAt: new Date(),
92
+ rocrate: {},
93
+ },
94
+ {
95
+ id: 2,
96
+ rocrateId: 'http://example.com/entity/2',
97
+ name: 'Test Object',
98
+ description: 'Second test entity',
99
+ entityType: 'http://pcdm.org/models#Object',
100
+ memberOf: 'http://example.com/entity/1',
101
+ rootCollection: 'http://example.com/entity/1',
102
+ metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
103
+ contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
104
+ createdAt: new Date(),
105
+ updatedAt: new Date(),
106
+ rocrate: {},
107
+ },
108
+ {
109
+ id: 3,
110
+ rocrateId: 'http://example.com/entity/3',
111
+ name: 'Test Person',
112
+ description: 'Third test entity',
113
+ entityType: 'http://schema.org/Person',
114
+ memberOf: 'http://example.com/entity/1',
115
+ rootCollection: 'http://example.com/entity/1',
116
+ metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
117
+ contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
118
+ createdAt: new Date(),
119
+ updatedAt: new Date(),
120
+ rocrate: {},
121
+ },
122
+ {
123
+ id: 4,
124
+ rocrateId: 'http://example.com/entity/4',
125
+ name: 'test-audio.wav',
126
+ description: 'Test audio file',
127
+ entityType: 'http://schema.org/MediaObject',
128
+ memberOf: 'http://example.com/entity/2',
129
+ rootCollection: 'http://example.com/entity/1',
130
+ metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
131
+ contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
132
+ createdAt: new Date(),
133
+ updatedAt: new Date(),
134
+ rocrate: {
135
+ '@type': ['http://schema.org/MediaObject', 'MediaObject'],
136
+ encodingFormat: 'audio/wav',
137
+ contentSize: '88200',
138
+ },
139
+ },
140
+ {
141
+ id: 5,
142
+ rocrateId: 'http://example.com/entity/5',
143
+ name: 'collection-metadata.csv',
144
+ description: 'Collection metadata file',
145
+ entityType: 'http://schema.org/MediaObject',
146
+ memberOf: 'http://example.com/entity/1',
147
+ rootCollection: 'http://example.com/entity/1',
148
+ metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
149
+ contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
150
+ createdAt: new Date(),
151
+ updatedAt: new Date(),
152
+ rocrate: {
153
+ '@type': ['http://schema.org/MediaObject', 'MediaObject'],
154
+ encodingFormat: 'text/csv',
155
+ contentSize: '1024',
156
+ },
157
+ },
158
+ ];
159
+
160
+ await prisma.entity.createMany({
161
+ data: testEntities,
162
+ });
163
+
164
+ await opensearch.indices.create({
165
+ index: 'entities',
166
+ body: {
167
+ mappings: {
168
+ properties: {
169
+ rocrateId: { type: 'keyword' },
170
+ name: {
171
+ type: 'text',
172
+ fields: {
173
+ keyword: { type: 'keyword' },
174
+ },
175
+ },
176
+ description: { type: 'text' },
177
+ entityType: { type: 'keyword' },
178
+ memberOf: { type: 'keyword' },
179
+ rootCollection: { type: 'keyword' },
180
+ location: { type: 'geo_point' },
181
+ inLanguage: { type: 'keyword' },
182
+ mediaType: { type: 'keyword' },
183
+ communicationMode: { type: 'keyword' },
184
+ },
185
+ },
186
+ },
187
+ });
188
+
189
+ const testDocs = testEntities.flatMap((entity, index) => [
190
+ { index: { _index: 'entities', _id: `${index + 1}` } },
191
+ entity,
192
+ ]);
193
+
194
+ await opensearch.bulk({
195
+ body: testDocs,
196
+ refresh: true,
197
+ });
198
+
199
+ return testEntities;
200
+ }