-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.js
407 lines (386 loc) · 11.8 KB
/
document.js
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
var _ = require("underscore");
const config = require("./config");
const knex = require("knex")({
// debug: true,
client: "mysql",
connection: config.mysql
});
/** All the verbose code for storing and retreiving from MySQL and converting to/from artwork-centric document objects. */
/**
* Insert a new artwork.
*
* Some parts of this code are motivated by inconsistencies in imported Elasticsearch data.
*/
async function insertDocument(artwork) {
const values = formatArtworkRow(artwork);
// Insert persons to reference them.
values.sender = await ensurePerson(artwork.sender);
values.recipient = await ensurePerson(artwork.recipient);
const insertIds = await knex("artwork").insert(noUndefined(values));
const artworkId = insertIds[0];
function insertKeyword(field, type) {
return Promise.all(
(Array.isArray(artwork[field]) ? artwork[field] : [artwork[field]])
.filter(x => x)
.map(name =>
knex("keyword").insert({
artwork: artworkId,
type,
name
})
)
);
}
return await Promise.all([
insertKeyword("type", "type"),
insertKeyword("genre", "genre"),
insertKeyword("tags", "tag"),
insertKeyword("persons", "person"),
insertKeyword("places", "place"),
insertKeyword("series", "series"),
...artwork.images.map(image =>
knex("image").insert(formatImageRow(artworkId, image))
)
]);
}
/**
* Update an existing artwork.
*/
async function updateDocument(artwork) {
const values = formatArtworkRow(artwork);
// Insert persons to reference them.
values.sender = await ensurePerson(artwork.sender);
values.recipient = await ensurePerson(artwork.recipient);
await knex("artwork").where({ insert_id: artwork.id }).update(values);
const artworkId = await knex("artwork")
.where({ insert_id: artwork.id })
.pluck("id");
// Insert and delete keywords and images.
async function updateKeywords(field, type) {
const keywords = (artwork[field] || [])
.map(name => name.trim())
.filter(Boolean);
// Delete exisiting keywords to ensure they are stored in order.
await knex("keyword").where({ artwork: artworkId, type }).delete();
// Insert new list.
return knex("keyword").insert(
keywords.map(name => ({ artwork: artworkId, type, name }))
);
}
return await Promise.all([
updateKeywords("type", "type"),
updateKeywords("genre", "genre"),
updateKeywords("tags", "tag"),
updateKeywords("persons", "person"),
updateKeywords("places", "place"),
updateKeywords("series", "series"),
updateImages(artworkId, artwork.images)
]);
}
/** Ensure a sender/recipient exists, and return its id. */
async function ensurePerson(person) {
if (!person || !(person.surname || person.name)) return null;
return await ensure("person", ["name"], {
name: person.surname
? `${person.firstname} ${person.surname}`
: person.name,
birth_year: person.birth_year,
death_year: person.death_year
});
}
/** Format most of the fields for a row in the artwork table, using an Elasticsearch-formatted object. */
function formatArtworkRow(artwork) {
return {
insert_id: artwork.insert_id,
name: artwork.insert_id,
title: artwork.title,
title_en: artwork.title_en,
subtitle: artwork.subtitle,
deleted: artwork.deleted || false,
published: artwork.published || false,
description: artwork.description,
museum_int_id: Array.isArray(artwork.museum_int_id)
? artwork.museum_int_id.join("|")
: artwork.museum_int_id,
museum: artwork.collection && artwork.collection.museum,
museum_url: artwork.museumLink,
date_human: artwork.item_date_str,
date: artwork.item_date_string,
size: artwork.size ? JSON.stringify(artwork.size) : undefined,
technique_material: artwork.technique_material,
acquisition: artwork.acquisition || undefined,
content: artwork.content,
inscription: artwork.inscription,
material: Array.isArray(artwork.material)
? artwork.material.pop()
: undefined,
creator: artwork.creator,
signature: artwork.signature,
// sender set below
// recipient set below
exhibitions:
artwork.exhibitions && artwork.exhibitions.length
? JSON.stringify(
artwork.exhibitions
.filter(s => s)
.map(s => {
// "<location>|<year>" or "<location> <year>"
const match = s.match(/(.*)(.(\d{4}))?/);
return {
location: match[1],
year: match[3]
};
})
)
: undefined,
literature: artwork.literature,
reproductions: artwork.reproductions,
bundle: artwork.bundle,
bundle_order: artwork.page && artwork.page.order,
bundle_side: artwork.page && artwork.page.side
};
}
/**
* Ensure (find or insert) a row and return its (existing or new) id.
*
* This is not an upsert, it does not update any existing rows.
*/
async function ensure(table, uniqueCols, row) {
// Find a row by the unique columns.
const rows = await knex(table).select("id").where(_.pick(row, uniqueCols));
if (rows.length) return rows[0].id;
// If not found, insert the full row.
const insertIds = await knex(table).insert(row);
return insertIds[0];
}
/** Format the fields for a row in the image table, using an Elasticsearch-formatted object. */
function formatImageRow(artworkId, image) {
return noUndefined({
artwork: artworkId,
filename: image.image,
type: image.imagesize.type,
width: image.imagesize.width,
height: image.imagesize.height,
page: image.page && (image.page.number || undefined),
pageid: image.page && image.page.id,
order: image.page && (image.page.order || undefined),
side: image.page && image.page.side,
color:
image.googleVisionColors &&
JSON.stringify(
image.googleVisionColors.sort((a, b) => b.score - a.score)[0].color
)
});
}
/** Update the list of images for a given artwork. */
async function updateImages(artworkId, images) {
const existing = await knex("image")
.select("*")
.where({ artwork: artworkId });
// Insert images. For images that already exist, update them.
const upserts = (images || []).map(image =>
knex("image")
.insert(formatImageRow(artworkId, image))
.catch(err =>
err.code === "ER_DUP_ENTRY"
? knex("image")
.where({ artwork: artworkId, filename: image.image })
.update(formatImageRow(artworkId, image))
: Promise.reject(err)
)
);
// Delete images that are not in the incoming data.
const deletes = existing
.filter(e => !images.find(i => i.image === e.filename))
.map(image =>
knex("image")
.where({ artwork: artworkId, filename: image.image })
.delete()
);
// Return a promise of the promises.
return Promise.all(upserts.concat(deletes));
}
/** Load a document from the database and format it. */
async function loadDocuments(insert_ids, includeInternalId = false) {
// Convert legacy ids.
insert_ids = insert_ids.map(parseId);
const artworks = await knex("artwork")
.leftJoin({ sender: "person" }, "artwork.sender", "sender.id")
.leftJoin({ recipient: "person" }, "artwork.recipient", "recipient.id")
.select("artwork.*", {
// Rename person columns to avoid collision.
sender_name: "sender.name",
sender_birth_year: "sender.birth_year",
sender_death_year: "sender.death_year",
recipient_name: "recipient.name",
recipient_birth_year: "recipient.birth_year",
recipient_death_year: "recipient.death_year"
})
.whereIn("artwork.insert_id", insert_ids)
.orderByRaw("FIND_IN_SET(??, ?)", [
"artwork.insert_id",
insert_ids.join(",")
]);
const ids = artworks.map(artwork => artwork.id);
// Load all associated records at once to reduce the amount of MySQL queries.
const [imagesAll, keywordsAll] = await Promise.all([
knex("image").whereIn("artwork", ids).orderBy("order", "asc"),
knex("keyword").whereIn("artwork", ids).orderBy("id", "asc")
]);
// Re-associate each image and keyword to their corresponding artwork objects.
return artworks.map(artwork => {
const images = imagesAll.filter(image => image.artwork == artwork.id);
// Group keywords by type.
const keywords = {};
keywordsAll
.filter(keyword => keyword.artwork == artwork.id)
.forEach(row => {
keywords[row.type] = keywords[row.type] || [];
keywords[row.type].push(row.name);
});
const sender = {
name: artwork.sender_name,
birth_year: artwork.sender_birth_year,
death_year: artwork.sender_death_year
};
const recipient = {
name: artwork.recipient_name,
birth_year: artwork.recipient_birth_year,
death_year: artwork.recipient_death_year
};
const document = formatDocument({
artwork,
images,
keywords,
sender,
recipient
});
if (includeInternalId) document._id = artwork.id;
return document;
});
}
/** Combine rows related to an object into a single structured object. */
function formatDocument({ artwork, images, keywords, sender, recipient }) {
const imagesFormatted =
images &&
images.map(image => ({
image: image.filename,
imagesize: {
width: image.width,
height: image.height,
type: image.type || undefined
},
page: {
number: image.page,
order: image.order,
side: image.side,
id: image.pageid || undefined
},
googleVisionColors: image.color
? [
{
color: JSON.parse(image.color),
score: 1
}
]
: undefined
}));
return {
insert_id: artwork.insert_id,
id: artwork.insert_id,
title: artwork.title,
title_en: artwork.title_en,
subtitle: artwork.subtitle,
deleted: artwork.deleted,
published: artwork.published,
description: artwork.description,
museum_int_id: artwork.museum_int_id.split("|"),
collection: {
museum: artwork.museum
},
museumLink: artwork.museum_url,
item_date_str: artwork.date_human,
item_date_string: artwork.date,
size: artwork.size ? JSON.parse(artwork.size) : undefined,
technique_material: artwork.technique_material,
acquisition: artwork.acquisition,
content: artwork.content,
inscription: artwork.inscription,
material: artwork.material,
creator: artwork.creator,
signature: artwork.signature,
literature: artwork.literature,
reproductions: artwork.reproductions,
bundle: artwork.bundle,
page: {
order: artwork.bundle_order,
side: artwork.bundle_side
},
images: imagesFormatted,
image: imagesFormatted && imagesFormatted[0] && imagesFormatted[0].image,
type: keywords.type,
tags: keywords.tag,
persons: keywords.person,
places: keywords.place,
genre: keywords.genre,
series: keywords.series,
exhibitions: artwork.exhibitions
? JSON.parse(artwork.exhibitions).map(
({ location, year }) => `${location}|${year}`
)
: undefined,
sender: sender
? {
name: sender.name,
birth_year: sender.birth_year,
death_year: sender.death_year
}
: {},
recipient: recipient
? {
name: recipient.name,
birth_year: recipient.birth_year,
death_year: recipient.death_year
}
: {}
};
}
async function deleteDocuments(insert_ids) {
const ids = await knex("artwork")
.pluck("id")
.where("insert_id", "in", insert_ids);
await Promise.all([
knex("artwork").delete().where("id", "in", ids),
knex("image").delete().where("artwork", "in", ids),
knex("keyword").delete().where("artwork", "in", ids)
]);
}
/**
* Convert legacy ids (aka names) to insert_id, e.g. "PRIV-4844" to "4844".
*
* We used to build ids from a museum code (e.g. PRIV, GUB) and the insert_id,
* but abolished that in Jan 2021 since the owners of the artworks may change.
* As legacy ids may still be around in links from external sites, any incoming
* id should be converted to the insert_id form.
* This function takes both forms and normalizes to the latter.
*/
function parseId(id) {
return id.replace(/[A-Za-z]+-/, "");
}
/**
* Convert `undefined` values in an object to `null`.
*
* Useful when providing objects to knex.insert and knex.update.
* Not recursive.
*/
function noUndefined(obj) {
return _.mapObject(obj, v => (v === undefined ? null : v));
}
module.exports = {
insertDocument,
updateDocument,
updateImages,
loadDocuments,
formatDocument,
deleteDocuments
};