@postnord/pretty-dynamo
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

An abstraction layer on top of the DynamoDB DataMapper. Takes care of the ugly work and exposes a set of readable methods to interact with the database.

Installation

Run npm i @postnord/pretty-dynamo @aws/dynamodb-data-mapper-annotations and you're good to go.

import { PrettyDynamo } from '@postnord/pretty-dynamo'
import { attribute, hashKey, table } from '@aws/dynamodb-data-mapper-annotations'
import * as https from 'https'

@table('users')
class User {
  @hashKey()
  id!: string

  @attribute()
  name!: string
}

const userService = new PrettyDynamo(User, {
  region: 'eu-west-1',
  httpOptions: {
    agent: new https.Agent({
      keepAlive: true,
    }),
  },
})

const createUser = async () => {
  await userService.initDb()
  // The input to those methods will be typed and expecting the schema declared above.
  await userService.createOneRecord({ id: '1', name: 'Jerry' })

  /*
   * The output of the fetching methods will also be typed, so you should expect it to match the declared schema
   * Methods will not throw errors unnecessarily (like when fetching a non existing object) which fixes
   * one of the main design flaws of the native driver.
   */
  const user = await userService.getOneRecord({ id: '1' })
  console.log(user) // variable `user` is undefined if the user does not exist.
}

API

initDb()

Will initialize the DB connection and allow you to use the rest of the functions.

await userService.initDb()

getDb()

Will return the DataMapper object.

const dataMapper = await userService.getDb()

getOneRecord(criteria)

Will grab an object from the database by the specified criteria. Returns undefined if no objects match the given criteria.

const user = await userService.getOneRecord({ id: '1' })

getManyRecords(criteria)

Will query the db for records matching the specified criteria and return an array of matching objects.

const users = await userService.getManyRecords({ name: 'Matt' })

createOneRecord(item)

Will store a new db record.

await userService.createOneRecord({ id: '1', name: 'Estelle' })

createManyRecords(item[])

Will store an array of items to the database.

await userService.createManyRecords([
  { id: '1', name: 'Elaine' },
  { id: '2', name: 'George' },
])

updateOneRecord(criteria, updates)

Will store an array of items to the database. Returns undefined if no items match the criteria.

const updated = await userService.updateOneRecord({ id: '1' }, { name: 'Jackie' })

updateManyRecords(criteria, updates)

Will update all matching objects.

const updated = await userService.updateManyRecords({ name: 'Kramer' }, { name: 'Cosmo' })

deleteOneRecord(criteria)

Will delete the first item matching the criteria. Will return undefined if there were no matches.

const deleted = await userService.deleteOneRecord({ id: '1' })

deleteManyRecords(criteria)

Will delete all matching the criteria. Will return undefined if there were no matches.

await userService.deleteManyRecords({ name: 'Susan' })

Readme

Keywords

none

Package Sidebar

Install

npm i @postnord/pretty-dynamo

Weekly Downloads

863

Version

0.1.1

License

Proprietary

Unpacked Size

8.32 kB

Total Files

4

Last publish

Collaborators

  • cielomuyot.postnord
  • pn_rroble
  • damiant-postnord
  • erikedling
  • adamkillander
  • sculaste
  • bernhardhettman-pn
  • ashwin_postnord
  • fbopdin
  • elrmndo_pn
  • aliaksei.stryi-postnord
  • sang-canencia-npm-pn
  • pn-ost-base
  • retail-team-npm
  • eriklind-pn
  • postnord.aldee_mativo
  • raymund.galapate
  • romeo.obane
  • elias_sidenbladh
  • ncppublicservice
  • oaipostnord
  • pn-jroma
  • pn-foundation-deployer
  • pn-chester
  • pn-akilumanga
  • sabbir-rahman-sakib
  • jkaske-pn
  • pnrutbergphilip
  • pnorbert
  • kheycee.matarlo
  • johankarlsson-pn
  • sesam-publisher
  • vanessa.faith.abordo.postnord
  • avsceriola-pn
  • junlue-pn
  • pnncp-aftersales
  • mipe-pn
  • vbacalso
  • jonas_elfstrom
  • pn-webteam
  • postnord.npm
  • vbrigoli
  • jvalmores
  • vijilgirish
  • frqu300
  • access-login-publisher
  • joakim.hedlund.postnord
  • pn-fipe
  • pn-aronsson
  • m.usman
  • mahadiapn
  • ambots13