<p align="center">
  <img src="https://db.atscript.dev/logo.svg" alt="Atscript" width="120" />
</p>

<h1 align="center">@atscript/db-postgres</h1>

<p align="center">
  <strong>Define your models once</strong> — get TypeScript types, runtime validation, and DB metadata from a single <code>.as</code> model.
</p>

<p align="center">
  <a href="https://db.atscript.dev">Documentation</a> · <a href="https://db.atscript.dev/adapters/postgresql">PostgreSQL Adapter</a>
</p>

---

PostgreSQL adapter for `@atscript/db` using the `pg` (node-postgres) driver. Full-featured with pgvector, CITEXT, GIN fulltext, HNSW indexes, transactional DDL, and batched inserts with RETURNING.

Includes `PostgresPlugin` for `@db.pg.*` annotations (type overrides, schema selection, native collation).

## Installation

```bash
pnpm add @atscript/db-postgres pg
```

## Quick Start

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

const db = createAdapter("postgresql://user@localhost:5432/mydb");
const users = db.getTable(UsersType);

await users.insertOne({ name: "Alice", email: "alice@example.com" });
```

## Features

- Full CRUD with batched multi-row INSERT + RETURNING (auto-chunking at ~65K params)
- pgvector: HNSW indexes with cosine, euclidean, and dot product similarity
- CITEXT for case-insensitive text via `@db.collate "nocase"`
- GIN fulltext search via `tsvector` / `tsquery` with ranking
- GENERATED BY DEFAULT AS IDENTITY for auto-increment
- Native UUID generation via `gen_random_uuid()`
- Schema selection via `@db.pg.schema`
- Transactional DDL — schema sync is fully atomic
- Foreign key constraints with referential actions
- Full schema sync: CREATE, ALTER, DROP, indexes, views

## Documentation

- [PostgreSQL Adapter Guide](https://db.atscript.dev/adapters/postgresql)
- [Full Documentation](https://db.atscript.dev)

## License

MIT
