jriecken / dependency-graph

A simple dependency graph for Node.js
http://jriecken.github.io/dependency-graph/
MIT License
330 stars 49 forks source link

Graph serialization and deserialization #49

Open huypham50 opened 6 months ago

huypham50 commented 6 months ago

Are there any suggestions on how to implement these two helpers?

const graph = new DepGraph() // adding nodes a, b, c, d, etc

const serializedGraph: Record<string, unknown> = serialize(graph) // unknown could be a specific type

const insertedRow = database.insert({ serializedGraph }) // putting serialized graph into a database using something like prisma or sequelize

const const deserializedGraph: DepGraph<T> = deserialize(insertedRow.serializedGraph) // T could be a specific type as well

Great library!

jriecken commented 5 months ago

I've been hesitant to add any sort of official serialization/deserialization support to this library as it's possible to add data to the graph that is not serializable.

See this comment chain for more information https://github.com/jriecken/dependency-graph/commit/c9238dceba22188f1c74d025af37fea20744bc03#commitcomment-137327165

Ah, this wasn't an intentional change (although there's never really been a promise that the graph is JSON-serializable)

The only thing I worry about adding an official toJSON() / fromJSON(string) is that it's possible for someone who is using this library to add a node that has data attached that is not JSON.stringifyable (e.g. as you've shown a Map, or a Function, etc) and it's not clear what the output should be (fromJSON(graph.toJSON()) would not return the same thing)

Someone else had luck using this lib to serialize the graph - https://github.com/yahoo/serialize-javascript