2.0.3 • Published 1 month ago

@growsari/dbal v2.0.3

Weekly downloads
37
License
-
Repository
-
Last release
1 month ago

Database Access Layer

Database Access Layer (DBAL) is a wrapper for DynamoDB Client. It provides the basic create, read, update and delete (CRUD) functionality and lets you group data into an entity.

Installation and Basic Usage

Install the Database Access Layer

npm i @growsari/dbal

Require or import the package as Entity

const Entity = require('@growsari/dbal')

Before we could use it, the project must have the following environment variables

ENVDescriptionDefault ValueOther Value
IS_OFFLINEFlag for local developmentfalsetrue
DB_ADAPTERDatabase to be useddynamodb~
DYNAMODB_ENDPOINTDynamoDB endpointhttp://localhost:8000~
TABLE_NAMEDynamoDB table name~~

Create a new class and extend Entity

'use strict'

const Entity = require('@growsari/dbal')

class User extends Entity {
  constructor() {
    super()
    this.attributes = {
      email: [
        'identifier'
      ],
      group_id: [
        'foreign'
      ]
    }
    this.sort = [
      {
        index: 'SortByCreatedAtIndex',
        field: 'created_at'
      },
      {
        index: 'SortByNameIndex',
        field: 'name'
      }
    ]
  }
}

module.exports = User

Require User entity and create item:

await User.create({
  name: 'John Doe',
  email: 'john.doe@example.com',
  group_id: 'BDS45HX47I9CT'
})

// {
//   "id": "659312a0-1b5c-11ea-a271-e732f3f9733b",
//   "created_at": "2019-12-10T14:50:24.330Z",
//   "updated_at": "2019-12-10T14:50:24.330Z",
//   "name": "John Doe",
//   "email": "john.doe@example.com",
//   "group_id": "BDS45HX47I9CT"
// }

Read item by:

await User.getById('659312a0-1b5c-11ea-a271-e732f3f9733b')

Update item by:

await User.updateById('659312a0-1b5c-11ea-a271-e732f3f9733b', {
  name: 'John Doe II'
})

Delete item by:

await User.deleteById('659312a0-1b5c-11ea-a271-e732f3f9733b"')

Entity Class Properties

These are the properties for configuring an entity

attributes

Lets you configure a special field in an item which implies uniqueness, foreign key and so on

ValueDescription
identifierCreates a field with unique value within the entity
foreignCreates a foreign key (id in another microservice)
namespace:fieldCreates an identifier within a namespace (determined by field)

Sample

this.attributes = {
  [attribute_name] : [
    'identifier',
    'foreign',
    'namespace:field'
  ]
}

sort

List of DynamoDB global secondary indexes (GSI) for data retrieval sorting

Entity Class Functions

These are the available functions for managing the data within an entity

async create(data)
async updateById(id, data)
async deleteById(id)
async bulkCreate(collection)
async bulkUpdate(collection)
async bulkDelete(collection)
async getById(id)
async getByIdOrFail(id)
async getByIdentifier(identifier, value)
async getByIdentifierWithinNamespace(namespace, identifier, value)
async getByIds(ids)
async getAll(first, after, sortBy, filterConditions)
async getAllByForeignKey(foreignKey, foreignValue, first, after, sortBy, filterConditions)
async search(query, rawQuery, fields, perPage, page, filters, sort)
async reindex()
async syncWithSearchEngine()

Sort By

A string which is composed of field name and order joined by |.

const sortBy = 'name|asc'
await Product.getAll(first, after, sortBy, filterConditions)

Filter Conditions

NOTE: To effeciently use the filter conditions in terms of DynamoDB RCU and WCU, I recommend using it with getAll() API. If you can't avoid using it with getAllByForeignKey() API please use filter with the defined sort fields.

These are the recognized compound expressions:

AND expression

Asserts that all of the subexpressions' conditions are satisfied.

const filterConditions = [
  'AND',
  { name: 'Coke' },
  { is_available: true },
  // more subexpressions
]
await Product.getAll(first, after, sortBy, filterConditions)

The above condition is equivalent to the following boolean expression:

name === 'COKE' && is_available === true && ...

OR expression

Asserts that at least one of the subexpressions' condtions are satisfied.

const filterConditions = [
  'OR',
  { name: 'Coke' },
  { brand: 'Coke' },
  // more subexpressions
]
await Product.getAll(first, after, sortBy, filterConditions)

The above condition is equivalent to the following boolean expression:

name === 'COKE' || brand === 'Coke' || ...

NOT expression

Asserts that at least one of the subexpressions' condtions are satisfied.

const filterConditions = [
  'AND',
  { name: 'Coke' },
  ['NOT', { is_available: true }]
]
await Product.getAll(first, after, sortBy, filterConditions)

The above condition is equivalent to the following boolean expression:

name === 'COKE' && is_available !== true

Equal expression

An object with one pair of key and value. The object's key represents the field name. The object's value is also an object and it represents the condition where the condition's key is the operation.

== operation creates a condtion which is true if the field is equal to the defined value.

const filterConditions = { name: { '==': 'Coke' } }
// can be simplified to:
// const filterConditions = { name: 'Coke' }
await Product.getAll(first, after, sortBy, filterConditions)

NotEqual expression

!= operation creates a condtion which is true if the field is not equal to the defined value.

const filterConditions = { is_available: { '!=': false } }
await Product.getAll(first, after, sortBy, filterConditions)

LessThan expression

< operation creates a condtion which is true if the field is less than to the defined value.

const filterConditions = { available: { '<': 10 } }
await Product.getAll(first, after, sortBy, filterConditions)

LessThanOrEqualTo expression

<= operation creates a condtion which is true if the field is less than or equal to the defined value.

const filterConditions = { available: { '<=': 10 } }
await Product.getAll(first, after, sortBy, filterConditions)

GreaterThan expression

> operation creates a condtion which is true if the field is greater than to the defined value.

const filterConditions = { available: { '>': 10 } }
await Product.getAll(first, after, sortBy, filterConditions)

GreaterThanOrEqualTo expression

>= operation creates a condtion which is true if the field is greater than or equal to the defined value.

const filterConditions = { available: { '>=': 10 } }
await Product.getAll(first, after, sortBy, filterConditions)

Between expression

between operation creates a condtion which is true if the field is between value[0] and value[1].

const filterConditions = {
  created_at: {
    'between': ['2020-06-01T00:00:00.000Z', '2020-06-15T00:00:00.000Z']
    }
  }
await Product.getAll(first, after, sortBy, filterConditions)

In expression

in operation creates a condtion which is true if the field is equal to an element the defined set of values.

const filterConditions = {
  tags: {
    'in': ['bottle', 'soda', 'promo', 'top_10', 'drink']
    }
  }
await Product.getAll(first, after, sortBy, filterConditions)

BeginsWith expression

NOTE! This operation will only work with sort fields

beginsWith operation creates a condtion which is true if the field is a string and begins with or has a prefix of value.

const filterConditions = { status: { 'beginsWith': 'active_' } }
await Product.getAll(first, after, sortBy, filterConditions)

Contains expression

NOTE! This operation will only work with sort fields

contains operation creates a condtion which is true if the field contains the defined value.

const filterConditions = { status: { 'contains': 'active' } }
await Product.getAll(first, after, sortBy, filterConditions)

Errors

CodeMessage
DBAL-001Database not supported
DBAL-002Item not found for 'entity' entity
DBAL-003Invalid pagination cursor
DBAL-004Identifier 'field' must be unique
DBAL-005Identifier 'field' within the namespace 'field' must be unique
DBAL-006Sort key 'field' is not provided
DBAL-007Invalid value for foreign key 'field'
DBAL-008Duplicate entries in array as input is not allowed
DBAL-009Invalid search engine setup
2.0.3

1 month ago

2.0.2

1 month ago

2.0.1

1 month ago

2.0.0

1 month ago

1.8.27

5 months ago

1.8.24

6 months ago

1.8.25

6 months ago

1.8.26

5 months ago

1.8.22

12 months ago

1.8.23

12 months ago

1.8.20

1 year ago

1.8.21

1 year ago

1.8.19

1 year ago

1.8.18

2 years ago

1.8.9

2 years ago

1.8.10

2 years ago

1.8.8

2 years ago

1.8.11

2 years ago

1.8.12

2 years ago

1.8.13

2 years ago

1.8.14

2 years ago

1.8.15

2 years ago

1.8.16

2 years ago

1.8.17

2 years ago

1.8.7

2 years ago

1.8.6

2 years ago

1.8.5

2 years ago

1.8.4

2 years ago

1.8.3

2 years ago

1.8.2

2 years ago

1.8.1

3 years ago

1.7.13

3 years ago

1.8.0

3 years ago

1.7.12

3 years ago

1.7.11

3 years ago

1.7.10

3 years ago

1.7.9

4 years ago

1.7.8

4 years ago

1.7.7

4 years ago

1.7.6

4 years ago

1.7.5

4 years ago

1.7.4

4 years ago

1.7.3

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.10

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago