0.1.19 • Published 4 years ago

tsooq v0.1.19

Weekly downloads
2
License
Apache License Ve...
Repository
github
Last release
4 years ago

NPM version

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:

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:

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.

{
  "ddlScript": "database/init.sql",
  "outputDir": "src/gen",
  "schemaName": "public"
}

Add the following line to the scripts-section of package.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.

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!

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

0.1.17

4 years ago

0.1.18

4 years ago

0.1.19

4 years ago

0.1.12

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.15

4 years ago

0.1.16

4 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.8

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago