0.0.11 • Published 8 months ago

@1mill/with-iota v0.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

with-iota

Getting started

  1. Run npm install @1mill/with-iota
  2. Configure your AWS Lambda handler

    import { CREATE, DELETE, INCREMENT, SET, withIota } from '@1mill/with-iota'
    
    const func = async ({ cloudevent, ctx, data, mutation, rapids }) => {
      const { companyId, name } = data
    
      if (!companyId) { throw new Error('companyId is required') }
      if (!name) { throw new Error('name is required') }
    
      // * Create some record
      const { id } = mutation.stage({
        action: CREATE,
        props: { companyId, name, comment: 'Hello world!' },
        type: 'myRecords',
      })
    
      // * Enqueue cloudevent that will be emitted after all staged mutations
      // * are comitted.
      rapids.stage({
        data: { id },
        type: 'fct.my-record-created.v0',
      })
    
      // * Increment some value on a different record
      mutation.stage({
        action: INCREMENT,
        id: companyId,
        props: { recordsCount: 1 },
        type: 'companies',
      })
    
      // * Set a value on the record we just created
      mutation.stage({
        action: SET,
        id,
        props: { comment: 'Update this comment!' },
        type: 'myRecords'
      })
    
      // * Selete the record we just created
      mutation.stage({
        action: DELETE,
        id,
        type: 'myRecords',
      })
    
      // * Enqueue another cloudevent that will be emitted after all
      // * staged mutations are committed.
      rapids.stage({
        data: { id },
        type: 'fct.my-record-deleted.v0',
      })
    
      // * Return some value for calls with InvocationType set to RequestResponse.
      return cloudevent.id
    }
    
    export const handler = async (event, ctx) => await withIota(event, ctx, { func })

Usage

Setup

If you ever change any of the values below once they are set, be sure to migrate any saved journal entries to the new settings. If you do not do this, then already processed cloudevents may run again.

EnvironmentRequiredTypesDefaultDescription
MILL_IOTA_AWS_ACCESS_KEY_IDyesstringAWS Access Key with permissions to AWS EventBridge.
MILL_IOTA_AWS_ENDPOINTstringSet by AWS
MILL_IOTA_AWS_REGIONyesstringValid AWS Region.
MILL_IOTA_AWS_SECRET_ACCESS_KEYyesstringAWS Secret Key with permissions to AWS EventBridge.
MILL_IOTA_AWS_SESSION_TOKENstringSet by AWSAWS Session Token with permissions to AWS EventBridge.
MILL_IOTA_EVENTBUS_NAMEstringdefaultName of the AWS EventBridge.
MILL_IOTA_JOURNAL_NAMEstringiotaJournalEntriesName of the MongoDB collection.
MILL_IOTA_MONGO_DByesstringName of the MongoDB Database.
MILL_IOTA_MONGO_URIyesstringURI of the MongoDB Cluster.
MILL_IOTA_SERVICE_IDyesstringUnique name of the service / application itself.

During the AWS Lambda runtime, AWS automatically provides all the MILL_IOTA_AWS_ credentials.

Func

PropertyTypeDescription
cloudeventobjectThe cloudevent payload from event.
countDocuments(name, args)functionAlias for db.collection(name).countDocuments(args) using mongo.
ctxobjectAWS Lambda context.
dataanyThe cloudevent.data. If cloudevent.datacontenttype is application/json, then the JSON parsed data will be returned.
distinct(name, args)functionAlias for db.collection(name).distinct(args) using mongo.
eventobjectThe event that invoked the AWS Lambda function.
find(name, args)functionAlias for db.collection(name).find(args) using mongo.
findOne(name, args)functionAlias for db.collection(name).findOne(args) using mongo.
mutation.stage(args)functionStage a mutation to be committed after return.
rapids.stage(args)functionStage a cloudevent to be sent to rapids after mutations are applied.

Lifecycle

Despite the straggered nature of staged mutations and staged rapids cloudevents, all mutations are applied first in order. Then, all rapids cloudevents are emitted in order.

LifecycleCommit order
Image communicating lifecycleImage communicating that all mutations are applied first followed by all rapids cloudevents

Development

  1. Run npm install
  2. Create .env file with with MILL_IOTA_AWS_ACCESS_KEY_ID, MILL_IOTA_AWS_REGION, and MILL_IOTA_AWS_SECRET_ACCESS_KEY.
  3. Run docker compose up -d mongo mongo-admin
  4. Run docker compose up test

Deploy

  1. Run npm version (major|minor|patch)
  2. Run git push
  3. Run npm run deploy
0.0.11

8 months ago

0.0.10

8 months ago

0.0.9

8 months ago

0.0.8

8 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago