npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynamodb-eloquent

v1.0.21

Published

Dynamodb eloquent ORM

Downloads

109

Readme

DynamoDB Eloquent

DynamoDB Eloquent is an ORM that can run in NodeJS, and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8).

Features

Quick Start

npm i dynamodb-eloquent

Config AWS credentials

Using environment variables

# .env
DDB_ENDPOINT=
AWS_REGION=

## way 1: using aws profile
AWS_PROFILE=
## way 2: using aws access key
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=

Dynamically set credentials

ddbRepo.setConfig({
  accessKeyId: 'AKIAxxx',
  secretAccessKey: 'xxxxxx',
  region: 'ap-northeast-1',
});
ddbRepo.setConfig({
  profile: 'your-aws-profile',
  region: 'ap-northeast-1',
});

Usage

Basic query

import { DynamoDBRepository } from "dynamodb-eloquent";

export class UserRepository extends DynamoDBRepository {
  protected table = `Users`;

  // Please put all indexes you have here
  protected mappingIndex = {
    email: "email",
  };
}

const userRepository = new UserRepository();

// Create
await userRepository.create({
  id: 1,
  name: "Bill",
});
// Update
await userRepository.update({
  id: 1,
  name: "John",
});
// List
await userRepository.findAll();
// Show
await userRepository.findOrFail(userId);
// Delete
await userRepository.delete(userId);

// Delete
await userRepository.delete(userId);

Filter

findBy

findBy must use with index

// Filter
const params = { email: "[email protected]" };
const indexName = "email";
const users = await userRepository.findBy(params, indexName);

findBy and Sorting

Create the following index:

{
  IndexName: 'status',
  KeySchema: [
    { AttributeName: 'status', KeyType: 'HASH' },
    { AttributeName: 'orderBy', KeyType: 'RANGE' },
  ],
  Projection: { ProjectionType: 'ALL' },
},
const indexName = 'status'
const params = { status: 'COMPLETED' }

// Sorting ASC
const users = await userRepository.findBy(params, indexName, { sortDirection: 'asc' });

// Sorting DESC
const users = await userRepository.findBy(params, indexName, { sortDirection: 'desc' });

findOneBy

findOneBy must includes the index

const params = { email: "[email protected]" };
const indexName = "email";
const user = await userRepository.findOneBy(params, indexName);
console.log(user.id)

ScanData

const where = {
  category: 'Clothes',
  // additional conditions
};
const page1 = await postRepository.scanDataV2({ filter: where });
const page2 = await postRepository.scanDataV2({ filter: where, nextKey: page1.nextKey });

paginate

const nextKey = undefined; // put nextKey here
const limit = 10;
const params = {
  limit,
  nextKey,
  scanIndexForward: false,
};
const posts = await postRepository.paginate(params);
// result
{
  data: [
    { id: 'aa03e3a0-a9c8-439f-9b22-362ea59fc0ec', email: '[email protected]'},
    { id: '527a850f-0bfc-4da5-84d3-8a03c451e868', email: '[email protected]'},
    ...
    { id: '527a850f-0bfc-4da5-84d3-8a03c451e868', email: '[email protected]'},
  ],
  pagination: {
    nextKey: 'eyJpZCI6ImFlMWRmYTRjLTM1NTUtNDM0ZC05NWU1LWFkZWVhMjllZDE1OCJ9',
    count: 10,
    perPage: 10
  }
}

paginateV2

const page1 = await postRepository.paginateV2({ limit: 10 });
const page2 = await postRepository.paginateV2({ limit: 10, nextKey: page1.pagination.nextKey });

const params = {
  limit,
  scanIndexForward: false,
  where: {
    shortDescription: `shortDescription7`,
  },
};
const posts = await postRepository.paginateV2(params);

Migrations

Update package.json

"scripts": {
  "migration:create": "dynamodb_eloquent migration:create",
  "migration:run": "dynamodb_eloquent migration:run",
  "migration:revert": "dynamodb_eloquent migration:revert",
}

Run commands

# For local
# export AWS_REGION="ap-northeast-1"
# export DDB_ENDPOINT="http://localhost:8000"

# Generate new migration
yarn migration:create --table Posts

# Run migrations
yarn migration:run
# Or yarn migration:run --tableSuffix test


# revert migrations
yarn migration:revert
# or yarn migration:revert --tableSuffix test