Home Reference Source Repository
import PyropeActions from 'pyrope/lib/actions.js'
public class | source

PyropeActions

Constructor Summary

Public Constructor
public

PyropeActions

Member Summary

Public Members
public
public
public
public

Method Summary

Public Methods
public

all(): Promise<array>

Returns an array with the mappings for every record in the table.

public

Creates associations between entities

public

Returns the table's item count

public

create(opts: object): object

Creates a new record in the specified table.

public

decreaseCounter(opts: *): *

public

Deletes an item

public

Dissociates two items if an association is found

public

findByIndex(opts: object): Promise<array | boolean>

Queries a table and looks for items matching the specified index.

public

first(opts: {}): *

public

Returns the associations of the specified item with respect to another item

public

increaseCounter(opts: *): *

public

last(opts: {}): *

public

take(opts: {}): *

public

Updates an item

public

updateCounter(opts: *): *

Public Constructors

public constructor(opts: object) source

PyropeActions

Low level DynamoDB actions.

Params:

NameTypeAttributeDescription
opts object

The options object.

opts.tablePrefix string

Prefix to the lookup table.

opts.tableName string

Table name.

opts.tableSuffix string

Suffix to the lookup table.

Public Members

public fullTableName: * source

public tableName: * source

public tablePrefix: * source

public tableSuffix: * source

Public Methods

public all(): Promise<array> source

Returns an array with the mappings for every record in the table.

Results are returned sorted ascending. You can specify a limit and a cursor to do batch fetching.

opts = {
 ascending: Boolean,    // Should the results be returned in a ascending manner?
 limit: Integer,        // The amount of items to fetch
 cursor: String         // A base64 encoded map of the key containing {uuid: String, createdAt: Number, _table: String}
}

Params:

NameTypeAttributeDescription
opts.order string

Order ('asc'|'desc')

opts.limit number

Limit to return.

opts.cursor string

Cursor.

Return:

Promise<array>

An array with the QUERY result, has the structure {Items: [], Count: 0, Cursor: ''}

@TODO: Select specific fields to return.

public associate(opts: Object): Boolean | Promise source

Creates associations between entities

Handles 1:1, 1:N and N:N associations. (!) Does not check for the existence of the items.

opts = {
  tableName: String,
  items: [
    {
      index: {[indexName]: '123'}, // Can specify multiple values to associate
      [hasMany: Boolean = false]   // Used to specify 1:N and 1:N associations
    }
  ]
}

Params:

NameTypeAttributeDescription
opts Object

The options object.

Return:

Boolean | Promise

Resolves to true if successful, throws otherwise @todo: use batchWriteItem and handle unprocessedItems

public count(): Promise<number> source

Returns the table's item count

Return:

Promise<number>

The item count.

public create(opts: object): object source

Creates a new record in the specified table.

It will create the record without any conditions. The following fields are auto-generated: uuid, createdAt, updatedAt

opts = {
  tableName: 'users',
  fields: {
    username: 'someguy55',
    password: 'myPassword123'
  }
}

return = {
  uuid: '3f3e1091-8e43-41ca-a19a-881241370c31' // String = 'S'
  username: 'someguy55',                       // String = 'S'
  password: 'myPassword123'                    // String = 'S'
  createdAt: 1470345881706,                    // Number = 'N'
  updatedAt: 1470345881755                     // Number = 'N'
}

Params:

NameTypeAttributeDescription
opts object

Options mapping.

Return:

object

The fields of the newly created record, rejection on error @todo: Handle unprocessedItems @todo: Handle failed increaseCounter() @todo: let uuid, createdAt, updatedAt to be cofigurable ?

public decreaseCounter(opts: *): * source

Params:

NameTypeAttributeDescription
opts *

Return:

*

public destroy(opts: Object): Object | Boolean source

Deletes an item

Previously checks using findByIndex() if the item exits. If it doesn't, returns false, otherwise returns the deleted item.

opts = {
  tableName: String,
  index: Object,       // Index used to query the item.
}

Params:

NameTypeAttributeDescription
opts Object

Options mapping

Return:

Object | Boolean

A key-value map with the deleted item's fields, false if not found @todo: Handle unprocessedItems

public dissociate(opts: Object): Promise<Boolean> source

Dissociates two items if an association is found

item[0].uuid should be a scalar item[1].uuid can be an array of associations to remove.

opts = {
  tableName: String,
  items: [
    {
      index: {indexName: any}, // index values should be a scalar
    },
    {
      index: {indexName: [any]}, // index values can be an array
    }
  ]
}

Params:

NameTypeAttributeDescription
opts Object

The options object

Return:

Promise<Boolean>

true if the association was removed, false if there is no association

@todo: Handle unprocessed items. (critical)

public findByIndex(opts: object): Promise<array | boolean> source

Queries a table and looks for items matching the specified index.

The index mapping has the following structure:

index = {
  username: 'john'
}

In order to find the table's index, the word 'Index' is appended, resulting in this example in 'usernameIndex'

Every index has a 'createdAt' range key. This is calculated and appended automatically before querying.

opts = {
  tableName: String,
  index: {hash: value, [range: value]},
  ascending: Boolean
}

Params:

NameTypeAttributeDescription
opts object

Options mapping.

Return:

Promise<array | boolean>

Array when there are multiple matches, false when not found

public first(opts: {}): * source

Params:

NameTypeAttributeDescription
opts {}
  • optional
  • default: {}

Return:

*

public getAssociations(opts: *): Promise source

Returns the associations of the specified item with respect to another item

 opts = {
   tableName: String,
   items: [
     {
       index: {key: value}
     },
     {
       index: String
     }
   ]
 }

Params:

NameTypeAttributeDescription
opts *

Return:

Promise

Array of uuids, [] if no assoc. found

TODO:

  • accept query expressions

public increaseCounter(opts: *): * source

Params:

NameTypeAttributeDescription
opts *

Return:

*

public last(opts: {}): * source

Params:

NameTypeAttributeDescription
opts {}
  • optional
  • default: {}

Return:

*

public take(opts: {}): * source

Params:

NameTypeAttributeDescription
opts {}
  • optional
  • default: {}

Return:

*

public update(opts: Object): Object | Boolean source

Updates an item

Previously checks using findByIndex() if the item exits. If it doesn't, returns false.

opts = {
  tableName: String,
  index: Object,       // Index used to query the item.
  args: Object,        // Key-Value mapping of the arguments to change.
  beforeHook: (attrName, args) Function // Callback function for every field.
                                           Used to modify the key-value mapping of the fields to
                                           change before the item is updated. Must return object of
                                           shape {fieldName: value}
                                           Attribute names may be also modified.
}

Params:

NameTypeAttributeDescription
opts Object

Options mapping

Return:

Object | Boolean

A key-value map with the updated fields or false if not found. @todo: Handle unprocessedItems

public updateCounter(opts: *): * source

Params:

NameTypeAttributeDescription
opts *

Return:

*