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.
Before you start
Section titled “Before you start”- 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.
- Write an object with
create,createMany,rowsPerInsert,findOne,findMany,update,delete,deleteRows,keep, andtransaction, of the typeStorageAdapter.- Set
isolatedon it and on each transaction port: true when the open transaction is isolated, andkeepanswers false in it.
- Set
- Read the models and fields from
schemaof@authtempo/core.- Map each model to a table and each field to a column.
- Write one migration for each schema version.
- Store the version in the table
ta_meta.
- Store the version in the table
- Refuse to start when the database reports an older version than
SCHEMA_VERSION.- For that refusal, throw
StorageErrorwithschema_mismatch.
- For that refusal, throw
- Throw
StorageErrorwithunique_violationwhen a unique key refuses a write. - Run
runAdapterConformancefrom@authtempo/core/conformanceon the 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 () => {},}));The rules
Section titled “The rules”- Clauses join with AND only. The operators are
eq,ne,in,lt,lte,gt,gte, andstartsWith. There is no join. startsWithmatches the exact text of a non-empty prefix on a string field, with case, aseqdoes. 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 useLIKE: SQLite’sLIKEignores the case of ASCII letters. No index serves the clause, so it reads the whole table. - Throw
invalid_argumentfor astartsWithprefix 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_argumentfor a string with NUL or with a lone surrogate in anywherevalue, on every op, also in aninlist. The conformance suite tries both in each op. updateanddeletetake awhereand answer the count of rows. An emptywhereon them is an error.- An empty
inlist matches nothing. Two null values never conflict on a unique key. sorttakes one key or a list of keys, each with its own direction. A key withnotNull: truenames a field that is not nullable, so the SQL needs no null term and an index can give the order. Throwinvalid_argumentfornotNullon 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.
eqon a date matches to the millisecond.
If it fails
Section titled “If it fails”- The suite names the rule that failed. Fix the adapter, never the suite.
- A
unique_violationcaught inside a transaction aborts the transaction on Postgres. Catch it outside.