Memory Adapter

The Memory Adapter keeps all of your applications data in-memory, persisting it to permanent storage every 10 seconds. The Memory Adapter has the benefit of requiring no additional server (i.e. no RethinkDB instance) and is incredibly performant.

When using this adapter you can configure how to save and retrieve this data in a persistent file structure. The adapter will automatically persist its data every 10 seconds. Below is an example configuration which persists data to data/database.json on the local filesystem.

const fs = require("fs");

{
  name: "database",
  service: require("@adamite/service-database"),
  options: {
    adapter: require("@adamite/service-database/adapters/memory")({
      readDatabase() {
        const filePath = path.join(process.cwd(), "data", "database.json");
        
        if (fs.existsSync(filePath)) {
          const contents = fs.readFileSync(filePath, "utf-8");
          return JSON.parse(contents);
        }
      },
      writeDatabase(data) {
        fs.writeFileSync(
          path.join(process.cwd(), "data", "database.json"),
          JSON.stringify(data)
        );
      },
      getDefaultDatabase() {
        return {};
      }
    }),
    rules: require("./database/rules")
  }
}

Last updated