https://github.com/bsdelf/dag-maker
DAG Maker
https://github.com/bsdelf/dag-maker
dag dependency-injection dependency-resolution directed-acyclic-graph javascript topological-sorting typescript
Last synced: 2 months ago
JSON representation
DAG Maker
- Host: GitHub
- URL: https://github.com/bsdelf/dag-maker
- Owner: bsdelf
- Created: 2020-05-12T03:52:10.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-02T05:54:07.000Z (over 2 years ago)
- Last Synced: 2025-03-13T11:20:04.702Z (3 months ago)
- Topics: dag, dependency-injection, dependency-resolution, directed-acyclic-graph, javascript, topological-sorting, typescript
- Language: TypeScript
- Homepage:
- Size: 163 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DAG Maker
DAG maker utilize [topological sorting](https://en.wikipedia.org/wiki/Topological_sorting) to make directed acyclic graphs.
It can be used to solve following problems:
- Dependency resolution & injection
- Figure out services start & stop order
- Create & destroy objects in reasonable order## Installation
```
npm i dag-maker
```## Usage
Suppose we have two classes A and B. The dependency graph as follows:
```
A <- B
```To instantiate them, first we create an instance of A, then pass the instance to B's constructor. Since there are only two classes, it's quite straightforward.
However, when it comes to a dozen classes with complex dependencies, it's no longer feasible to figure out the proper construction order manually. Not to mention the destruction order.
Here is an example (in TypeScript) presents how to solve this kind of problem with `dag-maker`:
```typescript
import { dependencies, DagMaker } from 'dag-maker';class A {
static async create() {
console.log('create A');
return new A();
}
static async destroy(a: A) {
console.log('destroy A:', a);
}
}@dependencies({
a: A,
})
class B {
constructor(a: A) {
this.a = a;
}
static async create(options: { a: A }) {
console.log('create B');
return new B(options.a);
}
static async destroy(b: B) {
console.log('destroy B:', b);
}
}const dagMaker = new DagMaker(A, B);
console.log(dagMaker.orderBy('dependencies'));
// [ [ 'A' ], [ 'B' ] ]console.log(dagMaker.orderBy('dependents'));
// [ [ 'B' ], [ 'A' ] ]const dag = await dagMaker.create();
// create A
// create Bconsole.log(dag);
// Map(2) { 'A' => A {}, 'B' => B { a: A {} } }await dagMaker.destroy(dag);
// destroy B: B { a: A {} }
// destroy A: A {}
```In this example, we implement factory methods `create()` and `destroy()` right inside class A and B, and declare B's dependencies with a decorator `@dependencies`. If you don't want to use decorator, declare a static variable named `dependencies` could achieve the same semantic. After then, we create a `DagMaker` for class A and B. Eventually, the DAG maker will inspect `dependencies` property and figure out how to construct A and B.