# ts-pg-orm

> Typescript PostgreSQL ORM

Latest version **5.1.7** (published 2023-01-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-pg-orm
pnpm add ts-pg-orm
yarn add ts-pg-orm
bun add ts-pg-orm
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.1.7 |
| Published | 2023-01-25 |
| First published | 2022-07-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 377.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Maintainers | samhuk |
| Keywords | orm, typescript, postgre, postgresql, storage, persistence, crud, sql |

## Links

- npm: https://www.npmjs.com/package/ts-pg-orm
- Repository: https://github.com/samhuk/ts-pg-orm
- Issues: https://github.com/samhuk/ts-pg-orm/issues
- Funding: https://www.buymeacoffee.com/samhuk
- npm.io page: https://npm.io/package/ts-pg-orm

## Dependencies (3)

- [simple-pg-client](https://npm.io/package/simple-pg-client.md) ^1.1.0
- [@samhuk/data-query](https://npm.io/package/@samhuk/data-query.md) ^1.2.1
- [@samhuk/data-filter](https://npm.io/package/@samhuk/data-filter.md) ^1.1.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 5.1.7 (latest) — 2023-01-25
- 5.1.0-beta1 (beta) — 2022-10-10
- 5.1.6 — 2022-10-22
- 5.1.5 — 2022-10-19
- 5.1.4 — 2022-10-19
- 5.1.3 — 2022-10-19
- 5.1.2 — 2022-10-15
- 5.1.1 — 2022-10-12
- 5.1.0 — 2022-10-10
- 5.1.0-beta4 — 2022-10-10
- 5.1.0-beta3 — 2022-10-10
- 5.1.0-beta2 — 2022-10-10
- 5.0.1 — 2022-10-05
- 5.0.0 — 2022-10-03
- 4.3.6 — 2022-09-24
- … 43 more at https://npm.io/package/ts-pg-orm/versions

## README

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="./images/wordmark-dark.png">
    <source media="(prefers-color-scheme: light)" srcset="./images/wordmark-light.png">
    <img alt="ts-pg-orm" src="./images/wordmark-light.svg">
  </picture>
</p>

<p align="center">
  <em>Delightful Typescript PostgreSQL ORM</em>
</p>

<p align="center">
  <a href="https://github.com/samhuk/ts-pg-orm/actions/workflows/ci.yaml/badge.svg" target="_blank">
    <img src="https://github.com/samhuk/ts-pg-orm/actions/workflows/ci.yaml/badge.svg" alt="ci status" />
  </a>
  <a href="https://codecov.io/gh/samhuk/ts-pg-orm" > 
    <img src="https://codecov.io/gh/samhuk/ts-pg-orm/branch/master/graph/badge.svg?token=N0WKDLEDNM"/> 
  </a>
  <a href="https://img.shields.io/badge/License-MIT-green.svg" target="_blank">
    <img src="https://img.shields.io/badge/License-MIT-green.svg" alt="license" />
  </a>
  <a href="https://app.fossa.com/projects/custom%2B33608%2Fgithub.com%2Fsamhuk%2Fts-pg-orm?ref=badge_shield" alt="FOSSA Status">
    <img src="https://app.fossa.com/api/projects/custom%2B33608%2Fgithub.com%2Fsamhuk%2Fts-pg-orm.svg?type=shield"/>
  </a>
  <a href="https://badge.fury.io/js/ts-pg-orm.svg" target="_blank">
    <img src="https://badge.fury.io/js/ts-pg-orm.svg" alt="npm version" />
  </a>
</p>

## Overview

ts-pg-orm provides PostgreSQL persistence for your Typescript application. Write expressive, zero guess-work, fully type-enforced typescript queries to perform CRUD operations on PostgreSQL databases.

Start by viewing the [Getting Started](https://github.com/samhuk/ts-pg-orm/wiki/Getting-Started) guide.

[ts-pg-orm is available on npm](https://www.npmjs.com/package/ts-pg-orm).

## Usage Overview

Define data formats, relations, and database connectivity to create type-safe auto-completing data stores:

```typescript
import { createDataFormat, createTsPgOrm, ... } from 'ts-pg-orm'
const userDF = createDataFormat(...)
const ORM = createTsPgOrm([userDF, ...] as const).setRelations([...] as const)
const orm = await ORM.connect({ host: 'localhost', port: 5432, ... })
await orm.provisionStores()
```

Perform CRUD operations on data stores:

```typescript
// Create
const userCreated = await orm.stores.user.create({ name: 'alice' })
// Get
const userFound = await orm.stores.user.get({
  fields: ['name'],
  filter: { field: 'id', op: Operator.EQUALS, val: 1 },
  relations: { // Recursively include related data
    userGroups: {
      query: { ... },
      relations: { ... }
    },
  },
})
// Update
const userUpdated = await orm.stores.user.update({
  query: {
    filter: { ... },
  },
  record: { name: 'bob' },
  return: true,
})
// Delete
const userDeleted = await orm.stores.user.delete({
  query: {
    filter: { ... },
  },
  return: true,
})
```

Create types to use throughout your application:

```typescript
export type UserRecord = ToRecord<typeof USER_DFD>
// { id: number, name: string, ... }
export type CreateUserRecordOptions = CreateRecordOptions<typeof USER_DFD>
// { name: string, ... }
export type UserGroupRecord = ToRecord<typeof USER_GROUP_DFD>
// { id: number, name: string, ... }
```

Use type-enforced sql information to create bespoke SQL statements:

```typescript
const userSql = ORM.dataFormats.user.sql
const customUserSql = `select ${userSql.columnNames.name} from ${userSql.tableName}`
```

## Examples

### Integration Tests

The integration test suite connects to a real PostgreSQL server at (by default) postgres@localhost:5432 and performs various ts-pg-orm queries with a set of example data formats and relations.

Run `npm run integration-tests` to build and run these. The database connection configuration is at `/.env-cmdrc.json`.

---

If you found this package delightful, feel free to [buy me a coffee](https://www.buymeacoffee.com/samhuk) ✨

---
_Source: https://npm.io/package/ts-pg-orm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
