interface ScriptDefinition {
... // ?? to be defined
__meta__: {
version: number; // this lets the editor know how to handle the def
}
}
- Runtime field editor returns a field
{
name: string;
emitType: string;
getScript() => {
script: string;
scriptDefinition: ScriptDefinition;
};
}
- Index pattern management needs to
a. Send the field to attach to the ES query
const { getScript, ...rest } = field;
const fieldForQuery = {
...rest,
script: getScript().script, // get the painless script
}
b. Save the field to the index pattern saved object
const fieldForIndexPatternSavedObject = {
...rest,
scriptDefinition: getScript().scriptDefinition, // save only the definition. This could be "raw" string and be simply painless
}
- Index pattern will need to retrieve the runtime fields from the saved object and return them both in the list of fields and ready for ES query
const runtimeFields = savedObject.somePath.runtimeFields.map((field) => {
// Here we need to return **both** the script for the ES query and the definition for the UI
return {
...field,
// call a method from the plugin (it could also be exposed statically)
script: runtimeFieldsPlugin.getScriptFromDefinition(field.scriptDefinition)
};
});
And the above returns runtime field objects like this:
{
name: string;
outputType: string;
script: string; // for the ES Query
scriptDefinition: ScriptDefinition; // for the UI
}
Okay, this seems similar to the approach of serialize, deserialize and getScript. You just combined
serialize
andgetScript
into a single function, and are callingdeserialize
getScriptFromDefinition
and it returns the script instead of the object (which I suspect will be a pain to work with, you will probably want the deserialize method to return the runtime field object).Your approach does avoid two sources of truth, so I'm okay with it, though I prefer the serialize/deserialize and getScript functions better because it separates out concerns. Sometimes you need the script definition for storage (step 2-
serialize(): ScriptDefinition
) and sometimes you need the script to send with the query (step 1 -getScript()
). I don't see a reason to merge these into one function.Step 1 and 2:
Step 3: