npm package: @projectolive/dbal
Description: Database Abstraction Layer for Project Olive
Latest version: 0.0.23

# [PKG-DBAL] Database Abstraction Layer

## Required Environment Variables

### `CACHE_ADAPTER`

 - `dyanamodb`

### `DB_ADAPTER`

- `dyanamodb`

### `MQ_ADAPTER`

- `sqs` `local` `none`

### `ROOT_DIRECTORY`

- For local development using `local` `MQ_ADAPTER`. Can be omitted for cloud deployment.

### `DYNAMODB_ENDPOINT`

- For local development. Can be omitted for cloud deployment.

### `TABLE_PREFIX`

- Will yield `${TABLE_PREFIX}_graph`

## Model Subclass Structure

```javascript
'use strict'

const Model = require('@projectolive/dbal')

class Entity extends Model {

    constructor() {
        
        super()
    
        this.attributes = {
            [attribute_name] : [
                'identifier',               // Creates index, implies uniqueness
                'foreign',                  // Creates a foreign key (id in another microservice)
                'namespace:field'           // Creates an identifier within a namespace (determined by field)
                'mutable',                  // Makes attribute updateable
                'format:email',
                'format:url',
                'format:slug',
                'format:json',
                'format:mobile',
                'in:value1,value2,value3'
            ]
            ...
        }
        
        this.edges = {
            [label_name]: [
                ordinality,         // 0 or 1
                cardinality         // 1 or N
            ]
            ...
        }

        this.broadcast_on_context = []      // Array of contexts of when to broadcast an update on MQ (defaults: create, update, delete)
    
    }
    
    async beforeCreate(data) {

        data.key = 'value'

    }
    
    async afterCreate(record) {
        
        // Some logic
    
    }
    
    async beforeUpdate(data) {

        data.key = 'value'

    }
    
    async afterUpdate(data) {
        
        // Some logic
    
    }

}

module.exports = Entity

```


## Model Class Functions

```javascript
async getAll()
async getByIds(ids)
async getById(id)
async getByIdOrThrow(id)
async getByIdentifier(identifier, value)
async getByIdentifierWithinNamespace(identifier, value, namespace)
async getByForeignKey(foreign_key, value)
async getEdge(id, edge)
async create(data = {}, context = 'create', contextVars = {})
async linkEdge(id, edge_label, edge_id, data = {})
async unlinkEdge(id, edge_label, edge_id)
async updateById(id, data = {}, context = 'update', contextVars = {})
async deleteById(id, context = 'delete', contextVars = {})
```

## Model Class Hooks

```javascript
async beforeCreate(data)
async afterCreate(record)
async beforeUpdate(data)
async afterUpdate(record)
```

## Attribute Formats

- `email`
- `url`
- `slug`
- `json`
- `mobile`

## Error Codes

- `MS1-MOD001: Invalid edge (:edge)`
- `MS1-MOD002: Missing edge (:edge)`
- `MS1-MOD003: Record not found (:label-:id)`
- `MS1-MOD004: Validation error`
- `MS1-MOD005: Cannot link more than :cardinality edges on :node-:edge`
- `MS1-MOD006: Cannot link less than :ordinality edges on :node-:edge`