# tsooq

> TypeScript Object Oriented Querying

Latest version **0.1.19** (published 2020-01-06) · Apache License Version 2.0 license · 0 weekly downloads

## Install

```sh
npm install tsooq
pnpm add tsooq
yarn add tsooq
bun add tsooq
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.19 |
| Published | 2020-01-06 |
| First published | 2019-09-18 |
| Weekly downloads | 0 |
| License | Apache License Version 2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 182 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Mathias Haessig |
| Maintainers | cornholio127 |

## Links

- npm: https://www.npmjs.com/package/tsooq
- Repository: https://github.com/cornholio127/tsooq
- Issues: https://github.com/cornholio127/tsooq/issues
- npm.io page: https://npm.io/package/tsooq

## Dependencies (1)

- [log4js](https://npm.io/package/log4js.md) 6.1.0

## Recent versions

- 0.1.19 (latest) — 2020-01-06
- 0.1.18 — 2020-01-06
- 0.1.17 — 2020-01-06
- 0.1.16 — 2019-12-12
- 0.1.15 — 2019-12-12
- 0.1.14 — 2019-12-12
- 0.1.13 — 2019-12-12
- 0.1.12 — 2019-12-12
- 0.1.11 — 2019-10-13
- 0.1.10 — 2019-10-06
- 0.1.8 — 2019-10-06
- 0.1.6 — 2019-09-27
- 0.1.5 — 2019-09-25
- 0.1.4 — 2019-09-25
- 0.1.3 — 2019-09-18
- … 2 more at https://npm.io/package/tsooq/versions

## README

[![NPM version](https://img.shields.io/npm/v/tsooq.svg?style=flat)](https://www.npmjs.com/package/tsooq)

# tsOOQ

TypeScript Object Oriented Querying

tsOOQ lets you write queries for a PostgreSQL in a typesafe fluent manner in TypeScript. It's not on OR-mapper. It's just SQL with language support. 

Plain SQL:
```sql
SELECT person.first_name, person.last_name, address.street, address.city 
FROM person 
LEFT OUTER JOIN address ON person.address_id = address.id 
WHERE address.city = 'London'
```

Same query using tsOOQ:
```javascript
create
  .select(Person.FIRST_NAME, Person.LAST_NAME, Address.STREET, Address.CITY)
  .from(Tables.PERSON)
  .leftOuterJoin(Tables.ADDRESS)
  .on(Person.ADDRESS_ID.eq(Address.ID))
  .where(Address.city.eq('London'))
  .fetch();
```

## Requirements
- Docker (for generating the metamodel with tsooq-gen)

## Getting started
Install `tsooq` and `tsooq-gen` using npm.
```
npm install tsooq --save
npm install tsooq-gen --save-dev
```
Put you DDL file somewhere into you project, e.g. `database/init.sql`.

Create a file called `.tsooq.json` in the root of your project.
```json
{
  "ddlScript": "database/init.sql",
  "outputDir": "src/gen",
  "schemaName": "public"
}
```
Add the following line to the `scripts`-section of `package.json`:
```json
"gen": "tsooq-gen"
```
Run the metamodel generator.
```
npm run gen
```
This creates a docker container with PostgreSQL and runs the DDL script. After 
it retrieves the metadata from the database and generates the metamodel. In the
example above the generated file will be `src/gen/public.ts` ("public" beeing 
the schema name).

Now set up the database connection factory.
```javascript
import { PoolConfig, Pool } from 'pg';
import { QueryFactory } from 'tsooq';

const config: PoolConfig = {
  host: 'localhost',
  port: 5432,
  database: 'example',
  user: 'example',
  password: 'password',
};

const pool = new Pool(config);

export const create = QueryFactory.create(pool);
```
That's it! Now you can start writing queries using tsOOQ!
```javascript
export const add = (dto: PersonDto): Promise<Result<void>> => {
  return create
    .insertInto(Tables.PERSON, Person.FIRST_NAME, Person.LAST_NAME, Person.EMAIL)
    .values(dto.firstName, dto.lastName, dto.email)
    .execute();
};

export const getById = (id: number): Promise<PersonDto> => {
  return create
    .select(Person.ID, Person.FIRST_NAME, Person.LAST_NAME, Person.EMAIL)
    .from(Tables.PERSON)
    .where(Person.ID.eq(id))
    .fetchSingleMapped(rec => ({
      id: rec.get(Person.ID),
      firstName: rec.get(Person.FIRST_NAME),
      lastName: rec.get(Person.LAST_NAME),
      email: rec.get(Person.EMAIL),
    }));
};
```

See the [full example application here](https://github.com/cornholio127/tsooq.example)

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