Adamite
  • Initial page
  • Server
    • Introduction
    • Database
      • Configuration
      • Using different adapters
        • Memory Adapter
      • Securing your data
        • Securing read operations
    • Functions
      • Invokable Functions
      • Runtime Functions
      • Scheduled Functions
  • SDK
    • Introduction
    • Database
      • Introduction
      • Read a document
      • Read multiple documents
      • Subscribe to changes
      • Create a new document
      • Update an existing document
      • Delete a document
    • Authentication
      • Create a user
      • Log in and log out
      • Get the logged in user
    • Functions
      • Invoke a function
Powered by GitBook
On this page

Was this helpful?

  1. Server
  2. Database
  3. Using different adapters

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")
  }
}
PreviousUsing different adaptersNextSecuring your data

Last updated 5 years ago

Was this helpful?