Skip to content

Adapter Overview ​

Atscript's DB layer is adapter-agnostic. The same .as definitions, queries, and operations work across all supported adapters. Each adapter translates the generic API to a specific database engine, handling differences in SQL dialects, type systems, indexing strategies, and schema management transparently.

Quick Decision Guide ​

PostgreSQL is the best choice for production workloads. It offers the most complete feature set: native foreign key enforcement, transactional DDL (schema changes are atomic), pgvector for vector similarity search, CITEXT for case-insensitive text, and GIN-based fulltext search. If you need a battle-tested relational database with advanced capabilities, PostgreSQL is the recommended adapter.

SQLite is ideal for development and testing. Zero-config, single-file storage, and no server process make it the fastest way to get started. SQLite supports foreign keys via PRAGMA foreign_keys and fulltext search via FTS5 virtual tables. Its main limitation is ALTER TABLE support — column type changes and some modifications require table recreation.

MongoDB is best for flexible schemas and document-oriented data. It stores nested objects natively (no flattening), supports native array patch operations ($push, $pull), and loads relations via $lookup in a single pipeline. Atlas Search enables text and vector search. Transactions require a replica set.

MySQL is a solid choice for existing MySQL infrastructure. It offers wide hosting support, native FULLTEXT indexing, and VECTOR(N) support in MySQL 9.0+. In-place column modification via ALTER TABLE MODIFY COLUMN means schema changes are straightforward.

Memory is not a datastore at all — it is an in-memory adapter with no engine, no I/O, and no persistence. Its primary use is exposing a runtime-owned surface (a Redis-backed job list, an in-code registry, a computed snapshot) as a read-only atscript table via a provider closure, so it renders in atscript-ui and carries @DbActions without acquiring a database. It also doubles as a fast in-memory DbSpace with full CRUD for tests and trivial tables. Comparison semantics are JS-native, not SQL-identical.

Feature Comparison Matrix ​

FeaturePostgreSQLSQLiteMongoDBMySQLMemory
Native FK constraintsYesYes (PRAGMA)EmulatedYesApp-level
Transactional DDLYesNoN/ANoN/A
Text searchGIN + tsvectorFTS5Atlas SearchFULLTEXTNo
Vector searchpgvectorsqlite-vec (opt-in)Atlas vectorSearchVECTOR(N) 9.0+No
JSON storageJSONBTEXTNativeJSONNative
Boolean typeNativeINTEGER 0/1NativeTINYINT(1)Native
UUID generationgen_random_uuid()App-sideApp-sideApp-sideApp-side
Nested objectsFlattenedFlattenedNativeFlattenedNative
Native patch opsNoNoYes ($push/$pull)NoNo
Native relationsNoNoYes ($lookup)NoNo
Relational predicatesEXISTSEXISTS$lookup pipelineEXISTSIn-process
In-place column modifyYesNo (recreate)N/AYesN/A
TransactionsFull (incl. DDL)YesReplica set requiredYes (no DDL)No
Schema namespacesSchemasNoNoDatabasesNo
Grouped queriesYesYesYesYesYes
Calendar bucketsYesDriver hook4.0+tz tablesYes
Adapter-specific annotations@db.pg.*None@db.mongo.*@db.mysql.*None

Installation Quick Reference ​

AdapterPackagePeer DependencyImport
PostgreSQL@atscript/db-postgrespgPostgresAdapter
SQLite@atscript/db-sqlitebetter-sqlite3SqliteAdapter
MongoDB@atscript/db-mongomongodbMongoAdapter
MySQL@atscript/db-mysqlmysql2MysqlAdapter
Memory@atscript/db-memory(none)MemoryAdapter

All adapters follow the same DbSpace + factory pattern:

typescript
import { DbSpace } from "@atscript/db";
import { PgDriver, PostgresAdapter } from "@atscript/db-postgres";

const driver = new PgDriver("postgresql://localhost:5432/mydb");
const db = new DbSpace(() => new PostgresAdapter(driver));

Or use the shorthand createAdapter helper where available:

typescript
import { createAdapter } from "@atscript/db-postgres";

const db = createAdapter("postgresql://localhost:5432/mydb");

TIP

Each adapter page covers detailed setup, driver options, and adapter-specific configuration. See the individual pages linked in Next Steps.

Capability Flags ​

Adapters declare their capabilities via boolean flags on the BaseDbAdapter class. The generic DB layer reads these flags and adapts its behavior automatically — for example, skipping client-side object flattening when the adapter stores nested objects natively, or falling back to application-level cascade logic when the database does not enforce foreign keys.

FlagDescriptionPGSQLiteMongoMySQLMemory
supportsNativeForeignKeysDB enforces FK constraintsYesYesNoYesNo
supportsNestedObjectsStores nested objects nativelyNoNoYesNoYes
supportsNativePatchHas native array operationsNoNoYesNoNo
supportsNativeRelationsJoins relations in one queryNoNoYesNoNo
supportsRelationFiltersRenders $some / $none in reads and writes (since 0.1.147)YesYesYesYesYes
supportsNativeValueDefaultsDDL carries static defaults (deprecated 0.1.128 — no longer consulted)YesYesNoYesNo
supportsColumnModifyALTER COLUMN type changesYesNoN/AYesN/A

How the generic layer uses these flags

When supportsNativeForeignKeys is false, the generic layer implements cascade and set-null behavior in application code before deleting parent records. When supportsNestedObjects is false, nested objects are automatically flattened to __-separated column names (e.g., address__city). When supportsNativePatch is true, patch operations like array $push/$pull are delegated directly to the adapter instead of being decomposed into read-modify-write cycles.

Next Steps ​

  • PostgreSQL — full setup, pgvector, CITEXT, connection pooling
  • SQLite — driver setup, FTS5 search, table recreation
  • MongoDB — Atlas Search, vector search, capped collections
  • MySQL — engine/charset options, FULLTEXT, VECTOR support
  • Memory — in-memory adapter for runtime-owned surfaces and tests (provider-backed read-only + stored read-write)
  • Creating Custom Adapters — extend BaseDbAdapter for other databases
  • Annotations Reference — all @db.* annotations in one place

For general usage that applies to all adapters:

Released under the MIT License.