0.1.1 • Published 3 years ago

@zoetrope-source/mapper v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@zoetrope-source/mapper

Lightweight Typescript PostgreSQL mapper.

import mapper from '@zoetrope-source/mapper';

mapper.config({
  db: {
    host: 'localhost',
    port: 5432,
    name: 'booksDb',
    user: 'mdewey',
    pass: 'd3c1m@l'
  }
});

interface BookRequest {
  title: string;
  author: string;
  datePublished: Date;
}

interface Book extends BookRequest {
  id: number;
}

const getBooks = async (): Promise<Array<Book>> =>
  await mapper.query<Array<Book>>(
    'SELECT id, title, author, datePublished FROM books'
  );

const getBook = async (id: number): Promise<Book> =>
  await mapper.queryOne<Book>(
    'SELECT id, title, author, datePublished FROM books WHERE id = $1',
    id
  );

const createBook = async (request: BookRequest): Promise<Book> =>
  await mapper.query<Book>(
    `INSERT INTO books (title, author, datePublished) VALUES ($1, $2, $3)
       RETURNING id, title, author, datePublished`,
    request.title,
    request.author,
    request.datePublished
  );

const updateBook = async (id: number, request: BookRequest): Promise<Book> =>
  await mapper.query<Book>(
    `UPDATE books SET title = $1, author = $2, datePublished = $3
       WHERE id =$4`,
    request.title,
    request.author,
    request.datePublished,
    id
  );

const deleteBook = async (id: number): Promise<void> =>
  await mapper.exec('DELETE FROM books WHERE id = $1', id);
0.1.1

3 years ago

0.1.0

3 years ago