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 |
humanName: * |
|
public |
name: * |
|
public |
tableName: * |
|
public |
tablePrefix: * |
|
public |
tableSuffix: * |
|
public |
validations: * |
Method Summary
Public Methods | ||
public |
Retrieves the record count given the Model's table |
|
public |
Create a new record |
|
public |
Destroys a record |
|
public |
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 |
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 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:
Name | Type | Attribute | Description |
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 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 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:
Name | Type | Attribute | Description |
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. |
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:
Name | Type | Attribute | Description |
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:
Name | Type | Attribute | Description |
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 getChild(uuid: any, childIndexKey: string, childResolver: function(source: object): any): Promise<object | null> source
Get a child (1:1)
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)
public setChildren(uuid: string, childIndex: object): Promise<boolean> source
Associates the specified children (1:N, N:N)
Params:
Name | Type | Attribute | Description |
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. |
public unsetChild(uuid: any, childIndexKey: string): Promise<boolean> source
Dissociates a child (1:1)
Params:
Name | Type | Attribute | Description |
uuid | any | The index value to query the parent. |
|
childIndexKey | string | The index key used to query the child. |
public unsetChildren(uuid: any, childIndex: object): Promise<boolean> source
Dissociates children (1:N, N:N)
Params:
Name | Type | Attribute | Description |
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. |
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:
Name | Type | Attribute | Description |
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
}
}
}