dependency-graph
dependency-graph copied to clipboard
Graph serialization and deserialization
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!
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 notJSON.stringify
able (e.g. as you've shown aMap
, or aFunction
, 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