Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/reduce spread reduce #316

Merged
merged 4 commits into from
Dec 22, 2021
Merged
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
33 changes: 16 additions & 17 deletions src/processors/knex-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,12 @@ const parseOperationIncludedRelationships = (

const nestedRelationships = includes
.filter((include) => include.length > 1)
.reduce(
(acumRelationships, [nestedOrigin, nestedRelationshipName]) => ({
...acumRelationships,
[nestedOrigin]: {
[nestedRelationshipName]: relationships[nestedOrigin].type().schema.relationships[nestedRelationshipName],
},
}),
{},
);
.reduce((acumRelationships, [nestedOrigin, nestedRelationshipName]) => {
acumRelationships[nestedOrigin] = {
[nestedRelationshipName]: relationships[nestedOrigin].type().schema.relationships[nestedRelationshipName],
};
return acumRelationships;
}, {});

return { relationships, nestedRelationships };
};
Expand Down Expand Up @@ -197,11 +194,12 @@ export default class KnexProcessor<ResourceT extends Resource> extends Operation
const primaryKey = this.resourceClass.schema.primaryKeyName || DEFAULT_PRIMARY_KEY;
const filters = params ? { [primaryKey]: id, ...(params.filter || {}) } : { [primaryKey]: id };

const dataToUpdate = Object.keys(data.attributes)
.map((attribute) => ({
const dataToUpdate = Object.assign(
{},
...Object.keys(data.attributes).map((attribute) => ({
[this.appInstance.app.serializer.attributeToColumn(attribute)]: data.attributes[attribute],
}))
.reduce((keyValues, keyValue) => ({ ...keyValues, ...keyValue }), {});
})),
);

const updated = await this.getQuery()
.where((queryBuilder) => this.filtersToKnex(queryBuilder, filters))
Expand All @@ -222,11 +220,12 @@ export default class KnexProcessor<ResourceT extends Resource> extends Operation
const primaryKeyName = this.resourceClass.schema.primaryKeyName || DEFAULT_PRIMARY_KEY;
const data = op.data as Resource;

const dataToInsert = Object.keys(data.attributes)
.map((attribute) => ({
const dataToInsert = Object.assign(
{},
...Object.keys(data.attributes).map((attribute) => ({
[this.appInstance.app.serializer.attributeToColumn(attribute)]: data.attributes[attribute],
}))
.reduce((keyValues, keyValue) => ({ ...keyValues, ...keyValue }), {});
})),
);

if (data.id) {
dataToInsert[primaryKeyName] = data.id;
Expand Down
2 changes: 1 addition & 1 deletion src/processors/operation-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class OperationProcessor<ResourceT extends Resource> {
}
const value = directRelations[includedRelationResource];
const computed = await resourceProcessor.getComputedProperties(op, relationResourceClass, value, {});
directRelations[includedRelationResource] = { ...value, ...computed };
Object.assign(directRelations[includedRelationResource], computed);
}
return directRelations;
}
Expand Down
10 changes: 6 additions & 4 deletions src/processors/session-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ export default class SessionProcessor<T extends Session> extends KnexProcessor<T
}

public async add(op: Operation): Promise<HasId> {
const fields = Object.keys(op.data?.attributes as { [key: string]: Function })
.filter((attribute) => this.resourceClass.schema.attributes[attribute] !== Password)
.map((attribute) => ({ [attribute]: op.data?.attributes[attribute] }))
.reduce((attributes, attribute) => ({ ...attributes, ...attribute }), {});
const fields = Object.assign(
{},
...Object.keys(op.data?.attributes as { [key: string]: Function })
.filter((attribute) => this.resourceClass.schema.attributes[attribute] !== Password)
.map((attribute) => ({ [attribute]: op.data?.attributes[attribute] })),
);

if (Object.keys(fields).length === 0) {
throw jsonApiErrors.InvalidData();
Expand Down
12 changes: 7 additions & 5 deletions src/processors/user-processor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Authorize from "../decorators/authorize";
import { Operation, HasId, DEFAULT_PRIMARY_KEY } from "../types";
import { Operation, HasId } from "../types";
import User from "../resources/user";
import KnexProcessor from "./knex-processor";
import Password from "../attribute-types/password";
Expand All @@ -18,10 +18,12 @@ export default class UserProcessor<T extends User> extends KnexProcessor<T> {
}

async add(op: Operation): Promise<HasId> {
const fields = Object.keys(op.data?.attributes as { [key: string]: Function })
.filter((attribute) => this.resourceClass.schema.attributes[attribute] !== Password)
.map((attribute) => ({ [attribute]: op.data?.attributes[attribute] }))
.reduce((attributes, attribute) => ({ ...attributes, ...attribute }), {});
const fields = Object.assign(
{},
...Object.keys(op.data?.attributes as { [key: string]: Function })
.filter((attribute) => this.resourceClass.schema.attributes[attribute] !== Password)
.map((attribute) => ({ [attribute]: op.data?.attributes[attribute] })),
);

const id = await this.generateId();

Expand Down
56 changes: 26 additions & 30 deletions src/serializers/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ export default class JsonApiSerializer implements IJsonApiSerializer {
.reduce((relationAttributes, relName) => {
const key = schemaRelationships[relName].foreignKeyName || this.relationshipToColumn(relName, primaryKey);
const value = (<ResourceRelationshipData>op.data?.relationships[relName].data).id;

return {
...relationAttributes,
[key]: value,
};
relationAttributes[key] = value;
return relationAttributes;
}, op.data.attributes);
return op;
}
Expand All @@ -77,23 +74,22 @@ export default class JsonApiSerializer implements IJsonApiSerializer {
this.relationshipToColumn(relName, schemaRelationships[relName].type().schema.primaryKeyName),
}));

data.relationships = relationshipsFound.reduce(
(relationships, relationship) => ({
...relationships,
[relationship.name]: {
id: data.attributes[relationship.key],
type: schemaRelationships[relationship.name].type().type,
},
}),
Object.entries(data.relationships as EagerLoadedData).reduce(
(includedDirectRelationships, [relName, relData]: [string, ResourceRelationshipDescriptor]) => ({
...includedDirectRelationships,
[relName]: relData.direct,
}),
{},
),
const eagerlyLoadedRelationships = Object.entries(data.relationships as EagerLoadedData).reduce(
(includedDirectRelationships, [relName, relData]: [string, ResourceRelationshipDescriptor]) => {
includedDirectRelationships[relName] = relData.direct;
return includedDirectRelationships;
},
{},
);

data.relationships = relationshipsFound.reduce((relationships, relationship) => {
relationships[relationship.name] = {
id: data.attributes[relationship.key],
type: schemaRelationships[relationship.name].type().type,
};
return relationships;
}, eagerlyLoadedRelationships);

Object.keys(data.relationships)
.filter((relName) => data.relationships[relName] && schemaRelationships[relName])
.forEach((relName) => {
Expand Down Expand Up @@ -122,11 +118,12 @@ export default class JsonApiSerializer implements IJsonApiSerializer {
),
);

data.attributes = Object.keys(data.attributes)
.map((attribute) => ({
data.attributes = Object.assign(
{},
...Object.keys(data.attributes).map((attribute) => ({
[this.columnToAttribute(attribute)]: data.attributes[attribute],
}))
.reduce((keyValues, keyValue) => ({ ...keyValues, ...keyValue }), {});
})),
);

return data;
}
Expand Down Expand Up @@ -167,7 +164,7 @@ export default class JsonApiSerializer implements IJsonApiSerializer {
}

const schemaRelationships = resourceType.schema.relationships;
const includedData: (Resource | undefined)[] = [];
let includedData: (Resource | undefined)[] = [];

Object.keys(data.relationships)
.filter((relationshipName) => data.relationships[relationshipName])
Expand All @@ -180,9 +177,8 @@ export default class JsonApiSerializer implements IJsonApiSerializer {
const { direct: directResources = [], nested: nestedResources = [] } = resources;
const relatedResourceClass = schemaRelationships[relationshipName].type();
const pkName = relatedResourceClass.schema.primaryKeyName || DEFAULT_PRIMARY_KEY;

includedData.push(
...directResources.map((resource) => {
includedData = includedData.concat(
directResources.map((resource) => {
if (resource[pkName]) {
return {
...this.serializeResource(
Expand All @@ -205,8 +201,8 @@ export default class JsonApiSerializer implements IJsonApiSerializer {
);

if (nestedResources) {
includedData.push(
...Object.entries(nestedResources).map(([subRelationName, nestedRelationData]) => {
includedData = includedData.concat(
Object.entries(nestedResources).map(([subRelationName, nestedRelationData]) => {
const subResourceClass = relatedResourceClass.schema.relationships[subRelationName].type();
const subPkName = subResourceClass.schema.primaryKeyName || DEFAULT_PRIMARY_KEY;
return nestedRelationData.map((resource: Resource) => {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/pick.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const pick = <R = Record<string, unknown>, T = Record<string, unknown>>(object: R, list: string[] = []): T => {
return list.reduce((acc, key) => {
const hasProperty = key in object;
return hasProperty ? { ...acc, [key]: object[key] } : acc;
if (!hasProperty) return acc;
acc[key] = object[key];
return acc;
}, {}) as T;
};

Expand Down