Skip to content

Commit

Permalink
feat: add customizer method
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-gilliotte committed Apr 19, 2023
1 parent 5c24abc commit 67d11cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
14 changes: 14 additions & 0 deletions packages/datasource-customizer/src/collection-customizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {

import DataSourceCustomizer from './datasource-customizer';
import { ActionDefinition } from './decorators/actions/types/actions';
import { BinaryMode } from './decorators/binary/types';
import { CollectionChartDefinition } from './decorators/chart/types';
import { ComputedDefinition } from './decorators/computed/types';
import DecoratorsStack from './decorators/decorators-stack';
Expand Down Expand Up @@ -493,6 +494,19 @@ export default class CollectionCustomizer<
});
}

/**
* Choose how binary data should be converted to the transport layer.
* @param name the name of the field
* @param binaryMode either 'datauri' or 'hex'
* @example
* .replaceFieldBinaryMode('avatar', 'datauri');
*/
replaceFieldBinaryMode(name: TColumnName<S, N>, binaryMode: BinaryMode): this {
return this.pushCustomization(async () => {
this.stack.binary.getCollection(this.name).setBinaryMode(name, binaryMode);
});
}

private pushRelation(name: string, definition: RelationDefinition): this {
return this.pushCustomization(async () => {
this.stack.relation.getCollection(this.name).addRelation(name, definition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '@forestadmin/datasource-toolkit';
import FileType from 'file-type';

import { ConvertionType } from './types';
import { BinaryMode } from './types';
import CollectionDecorator from '../collection-decorator';
import DataSourceDecorator from '../datasource-decorator';

Expand All @@ -27,9 +27,9 @@ type Direction = 'toFrontend' | 'toBackend';
export default class BinaryCollectionDecorator extends CollectionDecorator {
override dataSource: DataSourceDecorator<BinaryCollectionDecorator>;

convertionTypes: Map<string, ConvertionType> = new Map();
convertionTypes: Map<string, BinaryMode> = new Map();

setConvertionType(name: string, type: ConvertionType): void {
setBinaryMode(name: string, type: BinaryMode): void {
this.convertionTypes.set(name, type);
this.markSchemaAsDirty();
}
Expand Down Expand Up @@ -158,7 +158,7 @@ export default class BinaryCollectionDecorator extends CollectionDecorator {
private async convertValueHelper(
direction: Direction,
columnType: ColumnType,
conversionType: ConvertionType,
conversionType: BinaryMode,
value: unknown,
): Promise<unknown> {
if (value) {
Expand Down Expand Up @@ -191,7 +191,7 @@ export default class BinaryCollectionDecorator extends CollectionDecorator {

private async convertScalar(
direction: Direction,
convertionType: ConvertionType,
convertionType: BinaryMode,
value: unknown,
): Promise<unknown> {
if (direction === 'toFrontend') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type ConvertionType = 'hex' | 'datauri';
export type BinaryMode = 'hex' | 'datauri';

0 comments on commit 67d11cc

Please sign in to comment.