Skip to content

Adapter Overview

Experimental

The DB integrations layer is experimental. APIs and annotations described in this section may change in future releases.

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.

Feature Comparison Matrix

FeaturePostgreSQLSQLiteMongoDBMySQL
Native FK constraintsYesYes (PRAGMA)EmulatedYes
Transactional DDLYesNoN/ANo
Text searchGIN + tsvectorFTS5Atlas SearchFULLTEXT
Vector searchpgvectorNoAtlas vectorSearchVECTOR(N) 9.0+
JSON storageJSONBTEXTNativeJSON
Boolean typeNativeINTEGER 0/1NativeTINYINT(1)
UUID generationgen_random_uuid()App-sideApp-sideApp-side
Nested objectsFlattenedFlattenedNativeFlattened
Native patch opsNoNoYes ($push/$pull)No
Native relationsNoNoYes ($lookup)No
In-place column modifyYesNo (recreate)N/AYes
TransactionsFull (incl. DDL)YesReplica set requiredYes (no DDL)
Schema namespacesSchemasNoNoDatabases
Adapter-specific annotations@db.pg.*None@db.mongo.*@db.mysql.*

Installation Quick Reference

AdapterPackagePeer DependencyImport
PostgreSQL@atscript/db-postgrespgPostgresAdapter
SQLite@atscript/db-sqlitebetter-sqlite3SqliteAdapter
MongoDB@atscript/db-mongomongodbMongoAdapter
MySQL@atscript/db-mysqlmysql2MysqlAdapter

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.

FlagDescriptionPGSQLiteMongoMySQL
supportsNativeForeignKeysDB enforces FK constraintsYesYesNoYes
supportsNestedObjectsStores nested objects nativelyNoNoYesNo
supportsNativePatchHas native array operationsNoNoYesNo
supportsNativeRelationsJoins relations in one queryNoNoYesNo
supportsNativeValueDefaultsDB handles default valuesYesYesNoYes
supportsColumnModifyALTER COLUMN type changesYesNoN/AYes

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

For general usage that applies to all adapters:

Released under the MIT License.