Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/cached-value.js
Show First 20 Lines • Show All 111 Lines • ▼ Show 20 Lines | ) { | ||||
const referenced_collection_name = | const referenced_collection_name = | ||||
create_action.event_matcher.collection_name; | create_action.event_matcher.collection_name; | ||||
const last_modified_resource = (await app.run_action( | const last_modified_resource = (await app.run_action( | ||||
new app.Sealious.SuperContext(), | new app.Sealious.SuperContext(), | ||||
["collections", referenced_collection_name], | ["collections", referenced_collection_name], | ||||
"show", | "show", | ||||
{ | { | ||||
sort: { "last_modified_context.timestamp": "desc" }, | sort: { "last_modified_context._metadata.timestamp": "desc" }, | ||||
pagination: { items: 1 }, | pagination: { items: 1 }, | ||||
} | } | ||||
)).items[0]; | )).items[0]; | ||||
if (!last_modified_resource) { | if (!last_modified_resource) { | ||||
return; | return; | ||||
} | } | ||||
const last_modified_timestamp = | const last_modified_timestamp = | ||||
last_modified_resource.last_modified_context.timestamp; | last_modified_resource._metadata.last_modified_context.timestamp; | ||||
const outdated_resources = await app.Datastore.aggregate( | const outdated_resources = await app.Datastore.aggregate( | ||||
collection.name, | collection.name, | ||||
[ | [ | ||||
{ | { | ||||
$match: { | $match: { | ||||
$or: [ | $or: [ | ||||
{ | { | ||||
[`body.${field_name}.timestamp`]: { | [`${field_name}.timestamp`]: { | ||||
$lt: last_modified_timestamp, | $lt: last_modified_timestamp, | ||||
}, | }, | ||||
}, | }, | ||||
{ [`body.${field_name}`]: { $exists: false } }, | { [field_name]: { $exists: false } }, | ||||
], | ], | ||||
}, | }, | ||||
}, | }, | ||||
] | ] | ||||
); | ); | ||||
if (!outdated_resources) { | if (!outdated_resources) { | ||||
return; | return; | ||||
} | } | ||||
const context = new app.Sealious.SuperContext(); | const context = new app.Sealious.SuperContext(); | ||||
for (let resource of outdated_resources) { | for (let resource of outdated_resources) { | ||||
const cache_value = await this.encode( | const cache_value = await this.encode( | ||||
context, | context, | ||||
params, | params, | ||||
await params.get_value(context, resource.sealious_id) | await params.get_value(context, resource.sealious_id) | ||||
); | ); | ||||
await app.Datastore.update( | await app.Datastore.update( | ||||
collection.name, | collection.name, | ||||
{ sealious_id: resource.sealious_id }, | { sealious_id: resource.sealious_id }, | ||||
{ $set: { [`body.${field_name}`]: cache_value } } | { $set: { [field_name]: cache_value } } | ||||
); | ); | ||||
} | } | ||||
}, | }, | ||||
encode: async function(context, params, value) { | encode: async function(context, params, value) { | ||||
return { | return { | ||||
timestamp: context.timestamp, | timestamp: context.timestamp, | ||||
value: await this._call_base_method( | value: await this._call_base_method( | ||||
"encode", | "encode", | ||||
Show All 32 Lines |