arocapi 1.2.2 → 2.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.
- package/dist/routes/crate.test.js +0 -16
- package/dist/routes/crate.test.js.map +1 -1
- package/dist/routes/entities.js +8 -2
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.js +29 -2
- package/dist/routes/entities.test.js.map +1 -1
- package/dist/routes/entity.js +8 -2
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.js +26 -1
- package/dist/routes/entity.test.js.map +1 -1
- package/dist/routes/search.js +8 -2
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.js +49 -0
- package/dist/routes/search.test.js.map +1 -1
- package/dist/test/integration.setup.d.ts +0 -27
- package/dist/test/integration.setup.js +0 -13
- package/dist/test/integration.setup.js.map +1 -1
- package/dist/test/integration.test.js +4 -4
- package/dist/test/integration.test.js.map +1 -1
- package/dist/transformers/default.d.ts +26 -3
- package/dist/transformers/default.js +19 -0
- package/dist/transformers/default.js.map +1 -1
- package/dist/transformers/default.test.js +2 -7
- package/dist/transformers/default.test.js.map +1 -1
- package/dist/transformers/transformer.test.js +23 -8
- package/dist/transformers/transformer.test.js.map +1 -1
- package/package.json +1 -1
- package/prisma/migrations/20251211025510_remove_rocrate_column/migration.sql +8 -0
- package/prisma/models/entity.prisma +0 -1
- package/src/routes/__snapshots__/entities.test.ts.snap +30 -2
- package/src/routes/__snapshots__/entity.test.ts.snap +17 -0
- package/src/routes/__snapshots__/search.test.ts.snap +26 -0
- package/src/routes/crate.test.ts +0 -16
- package/src/routes/entities.test.ts +34 -2
- package/src/routes/entities.ts +10 -2
- package/src/routes/entity.test.ts +31 -1
- package/src/routes/entity.ts +10 -2
- package/src/routes/search.test.ts +56 -0
- package/src/routes/search.ts +10 -2
- package/src/test/integration.setup.ts +0 -13
- package/src/test/integration.test.ts +4 -4
- package/src/transformers/default.test.ts +2 -7
- package/src/transformers/default.ts +65 -7
- package/src/transformers/transformer.test.ts +30 -9
|
@@ -30,7 +30,6 @@ describe('Entities Route', () => {
|
|
|
30
30
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
31
31
|
createdAt: new Date(),
|
|
32
32
|
updatedAt: new Date(),
|
|
33
|
-
rocrate: {},
|
|
34
33
|
meta: {},
|
|
35
34
|
},
|
|
36
35
|
{
|
|
@@ -46,7 +45,6 @@ describe('Entities Route', () => {
|
|
|
46
45
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
47
46
|
createdAt: new Date(),
|
|
48
47
|
updatedAt: new Date(),
|
|
49
|
-
rocrate: {},
|
|
50
48
|
meta: {},
|
|
51
49
|
},
|
|
52
50
|
];
|
|
@@ -280,5 +278,39 @@ describe('Entities Route', () => {
|
|
|
280
278
|
expect(response.statusCode).toBe(200);
|
|
281
279
|
expect(body).toMatchSnapshot();
|
|
282
280
|
});
|
|
281
|
+
|
|
282
|
+
it('should return null for memberOf/rootCollection when parent entity not found', async () => {
|
|
283
|
+
const mockEntities = [
|
|
284
|
+
{
|
|
285
|
+
id: 1,
|
|
286
|
+
rocrateId: 'http://example.com/entity/1',
|
|
287
|
+
name: 'Test Entity 1',
|
|
288
|
+
description: 'Entity with missing parent',
|
|
289
|
+
entityType: 'http://pcdm.org/models#Object',
|
|
290
|
+
fileId: null,
|
|
291
|
+
memberOf: 'http://example.com/entity/deleted',
|
|
292
|
+
rootCollection: 'http://example.com/entity/deleted',
|
|
293
|
+
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
294
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
295
|
+
createdAt: new Date(),
|
|
296
|
+
updatedAt: new Date(),
|
|
297
|
+
meta: {},
|
|
298
|
+
},
|
|
299
|
+
];
|
|
300
|
+
|
|
301
|
+
// First call returns entities, second call (for reference resolution) returns empty
|
|
302
|
+
prisma.entity.findMany.mockResolvedValueOnce(mockEntities);
|
|
303
|
+
prisma.entity.findMany.mockResolvedValueOnce([]);
|
|
304
|
+
prisma.entity.count.mockResolvedValue(1);
|
|
305
|
+
|
|
306
|
+
const response = await fastify.inject({
|
|
307
|
+
method: 'GET',
|
|
308
|
+
url: '/entities',
|
|
309
|
+
});
|
|
310
|
+
const body = JSON.parse(response.body);
|
|
311
|
+
|
|
312
|
+
expect(response.statusCode).toBe(200);
|
|
313
|
+
expect(body).toMatchSnapshot();
|
|
314
|
+
});
|
|
283
315
|
});
|
|
284
316
|
});
|
package/src/routes/entities.ts
CHANGED
|
@@ -1,7 +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 { baseEntityTransformer } from '../transformers/default.js';
|
|
4
|
+
import { baseEntityTransformer, resolveEntityReferences } from '../transformers/default.js';
|
|
5
5
|
import type { AccessTransformer, EntityTransformer } from '../types/transformers.js';
|
|
6
6
|
import { createInternalError } from '../utils/errors.js';
|
|
7
7
|
|
|
@@ -65,10 +65,18 @@ const entities: FastifyPluginAsync<EntitiesRouteOptions> = async (fastify, opts)
|
|
|
65
65
|
|
|
66
66
|
const total = await fastify.prisma.entity.count({ where });
|
|
67
67
|
|
|
68
|
+
// Resolve memberOf and rootCollection references
|
|
69
|
+
const refMap = await resolveEntityReferences(dbEntities, fastify.prisma);
|
|
70
|
+
|
|
68
71
|
// Apply transformers to each entity: base -> access -> additional
|
|
69
72
|
const entities = await Promise.all(
|
|
70
73
|
dbEntities.map(async (dbEntity) => {
|
|
71
|
-
const
|
|
74
|
+
const base = baseEntityTransformer(dbEntity);
|
|
75
|
+
const standardEntity = {
|
|
76
|
+
...base,
|
|
77
|
+
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
78
|
+
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
79
|
+
};
|
|
72
80
|
const authorisedEntity = await accessTransformer(standardEntity, {
|
|
73
81
|
request,
|
|
74
82
|
fastify,
|
|
@@ -30,7 +30,6 @@ describe('Entity Route', () => {
|
|
|
30
30
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
31
31
|
createdAt: new Date(),
|
|
32
32
|
updatedAt: new Date(),
|
|
33
|
-
rocrate: {},
|
|
34
33
|
meta: {},
|
|
35
34
|
};
|
|
36
35
|
|
|
@@ -128,5 +127,36 @@ describe('Entity Route', () => {
|
|
|
128
127
|
expect(response.statusCode).toBe(200);
|
|
129
128
|
expect(body).toMatchSnapshot();
|
|
130
129
|
});
|
|
130
|
+
|
|
131
|
+
it('should return null for memberOf/rootCollection when parent entity not found', async () => {
|
|
132
|
+
const mockEntity = {
|
|
133
|
+
id: 1,
|
|
134
|
+
rocrateId: 'http://example.com/entity/123',
|
|
135
|
+
name: 'Test Entity',
|
|
136
|
+
description: 'A test entity',
|
|
137
|
+
entityType: 'http://pcdm.org/models#Object',
|
|
138
|
+
fileId: null,
|
|
139
|
+
memberOf: 'http://example.com/entity/deleted',
|
|
140
|
+
rootCollection: 'http://example.com/entity/deleted',
|
|
141
|
+
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
142
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
143
|
+
createdAt: new Date(),
|
|
144
|
+
updatedAt: new Date(),
|
|
145
|
+
meta: {},
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// First call returns the entity, second call (for reference resolution) returns empty
|
|
149
|
+
prisma.entity.findFirst.mockResolvedValue(mockEntity);
|
|
150
|
+
prisma.entity.findMany.mockResolvedValue([]);
|
|
151
|
+
|
|
152
|
+
const response = await fastify.inject({
|
|
153
|
+
method: 'GET',
|
|
154
|
+
url: `/entity/${encodeURIComponent('http://example.com/entity/123')}`,
|
|
155
|
+
});
|
|
156
|
+
const body = JSON.parse(response.body);
|
|
157
|
+
|
|
158
|
+
expect(response.statusCode).toBe(200);
|
|
159
|
+
expect(body).toMatchSnapshot();
|
|
160
|
+
});
|
|
131
161
|
});
|
|
132
162
|
});
|
package/src/routes/entity.ts
CHANGED
|
@@ -1,7 +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 { baseEntityTransformer } from '../transformers/default.js';
|
|
4
|
+
import { baseEntityTransformer, resolveEntityReferences } from '../transformers/default.js';
|
|
5
5
|
import type { AccessTransformer, EntityTransformer } from '../types/transformers.js';
|
|
6
6
|
import { createInternalError, createNotFoundError } from '../utils/errors.js';
|
|
7
7
|
|
|
@@ -37,7 +37,15 @@ const entity: FastifyPluginAsync<EntityRouteOptions> = async (fastify, opts) =>
|
|
|
37
37
|
return reply.code(404).send(createNotFoundError('The requested entity was not found', id));
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
// Resolve memberOf and rootCollection references
|
|
41
|
+
const refMap = await resolveEntityReferences([entity], fastify.prisma);
|
|
42
|
+
|
|
43
|
+
const base = baseEntityTransformer(entity);
|
|
44
|
+
const standardEntity = {
|
|
45
|
+
...base,
|
|
46
|
+
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
47
|
+
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
48
|
+
};
|
|
41
49
|
const authorisedEntity = await accessTransformer(standardEntity, {
|
|
42
50
|
request,
|
|
43
51
|
fastify,
|
|
@@ -803,5 +803,61 @@ describe('Search Route', () => {
|
|
|
803
803
|
expect(body.geohashGrid).toBeUndefined();
|
|
804
804
|
expect(body.entities).toHaveLength(1);
|
|
805
805
|
});
|
|
806
|
+
|
|
807
|
+
it('should return null for memberOf/rootCollection when parent entity not found', async () => {
|
|
808
|
+
const mockEntities = [
|
|
809
|
+
{
|
|
810
|
+
id: 1,
|
|
811
|
+
fileId: null,
|
|
812
|
+
meta: {},
|
|
813
|
+
rocrateId: 'http://example.com/entity/1',
|
|
814
|
+
name: 'Test Entity 1',
|
|
815
|
+
description: 'Entity with missing parent',
|
|
816
|
+
entityType: 'http://pcdm.org/models#Object',
|
|
817
|
+
memberOf: 'http://example.com/entity/deleted',
|
|
818
|
+
rootCollection: 'http://example.com/entity/deleted',
|
|
819
|
+
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
820
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
821
|
+
createdAt: new Date('2024-01-01'),
|
|
822
|
+
updatedAt: new Date('2024-01-01'),
|
|
823
|
+
},
|
|
824
|
+
];
|
|
825
|
+
|
|
826
|
+
const mockSearchResponse = {
|
|
827
|
+
body: {
|
|
828
|
+
took: 10,
|
|
829
|
+
hits: {
|
|
830
|
+
total: { value: 1 },
|
|
831
|
+
hits: [
|
|
832
|
+
{
|
|
833
|
+
_score: 1.5,
|
|
834
|
+
_source: {
|
|
835
|
+
rocrateId: 'http://example.com/entity/1',
|
|
836
|
+
},
|
|
837
|
+
},
|
|
838
|
+
],
|
|
839
|
+
},
|
|
840
|
+
aggregations: {},
|
|
841
|
+
},
|
|
842
|
+
};
|
|
843
|
+
|
|
844
|
+
// @ts-expect-error TS is looking at the wrong function signature
|
|
845
|
+
opensearch.search.mockResolvedValue(mockSearchResponse);
|
|
846
|
+
// First findMany returns the entities, second (for reference resolution) returns empty
|
|
847
|
+
prisma.entity.findMany.mockResolvedValueOnce(mockEntities);
|
|
848
|
+
prisma.entity.findMany.mockResolvedValueOnce([]);
|
|
849
|
+
|
|
850
|
+
const response = await fastify.inject({
|
|
851
|
+
method: 'POST',
|
|
852
|
+
url: '/search',
|
|
853
|
+
payload: {
|
|
854
|
+
query: 'test',
|
|
855
|
+
},
|
|
856
|
+
});
|
|
857
|
+
const body = JSON.parse(response.body);
|
|
858
|
+
|
|
859
|
+
expect(response.statusCode).toBe(200);
|
|
860
|
+
expect(body).toMatchSnapshot();
|
|
861
|
+
});
|
|
806
862
|
});
|
|
807
863
|
});
|
package/src/routes/search.ts
CHANGED
|
@@ -4,7 +4,7 @@ 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';
|
|
7
|
+
import { baseEntityTransformer, resolveEntityReferences } from '../transformers/default.js';
|
|
8
8
|
import type { AccessTransformer, EntityTransformer } from '../types/transformers.js';
|
|
9
9
|
import { createInternalError } from '../utils/errors.js';
|
|
10
10
|
|
|
@@ -219,6 +219,9 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
|
|
|
219
219
|
|
|
220
220
|
const entityMap = new Map(dbEntities.map((entity) => [entity.rocrateId, entity]));
|
|
221
221
|
|
|
222
|
+
// Resolve memberOf and rootCollection references
|
|
223
|
+
const refMap = await resolveEntityReferences(dbEntities, fastify.prisma);
|
|
224
|
+
|
|
222
225
|
const entities = await Promise.all(
|
|
223
226
|
response.body.hits.hits.map(async (hit) => {
|
|
224
227
|
if (!hit._source?.rocrateId) {
|
|
@@ -232,7 +235,12 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
|
|
|
232
235
|
return null;
|
|
233
236
|
}
|
|
234
237
|
|
|
235
|
-
const
|
|
238
|
+
const base = baseEntityTransformer(dbEntity);
|
|
239
|
+
const standardEntity = {
|
|
240
|
+
...base,
|
|
241
|
+
memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
|
|
242
|
+
rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
|
|
243
|
+
};
|
|
236
244
|
const authorisedEntity = await accessTransformer(standardEntity, {
|
|
237
245
|
request,
|
|
238
246
|
fastify,
|
|
@@ -89,7 +89,6 @@ export async function seedTestData() {
|
|
|
89
89
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
90
90
|
createdAt: new Date(),
|
|
91
91
|
updatedAt: new Date(),
|
|
92
|
-
rocrate: {},
|
|
93
92
|
},
|
|
94
93
|
{
|
|
95
94
|
id: 2,
|
|
@@ -103,7 +102,6 @@ export async function seedTestData() {
|
|
|
103
102
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
104
103
|
createdAt: new Date(),
|
|
105
104
|
updatedAt: new Date(),
|
|
106
|
-
rocrate: {},
|
|
107
105
|
},
|
|
108
106
|
{
|
|
109
107
|
id: 3,
|
|
@@ -117,7 +115,6 @@ export async function seedTestData() {
|
|
|
117
115
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
118
116
|
createdAt: new Date(),
|
|
119
117
|
updatedAt: new Date(),
|
|
120
|
-
rocrate: {},
|
|
121
118
|
},
|
|
122
119
|
{
|
|
123
120
|
id: 4,
|
|
@@ -131,11 +128,6 @@ export async function seedTestData() {
|
|
|
131
128
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
132
129
|
createdAt: new Date(),
|
|
133
130
|
updatedAt: new Date(),
|
|
134
|
-
rocrate: {
|
|
135
|
-
'@type': ['http://schema.org/MediaObject', 'MediaObject'],
|
|
136
|
-
encodingFormat: 'audio/wav',
|
|
137
|
-
contentSize: '88200',
|
|
138
|
-
},
|
|
139
131
|
},
|
|
140
132
|
{
|
|
141
133
|
id: 5,
|
|
@@ -149,11 +141,6 @@ export async function seedTestData() {
|
|
|
149
141
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
150
142
|
createdAt: new Date(),
|
|
151
143
|
updatedAt: new Date(),
|
|
152
|
-
rocrate: {
|
|
153
|
-
'@type': ['http://schema.org/MediaObject', 'MediaObject'],
|
|
154
|
-
encodingFormat: 'text/csv',
|
|
155
|
-
contentSize: '1024',
|
|
156
|
-
},
|
|
157
144
|
},
|
|
158
145
|
];
|
|
159
146
|
|
|
@@ -53,8 +53,8 @@ describe('Integration Tests', () => {
|
|
|
53
53
|
expect(body.id).toBe('http://example.com/entity/4');
|
|
54
54
|
expect(body.name).toBe('test-audio.wav');
|
|
55
55
|
expect(body.entityType).toBe('http://schema.org/MediaObject');
|
|
56
|
-
expect(body.memberOf).toBe('http://example.com/entity/2');
|
|
57
|
-
expect(body.rootCollection).toBe('http://example.com/entity/1');
|
|
56
|
+
expect(body.memberOf?.id).toBe('http://example.com/entity/2');
|
|
57
|
+
expect(body.rootCollection?.id).toBe('http://example.com/entity/1');
|
|
58
58
|
});
|
|
59
59
|
|
|
60
60
|
it('should return 404 for non-existent entity', async () => {
|
|
@@ -173,7 +173,7 @@ describe('Integration Tests', () => {
|
|
|
173
173
|
expect(body.entities).toHaveLength(1);
|
|
174
174
|
expect(body.entities[0].id).toBe('http://example.com/entity/4');
|
|
175
175
|
expect(body.entities[0].name).toBe('test-audio.wav');
|
|
176
|
-
expect(body.entities[0].memberOf).toBe('http://example.com/entity/2');
|
|
176
|
+
expect(body.entities[0].memberOf?.id).toBe('http://example.com/entity/2');
|
|
177
177
|
});
|
|
178
178
|
|
|
179
179
|
it('should filter File entities by memberOf (Collection parent)', async () => {
|
|
@@ -194,7 +194,7 @@ describe('Integration Tests', () => {
|
|
|
194
194
|
expect(body.entities).toHaveLength(1);
|
|
195
195
|
expect(body.entities[0].id).toBe('http://example.com/entity/5');
|
|
196
196
|
expect(body.entities[0].name).toBe('collection-metadata.csv');
|
|
197
|
-
expect(body.entities[0].memberOf).toBe('http://example.com/entity/1');
|
|
197
|
+
expect(body.entities[0].memberOf?.id).toBe('http://example.com/entity/1');
|
|
198
198
|
});
|
|
199
199
|
|
|
200
200
|
it('should handle pagination', async () => {
|
|
@@ -17,7 +17,6 @@ describe('baseEntityTransformer', () => {
|
|
|
17
17
|
contentLicenseId: 'https://creativecommons.org/licenses/by-sa/4.0/',
|
|
18
18
|
createdAt: new Date('2025-01-01'),
|
|
19
19
|
updatedAt: new Date('2025-01-02'),
|
|
20
|
-
rocrate: { '@context': 'test' },
|
|
21
20
|
meta: { test: 'data' },
|
|
22
21
|
};
|
|
23
22
|
|
|
@@ -49,7 +48,6 @@ describe('baseEntityTransformer', () => {
|
|
|
49
48
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
50
49
|
createdAt: new Date(),
|
|
51
50
|
updatedAt: new Date(),
|
|
52
|
-
rocrate: {},
|
|
53
51
|
meta: null,
|
|
54
52
|
};
|
|
55
53
|
|
|
@@ -73,7 +71,6 @@ describe('baseEntityTransformer', () => {
|
|
|
73
71
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
74
72
|
createdAt: new Date(),
|
|
75
73
|
updatedAt: new Date(),
|
|
76
|
-
rocrate: { test: 'value' },
|
|
77
74
|
meta: { storage: 'path' },
|
|
78
75
|
};
|
|
79
76
|
|
|
@@ -83,7 +80,6 @@ describe('baseEntityTransformer', () => {
|
|
|
83
80
|
expect(result.id).toBe('http://example.com/entity/456');
|
|
84
81
|
expect(result).not.toHaveProperty('createdAt');
|
|
85
82
|
expect(result).not.toHaveProperty('updatedAt');
|
|
86
|
-
expect(result).not.toHaveProperty('rocrate');
|
|
87
83
|
expect(result).not.toHaveProperty('meta');
|
|
88
84
|
expect(Object.keys(result)).toEqual([
|
|
89
85
|
'id',
|
|
@@ -111,7 +107,6 @@ describe('baseEntityTransformer', () => {
|
|
|
111
107
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
112
108
|
createdAt: new Date(),
|
|
113
109
|
updatedAt: new Date(),
|
|
114
|
-
rocrate: {},
|
|
115
110
|
meta: null,
|
|
116
111
|
};
|
|
117
112
|
|
|
@@ -139,8 +134,8 @@ describe('AllPublicAccessTransformer', () => {
|
|
|
139
134
|
name: 'Test Entity',
|
|
140
135
|
description: 'A test entity',
|
|
141
136
|
entityType: 'http://schema.org/MediaObject',
|
|
142
|
-
memberOf: 'http://example.com/parent',
|
|
143
|
-
rootCollection: 'http://example.com/root',
|
|
137
|
+
memberOf: { id: 'http://example.com/parent', name: 'Parent Entity' },
|
|
138
|
+
rootCollection: { id: 'http://example.com/root', name: 'Root Collection' },
|
|
144
139
|
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
145
140
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
146
141
|
};
|
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
import type { Entity, File } from '../generated/prisma/client.js';
|
|
1
|
+
import type { Entity, File, PrismaClient } from '../generated/prisma/client.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* Does not include access information
|
|
4
|
+
* Entity reference - used for memberOf and rootCollection
|
|
6
5
|
*/
|
|
7
|
-
export type
|
|
6
|
+
export type EntityReference = {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Base entity shape - output of base transformation with unresolved references
|
|
13
|
+
* Used internally before reference resolution
|
|
14
|
+
*/
|
|
15
|
+
export type BaseEntity = {
|
|
8
16
|
id: string;
|
|
9
17
|
name: string;
|
|
10
18
|
description: string;
|
|
@@ -14,6 +22,20 @@ export type StandardEntity = {
|
|
|
14
22
|
contentLicenseId: string;
|
|
15
23
|
} & ({ entityType: string; fileId?: never } | { entityType: 'http://schema.org/MediaObject'; fileId: string });
|
|
16
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Standard entity shape - with resolved references
|
|
27
|
+
* Does not include access information
|
|
28
|
+
*/
|
|
29
|
+
export type StandardEntity = {
|
|
30
|
+
id: string;
|
|
31
|
+
name: string;
|
|
32
|
+
description: string;
|
|
33
|
+
memberOf: EntityReference | null;
|
|
34
|
+
rootCollection: EntityReference | null;
|
|
35
|
+
metadataLicenseId: string;
|
|
36
|
+
contentLicenseId: string;
|
|
37
|
+
} & ({ entityType: string; fileId?: never } | { entityType: 'http://schema.org/MediaObject'; fileId: string });
|
|
38
|
+
|
|
17
39
|
/**
|
|
18
40
|
* Access information for an entity
|
|
19
41
|
*/
|
|
@@ -66,10 +88,10 @@ export type AuthorisedFile = StandardFile & {
|
|
|
66
88
|
|
|
67
89
|
/**
|
|
68
90
|
* Base entity transformer - always applied first
|
|
69
|
-
* Transforms raw database entity to
|
|
91
|
+
* Transforms raw database entity to base entity shape with unresolved references
|
|
70
92
|
*/
|
|
71
|
-
export const baseEntityTransformer = (entity: Entity):
|
|
72
|
-
const base:
|
|
93
|
+
export const baseEntityTransformer = (entity: Entity): BaseEntity => {
|
|
94
|
+
const base: BaseEntity = {
|
|
73
95
|
id: entity.rocrateId,
|
|
74
96
|
name: entity.name,
|
|
75
97
|
description: entity.description,
|
|
@@ -163,3 +185,39 @@ export const AllPublicFileAccessTransformer = (file: StandardFile): AuthorisedFi
|
|
|
163
185
|
content: true,
|
|
164
186
|
},
|
|
165
187
|
});
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Resolve entity references (memberOf/rootCollection) to objects with id and name
|
|
191
|
+
* Batch-fetches all referenced entities in a single query for performance
|
|
192
|
+
*
|
|
193
|
+
* @param entities - Array of entities with memberOf/rootCollection string IDs
|
|
194
|
+
* @param prisma - Prisma client instance
|
|
195
|
+
* @returns Map of entity ID to EntityReference object
|
|
196
|
+
*/
|
|
197
|
+
export const resolveEntityReferences = async (
|
|
198
|
+
entities: Array<{ memberOf: string | null; rootCollection: string | null }>,
|
|
199
|
+
prisma: PrismaClient,
|
|
200
|
+
): Promise<Map<string, EntityReference>> => {
|
|
201
|
+
const refIds = new Set<string>();
|
|
202
|
+
|
|
203
|
+
entities.forEach((e) => {
|
|
204
|
+
if (e.memberOf) {
|
|
205
|
+
refIds.add(e.memberOf);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (e.rootCollection) {
|
|
209
|
+
refIds.add(e.rootCollection);
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
if (refIds.size === 0) {
|
|
214
|
+
return new Map();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const refs = await prisma.entity.findMany({
|
|
218
|
+
where: { rocrateId: { in: [...refIds] } },
|
|
219
|
+
select: { rocrateId: true, name: true },
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
return new Map(refs.map((r) => [r.rocrateId, { id: r.rocrateId, name: r.name }]));
|
|
223
|
+
};
|
|
@@ -4,6 +4,7 @@ import type { AccessTransformer, EntityTransformer, TransformerContext } from '.
|
|
|
4
4
|
import {
|
|
5
5
|
AllPublicAccessTransformer,
|
|
6
6
|
type AuthorisedEntity,
|
|
7
|
+
type BaseEntity,
|
|
7
8
|
baseEntityTransformer,
|
|
8
9
|
type StandardEntity,
|
|
9
10
|
} from './default.js';
|
|
@@ -19,8 +20,8 @@ describe('Entity Transformers', () => {
|
|
|
19
20
|
name: 'Test Entity',
|
|
20
21
|
description: 'A test entity',
|
|
21
22
|
entityType: 'http://schema.org/Person',
|
|
22
|
-
memberOf: 'http://example.com/collection',
|
|
23
|
-
rootCollection: 'http://example.com/root',
|
|
23
|
+
memberOf: { id: 'http://example.com/collection', name: 'Test Collection' },
|
|
24
|
+
rootCollection: { id: 'http://example.com/root', name: 'Root Collection' },
|
|
24
25
|
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
25
26
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
26
27
|
};
|
|
@@ -34,7 +35,7 @@ describe('Entity Transformers', () => {
|
|
|
34
35
|
};
|
|
35
36
|
|
|
36
37
|
describe('baseEntityTransformer', () => {
|
|
37
|
-
it('should transform raw entity to
|
|
38
|
+
it('should transform raw entity to base shape with unresolved references', () => {
|
|
38
39
|
const rawEntity = {
|
|
39
40
|
id: 1,
|
|
40
41
|
rocrateId: 'http://example.com/entity/123',
|
|
@@ -46,7 +47,6 @@ describe('Entity Transformers', () => {
|
|
|
46
47
|
rootCollection: 'http://example.com/root',
|
|
47
48
|
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
48
49
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
49
|
-
rocrate: {},
|
|
50
50
|
meta: {},
|
|
51
51
|
createdAt: new Date(),
|
|
52
52
|
updatedAt: new Date(),
|
|
@@ -54,7 +54,19 @@ describe('Entity Transformers', () => {
|
|
|
54
54
|
|
|
55
55
|
const result = baseEntityTransformer(rawEntity);
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
// baseEntityTransformer returns BaseEntity with string references
|
|
58
|
+
const expectedBaseEntity: BaseEntity = {
|
|
59
|
+
id: 'http://example.com/entity/123',
|
|
60
|
+
name: 'Test Entity',
|
|
61
|
+
description: 'A test entity',
|
|
62
|
+
entityType: 'http://schema.org/Person',
|
|
63
|
+
memberOf: 'http://example.com/collection',
|
|
64
|
+
rootCollection: 'http://example.com/root',
|
|
65
|
+
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
66
|
+
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
expect(result).toEqual(expectedBaseEntity);
|
|
58
70
|
});
|
|
59
71
|
});
|
|
60
72
|
|
|
@@ -181,7 +193,7 @@ describe('Entity Transformers', () => {
|
|
|
181
193
|
});
|
|
182
194
|
});
|
|
183
195
|
|
|
184
|
-
it('should demonstrate full pipeline: base -> access -> custom', async () => {
|
|
196
|
+
it('should demonstrate full pipeline: base -> resolve -> access -> custom', async () => {
|
|
185
197
|
const rawEntity = {
|
|
186
198
|
id: 1,
|
|
187
199
|
rocrateId: 'http://example.com/entity/123',
|
|
@@ -193,7 +205,6 @@ describe('Entity Transformers', () => {
|
|
|
193
205
|
rootCollection: 'http://example.com/root',
|
|
194
206
|
metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
195
207
|
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
196
|
-
rocrate: {},
|
|
197
208
|
meta: {},
|
|
198
209
|
createdAt: new Date(),
|
|
199
210
|
updatedAt: new Date(),
|
|
@@ -213,14 +224,24 @@ describe('Entity Transformers', () => {
|
|
|
213
224
|
processed: true,
|
|
214
225
|
});
|
|
215
226
|
|
|
216
|
-
// Full pipeline
|
|
217
|
-
const
|
|
227
|
+
// Full pipeline: base -> resolve references -> access -> custom
|
|
228
|
+
const base = baseEntityTransformer(rawEntity);
|
|
229
|
+
|
|
230
|
+
// Simulate reference resolution (in real code, this uses resolveEntityReferences)
|
|
231
|
+
const standard: StandardEntity = {
|
|
232
|
+
...base,
|
|
233
|
+
memberOf: base.memberOf ? { id: base.memberOf, name: 'Parent Collection' } : null,
|
|
234
|
+
rootCollection: base.rootCollection ? { id: base.rootCollection, name: 'Root Collection' } : null,
|
|
235
|
+
};
|
|
236
|
+
|
|
218
237
|
const authorised = await customAccessTransformer(standard, mockContext);
|
|
219
238
|
const final = await addMetadata(authorised, mockContext);
|
|
220
239
|
|
|
221
240
|
expect(final).toMatchObject({
|
|
222
241
|
id: 'http://example.com/entity/123',
|
|
223
242
|
name: 'Test Entity',
|
|
243
|
+
memberOf: { id: 'http://example.com/collection', name: 'Parent Collection' },
|
|
244
|
+
rootCollection: { id: 'http://example.com/root', name: 'Root Collection' },
|
|
224
245
|
access: {
|
|
225
246
|
metadata: true,
|
|
226
247
|
content: false,
|