Skip to content

Commit

Permalink
fix: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alban bertolini committed Oct 2, 2024
1 parent d66b91e commit 4eec229
Show file tree
Hide file tree
Showing 19 changed files with 217 additions and 64 deletions.
4 changes: 0 additions & 4 deletions packages/agent/src/routes/access/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,6 @@ export default class ChartRoute extends CollectionRoute {
value: row.value as number,
}));
}

throw new ValidationError(
`Failed to generate leaderboard chart: parameters do not match pre-requisites`,
);
}

private async computeValue(context: Context, filter: Filter): Promise<number> {
Expand Down
12 changes: 6 additions & 6 deletions packages/agent/src/utils/forest-schema/generator-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ export default class SchemaGeneratorFields {

if (relation.type === 'OneToMany') {
targetName = relation.originKeyTarget;
targetField = SchemaUtils.getColumn(
foreignCollection.schema,
targetName,
foreignCollection.name,
);
targetField = SchemaUtils.getColumn(collection.schema, targetName, collection.name);

const originKey = SchemaUtils.getColumn(
foreignCollection.schema,
Expand Down Expand Up @@ -178,7 +174,11 @@ export default class SchemaGeneratorFields {
relation.originKeyTarget,
collection.name,
);
const keyField = SchemaUtils.getColumn(collection.schema, relation.originKey, collection.name);
const keyField = SchemaUtils.getColumn(
foreignCollection.schema,
relation.originKey,
foreignCollection.name,
);

return {
...baseSchema,
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/src/utils/query-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class QueryStringParser {

return new Projection(...explicitRequest);
} catch (e) {
throw new ValidationError(`Invalid projection: ${e.message}}`);
throw new ValidationError(`Invalid projection: ${e.message}`);
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/agent/test/routes/access/chart.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ConditionTreeFactory, ValidationError } from '@forestadmin/datasource-toolkit';
import {
ConditionTreeFactory,
MissingRelationError,
ValidationError,
} from '@forestadmin/datasource-toolkit';
import { createMockContext } from '@shopify/jest-koa-mocks';

import Chart from '../../../src/routes/access/chart';
Expand Down Expand Up @@ -913,11 +917,7 @@ describe('ChartRoute', () => {
...defaultContext,
});

await expect(chart.handleChart(context)).rejects.toThrow(
new ValidationError(
'Failed to generate leaderboard chart: parameters do not match pre-requisites',
),
);
await expect(chart.handleChart(context)).rejects.toThrow(MissingRelationError);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/test/utils/query-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('QueryStringParser', () => {
const fn = () => QueryStringParser.parseProjection(collectionSimple, context);

expect(fn).toThrow(
"Invalid projection: field not found 'books.field-that-do-not-exist'",
"Invalid projection: The 'books.field-that-do-not-exist' field was not found. Available fields are: [id,name]. Please check if the field name is correct.",
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export default class BinaryCollectionDecorator extends CollectionDecorator {
private useHexConversion: Map<string, boolean> = new Map();

setBinaryMode(name: string, type: BinaryMode): void {
const field = SchemaUtils.getField(this.childCollection.schema, this.childCollection.name);
const field = SchemaUtils.getField(
this.childCollection.schema,
name,
this.childCollection.name,
);

if (type !== 'datauri' && type !== 'hex') {
throw new Error('Invalid binary mode');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export default class OverrideCollectionDecorator extends CollectionDecorator {
records.forEach(result => {
let hasPrimaryKey = false;
Object.keys(result).forEach(key => {
const field = SchemaUtils.getField(this.schema, key, this.name);
// don't use SchemaUtils.getField util because if the field does not exist, we remove it.
const field = this.schema.fields[key];

if (!field || field.type !== 'Column') {
delete result[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ export default class WriteReplacerCollectionDecorator extends CollectionDecorato

return { [key]: await relation.rewritePatch(caller, action, record[key]) };
}

throw new Error(`Unknown field: "${key}"`);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/datasource-customizer/test/context/wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ConditionTreeLeaf,
DataSource,
Filter,
MissingFieldError,
Page,
PaginatedFilter,
Projection,
Expand Down Expand Up @@ -168,7 +169,7 @@ describe('RelaxedWrappers', () => {
test('should validate patch operation before forwarding update', async () => {
await expect(() =>
relaxed.update({ segment: 'some_segment' }, { nonexistingField: 'newValue' }),
).toThrow('Unknown field "nonexistingField"');
).toThrow(MissingFieldError);

expect(collection.update).not.toHaveBeenCalled();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('OverrideCollectionDecorator', () => {

describe('when the handler does not return a primary key in each record', () => {
it('should throw an error', async () => {
const handler = jest.fn().mockResolvedValue([{ id: 'valid' }, { noId: 1 }]);
const handler = jest.fn().mockResolvedValue([{ id: 'valid' }, { description: 1 }]);

const currentCaller = factories.caller.build();
const currentData = [factories.recordData.build()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
Filter,
ManyToManySchema,
MissingColumnError,
MissingFieldError,
PaginatedFilter,
Projection,
Sort,
Expand Down Expand Up @@ -284,7 +283,7 @@ describe('RelationCollectionDecorator', () => {
foreignCollection: 'persons',
foreignKey: '__nonExisting__',
}),
).toThrow(MissingFieldError);
).toThrow(MissingColumnError);
});
});

Expand Down Expand Up @@ -352,7 +351,7 @@ describe('RelationCollectionDecorator', () => {
originKey: '__nonExisting__',
throughCollection: 'passports',
} as ManyToManySchema),
).toThrow(MissingFieldError);
).toThrow(MissingColumnError);
});

test('should throw with a non existent fk', () => {
Expand All @@ -364,7 +363,7 @@ describe('RelationCollectionDecorator', () => {
originKey: 'ownerId',
throughCollection: 'passports',
} as ManyToManySchema),
).toThrow(MissingFieldError);
).toThrow(MissingColumnError);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection } from '@forestadmin/datasource-toolkit';
import { Collection, MissingFieldError } from '@forestadmin/datasource-toolkit';
import * as factories from '@forestadmin/datasource-toolkit/dist/test/__factories__';

import WriteDataSourceDecorator from '../../../../src/decorators/write/datasource';
Expand Down Expand Up @@ -160,17 +160,13 @@ describe('WriteDecorator > Create with no relations', () => {
test('when the handler returns non existent fields', async () => {
decorator.replaceFieldWriting('age', () => ({ author: 'Asimov' }));

await expect(decorator.create(caller, [{ age: '10' }])).rejects.toThrow(
'Unknown field: "author"',
);
await expect(decorator.create(caller, [{ age: '10' }])).rejects.toThrow(MissingFieldError);
});

test('when the handler returns non existent relations', async () => {
decorator.replaceFieldWriting('age', () => ({ author: { lastname: 'Asimov' } }));

await expect(decorator.create(caller, [{ age: '10' }])).rejects.toThrow(
'Unknown field: "author"',
);
await expect(decorator.create(caller, [{ age: '10' }])).rejects.toThrow(MissingFieldError);
});

test('if the customer attemps to update the patch in the handler', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection } from '@forestadmin/datasource-toolkit';
import { Collection, MissingFieldError } from '@forestadmin/datasource-toolkit';
import * as factories from '@forestadmin/datasource-toolkit/dist/test/__factories__';

import WriteReplacerCollectionDecorator from '../../../../src/decorators/write/write-replace/collection';
Expand Down Expand Up @@ -155,15 +155,15 @@ describe('WriteDecorator > Simple structure', () => {
decorator.replaceFieldWriting('age', () => ({ author: 'Asimov' }));

await expect(decorator.update(caller, filter, { age: '10' })).rejects.toThrow(
'Unknown field: "author"',
MissingFieldError,
);
});

test('should throw when the handler returns non existent relations', async () => {
decorator.replaceFieldWriting('age', () => ({ author: { lastname: 'Asimov' } }));

await expect(decorator.update(caller, filter, { age: '10' })).rejects.toThrow(
'Unknown field: "author"',
MissingFieldError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ export default class ProjectionFactory {
}

static columns(collection: Collection): Projection {
return new Projection(
...Object.keys(collection.schema.fields).filter(
f => SchemaUtils.getColumn(collection.schema, f, collection.name).type === 'Column',
),
);
return new Projection(...SchemaUtils.getColumnNames(collection.schema));
}
}
12 changes: 9 additions & 3 deletions packages/datasource-toolkit/src/utils/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default class SchemaUtils {
fieldName: string,
collectionName?: string,
): void {
SchemaUtils.throwIfAccessingFieldFromRelation(fieldName, collectionName);

if (schema.fields[fieldName]) {
throw new AlreadyDefinedFieldError({ fieldName, collectionName });
}
Expand Down Expand Up @@ -58,9 +60,7 @@ export default class SchemaUtils {
): ColumnSchema {
SchemaUtils.throwIfAccessingFieldFromRelation(fieldName, collectionName);

const columns = Object.keys(schema.fields).filter(
name => schema.fields[name].type === 'Column',
);
const columns = SchemaUtils.getColumnNames(schema);

if (!columns.find(name => name === fieldName)) {
throw new MissingColumnError({ fieldName, availableFields: columns, collectionName });
Expand Down Expand Up @@ -128,6 +128,12 @@ export default class SchemaUtils {
return relationFieldSchema as ManyToManySchema | OneToManySchema;
}

static getColumnNames(schema: CollectionSchema): string[] {
return Object.keys(schema.fields).filter(
name => SchemaUtils.getField(schema, name).type === 'Column',
);
}

private static throwIfAccessingFieldFromRelation(
fieldName: string,
collectionName?: string,
Expand Down
13 changes: 3 additions & 10 deletions packages/datasource-toolkit/src/validation/field.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MAP_ALLOWED_TYPES_FOR_COLUMN_TYPE } from './rules';
import TypeGetter from './type-getter';
import { MissingFieldError, MissingRelationError, ValidationError } from '../errors';
import { MissingRelationError, ValidationError } from '../errors';
import { SchemaUtils } from '../index';
import { Collection } from '../interfaces/collection';
import { ColumnSchema, PrimitiveTypes } from '../interfaces/schema';

Expand All @@ -9,15 +10,7 @@ export default class FieldValidator {
const dotIndex = field.indexOf(':');

if (dotIndex === -1) {
const schema = collection.schema.fields[field];

if (!schema) {
throw new MissingFieldError({
collectionName: collection.name,
fieldName: field,
availableFields: Object.keys(collection.schema.fields),
});
}
const schema = SchemaUtils.getField(collection.schema, field, collection.name);

if (schema.type !== 'Column') {
throw new ValidationError(
Expand Down
Loading

0 comments on commit 4eec229

Please sign in to comment.