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

PyropeModel

PyropeModel ORM

Constructor Summary

Public Constructor
public

constructor(schema: GraphQLObjectType, opts: object)

PyropeModel#constructor

Member Summary

Public Members
public

actions: *

public
public

fields: *

public
public
public

name: *

public
public
public
public

Method Summary

Public Methods
public

Retrieves the record count given the Model's table

public

create(fields: object, opts: object): Promise<object>

Create a new record

public

destroy(index: object, opts: object): Promise<object | boolean>

Destroys a record

public

get(index: object): Promise<object | boolean>

Retrieve a record given an index

public

Retrieve all records

public

getChild(uuid: any, childIndexKey: string, childResolver: function(source: object): any): Promise<object | null>

Get a child (1:1)

public

getChildren(uuid: any, childIndexKey: string, childResolver: function(source: object): any): Promise<array>

Gets the associated children (1:N, N:N)

public

setChild(uuid: string, childIndex: object): Promise<boolean>

Associates a child (1:1)

public

setChildren(uuid: string, childIndex: object): Promise<boolean>

Associates the specified children (1:N, N:N)

public

unsetChild(uuid: any, childIndexKey: string): Promise<boolean>

Dissociates a child (1:1)

public

unsetChildren(uuid: any, childIndex: object): Promise<boolean>

Dissociates children (1:N, N:N)

public

update(index: object, fields: object, opts: object): Promise<object | boolean>

Update a record

Public Constructors

public constructor(schema: GraphQLObjectType, opts: object) source

PyropeModel#constructor

Generates a new PyropeModel object. Useful for interacting with DynamoDB with ease.

Params:

NameTypeAttributeDescription
schema GraphQLObjectType

The GraphQL Object Type.

opts object

The options object.

opts.name string

Custom name (used to calculate table names).

opts.humanName string

Custom name for errors.

opts.tablePrefix string

Custom table to query from.

opts.tableName string

Custom table to query from.

opts.tableSuffix string

Custom table to query from.

opts.validations function(fields: object, fieldName: string): object

Validation function, should resolve the [modified] fields.

Public Members

public actions: * source

public defaultQueryKey: string source

public fields: * source

public fullTableName: * source

public humanName: * source

public name: * source

public tableName: * source

public tablePrefix: * source

public tableSuffix: * source

public validations: * source

Public Methods

public count(): Promise<number> source

Retrieves the record count given the Model's table

Return:

Promise<number>

The record count.

public create(fields: object, opts: object): Promise<object> source

Create a new record

(!) It does not check if a record with the provided index previously exists.

Params:

NameTypeAttributeDescription
fields object

Fields to the new record.

opts object

Options object.

opts.beforeValidation function(fields: object, fieldName: string): object

beforeValidation hook, should return the [modified] fields.

opts.afterValidation function(fields: object, fieldName: string): object

afterValidation hook, should return the [modified] fields.

opts.beforeCreate function(fields: object, fieldName: string): object

beforeCreate hook, should return the [modified] fields.

opts.afterCreate function(fields: object, fieldName: string): object

afterCreate hook, should return the [modified] fields.

opts.fieldName string

fieldName. Passed by the resolver function from GraphQL. ie. the name of the Query or Mutation.

Return:

Promise<object>

A promise that resolves to the newly created record.

public destroy(index: object, opts: object): Promise<object | boolean> source

Destroys a record

If the schema (GraphQLObjectTYpe) specifies fields with dependent = 'destroy' || 'nullify' the associated records will be either destroyed or dissociated (nullified)

Params:

NameTypeAttributeDescription
index object

The index used to query the record.

index.key any

The index key used to query the record.

index.value any

The index value used to query the record.

opts object

Options object.

opts.beforeValidation function(fields: object, fieldName: string): object

beforeValidation hook, should return the [modified] fields.

opts.afterValidation function(fields: object, fieldName: string): object

afterValidation hook, should return the [modified] fields.

opts.beforeDestroy function(fields: object, fieldName: string): object

beforeDestroy hook, should return the [modified] fields.

opts.afterDestroy function(fields: object, fieldName: string): object

afterDestroy hook, should return the [modified] fields.

opts.fieldName string

fieldName. Passed by the resolver function from GraphQL. ie. the name of the Query or Mutation.

Return:

Promise<object | boolean>

The object of the destroyed record. False if the record was not found.

public get(index: object): Promise<object | boolean> source

Retrieve a record given an index

Params:

NameTypeAttributeDescription
index object

The index used to query the record.

index.key any

The index key used to query the record.

index.value any

The index value used to query the record.

Return:

Promise<object | boolean>

The retrieved record map. False if record is not found.

public getAll(opts: object): Promise<Array<object>> source

Retrieve all records

Params:

NameTypeAttributeDescription
opts object

Options object.

opts.order string

Sort by 'asc' or 'desc'

opts.limit number

The amount of records to retrieve.

opts.cursor string

The cursor from which the query continues.

Return:

Promise<Array<object>>

An array with the maps of the retrieved objects. Empty array if none is found.

public getChild(uuid: any, childIndexKey: string, childResolver: function(source: object): any): Promise<object | null> source

Get a child (1:1)

Params:

NameTypeAttributeDescription
uuid any

The index value to query the parent.

childIndexKey string

The index key used to query the child.

childResolver function(source: object): any

Resolver function.

Return:

Promise<object | null>

An object with the record's fields, null if not found.

public getChildren(uuid: any, childIndexKey: string, childResolver: function(source: object): any): Promise<array> source

Gets the associated children (1:N, N:N)

Params:

NameTypeAttributeDescription
uuid any

The index value to query the parent item.

childIndexKey string

The index key used to query the children.

childResolver function(source: object): any

Resolver function.

Return:

Promise<array>

An array with the associated records.

public setChild(uuid: string, childIndex: object): Promise<boolean> source

Associates a child (1:1)

Params:

NameTypeAttributeDescription
uuid string

The index value to query the parent.

childIndex object

The index of the child to associate.

childIndex.key string

The index key of the child to associate.

childIndex.value string

The index value of the child to associate.

Return:

Promise<boolean>

True if the association was made, false otherwise.

public setChildren(uuid: string, childIndex: object): Promise<boolean> source

Associates the specified children (1:N, N:N)

Params:

NameTypeAttributeDescription
uuid string

The index value to query the parent item.

childIndex object

The index of the children to associate.

childIndex.key string

The index key of the children to associate.

childIndex.value string[] | string

The index value[s] of the children to associate. Can be an array of uuids.

Return:

Promise<boolean>

True if the association was made, false otherwise.

public unsetChild(uuid: any, childIndexKey: string): Promise<boolean> source

Dissociates a child (1:1)

Params:

NameTypeAttributeDescription
uuid any

The index value to query the parent.

childIndexKey string

The index key used to query the child.

Return:

Promise<boolean>

True if successful, false otherwise.

public unsetChildren(uuid: any, childIndex: object): Promise<boolean> source

Dissociates children (1:N, N:N)

Params:

NameTypeAttributeDescription
uuid any

The index value to query the parent item.

childIndex object

The index used to query the children to dissociate. If empty is passed, all children are dissociated.

childIndex.key string

The index key used to query the children to dissociate. If empty is passed, all children are dissociated.

childIndex.value <array<string>> | string | empty

The index value used to query the children to dissociate. If empty is passed, all children are dissociated.

Return:

Promise<boolean>

True if successful, false otherwise.

public update(index: object, fields: object, opts: object): Promise<object | boolean> source

Update a record

To set or unset child/children, pass in the fields the setContact/setContacts/unsetContact/unsetContacts argument.

It will look in the schema for fields that are a scalar Object Type or GraphQLList with the given name.

You should use these arguments in your GraphQL query.

  • setRecord accepts a scalar value of the uuid and calls this.setChild()
  • setRecords accepts an array of uuids and calls this.setChildren()
  • unsetRecord accepts an empty string and calls this.unsetChild()
  • unsetRecords accepts an array and null for the uuid and calls this.unsetChildren()
  • (!) When empty is passed, all the records are dissociated.

Record being the name of the associated field.

Params:

NameTypeAttributeDescription
index object

The index used to query the record.

index.key any

The index key used to query the record.

index.value any

The index value used to query the record.

fields object

The map to the fields to update.

opts object

Options object.

opts.beforeValidation function(fields: object, fieldName: string): object

beforeValidation hook, should return the [modified] fields.

opts.afterValidation function(fields: object, fieldName: string): object

afterValidation hook, should return the [modified] fields.

opts.beforeUpdate function(fields: object, fieldName: string): object

beforeUpdate hook, should return the [modified] fields.

opts.afterUpdate function(fields: object, fieldName: string): object

afterUpdate hook, should return the [modified] fields.

opts.fieldName string

fieldName. Passed by the resolver function from GraphQL. ie. the name of the Query or Mutation.

Return:

Promise<object | boolean>

A promise tha resolves to the updated record fields. Promise resolves to false if the record does not exist.

Example:


 mutation {
   updateUser(
     uuid: "123"
     setContact: "contact-uuid"
   ) {
     uuid
     contact {
       uuid
     }
   }
 }

 mutation {
   updateUser(
     uuid: "123"
     setContacts: ["contact-uuid-1", "contact-uuid-2"]
   ) {
     uuid
     contact {
       uuid
     }
   }
 }

 mutation {
   updateUser(
     uuid: "123"
     unsetContact: ""
   ) {
     uuid
     contact {
       uuid
     }
   }
 }

 mutation {
   updateUser(
     uuid: "123"
     unsetContacts: ["contact-uuid-1", "contact-uuid-2"]
   ) {
     uuid
     contact {
       uuid
     }
   }
 }

 mutation {
   updateUser(
     uuid: "123"
     unsetContacts: ""
   ) {
     uuid
     contact {
       uuid
     }
   }
 }