Changeset View
Changeset View
Standalone View
Standalone View
lib/app/base-chips/field-types/reverse-single-reference.js
"use strict"; | |||||
const locreq = require("locreq")(__dirname); | |||||
const Promise = require("bluebird"); | const Promise = require("bluebird"); | ||||
const Errors = locreq("lib/response/error.js"); | |||||
const assert = require("assert"); | const assert = require("assert"); | ||||
function params_to_cache_key(collection, field_name, params) { | function params_to_cache_key(collection, field_name, params) { | ||||
return `${collection.name}___${field_name}-reverse-single-reference(${ | return `${collection.name}___${field_name}-reverse-single-reference(${ | ||||
params.collection.name | params.collection.name | ||||
},${params.collection.field_name}).last_update`; | },${params.collection.field_name}).last_update`; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | for (let entry of to_update) { | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
const reverse_single_reference_factory = app => { | const reverse_single_reference_factory = app => { | ||||
return { | return { | ||||
name: "reverse-single-reference", | name: "reverse-single-reference", | ||||
get_description: function() { | get_description() { | ||||
return "Shows which resources from given collection point to this resource in a given field."; | return "Shows which resources from given collection point to this resource in a given field."; | ||||
}, | }, | ||||
get_default_value: async () => [], | get_default_value: async () => [], | ||||
is_old_value_sensitive: true, | is_old_value_sensitive: true, | ||||
is_proper_value: function(context, params, new_value) { | is_proper_value(context, params, new_value) { | ||||
return context.is_super | return context.is_super | ||||
? Promise.resolve() | ? Promise.resolve() | ||||
: Promise.reject("This is a read-only field"); | : Promise.reject("This is a read-only field"); | ||||
}, | }, | ||||
filter_to_query: async function(context, params, field_filter) { | async filter_to_query(context, params, field_filter) { | ||||
if (typeof field_filter !== "object") { | if (typeof field_filter !== "object") { | ||||
return { | return { | ||||
$eq: field_filter, | $eq: field_filter, | ||||
}; | }; | ||||
} | } | ||||
const { items: matches } = await app.run_action( | const { items: matches } = await app.run_action( | ||||
context, | context, | ||||
["collections", params.collection.name], | ["collections", params.collection.name], | ||||
"show", | "show", | ||||
{ filter: field_filter } | { filter: field_filter } | ||||
); | ); | ||||
const ids = matches.map(resource => resource.id); | const ids = matches.map(resource => resource.id); | ||||
return { | return { | ||||
$in: ids, | $in: ids, | ||||
}; | }; | ||||
}, | }, | ||||
format: function(context, params, decoded_value, format) { | format(context, params, decoded_value, format) { | ||||
// format can be "expand" or "deep-expand:<depth>", like "deep-expand:3" | // format can be "expand" or "deep-expand:<depth>", like "deep-expand:3" | ||||
if (!format) { | if (!format) { | ||||
return decoded_value; // just the IDs | return decoded_value; // just the IDs | ||||
} | } | ||||
const format_params = format.split(":"); | const format_params = format.split(":"); | ||||
if (!["expand", "deep-expand"].includes(format_params[0])) { | if (!["expand", "deep-expand"].includes(format_params[0])) { | ||||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | init: async (collection, field_name, params) => { | ||||
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: "edit", | action: "edit", | ||||
}), | }), | ||||
async ({ metadata, subject_path }, resource) => { | async ({ metadata, subject_path }, resource) => { | ||||
if (!metadata.params.hasOwnProperty(params.field_name)) | if ( | ||||
!Object.prototype.hasOwnProperty.call( | |||||
metadata.params, | |||||
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, | ||||
{ | { | ||||
[field_name]: edited_id, | [field_name]: edited_id, | ||||
} | } | ||||
); | ); | ||||
Show All 20 Lines |