0.2.4 • Published 1 year ago

@postnord/pretty-dynamo v0.2.4

Weekly downloads
-
License
Proprietary
Repository
-
Last release
1 year ago

Note! This package is deprecated

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' })
0.2.1

1 year ago

0.2.0

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.4

1 year ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago