import type DataSourceCustomizer from './datasource-customizer'; import type { ActionDefinition } from './decorators/actions/types/actions'; import type { BinaryMode } from './decorators/binary/types'; import type { CollectionChartDefinition } from './decorators/chart/types'; import type { ComputedDefinition } from './decorators/computed/types'; import type DecoratorsStackBase from './decorators/decorators-stack-base'; import type { HookHandler, HookPosition, HookType, HooksContext } from './decorators/hook/types'; import type { OperatorDefinition } from './decorators/operators-emulate/types'; import type { CreateOverrideHandler, DeleteOverrideHandler, UpdateOverrideHandler } from './decorators/override/types'; import type { SearchReplaceDefinition } from './decorators/search/types'; import type { SegmentDefinition } from './decorators/segment/types'; import type { WriteDefinition } from './decorators/write/write-replace/types'; import type { TCollectionName, TColumnName, TColumnNameAndRelationName, TFieldName, TSchema, TSortClause } from './templates'; import type { OneToManyEmbeddedDefinition, Plugin } from './types'; import type { CollectionSchema, Operator } from '@forestadmin/datasource-toolkit'; export default class CollectionCustomizer = TCollectionName> { private readonly dataSourceCustomizer; private readonly stack; readonly name: string; get schema(): CollectionSchema; constructor(dataSourceCustomizer: DataSourceCustomizer, stack: DecoratorsStackBase, name: string); /** * Load a plugin on the collection. * @param plugin reference to the plugin function * @param options options to pass to the plugin * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/plugins Documentation Link} * @example * import { createFileField } from '@forestadmin/plugin-s3'; * * collection.use(createFileField, { fieldname: 'avatar' }), */ use(plugin: Plugin, options?: Options): this; /** * Disable count in list view pagination for improved performance. * * @example * .disableCount() */ disableCount(): this; /** * Disable search on the collection * * * @example * .disableSearch() */ disableSearch(): this; /** * Import a field from a many to one or one to one relation. * * @param name the name of the field that will be created on the collection * @param options options to import the field * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/import-rename-remove#moving-fields Documentation Link} * * @example * .importField('authorName', { path: 'author:fullName' }) */ importField(name: string, options: { path: TFieldName; readonly?: boolean; }): this; /** * Rename fields from the exported schema. * @param currentName the current name of the field or the relation in a given collection * @param newName the new name of the field or the relation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/import-rename-remove#renaming-and-removing-fields Documentation Link} * @example * .renameField('currentFieldOrRelationName', 'newFieldOrRelationName') */ renameField(currentName: TColumnNameAndRelationName, newName: string): this; /** * Remove fields from the exported schema (they will still be usable within the agent). * @param names the names of the field or the relation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/import-rename-remove#renaming-and-removing-fields Documentation Link} * @example * .removeField('fieldNameToRemove', 'relationNameToRemove'); */ removeField(...names: TColumnNameAndRelationName[]): this; /** * Add a new action on the collection. * @param name the name of the action * @param definition the definition of the action * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/actions Documentation Link} * @example * .addAction('is live', { * scope: 'Single', * execute: async (context, resultBuilder) => { * return resultBuilder.success('Is live!'); * }, * }) */ addAction(name: string, definition: ActionDefinition): this; /** * Create a new API chart * @param name name of the chart * @param definition definition of the chart * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/charts Documentation Link} * @example * .addChart('numCustomers', (context, resultBuilder) => { * return resultBuilder.distribution({ * tomatoes: 10, * potatoes: 20, * carrots: 30, * }); * }) */ addChart(name: string, definition: CollectionChartDefinition): this; /** * Add a new field on the collection. * @param name the name of the field * @param definition The definition of the field * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/computed Documentation Link} * @example * .addField('fullName', { * columnType: 'String', * dependencies: ['firstName', 'lastName'], * getValues: (records) => records.map(record => \`${record.lastName} ${record.firstName}\`), * }); */ addField: { (name: string, definition: ComputedDefinition): CollectionCustomizer; }; /** * Add a new validator to the edition form of a given field * @param name The name of the field * @param operator The validator that you wish to add * @param value A configuration value that the validator may need * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/validation Documentation Link} * @example * .addFieldValidation('firstName', 'LongerThan', 2); */ addFieldValidation(name: TColumnName, operator: Operator, value?: unknown): this; /** * Add a new hook handler to an action * @param position Either if the hook is executed before or after the action * @param type Type of action which should be hooked * @param handler Callback that should be executed when the hook is triggered * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/hooks Documentation Link} * @example * .addHook('Before', 'List', async (context) => { * // Do something before the list action * }); */ addHook

(position: P, type: T, handler: HookHandler[P][T]>): this; /** * Like {@link addHook} but also fires for writes initiated from inside a smart action * (`context.collection.update`/`create`/`delete`). Used by the audit-trail plugin. */ addInternalHook

(position: P, type: T, handler: HookHandler[P][T]>): this; /** * Add a many to one relation to the collection * @param name name of the new relation * @param foreignCollection name of the targeted collection * @param options extra information about the relation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/relationships/single-record#many-to-one-relations Documentation Link} * @example * books.addManyToOneRelation('myAuthor', 'persons', { foreignKey: 'authorId' }) */ addManyToOneRelation>(name: string, foreignCollection: T, options: { foreignKey: TColumnName; foreignKeyTarget?: TColumnName; }): this; /** * Add a one to many relation to the collection * @param name name of the new relation * @param foreignCollection name of the targeted collection * @param options extra information about the relation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/relationships/multiple-records#one-to-many-relations Documentation Link} * @example * persons.addOneToManyRelation('writtenBooks', 'books', { originKey: 'authorId' }) */ addOneToManyRelation>(name: string, foreignCollection: T, options: { originKey: TColumnName; originKeyTarget?: TColumnName; }): this; /** * Add a one to one relation to the collection * @param name name of the new relation * @param foreignCollection name of the targeted collection * @param options extra information about the relation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/relationships/single-record#one-to-one-relations Documentation Link} * @example * persons.addOneToOneRelation('bestFriend', 'persons', { originKey: 'bestFriendId' }) */ addOneToOneRelation>(name: string, foreignCollection: T, options: { originKey: TColumnName; originKeyTarget?: TColumnName; }): this; /** * Add a many to many relation to the collection * @param name name of the new relation * @param foreignCollection name of the targeted collection * @param throughCollection name of the intermediary collection * @param options extra information about the relation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/relationships/multiple-records#many-to-many-relations Documentation Link} * @example * dvds.addManyToManyRelation('rentalsOfThisDvd', 'rentals', 'dvdRentals', { * originKey: 'dvdId', * foreignKey: 'rentalId' * }) */ addManyToManyRelation, Through extends TCollectionName>(name: string, foreignCollection: Foreign, throughCollection: Through, options: { originKey: TColumnName; foreignKey: TColumnName; originKeyTarget?: TColumnName; foreignKeyTarget?: TColumnName; }): this; /** * Add a virtual collection into the related data of a record. * * @param name name of the relation * @param definition the definition of the new relation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/relationships/multiple-records#external-relations Documentation Link} * @example * .addExternalRelation('states', { * schema: { code: 'Number', name: 'String' }, * listRecords: ({ id }) => { * return record.id == 34 ? * [{ code: 'AL', name: 'Alabama' }, { code: 'AK', name: 'Alaska' }] : * [{ code: 'AZ', name: 'Arizona' }, { code: 'TX', name: 'Texas' }]; * } * }) */ addExternalRelation(name: string, definition: OneToManyEmbeddedDefinition): this; /** * Add a new segment on the collection. * @param name the name of the segment * @param definition a function used to generate a condition tree or a condition tree * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/segments Documentation Link} * @example * .addSegment( * 'Wrote more than 2 books', * { field: 'booksCount', operator: 'GreaterThan', value: 2 } * ); */ addSegment(name: string, definition: SegmentDefinition): this; /** * Disable sorting on a specific field. * @param name the name of the field with sorting to be disabled * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/sort#disabling-sort Documentation Link} * @example * .disableFieldSorting('fullName'); */ disableFieldSorting(name: TColumnName): this; /** * Enable sorting on a specific field using emulation. * As for all the emulation method, the field sorting will be done in-memory. * @param name the name of the field to enable emulation on * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/sort#emulation Documentation Link} * @example * .emulateFieldSorting('fullName'); */ emulateFieldSorting(name: TColumnName): this; /** * Replace an implementation for the sorting. * The field sorting will be done by the datasource. * @param name the name of the field to enable sort * @param equivalentSort the sort equivalent * @see @{@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/sort Documentation Link} * @example * .replaceFieldSorting( * 'fullName', * [ * { field: 'firstName', ascending: true }, * { field: 'lastName', ascending: true }, * ] * ) */ replaceFieldSorting(name: TColumnName, equivalentSort: TSortClause[]): this; /** * Enable filtering on a specific field using emulation. * As for all the emulation method, the field filtering will be done in-memory. * @param name the name of the field to enable emulation on * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/filter#emulation Documentation Link} * @example * .emulateFieldFiltering('aField'); */ emulateFieldFiltering(name: TColumnName): this; /** * Enable filtering on a specific field with a specific operator using emulation. * As for all the emulation method, the field filtering will be done in-memory. * @param name the name of the field to enable emulation on * @param operator the operator to emulate * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/filter Documentation Link} * @example * .emulateFieldOperator('aField', 'In'); */ emulateFieldOperator(name: TColumnName, operator: Operator): this; /** * Choose how binary data should be transported to the GUI. * By default, all fields are transported as 'datauri', with the exception of primary and foreign * keys. * * Using 'datauri' allows to use the FilePicker widget, while 'hex' is more suitable for * short binary data (for instance binary uuids). * * @param name the name of the field * @param binaryMode either 'datauri' or 'hex' * @example * .replaceFieldBinaryMode('avatar', 'datauri'); */ replaceFieldBinaryMode(name: TColumnName, binaryMode: BinaryMode): this; /** * Replace an implementation for a specific operator on a specific field. * The operator replacement will be done by the datasource. * @param name the name of the field to filter on * @param operator the operator to replace * @param replacer the proposed implementation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/filter#substitution Documentation Link} * @example * .replaceFieldOperator('fullName', 'Contains', (value) => { * return { * aggregator: 'Or', * conditions: [{ * field: 'firstName', * operator: 'Contains', * value * }, { * field: 'lastName', * operator: 'Contains', * value * }] * } * }); */ replaceFieldOperator>(name: C, operator: Operator, replacer: OperatorDefinition): this; /** * Replace the write behavior of a field. * @param name the name of the field * @param definition the function or a value to represent the write behavior * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/write Documentation Link} * @example * .replaceFieldWriting('fullName', fullName => { * const [firstName, lastName] = fullName.split(' '); * return { firstName, lastName }; * }); */ replaceFieldWriting>(name: C, definition: WriteDefinition): this; /** * Replace the behavior of the search bar, either with a handler or with a field selection. * * A field selection narrows the same default search, so the agent knows which columns are read * and checks them against the caller's read permissions: an extended search keeps working, and a * path the role may not read is refused by name. Prefer it whenever it expresses what you need. * On a collection whose datasource searches natively (`enableSearch()`), it does not narrow that * native search — it replaces it with the agent's own per-column one, restricted to the selection. * * A handler is unrestricted, and pays for it. The fields it reads are exempt from read * permissions on a plain search: the caller supplies the text and the handler picks the fields, * so the agent cannot tell an intended one from one the role may not read. Point a handler at a * column of a collection a role cannot read and that role can test values against it, reading * each answer from whether rows come back. An extended search is refused outright rather than * exempted, because the caller owns that flag and can compare the same term with it off and on. * @param definition a handler describing the new behavior, or the fields the default search reads * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/search Documentation Link} * @example * .replaceSearch({ includeFields: ['project:name'], excludeFields: ['description'] }); * @example * .replaceSearch(async (searchString) => { * return { field: 'name', operator: 'Contains', value: searchString }; * }); */ replaceSearch(definition: SearchReplaceDefinition): this; /** * Replace the default create operation * @param handler the new behavior for the create operation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/hooks/collection-override#custom-create-operation Documentation Link} * @example * .overrideCreate(async (context) => { * const { data } = context; * const record = await createRecord(data); * return [record]; * }); */ overrideCreate(handler: CreateOverrideHandler): this; /** * Replace the default update operation * @param handler the new behavior for the update operation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/hooks/collection-override#custom-update-operation Documentation Link} * @example * .overrideUpdate(async (context) => { * const { filter, patch } = context; * await updateRecord(filter, patch); * }); */ overrideUpdate(handler: UpdateOverrideHandler): this; /** * Replace the default delete operation * @param handler the new behavior for the delete operation * @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/hooks/collection-override#custom-delete-operation Documentation Link} * @example * .overrideDelete(async (context) => { * const { filter } = context; * await deleteRecord(filter); * }); */ overrideDelete(handler: DeleteOverrideHandler): this; /** * Mark a field as optional * * Be wary that your database might still refuse empty values if it requires one * @param name the name of the column you would like optional * @example * .setFieldNullable('userName'); */ setFieldNullable(name: TColumnName): this; private pushRelation; private pushCustomization; } //# sourceMappingURL=collection-customizer.d.ts.map