Page MenuHomeSealhub

field.ts
No OneTemporary

field.ts

import Collection from "./collection";
import Context from "../context";
import { ActionName } from "../action";
import { ChipTypeName } from "../app/chip-manager";
import { App } from "../main";
import QueryStage from "../datastore/query-stage";
import AttachmentLoader from "../subject/attachments/attachment-loader";
import ReferenceToCollection from "../subject/attachments/reference-to-collection";
export type Depromisify<T> = T extends Promise<infer V> ? V : T;
export type ExtractParams<F extends Field> = Parameters<F["setParams"]>[0];
export type ExtractInput<F extends Field> = Parameters<F["encode"]>[1];
export type ExtractOutput<F extends Field> = Depromisify<
ReturnType<F["decode"]>
>;
export type ExtractStorage<F extends Field<any>> = Depromisify<
ReturnType<F["encode"]>
>;
export type ExtractFormatParams<F> = F extends Field<any, infer T> ? T : never;
export type FieldDefinition<ParamsType> = {
name: string;
required?: boolean;
type: string | FieldClass;
params?: ParamsType;
display_hints?: any;
};
export type FieldClass<ParamsType = any> = new (
app: App,
collection: Collection,
name: string,
required: boolean,
display_hints: any
) => Field<ParamsType>;
type ValidationResult = {
valid: boolean;
reason?: string;
};
export default abstract class Field<
InputType = any,
OutputType = InputType,
FormatParams = any
> {
name: string;
app: App;
display_hints: any;
handles_large_data: boolean = false;
type_name: ChipTypeName = "field_type";
collection: Collection;
required: boolean;
constructor(
app: App,
collection: Collection,
name: string,
required: boolean,
display_hints: any
) {
this.app = app;
this.name = name;
this.display_hints = display_hints || {};
this.collection = collection;
this.required = required;
}
static fromDefinition<ParamsType>(
app: App,
collection: Collection,
definition: FieldDefinition<ParamsType>
) {
let type: FieldClass;
if (typeof definition.type === "string") {
type = app.ChipManager.getFieldType(definition.type);
} else {
type = definition.type;
}
return new type(
app,
collection,
definition.name,
definition.required || false,
definition.display_hints
);
}
setParams(_: any): void | Promise<void> {}
getSpecification() {
return {
name: this.name,
type: this.getTypeName(),
display_hints: this.display_hints,
};
}
hasIndex(): boolean | { [field_name: string]: "text" } {
return false;
}
async getAggregationStages(
context: Context,
query_params: { filter?: { [field_name: string]: any } }
): Promise<QueryStage[]> {
if (!query_params || !query_params.filter) return [];
let field_filter = query_params.filter[this.name];
if (
field_filter &&
field_filter.length === 1 &&
field_filter[0] instanceof Array
) {
field_filter = field_filter[0]; // to fix an edge case where instead of array of values the array is wrapped within another array
}
if (!(this.name in query_params.filter)) {
return [];
}
if (this.name in query_params.filter && field_filter === undefined)
return [
{
$match: { [await this.getValuePath()]: { $exists: false } },
} as QueryStage,
];
let new_filter = null;
if (field_filter instanceof Array) {
new_filter = await Promise.all(
field_filter.map((element) => this.encode(context, element))
).then((filters) => {
return { $in: filters };
});
} else {
new_filter = await this.filterToQuery(context, field_filter);
}
return [
{ $match: { [await this.getValuePath()]: new_filter } },
] as QueryStage[];
}
async getValuePath(): Promise<string> {
return this.name;
}
abstract getTypeName(): string;
abstract isProperValue(
context: Context,
new_value: InputType,
old_value: InputType
): Promise<ValidationResult>;
format: never;
filter_to_query: never;
async encode(_: Context, value: InputType, __?: InputType): Promise<any> {
return value as any;
}
async decode(
_: Context,
storage_value: Depromisify<ReturnType<this["encode"]>>,
__: OutputType,
___: FormatParams
): Promise<OutputType> {
return (storage_value as unknown) as OutputType;
}
async filterToQuery(context: Context, filter: any): Promise<any> {
return this.encode(context, filter);
}
async fullTextSearchEnabled(): Promise<boolean> {
return false;
}
hasDefaultValue = () => true;
getDefaultValue(): Depromisify<ReturnType<this["decode"]>> | null {
return null;
}
isOldValueSensitive(_: ActionName) {
return false;
}
static valid(): ValidationResult {
return { valid: true };
}
static invalid(reason: string): ValidationResult {
return { valid: false, reason };
}
init(_: App): Promise<void> | void {}
getAttachmentLoader(
context: Context,
omit_it: Boolean,
attachment_params: ConstructorParameters<
typeof ReferenceToCollection
>[2]
): AttachmentLoader | null {
return null;
}
}
export { default as HybridField } from "./field-hybrid";
export * from "./field-hybrid";

File Metadata

Mime Type
text/x-java
Expires
Tue, Dec 24, 14:05 (12 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557305
Default Alt Text
field.ts (4 KB)

Event Timeline