To improve storage efficiency in databases or files, you can use shared structures. This allows multiple objects with common structures to reference a single shared definition.
To use this, provide a structures array to the Packr constructor. If you are persisting data, you must implement getStructures and saveStructures to load and save the shared structure definitions. msgpackr automatically adds new structures (up to a limit) in an incremental, compatible way.
import { Packr, unpack, pack } from 'msgpackr';
let packr = new Packr({
getStructures() {
// Load existing structures from a file, DB, or KV store
return unpack(readFileSync('my-shared-structures.mp')) || [];
},
saveStructures(structures) {
// Persist the updated structures array
writeFileSync('my-shared-structures.mp', pack(structures));
}
});