Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions packages/datasource-customizer/src/collection-customizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ export default class CollectionCustomizer<
*/
addChart(name: string, definition: CollectionChartDefinition<S, N>): this {
return this.pushCustomization(async () => {
this.stack.chart.getCollection(this.name).addChart(name, definition);
this.stack.chart
.getCollection(this.name)
.addChart(name, definition as unknown as CollectionChartDefinition);
});
}

Expand Down Expand Up @@ -228,7 +230,10 @@ export default class CollectionCustomizer<
? collectionBeforeRelations
: collectionAfterRelations;

collection.registerComputed(name, mapDeprecated<S, N>(definition));
collection.registerComputed(
name,
mapDeprecated<S, N>(definition) as unknown as ComputedDefinition,
);
});
};

Expand Down Expand Up @@ -261,7 +266,7 @@ export default class CollectionCustomizer<
addHook<P extends HookPosition, T extends HookType>(
position: P,
type: T,
handler: HookHandler<HooksContext<S, N>[P][T]>,
handler: HookHandler<HooksContext<S, N>[P][T], void, S, N>,
): this {
return this.pushCustomization(async () => {
this.stack.hook
Expand Down Expand Up @@ -554,7 +559,7 @@ export default class CollectionCustomizer<
? this.stack.earlyOpEmulate.getCollection(this.name)
: this.stack.lateOpEmulate.getCollection(this.name);

collection.replaceFieldOperator(name, operator, replacer as OperatorDefinition);
collection.replaceFieldOperator(name, operator, replacer as unknown as OperatorDefinition);
});
}

Expand All @@ -574,7 +579,9 @@ export default class CollectionCustomizer<
definition: WriteDefinition<S, N, C>,
): this {
return this.pushCustomization(async () => {
this.stack.write.getCollection(this.name).replaceFieldWriting(name, definition);
this.stack.write
.getCollection(this.name)
.replaceFieldWriting(name, definition as unknown as WriteDefinition);
});
}

Expand Down Expand Up @@ -609,7 +616,9 @@ export default class CollectionCustomizer<
*/
overrideCreate(handler: CreateOverrideHandler<S, N>): this {
return this.pushCustomization(async () => {
this.stack.override.getCollection(this.name).addCreateHandler(handler);
this.stack.override
.getCollection(this.name)
.addCreateHandler(handler as unknown as CreateOverrideHandler);
});
}

Expand All @@ -625,7 +634,9 @@ export default class CollectionCustomizer<
*/
overrideUpdate(handler: UpdateOverrideHandler<S, N>): this {
return this.pushCustomization(async () => {
this.stack.override.getCollection(this.name).addUpdateHandler(handler);
this.stack.override
.getCollection(this.name)
.addUpdateHandler(handler as unknown as UpdateOverrideHandler);
});
}

Expand All @@ -641,7 +652,9 @@ export default class CollectionCustomizer<
*/
overrideDelete(handler: DeleteOverrideHandler<S, N>): this {
return this.pushCustomization(async () => {
this.stack.override.getCollection(this.name).addDeleteHandler(handler);
this.stack.override
.getCollection(this.name)
.addDeleteHandler(handler as unknown as DeleteOverrideHandler);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class DataSourceCustomizer<S extends TSchema = TSchema> {
*/
addChart(name: string, definition: DataSourceChartDefinition<S>): this {
this.stack.queueCustomization(async () => {
this.stack.chart.addChart(name, definition);
this.stack.chart.addChart(name, definition as unknown as DataSourceChartDefinition);
});

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
SearchOptionsHandler,
ValueOrHandler,
} from './types/fields';
import type { TSchema } from '../../templates';
import type { TFilter, TSchema } from '../../templates';
import type {
ActionFormElement,
ActionResult,
Expand All @@ -18,7 +18,6 @@ import type {
Filter,
GetFormMetas,
LayoutElementPageWithField,
PlainFilter,
RecordData,
} from '@forestadmin/datasource-toolkit';

Expand Down Expand Up @@ -177,7 +176,7 @@ export default class ActionCollectionDecorator extends CollectionDecorator {
Global: ActionContext,
Bulk: ActionContext,
Single: ActionContextSingle,
}[action.scope](this, caller, formValues, filter as unknown as PlainFilter, used, changedField);
}[action.scope](this, caller, formValues, filter as unknown as TFilter, used, changedField);
}

private getSearchedField(element: DynamicFormElementOrPage, search: string): DynamicField | null {
Expand Down
46 changes: 42 additions & 4 deletions packages/datasource-customizer/src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,52 @@ export type TPartialFlatRow<
N extends TCollectionName<S> = TCollectionName<S>,
> = RecursivePartial<S[N]['plain'] & S[N]['flat']>;

/** Operators that require no value */
type NoValueOperator =
| 'Blank'
| 'Present'
| 'Missing'
| 'Today'
| 'Yesterday'
| 'PreviousMonth'
| 'PreviousQuarter'
| 'PreviousWeek'
| 'PreviousYear'
| 'PreviousMonthToDate'
| 'PreviousQuarterToDate'
| 'PreviousWeekToDate'
| 'PreviousYearToDate'
| 'Past'
| 'Future';

/** Operators that require an array of values */
type ArrayValueOperator = 'In' | 'NotIn' | 'IncludesAll' | 'IncludesNone';

/** Operators that always require a number value */
type NumberValueOperator =
| 'PreviousXDaysToDate'
| 'PreviousXDays'
| 'BeforeXHoursAgo'
| 'AfterXHoursAgo'
| 'LongerThan'
| 'ShorterThan';

/** Operators that require a single value matching the field type */
type SingleValueOperator = Exclude<
Operator,
NoValueOperator | ArrayValueOperator | NumberValueOperator
>;

export type TConditionTreeLeaf<
S extends TSchema = TSchema,
N extends TCollectionName<S> = TCollectionName<S>,
> = {
field: TFieldName<S, N>;
operator: Operator;
value?: unknown;
};
[F in TFieldName<S, N>]:
| { field: F; operator: NoValueOperator }
| { field: F; operator: ArrayValueOperator; value: TFieldType<S, N, F>[] }
| { field: F; operator: NumberValueOperator; value: number }
| { field: F; operator: SingleValueOperator; value: TFieldType<S, N, F> };
}[TFieldName<S, N>];

export type TConditionTreeBranch<
S extends TSchema = TSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ActionDefinition } from '../src/decorators/actions/types/actions';
import type { WriteDefinition } from '../src/decorators/write/write-replace/types';
import type { ColumnSchema } from '@forestadmin/datasource-toolkit';

import { ConditionTreeLeaf, MissingFieldError, Sort } from '@forestadmin/datasource-toolkit';
import { MissingFieldError, Sort } from '@forestadmin/datasource-toolkit';
import * as factories from '@forestadmin/datasource-toolkit/dist/test/__factories__';

import { CollectionCustomizer, DataSourceCustomizer } from '../src';
Expand Down Expand Up @@ -639,7 +639,7 @@ describe('Builder > Collection', () => {
it('should add a segment', async () => {
const { dsc, customizer } = await setup();

const generator = async () => new ConditionTreeLeaf('fieldName', 'Present');
const generator = async () => ({ field: 'fieldName', operator: 'Present' } as const);

const self = customizer.addSegment('new segment', generator);
await dsc.getDataSource(logger);
Expand Down Expand Up @@ -773,7 +773,8 @@ describe('Builder > Collection', () => {
it('should replace operator on field', async () => {
const { dsc, customizer } = await setup();

const replacer = async () => new ConditionTreeLeaf('fieldName', 'NotEqual', null);
const replacer = async () =>
({ field: 'fieldName', operator: 'NotEqual', value: null } as const);

const self = customizer.replaceFieldOperator('firstName', 'Present', replacer);
await dsc.getDataSource(logger);
Expand Down
Loading