Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/samhuk/ts-pg-orm
- Owner: samhuk
- License: mit
- Created: 2022-06-25T13:00:13.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-25T21:16:58.000Z (almost 2 years ago)
- Last Synced: 2024-12-06T08:35:49.422Z (about 1 month ago)
- Topics: orm, postgresql, sql, typescript
- Language: TypeScript
- Homepage:
- Size: 415 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: contributing/development.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Delightful Typescript PostgreSQL ORM## 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) ✨