Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/samhuk/ts-pg-orm

Delightful Typescript PostgreSQL ORM
https://github.com/samhuk/ts-pg-orm

orm postgresql sql typescript

Last synced: about 1 month ago
JSON representation

Delightful Typescript PostgreSQL ORM

Awesome Lists containing this project

README

        





ts-pg-orm


Delightful Typescript PostgreSQL ORM



ci status





license





npm version

## 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
// { id: number, name: string, ... }
export type CreateUserRecordOptions = CreateRecordOptions
// { name: string, ... }
export type UserGroupRecord = ToRecord
// { 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) ✨