Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/reverse-single-reference.js
Show All 19 Lines | ) { | ||||
let pipeline; | let pipeline; | ||||
const referencing_field_name = params.field_name; | const referencing_field_name = params.field_name; | ||||
const referencing_collection = params.collection; | const referencing_collection = params.collection; | ||||
if (resource_ids) { | if (resource_ids) { | ||||
assert(Array.isArray(resource_ids)); | assert(Array.isArray(resource_ids)); | ||||
pipeline = [ | pipeline = [ | ||||
{ | { | ||||
$match: { | $match: { | ||||
[`body.${referencing_field_name}`]: { $in: resource_ids }, | [referencing_field_name]: { $in: resource_ids }, | ||||
}, | }, | ||||
}, | }, | ||||
]; | ]; | ||||
} else { | } else { | ||||
pipeline = []; | pipeline = []; | ||||
} | } | ||||
pipeline.push({ | pipeline.push({ | ||||
$group: { | $group: { | ||||
_id: `$body.${referencing_field_name}`, | _id: `$${referencing_field_name}`, | ||||
referenced_by: { $push: `$sealious_id` }, | referenced_by: { $push: "$sealious_id" }, | ||||
}, | }, | ||||
}); | }); | ||||
const to_update = await app.Datastore.aggregate( | const to_update = await app.Datastore.aggregate( | ||||
referencing_collection.name, | referencing_collection.name, | ||||
pipeline | pipeline | ||||
); | ); | ||||
if (resource_ids) { | if (resource_ids) { | ||||
for (let resource_id of resource_ids) { | for (let resource_id of resource_ids) { | ||||
if (to_update.filter(e => e._id === resource_id).length === 0) { | if (to_update.filter(e => e._id === resource_id).length === 0) { | ||||
to_update.push({ _id: resource_id, referenced_by: [] }); | to_update.push({ _id: resource_id, referenced_by: [] }); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
for (let entry of to_update) { | for (let entry of to_update) { | ||||
await app.Datastore.update( | await app.Datastore.update( | ||||
collection.name, | collection.name, | ||||
{ sealious_id: entry._id }, | { sealious_id: entry._id }, | ||||
{ $set: { [`body.${field_name}`]: entry.referenced_by } } | { $set: { [field_name]: entry.referenced_by } } | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
const reverse_single_reference_factory = app => { | const reverse_single_reference_factory = app => { | ||||
return { | return { | ||||
name: "reverse-single-reference", | name: "reverse-single-reference", | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | init: async (collection, field_name, params) => { | ||||
); | ); | ||||
app.addHook({ when: "after", action: "start" }, async () => { | app.addHook({ when: "after", action: "start" }, async () => { | ||||
const last_modified_resource_in_reference_collection = (await app.run_action( | const last_modified_resource_in_reference_collection = (await app.run_action( | ||||
new app.Sealious.SuperContext(), | new app.Sealious.SuperContext(), | ||||
["collections", params.collection.name], | ["collections", params.collection.name], | ||||
"show", | "show", | ||||
{ | { | ||||
sort: { "last_modified_context.timestamp": "desc" }, | sort: { | ||||
"_metadata.last_modified_context.timestamp": "desc", | |||||
}, | |||||
pagination: { items: 1 }, | pagination: { items: 1 }, | ||||
} | } | ||||
)).items[0]; | )).items[0]; | ||||
if (last_modified_resource_in_reference_collection) { | if (last_modified_resource_in_reference_collection) { | ||||
const last_modified_resource_timestamp = | const last_modified_resource_timestamp = | ||||
last_modified_resource_in_reference_collection | last_modified_resource_in_reference_collection._metadata | ||||
.last_modified_context.timestamp; | .last_modified_context.timestamp; | ||||
const last_field_cache_update = | const last_field_cache_update = | ||||
(await app.Metadata.get( | (await app.Metadata.get( | ||||
params_to_cache_key(collection, field_name, params) | params_to_cache_key(collection, field_name, params) | ||||
)) || 0; | )) || 0; | ||||
if ( | if ( | ||||
last_modified_resource_timestamp > | last_modified_resource_timestamp > | ||||
last_field_cache_update | last_field_cache_update | ||||
Show All 9 Lines | init: async (collection, field_name, params) => { | ||||
app.addHook( | app.addHook( | ||||
new app.Sealious.EventMatchers.Collection({ | new app.Sealious.EventMatchers.Collection({ | ||||
when: "after", | when: "after", | ||||
collection_name: params.collection.name, | collection_name: params.collection.name, | ||||
action: "create", | action: "create", | ||||
}), | }), | ||||
async (emitted_event, resource) => { | async (emitted_event, resource) => { | ||||
const referenced_id = resource.body[params.field_name]; | const referenced_id = resource[params.field_name]; | ||||
await update_cache(app, collection, field_name, params, [ | await update_cache(app, collection, field_name, params, [ | ||||
referenced_id, | referenced_id, | ||||
]); | ]); | ||||
} | } | ||||
); | ); | ||||
app.addHook( | app.addHook( | ||||
new app.Sealious.EventMatchers.Resource({ | new app.Sealious.EventMatchers.Resource({ | ||||
when: "after", | when: "after", | ||||
collection_name: params.collection.name, | collection_name: params.collection.name, | ||||
action: "delete", | action: "delete", | ||||
}), | }), | ||||
async (emitted_event, data) => { | async (emitted_event, data) => { | ||||
const deleted_id = emitted_event.subject_path.split(".")[2]; | const deleted_id = emitted_event.subject_path.split(".")[2]; | ||||
const affected = await app.Datastore.find(collection.name, { | const affected = await app.Datastore.find(collection.name, { | ||||
[`body.${field_name}`]: deleted_id, | [field_name]: deleted_id, | ||||
}); | }); | ||||
const affected_ids = affected.map( | const affected_ids = affected.map( | ||||
document => document.sealious_id | document => document.sealious_id | ||||
); | ); | ||||
await update_cache( | await update_cache( | ||||
app, | app, | ||||
collection, | collection, | ||||
field_name, | field_name, | ||||
Show All 11 Lines | init: async (collection, field_name, params) => { | ||||
}), | }), | ||||
async ({ metadata, subject_path }, resource) => { | async ({ metadata, subject_path }, resource) => { | ||||
if (!metadata.params.hasOwnProperty(params.field_name)) | if (!metadata.params.hasOwnProperty(params.field_name)) | ||||
return; | return; | ||||
const edited_id = subject_path.split(".")[2]; | const edited_id = subject_path.split(".")[2]; | ||||
const no_longer_referenced = await app.Datastore.find( | const no_longer_referenced = await app.Datastore.find( | ||||
collection.name, | collection.name, | ||||
{ | { | ||||
[`body.${field_name}`]: edited_id, | [field_name]: edited_id, | ||||
} | } | ||||
); | ); | ||||
const affected_ids = no_longer_referenced.map( | const affected_ids = no_longer_referenced.map( | ||||
document => document.sealious_id | document => document.sealious_id | ||||
); | ); | ||||
if (metadata.params[params.field_name]) { | if (metadata.params[params.field_name]) { | ||||
affected_ids.push(metadata.params[params.field_name]); | affected_ids.push(metadata.params[params.field_name]); | ||||
} | } | ||||
Show All 14 Lines |