0.0.28 • Published 1 year ago

cyclic-dynamodb v0.0.28

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

cyclic-dynamodb

DEPRECATED This package has moved to:

https://www.npmjs.com/package/@cyclic.sh/dynamodb

NodeJS SDK for interacting with Cyclic.sh app AWS DynamoDB databases.

CI

Together with the Cyclic.sh DynamoDB indexing strategy and data model, the sdk simplifies the DynamoDB interface and enables collection organization of records, queries and data scheme discovery among other features.

The sdk and database feature are in preview - use it with the assumption that the interface and data structures will change

Prerequisites

  • A cyclic app with database enabled
  • For use on local:
    • AWS credentials set in environment (available on an app's database tab)

Getting started

  1. Install
    npm install cyclic-dynamodb
  2. Copy the temporary credentials from the cyclic console and set them in the shell environment where your code will be running.

Credentials are required only for connecting to the database from local and expire after one hour, don't add them to an environment configuration.

  1. Set the database name as an environment variable before requiring the sdk - this can be added to environment configurations.
    process.env.CYCLIC_DB = 'your-url-subdomainCyclicDB'
    const db = require('cyclic-dynamodb')

Example

// example.js
const CyclicDB = require('cyclic-dynamodb')
const db = CyclicDB('your-table-name')

const run = async function(){
    let animals = db.collection('animals')

    // create an item in collection with key "leo"
    let leo = await animals.set('leo', {
        type:'cat',
        color:'orange'
    })

    // get an item at key "leo" from collection animals
    let item = await animals.get('leo')
    console.log(item)
}
run()

Collection Items

{
  "collection": "animals",
  "key": "luna",
  "props": {
    "updated": "2022-03-23T13:02:12.702Z",
    "created": "2022-03-23T12:32:02.526Z",
    "color": "orange",
    "type": "cat"
  },
  "$index": [
    "color"
  ]
}

Fragments

With the cyclic.sh data model, items can have fragments. These can be thought of as children or attachments to items.

Another way to think of fragments is by thinking of an item itself as its own collection of other items that are stored closely together.

An example use case for a user record would be something like:

  • item user: name, last name, id
    • fragment home: address, city
    • fragment work: company name, position, work address

Fragments objects look just like items but give you a way to better organize your data with higher query performance.

Example:

let users = db.collection('users')

await users.item('mike')
        .fragment('work').set({
            company: 'cyclic'
        })

let mikes_work = await users.item('mike').fragment('work').get()

TTL - time to live

You optionally may set a TTL for any item. The ttl is the UNIX seconds timestamp when the item should expire.

The ttl setting passes through to the DynamoDB ttl setting. The expiration is only approximate within a few minutes.

Example

// example.js
const CyclicDB = require('cyclic-dynamodb')
const db = CyclicDB('your-table-name')

const run = async function(){
    let animals = db.collection('animals')

    // create an item in collection with key "leo"
    let leo = await animals.set('leo', {
        type:'cat',
        color:'orange',
        ttl: Math.floor(Date.now() / 1000) + 3
    })

    // get an item at key "leo" from collection animals
    let item = await animals.get('leo')
    console.log(item)

    await new Promise(resolve => setTimeout(resolve, 5000));
    
    item = await animals.get('leo')
    console.log(item)
}
run()
0.0.28

1 year ago

0.0.25

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago