Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #18 from GeorgeHulpoi/refactor/deps-collection
Browse files Browse the repository at this point in the history
refactor: getDependenciesOfCollection broken into methods
  • Loading branch information
GeorgeHulpoi authored Nov 13, 2023
2 parents 77841e9 + b64b1f2 commit 3c9b0f8
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "payload-dependency-graph",
"version": "1.2.0",
"version": "1.3.0",
"repository": "[email protected]:GeorgeHulpoi/payload-dependency-graph.git",
"description": "This plugin creates a dependency graph between collections and globals. The graph updates automatically, because the plugin observes the changes made on any collection or globals.",
"bugs": {
Expand Down
20 changes: 20 additions & 0 deletions src/dependency-graph/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ export abstract class DependencyGraphBase {
collection: string,
): DependencyGraphResource[] | Promise<DependencyGraphResource[]>;

/**
* Get dependencies from `resource` that is of `collection`. This function traverses from bottom to up, using `dependecyFor`.
* @param resource
* @param collection
*/
abstract getDependenciesForCollection(
resource: DependencyGraphResource,
collection: string,
): DependencyGraphResource[] | Promise<DependencyGraphResource[]>;

/**
* Get dependencies from `resource` that is of `collection`. This function traverses from up to bottom, using `dependentOn`.
* @param resource
* @param collection
*/
abstract getDependsOnCollection(
resource: DependencyGraphResource,
collection: string,
): DependencyGraphResource[] | Promise<DependencyGraphResource[]>;

/**
* Used at Payload initialization to populate the dependency graph.
* You shouldn't call this function by yourself.
Expand Down
76 changes: 47 additions & 29 deletions src/dependency-graph/in-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,37 @@ export class InMemoryDependencyGraph extends DependencyGraphBase {
return false;
}

getDependenciesOfCollection(
getDependenciesForCollection(
resource: DependencyGraphResource,
collection: string,
): DependencyGraphResource[] {
const resources = this.getDependenciesOfCollectionRecursive(resource, collection);
if (resource.collection === collection) {
return [resource];
}

const node = this.getDependencyGraphNode(resource);

if (node === undefined) {
return [];
}

const resources = [
...node.dependencyFor.reduce(
(result: DependencyGraphResource[], r: DependencyGraphResource) => [
...result,
...this.getDependenciesForCollection(r, collection),
],
[],
),
];

const set = new DependencyGraphResourceSet(resources);
return Array.from(set);
}

private getDependenciesOfCollectionRecursive(
getDependsOnCollection(
resource: DependencyGraphResource,
collection: string,
direction?: boolean,
): DependencyGraphResource[] {
if (resource.collection === collection) {
return [resource];
Expand All @@ -245,32 +263,32 @@ export class InMemoryDependencyGraph extends DependencyGraphBase {
return [];
}

const items: DependencyGraphResource[] = [];

if (direction === false || direction === undefined) {
items.push(
...node.dependencyFor.reduce(
(result: DependencyGraphResource[], r: DependencyGraphResource) => [
...result,
...this.getDependenciesOfCollectionRecursive(r, collection, false),
],
[],
),
);
}
const resources = [
...node.dependentOn.reduce(
(result: DependencyGraphResource[], r: DependencyGraphResource) => [
...result,
...this.getDependsOnCollection(r, collection),
],
[],
),
];

if (direction === true || direction === undefined) {
items.push(
...node.dependentOn.reduce(
(result: DependencyGraphResource[], r: DependencyGraphResource) => [
...result,
...this.getDependenciesOfCollectionRecursive(r, collection, true),
],
[],
),
);
}
const set = new DependencyGraphResourceSet(resources);
return Array.from(set);
}

return items;
getDependenciesOfCollection(
resource: DependencyGraphResource,
collection: string,
): DependencyGraphResource[] {
const dependencyFor = this.getDependenciesForCollection(resource, collection);
const dependsOn = this.getDependsOnCollection(resource, collection);

const set = new DependencyGraphResourceSet();

dependencyFor.forEach((r) => set.add(r));
dependsOn.forEach((r) => set.add(r));

return Array.from(set);
}
}
13 changes: 13 additions & 0 deletions tests/dependencies-graph/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ class DummyDependencyGraph extends DependencyGraphBase {
): DependencyGraphResource[] {
return [];
}

getDependenciesForCollection(
resource: DependencyGraphResource,
collection: string,
): DependencyGraphResource[] {
return [];
}
getDependsOnCollection(
resource: DependencyGraphResource,
collection: string,
): DependencyGraphResource[] {
return [];
}
}

describe('DependencyGraphBase', () => {
Expand Down
49 changes: 49 additions & 0 deletions tests/dependencies-graph/in-memory/in-memory-e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,55 @@ describe('InMemoryDependencyGraph e2e', () => {
});
});

it('getDependenciesForCollection should return right resources', () => {
const deps = graph.getDependenciesForCollection(
{
id: 'dog_charlie',
collection: 'dogs',
},
'pages',
);

expect(deps).toContainEqual({
collection: 'pages',
id: 'home',
});

expect(deps).toContainEqual({
collection: 'pages',
id: 'dogs_page',
});

expect(deps).toHaveLength(2);
});

it('getDependsOnCollection should return right resources', () => {
const deps = graph.getDependsOnCollection(
{
id: 'dog_charlie',
collection: 'dogs',
},
'toys',
);

expect(deps).toContainEqual({
collection: 'toys',
id: 'feathers_wand',
});

expect(deps).toContainEqual({
collection: 'toys',
id: 'dog_bone',
});

expect(deps).toContainEqual({
collection: 'toys',
id: 'rubber_ball',
});

expect(deps).toHaveLength(3);
});

it('getDependenciesOfCollection should return right resources', () => {
const deps = graph.getDependenciesOfCollection(
{
Expand Down

0 comments on commit 3c9b0f8

Please sign in to comment.