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
@@ -1,12 +1,12 @@
1
1
  import type { MultiBucketAggregateBaseFiltersBucket } from '@opensearch-project/opensearch/api/_types/_common.aggregations.js';
2
- import type { BoolQuery } from '@opensearch-project/opensearch/api/_types/_common.query_dsl.js';
3
- import type { Search_Request, Search_RequestBody } from '@opensearch-project/opensearch/api/index.js';
2
+ import type { Search_Request } from '@opensearch-project/opensearch/api/index.js';
4
3
  import type { FastifyPluginAsync } from 'fastify';
5
4
  import type { ZodTypeProvider } from 'fastify-type-provider-zod';
6
5
  import { z } from 'zod/v4';
7
6
  import { baseEntityTransformer, resolveEntityReferences } from '../transformers/default.js';
8
7
  import type { AccessTransformer, EntityTransformer } from '../types/transformers.js';
9
- import { createInternalError } from '../utils/errors.js';
8
+ import { createInternalError, createInvalidRequestError } from '../utils/errors.js';
9
+ import { OpensearchQueryBuilder, type QueryBuilderOptions } from '../utils/queryBuilder.js';
10
10
 
11
11
  const boundingBoxSchema = z.object({
12
12
  topRight: z.object({
@@ -24,154 +24,28 @@ const searchParamsSchema = z.object({
24
24
  query: z.string(),
25
25
  filters: z.record(z.string(), z.array(z.string())).optional(),
26
26
  boundingBox: boundingBoxSchema.optional(),
27
- geohashPrecision: z.number().int().min(0).max(12).default(5),
27
+ geohashPrecision: z.number().int().min(1).max(12).optional(),
28
28
  limit: z.number().int().min(1).max(1000).default(100),
29
29
  offset: z.number().int().min(0).default(0),
30
30
  sort: z.enum(['id', 'name', 'createdAt', 'updatedAt', 'relevance']).default('relevance'),
31
31
  order: z.enum(['asc', 'desc']).default('asc'),
32
32
  });
33
- type SearchParams = z.infer<typeof searchParamsSchema>;
34
-
35
- const buildQuery = (
36
- searchType: SearchParams['searchType'],
37
- query: SearchParams['query'],
38
- filters: SearchParams['filters'],
39
- boundingBox: SearchParams['boundingBox'],
40
- ) => {
41
- const must: BoolQuery['must'] = [];
42
- const filter: BoolQuery['filter'] = [];
43
-
44
- if (searchType === 'basic') {
45
- must.push({
46
- multi_match: {
47
- query,
48
- fields: ['name^2', 'description'],
49
- type: 'best_fields',
50
- fuzziness: 'AUTO',
51
- zero_terms_query: 'all',
52
- },
53
- });
54
- } else {
55
- must.push({
56
- query_string: {
57
- query,
58
- fields: ['name^2', 'description'],
59
- default_operator: 'AND',
60
- },
61
- });
62
- }
63
-
64
- if (filters) {
65
- Object.entries(filters).forEach(([field, values]) => {
66
- filter.push({
67
- terms: {
68
- [field]: values,
69
- },
70
- });
71
- });
72
- }
73
-
74
- if (boundingBox) {
75
- filter.push({
76
- geo_bounding_box: {
77
- location: {
78
- top_left: {
79
- lat: boundingBox.topRight.lat,
80
- lon: boundingBox.bottomLeft.lng,
81
- },
82
- bottom_right: {
83
- lat: boundingBox.bottomLeft.lat,
84
- lon: boundingBox.topRight.lng,
85
- },
86
- },
87
- },
88
- });
89
- }
90
-
91
- return {
92
- bool: {
93
- must,
94
- filter,
95
- },
96
- };
97
- };
98
-
99
- // TODO: Pull these from a config file
100
- const buildAggregations = (
101
- geohashPrecision: SearchParams['geohashPrecision'],
102
- boundingBox: SearchParams['boundingBox'],
103
- ) => {
104
- const aggs: Search_RequestBody['aggs'] = {
105
- inLanguage: {
106
- terms: {
107
- field: 'inLanguage.keyword',
108
- size: 20,
109
- },
110
- },
111
- mediaType: {
112
- terms: {
113
- field: 'mediaType.keyword',
114
- size: 20,
115
- },
116
- },
117
- communicationMode: {
118
- terms: {
119
- field: 'communicationMode.keyword',
120
- size: 20,
121
- },
122
- },
123
- entityType: {
124
- terms: {
125
- field: 'entityType.keyword',
126
- size: 20,
127
- },
128
- },
129
- };
130
-
131
- // Add geohash aggregation if precision is specified
132
- if (geohashPrecision && boundingBox) {
133
- aggs.geohash_grid = {
134
- geohash_grid: {
135
- field: 'location',
136
- precision: geohashPrecision,
137
- bounds: {
138
- top_left: {
139
- lat: boundingBox.topRight.lat,
140
- lon: boundingBox.bottomLeft.lng,
141
- },
142
- bottom_right: {
143
- lat: boundingBox.bottomLeft.lat,
144
- lon: boundingBox.topRight.lng,
145
- },
146
- },
147
- },
148
- };
149
- }
150
-
151
- return aggs;
152
- };
153
-
154
- const buildSort = (sort: SearchParams['sort'], order: SearchParams['order']) => {
155
- if (sort === 'relevance') {
156
- return;
157
- }
158
-
159
- const sortField = sort === 'id' ? 'rocrateId' : sort;
160
-
161
- if (sortField === 'name') {
162
- return [{ 'name.keyword': order }];
163
- }
164
-
165
- return [{ [sortField]: order }];
166
- };
167
33
 
168
34
  type SearchRouteOptions = {
169
35
  accessTransformer: AccessTransformer;
170
36
  entityTransformers?: EntityTransformer[];
37
+ queryBuilderClass?: typeof OpensearchQueryBuilder;
38
+ queryBuilderOptions?: QueryBuilderOptions;
171
39
  };
172
40
 
173
41
  const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) => {
174
- const { accessTransformer, entityTransformers = [] } = opts;
42
+ const {
43
+ accessTransformer,
44
+ entityTransformers = [],
45
+ queryBuilderClass = OpensearchQueryBuilder,
46
+ queryBuilderOptions,
47
+ } = opts;
48
+ const queryBuilder = new queryBuilderClass(queryBuilderOptions);
175
49
  fastify.withTypeProvider<ZodTypeProvider>().post(
176
50
  '/search',
177
51
  {
@@ -186,52 +60,51 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
186
60
  const opensearchQuery: Search_Request = {
187
61
  index: 'entities',
188
62
  body: {
189
- query: buildQuery(searchType, query, filters, boundingBox),
190
- aggs: buildAggregations(geohashPrecision, boundingBox),
63
+ query: queryBuilder.buildQuery(searchType, query, filters, boundingBox),
64
+ aggs: queryBuilder.buildAggregations(geohashPrecision, boundingBox),
191
65
  highlight: {
192
66
  fields: {
193
67
  name: {},
194
68
  description: {},
195
69
  },
196
70
  },
197
- sort: buildSort(sort, order),
71
+ sort: queryBuilder.buildSort(sort, order),
198
72
  from: offset,
199
73
  size: limit,
200
74
  },
201
75
  };
202
-
76
+ fastify.log.debug(opensearchQuery);
203
77
  const response = await fastify.opensearch.search(opensearchQuery);
204
78
 
205
79
  if (!response.body?.hits?.hits) {
206
80
  throw new Error('Invalid search response: missing hits data');
207
81
  }
208
82
 
209
- const rocrateIds = response.body.hits.hits
210
- .map((hit) => hit._source?.rocrateId as string | undefined)
211
- .filter(Boolean);
83
+ const entityIds = response.body.hits.hits.map((hit) => hit._source?.id as string | undefined).filter(Boolean);
212
84
 
213
85
  const dbEntities = await fastify.prisma.entity.findMany({
214
86
  where: {
215
- rocrateId: {
216
- in: rocrateIds,
87
+ id: {
88
+ in: entityIds,
217
89
  },
218
90
  },
91
+ include: { file: { select: { id: true } } },
219
92
  });
220
93
 
221
- const entityMap = new Map(dbEntities.map((entity) => [entity.rocrateId, entity]));
94
+ const entityMap = new Map(dbEntities.map((entity) => [entity.id, entity]));
222
95
 
223
96
  // Resolve memberOf and rootCollection references
224
97
  const refMap = await resolveEntityReferences(dbEntities, fastify.prisma);
225
98
 
226
99
  const entities = await Promise.all(
227
100
  response.body.hits.hits.map(async (hit) => {
228
- if (!hit._source?.rocrateId) {
229
- throw new Error('Missing rocrateId in search hit');
101
+ if (!hit._source?.id) {
102
+ throw new Error('Missing id in search hit');
230
103
  }
231
104
 
232
- const dbEntity = entityMap.get(hit._source.rocrateId);
105
+ const dbEntity = entityMap.get(hit._source.id);
233
106
  if (!dbEntity) {
234
- fastify.log.warn(`Entity ${hit._source.rocrateId} found in OpenSearch but not in database`);
107
+ fastify.log.warn(`Entity ${hit._source.id} found in OpenSearch but not in database`);
235
108
 
236
109
  return null;
237
110
  }
@@ -309,6 +182,14 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
309
182
  return result;
310
183
  } catch (error) {
311
184
  const err = error as Error;
185
+
186
+ // OpenSearch returns 400 for malformed queries (e.g. invalid query_string syntax)
187
+ if ('statusCode' in err && (err as { statusCode: number }).statusCode === 400) {
188
+ fastify.log.warn(`Invalid search query: ${err.message}`);
189
+
190
+ return reply.code(422).send(createInvalidRequestError(err.message));
191
+ }
192
+
312
193
  fastify.log.error(`Search error: ${err.message}`);
313
194
 
314
195
  return reply.code(500).send(createInternalError('Search failed'));
@@ -1,4 +1,5 @@
1
1
  import { Client } from '@opensearch-project/opensearch';
2
+ import { PrismaMariaDb } from '@prisma/adapter-mariadb';
2
3
  import type { FastifyInstance } from 'fastify';
3
4
  import Fastify from 'fastify';
4
5
  import app, { AllPublicAccessTransformer, AllPublicFileAccessTransformer } from '../app.js';
@@ -15,13 +16,17 @@ export async function setupIntegrationTests() {
15
16
  process.env.OPENSEARCH_URL = 'http://localhost:9201';
16
17
  process.env.NODE_ENV = 'test';
17
18
 
18
- prisma = new PrismaClient({
19
- datasources: {
20
- db: {
21
- url: process.env.DATABASE_URL,
22
- },
23
- },
19
+ const adapter = new PrismaMariaDb({
20
+ host: 'localhost',
21
+ user: 'root',
22
+ password: 'password',
23
+ database: 'catalog_test',
24
+ port: 3307,
25
+ connectionLimit: 5,
26
+ // 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.
27
+ allowPublicKeyRetrieval: true,
24
28
  });
29
+ prisma = new PrismaClient({ adapter });
25
30
 
26
31
  opensearch = new Client({
27
32
  node: process.env.OPENSEARCH_URL,
@@ -65,6 +70,7 @@ export async function teardownIntegrationTests() {
65
70
  }
66
71
 
67
72
  export async function cleanupTestData() {
73
+ await prisma.file.deleteMany({});
68
74
  await prisma.entity.deleteMany({});
69
75
 
70
76
  await opensearch.indices.delete({
@@ -78,8 +84,7 @@ export async function seedTestData() {
78
84
 
79
85
  const testEntities = [
80
86
  {
81
- id: 1,
82
- rocrateId: 'http://example.com/entity/1',
87
+ id: 'http://example.com/entity/1',
83
88
  name: 'Test Collection',
84
89
  description: 'First test entity',
85
90
  entityType: 'http://pcdm.org/models#Collection',
@@ -91,8 +96,7 @@ export async function seedTestData() {
91
96
  updatedAt: new Date(),
92
97
  },
93
98
  {
94
- id: 2,
95
- rocrateId: 'http://example.com/entity/2',
99
+ id: 'http://example.com/entity/2',
96
100
  name: 'Test Object',
97
101
  description: 'Second test entity',
98
102
  entityType: 'http://pcdm.org/models#Object',
@@ -104,8 +108,7 @@ export async function seedTestData() {
104
108
  updatedAt: new Date(),
105
109
  },
106
110
  {
107
- id: 3,
108
- rocrateId: 'http://example.com/entity/3',
111
+ id: 'http://example.com/entity/3',
109
112
  name: 'Test Person',
110
113
  description: 'Third test entity',
111
114
  entityType: 'http://schema.org/Person',
@@ -117,8 +120,7 @@ export async function seedTestData() {
117
120
  updatedAt: new Date(),
118
121
  },
119
122
  {
120
- id: 4,
121
- rocrateId: 'http://example.com/entity/4',
123
+ id: 'http://example.com/entity/4',
122
124
  name: 'test-audio.wav',
123
125
  description: 'Test audio file',
124
126
  entityType: 'http://schema.org/MediaObject',
@@ -130,8 +132,7 @@ export async function seedTestData() {
130
132
  updatedAt: new Date(),
131
133
  },
132
134
  {
133
- id: 5,
134
- rocrateId: 'http://example.com/entity/5',
135
+ id: 'http://example.com/entity/5',
135
136
  name: 'collection-metadata.csv',
136
137
  description: 'Collection metadata file',
137
138
  entityType: 'http://schema.org/MediaObject',
@@ -153,7 +154,7 @@ export async function seedTestData() {
153
154
  body: {
154
155
  mappings: {
155
156
  properties: {
156
- rocrateId: { type: 'keyword' },
157
+ id: { type: 'keyword' },
157
158
  name: {
158
159
  type: 'text',
159
160
  fields: {
@@ -173,10 +174,7 @@ export async function seedTestData() {
173
174
  },
174
175
  });
175
176
 
176
- const testDocs = testEntities.flatMap((entity, index) => [
177
- { index: { _index: 'entities', _id: `${index + 1}` } },
178
- entity,
179
- ]);
177
+ const testDocs = testEntities.flatMap((entity) => [{ index: { _index: 'entities', _id: entity.id } }, entity]);
180
178
 
181
179
  await opensearch.bulk({
182
180
  body: testDocs,
@@ -4,13 +4,11 @@ import { AllPublicAccessTransformer, baseEntityTransformer } from './default.js'
4
4
 
5
5
  describe('baseEntityTransformer', () => {
6
6
  it('should transform entity to standard entity shape', () => {
7
- const entity: Entity = {
8
- id: 1,
9
- rocrateId: 'http://example.com/entity/123',
7
+ const entity: Entity & { file?: { id: string } | null } = {
8
+ id: 'http://example.com/entity/123',
10
9
  name: 'Test Entity',
11
10
  description: 'A test entity description',
12
11
  entityType: 'http://pcdm.org/models#Collection',
13
- fileId: null,
14
12
  memberOf: 'http://example.com/parent',
15
13
  rootCollection: 'http://example.com/root',
16
14
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -35,13 +33,11 @@ describe('baseEntityTransformer', () => {
35
33
  });
36
34
 
37
35
  it('should handle null memberOf and rootCollection', () => {
38
- const entity: Entity = {
39
- id: 1,
40
- rocrateId: 'http://example.com/collection',
36
+ const entity: Entity & { file?: { id: string } | null } = {
37
+ id: 'http://example.com/collection',
41
38
  name: 'Top Collection',
42
39
  description: 'A top-level collection',
43
40
  entityType: 'http://pcdm.org/models#Collection',
44
- fileId: null,
45
41
  memberOf: null,
46
42
  rootCollection: null,
47
43
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -58,13 +54,11 @@ describe('baseEntityTransformer', () => {
58
54
  });
59
55
 
60
56
  it('should exclude database-specific fields', () => {
61
- const entity: Entity = {
62
- id: 1,
63
- rocrateId: 'http://example.com/entity/456',
57
+ const entity: Entity & { file?: { id: string } | null } = {
58
+ id: 'http://example.com/entity/456',
64
59
  name: 'Test',
65
60
  description: 'Test',
66
61
  entityType: 'http://pcdm.org/models#Object',
67
- fileId: null,
68
62
  memberOf: null,
69
63
  rootCollection: null,
70
64
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -76,7 +70,6 @@ describe('baseEntityTransformer', () => {
76
70
 
77
71
  const result = baseEntityTransformer(entity);
78
72
 
79
- // Result should have 'id' (mapped from rocrateId), but not the numeric database id
80
73
  expect(result.id).toBe('http://example.com/entity/456');
81
74
  expect(result).not.toHaveProperty('createdAt');
82
75
  expect(result).not.toHaveProperty('updatedAt');
@@ -93,14 +86,12 @@ describe('baseEntityTransformer', () => {
93
86
  ]);
94
87
  });
95
88
 
96
- it('should handle File entity (MediaObject) with fileId', () => {
97
- const entity: Entity = {
98
- id: 1,
99
- rocrateId: 'http://example.com/file/audio.wav',
89
+ it('should handle File entity (MediaObject) with file relation', () => {
90
+ const entity: Entity & { file?: { id: string } | null } = {
91
+ id: 'http://example.com/file/audio.wav',
100
92
  name: 'Audio File',
101
93
  description: 'An audio recording',
102
94
  entityType: 'http://schema.org/MediaObject',
103
- fileId: 'http://example.com/files/audio.wav',
104
95
  memberOf: 'http://example.com/collection',
105
96
  rootCollection: 'http://example.com/collection',
106
97
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -108,6 +99,7 @@ describe('baseEntityTransformer', () => {
108
99
  createdAt: new Date(),
109
100
  updatedAt: new Date(),
110
101
  meta: null,
102
+ file: { id: 'http://example.com/files/audio.wav' },
111
103
  };
112
104
 
113
105
  const result = baseEntityTransformer(entity);
@@ -117,13 +109,11 @@ describe('baseEntityTransformer', () => {
117
109
  name: 'Audio File',
118
110
  description: 'An audio recording',
119
111
  entityType: 'http://schema.org/MediaObject',
120
- fileId: 'http://example.com/files/audio.wav',
121
112
  memberOf: 'http://example.com/collection',
122
113
  rootCollection: 'http://example.com/collection',
123
114
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
124
115
  contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
125
116
  });
126
- expect(result.fileId).toBe('http://example.com/files/audio.wav');
127
117
  });
128
118
  });
129
119
 
@@ -1,9 +1,11 @@
1
1
  import type { Entity, File, PrismaClient } from '../generated/prisma/client.js';
2
2
 
3
+ type EntityWithFile = Entity & { file?: { id: string } | null };
4
+
3
5
  /**
4
6
  * Entity reference - used for memberOf and rootCollection
5
7
  */
6
- export type EntityReference = {
8
+ type EntityReference = {
7
9
  id: string;
8
10
  name: string;
9
11
  };
@@ -14,13 +16,14 @@ export type EntityReference = {
14
16
  */
15
17
  export type BaseEntity = {
16
18
  id: string;
19
+ entityType: string;
17
20
  name: string;
18
21
  description: string;
19
22
  memberOf: string | null;
20
23
  rootCollection: string | null;
21
24
  metadataLicenseId: string;
22
25
  contentLicenseId: string;
23
- } & ({ entityType: string; fileId?: never } | { entityType: 'http://schema.org/MediaObject'; fileId: string });
26
+ };
24
27
 
25
28
  /**
26
29
  * Standard entity shape - with resolved references
@@ -28,13 +31,14 @@ export type BaseEntity = {
28
31
  */
29
32
  export type StandardEntity = {
30
33
  id: string;
34
+ entityType: string;
31
35
  name: string;
32
36
  description: string;
33
37
  memberOf: EntityReference | null;
34
38
  rootCollection: EntityReference | null;
35
39
  metadataLicenseId: string;
36
40
  contentLicenseId: string;
37
- } & ({ entityType: string; fileId?: never } | { entityType: 'http://schema.org/MediaObject'; fileId: string });
41
+ };
38
42
 
39
43
  /**
40
44
  * Access information for an entity
@@ -42,6 +46,7 @@ export type StandardEntity = {
42
46
  type AccessInfo = {
43
47
  metadata: boolean;
44
48
  content: boolean;
49
+ metadataAuthorizationUrl?: string;
45
50
  contentAuthorizationUrl?: string;
46
51
  };
47
52
 
@@ -65,16 +70,12 @@ type FileAccessInfo = {
65
70
  /**
66
71
  * Standard file shape - output of base file transformation
67
72
  * Does not include access information
68
- * Files only have contentLicenseId (no metadataLicenseId)
69
73
  */
70
74
  export type StandardFile = {
71
75
  id: string;
72
76
  filename: string;
73
77
  mediaType: string;
74
78
  size: number;
75
- memberOf: string;
76
- rootCollection: string;
77
- contentLicenseId: string;
78
79
  };
79
80
 
80
81
  /**
@@ -89,10 +90,11 @@ export type AuthorisedFile = StandardFile & {
89
90
  /**
90
91
  * Base entity transformer - always applied first
91
92
  * Transforms raw database entity to base entity shape with unresolved references
93
+ * Accepts Entity with optional file relation included
92
94
  */
93
- export const baseEntityTransformer = (entity: Entity): BaseEntity => {
95
+ export const baseEntityTransformer = (entity: EntityWithFile): BaseEntity => {
94
96
  const base: BaseEntity = {
95
- id: entity.rocrateId,
97
+ id: entity.id,
96
98
  name: entity.name,
97
99
  description: entity.description,
98
100
  entityType: entity.entityType,
@@ -102,18 +104,6 @@ export const baseEntityTransformer = (entity: Entity): BaseEntity => {
102
104
  contentLicenseId: entity.contentLicenseId,
103
105
  };
104
106
 
105
- if (base.entityType === ('http://schema.org/MediaObject' as const)) {
106
- if (!entity.fileId) {
107
- return base;
108
- }
109
-
110
- return {
111
- ...base,
112
- entityType: base.entityType,
113
- fileId: entity.fileId,
114
- };
115
- }
116
-
117
107
  return base;
118
108
  };
119
109
 
@@ -148,13 +138,10 @@ export const AllPublicAccessTransformer = (entity: StandardEntity): AuthorisedEn
148
138
  * Transforms raw database file to standard file shape (without access)
149
139
  */
150
140
  export const baseFileTransformer = (file: File): StandardFile => ({
151
- id: file.fileId,
141
+ id: file.id,
152
142
  filename: file.filename,
153
143
  mediaType: file.mediaType,
154
144
  size: Number(file.size),
155
- memberOf: file.memberOf,
156
- rootCollection: file.rootCollection,
157
- contentLicenseId: file.contentLicenseId,
158
145
  });
159
146
 
160
147
  /**
@@ -215,9 +202,9 @@ export const resolveEntityReferences = async (
215
202
  }
216
203
 
217
204
  const refs = await prisma.entity.findMany({
218
- where: { rocrateId: { in: [...refIds] } },
219
- select: { rocrateId: true, name: true },
205
+ where: { id: { in: [...refIds] } },
206
+ select: { id: true, name: true },
220
207
  });
221
208
 
222
- return new Map(refs.map((r) => [r.rocrateId, { id: r.rocrateId, name: r.name }]));
209
+ return new Map(refs.map((r) => [r.id, { id: r.id, name: r.name }]));
223
210
  };
@@ -37,12 +37,11 @@ describe('Entity Transformers', () => {
37
37
  describe('baseEntityTransformer', () => {
38
38
  it('should transform raw entity to base shape with unresolved references', () => {
39
39
  const rawEntity = {
40
- id: 1,
41
- rocrateId: 'http://example.com/entity/123',
40
+ id: 'http://example.com/entity/123',
42
41
  name: 'Test Entity',
43
42
  description: 'A test entity',
44
43
  entityType: 'http://schema.org/Person',
45
- fileId: null,
44
+
46
45
  memberOf: 'http://example.com/collection',
47
46
  rootCollection: 'http://example.com/root',
48
47
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -195,12 +194,11 @@ describe('Entity Transformers', () => {
195
194
 
196
195
  it('should demonstrate full pipeline: base -> resolve -> access -> custom', async () => {
197
196
  const rawEntity = {
198
- id: 1,
199
- rocrateId: 'http://example.com/entity/123',
197
+ id: 'http://example.com/entity/123',
200
198
  name: 'Test Entity',
201
199
  description: 'A test entity',
202
200
  entityType: 'http://schema.org/Person',
203
- fileId: null,
201
+
204
202
  memberOf: 'http://example.com/collection',
205
203
  rootCollection: 'http://example.com/root',
206
204
  metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
@@ -29,16 +29,20 @@ const createErrorResponse = (code: ErrorCode, message: string, details?: ErrorDe
29
29
  },
30
30
  });
31
31
 
32
- export const createValidationError = (message: string, issues: string[]): StandardErrorResponse =>
33
- createErrorResponse(ERROR_CODES.VALIDATION_ERROR, message, { issues });
32
+ type ValidationViolation = {
33
+ field: string;
34
+ message: string;
35
+ value?: unknown;
36
+ };
37
+
38
+ export const createValidationError = (message: string, violations: ValidationViolation[]): StandardErrorResponse =>
39
+ createErrorResponse(ERROR_CODES.VALIDATION_ERROR, message, { violations });
34
40
 
35
41
  export const createNotFoundError = (message: string, entityId?: string): StandardErrorResponse =>
36
42
  createErrorResponse(ERROR_CODES.NOT_FOUND, message, entityId ? { entityId } : undefined);
37
43
 
38
- // export const createRateLimitError = (retryAfter: number): StandardErrorResponse =>
39
- // createErrorResponse(ERROR_CODES.RATE_LIMIT_EXCEEDED, 'Rate limit exceeded. Please retry after the specified time.', {
40
- // retryAfter,
41
- // });
44
+ export const createInvalidRequestError = (message: string): StandardErrorResponse =>
45
+ createErrorResponse(ERROR_CODES.INVALID_REQUEST, message);
42
46
 
43
47
  export const createInternalError = (message = 'Internal server error'): StandardErrorResponse => {
44
48
  return createErrorResponse(ERROR_CODES.INTERNAL_ERROR, message);
@@ -0,0 +1,16 @@
1
+ import type { FastifyReply } from 'fastify';
2
+
3
+ export const setFileHeaders = (
4
+ reply: FastifyReply,
5
+ metadata: { contentType: string; contentLength: number; etag?: string; lastModified?: Date },
6
+ ) => {
7
+ reply.header('Content-Type', metadata.contentType);
8
+ reply.header('Content-Length', metadata.contentLength.toString());
9
+
10
+ if (metadata.etag) {
11
+ reply.header('ETag', metadata.etag);
12
+ }
13
+ if (metadata.lastModified) {
14
+ reply.header('Last-Modified', metadata.lastModified.toUTCString());
15
+ }
16
+ };