Skip to content

Write a storage adapter

For: a developer who wants authtempo on a database that has no adapter yet.

What it does: you write the ten methods of the storage port, and core’s conformance suite proves the adapter.

  • Check that your database has no adapter yet. SQLite and MySQL have one, beside D1 and Postgres.
  • Read The storage port.
  • A way to open a fresh, empty database for each test. The contract runs on it.
  1. Write an object with create, createMany, rowsPerInsert, findOne, findMany, update, delete, deleteRows, keep, and transaction, of the type StorageAdapter.
    • Set isolated on it and on each transaction port: true when the open transaction is isolated, and keep answers false in it.
  2. Read the models and fields from schema of @authtempo/core.
    • Map each model to a table and each field to a column.
  3. Write one migration for each schema version.
    • Store the version in the table ta_meta.
  4. Refuse to start when the database reports an older version than SCHEMA_VERSION.
    • For that refusal, throw StorageError with schema_mismatch.
  5. Throw StorageError with unique_violation when a unique key refuses a write.
  6. Run runAdapterConformance from @authtempo/core/conformance on the adapter.
The conformance suite on an adapter
import { createMemoryAdapter } from "@authtempo/core";
import { runAdapterConformance } from "@authtempo/core/conformance";
// The contract of every storage adapter. Put this file beside your adapter,
// and replace the factory with one that opens a fresh, empty database. An
// adapter passes this suite, or it does not ship. Here the memory adapter of
// core stands in for yours.
runAdapterConformance("memory (the sample)", async () => ({
adapter: createMemoryAdapter(),
close: async () => {},
}));
  • Clauses join with AND only. The operators are eq, ne, in, lt, lte, gt, gte, and startsWith. There is no join.
  • startsWith matches the exact text of a non-empty prefix on a string field, with case, as eq does. It has no wildcard and never matches null.
  • Send the prefix as a parameter. Compare substr(column, 1, n), where n is the count of code points. Do not use LIKE: SQLite’s LIKE ignores the case of ASCII letters. No index serves the clause, so it reads the whole table.
  • Throw invalid_argument for a startsWith prefix that is empty or not a string, or for a field that is not a string. Throw it also for a prefix with a lone surrogate or with NUL.
  • Throw invalid_argument for a string with NUL or with a lone surrogate in any where value, on every op, also in an in list. The conformance suite tries both in each op.
  • update and delete take a where and answer the count of rows. An empty where on them is an error.
  • An empty in list matches nothing. Two null values never conflict on a unique key.
  • sort takes one key or a list of keys, each with its own direction. A key with notNull: true names a field that is not nullable, so the SQL needs no null term and an index can give the order. Throw invalid_argument for notNull on a nullable field.
  • Reads inside a transaction see the writes of the transaction.
  • A nested transaction rolls back its own writes only.
  • The field types are string, boolean, integer, date, and json. eq on a date matches to the millisecond.
  • The suite names the rule that failed. Fix the adapter, never the suite.
  • A unique_violation caught inside a transaction aborts the transaction on Postgres. Catch it outside.