-
Notifications
You must be signed in to change notification settings - Fork 4
/
datasource.ts
247 lines (218 loc) · 6.9 KB
/
datasource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import test from 'brittle'
import { setup } from './util/setup.js'
import { DataSource, repoRegistry } from '../lib.js'
import {
BaseDataSource,
DataSourceDefinition,
FetchUpdatesResult,
ingestUpdatesFromDataSource,
ingestUpdatesFromDataSources,
remapDataSource,
SourceRecordForm,
} from '../src/datasource.js'
import { EntityForm, TypedEntityForm } from '../src/entity.js'
import { DataSourcePlugin, DataSourcePluginRegistry } from '../src/plugins.js'
const intoSourceRecord = (body: EntityForm): SourceRecordForm => ({
body: JSON.stringify(body),
sourceType: 'entity',
sourceUri: 'test:foo',
contentType: 'text/repco-entity',
})
const DS_UID = 'urn:repco:datasource:test'
// const fromSourceRecord = (record: SourceRecordForm) =>
// JSON.parse(record.body) as EntityForm
class TestDataSourcePlugin implements DataSourcePlugin {
createInstance(_config: any) {
return new TestDataSource()
}
get definition() {
return {
name: 'test',
uid: 'ds:test',
}
}
}
class TestDataSource extends BaseDataSource implements DataSource {
mapUppercase = false
insertMissing = false
resolveMissing = false
get definition(): DataSourceDefinition {
return {
name: 'TestDataSource',
uid: DS_UID,
pluginUid: 'urn:repco:datasource:test',
}
}
canFetchUri(uid: string): boolean {
if (uid.startsWith('urn:test:')) return true
return false
}
async fetchUpdates(cursor: string | null): Promise<FetchUpdatesResult> {
if (cursor === '1') {
return { cursor, records: [] }
}
const nextCursor = '1'
const bodies: EntityForm[] = []
const contentItem: TypedEntityForm<'ContentItem'> = {
type: 'ContentItem',
content: {
title: 'Test1',
MediaAssets: [{ uri: 'urn:test:media:1' }],
content: 'helloworld',
contentFormat: 'text/plain',
summary: '{}',
contentUrl: '',
},
headers: { EntityUris: ['urn:test:content:1'] },
}
if (this.insertMissing) {
contentItem.content.MediaAssets!.push({ uri: 'urn:test:media:fail' })
}
bodies.push(contentItem)
return {
cursor: nextCursor,
records: bodies.map(intoSourceRecord),
}
}
async fetchByUri(uid: string): Promise<SourceRecordForm[] | null> {
if (uid === 'urn:test:file:1') {
return [
intoSourceRecord({
type: 'File',
content: {
contentUrl: 'http://example.org/file1.mp3',
},
headers: { EntityUris: ['urn:test:file:1'] },
}),
]
}
if (uid === 'urn:test:media:1') {
return [
intoSourceRecord({
type: 'MediaAsset',
content: {
title: 'Media1',
mediaType: 'audio/mp3',
Files: [{ uri: 'urn:test:file:1' }],
description: '{}',
},
headers: { EntityUris: ['urn:test:media:1'] },
}),
]
}
if (this.resolveMissing && uid === 'urn:test:media:fail') {
return [
intoSourceRecord({
type: 'MediaAsset',
content: {
title: 'MediaMissingResolved',
mediaType: 'audio/mp3',
Files: [{ uri: 'urn:test:file:1' }],
description: '{}',
},
headers: { EntityUris: ['urn:test:media:fail'] },
}),
]
}
throw new Error('Not found: ' + uid)
}
async mapSourceRecord(record: SourceRecordForm): Promise<EntityForm[]> {
const form = JSON.parse(record.body) as EntityForm
if (this.mapUppercase) {
if (form.type === 'ContentItem') {
form.content.title = form.content.title //.toUpperCase()
}
}
return [form]
}
}
test('datasource', async (assert) => {
const prisma = await setup(assert)
const repo = await repoRegistry.create(prisma, 'test')
const plugins = new DataSourcePluginRegistry()
plugins.register(new TestDataSourcePlugin())
await repo.dsr.create(repo.prisma, plugins, 'ds:test', {}, repo.did)
await ingestUpdatesFromDataSources(repo)
const uri = 'urn:test:content:1'
const entities = await prisma.contentItem.findMany({
where: { Revision: { entityUris: { has: uri } } },
include: {
MediaAssets: {
include: { Files: true },
},
},
})
assert.is(entities.length, 1)
const entity = entities[0]
assert.is(entity.MediaAssets.length, 1)
assert.is(
entity.MediaAssets[0].Files[0].contentUrl,
'http://example.org/file1.mp3',
)
})
test('remap', async (assert) => {
const prisma = await setup(assert)
const repo = await repoRegistry.create(prisma, 'test')
const plugins = new DataSourcePluginRegistry()
plugins.register(new TestDataSourcePlugin())
await repo.dsr.create(repo.prisma, plugins, 'ds:test', {}, repo.did)
await ingestUpdatesFromDataSources(repo)
const head = await repo.getHead()
let len = await prisma.revision.count()
assert.is(len, 3)
const datasource = repo.dsr.get(DS_UID)
assert.is(!!datasource, true)
await remapDataSource(repo, datasource!)
const head2 = await repo.getHead()
assert.is(head.toString(), head2.toString())
len = await prisma.revision.count()
assert.is(len, 3)
// @ts-ignore
datasource.mapUppercase = true
await remapDataSource(repo, datasource!)
const head3 = await repo.getHead()
assert.not(head2.toString(), head3.toString())
len = await prisma.revision.count()
assert.is(len, 4)
const entitiesAfter = await prisma.contentItem.findMany()
assert.is(entitiesAfter.length, 1)
assert.is(entitiesAfter[0].title, 'TEST1')
})
// TODO: This is not working with batching right now.
test.skip('failed fetches', async (assert) => {
const prisma = await setup(assert)
const repo = await repoRegistry.create(prisma, 'test')
const plugins = new DataSourcePluginRegistry()
plugins.register(new TestDataSourcePlugin())
await repo.dsr.create(repo.prisma, plugins, 'ds:test', {}, repo.did)
const datasource = repo.dsr.get(DS_UID)! as TestDataSource
datasource.insertMissing = true
{
const res = await ingestUpdatesFromDataSource(repo, datasource)
assert.is(res.count, 1)
const revisions = await repo.prisma.revision.count()
assert.is(revisions, 3)
const entities = await repo.prisma.entity.count()
assert.is(entities, 3)
const fails = await repo.prisma.failedDatasourceFetches.findMany()
assert.is(fails.length, 1)
assert.is(fails[0].uri, 'urn:test:media:fail')
}
datasource.resolveMissing = true
await remapDataSource(repo, datasource)
{
const revisions = await repo.prisma.revision.count()
assert.is(revisions, 5)
const entities = await repo.prisma.entity.count()
assert.is(entities, 4)
}
{
const res = await ingestUpdatesFromDataSource(repo, datasource)
assert.is(res.count, 0)
const revisions = await repo.prisma.revision.count()
assert.is(revisions, 5)
const entities = await repo.prisma.entity.count()
assert.is(entities, 4)
}
})