Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
c67da6a
Remove dynamo max get log
May 5, 2016
c1f07a9
Merge pull request #1 from Adslot/remove-dynamo-max-get-log
May 5, 2016
540dd06
Implement adapter to promisify methods
lteacher Nov 8, 2017
2a9e9e8
Merge pull request #2 from Adslot/es6-and-promise-support
vinteo Nov 14, 2017
2c5f75e
chore: ci fixes
bchew Sep 30, 2019
068aaae
Merge pull request #3 from Adslot/update-travis-ci
xiaofan2406 Oct 1, 2019
c461255
chore: update git repository in package.json
bchew Jul 21, 2020
4eff468
Merge pull request #4 from Adslot/update-package-json
vinteo Jul 21, 2020
285f4b1
chore: add release related scripts
bchew Jul 21, 2020
93a02fe
Merge pull request #5 from Adslot/release-scripts
xiaofan2406 Jul 21, 2020
2b67ef0
build: release patch version 0.2.1
adslotbuildagent Jul 21, 2020
b065668
Merge pull request #6 from Adslot/auto-release-20200721-064645
adslotapprovalagent Jul 21, 2020
b552f1c
build: release major version 1.0.0
adslotbuildagent Jul 21, 2020
df0731e
Merge pull request #7 from Adslot/auto-release-20200721-233327
adslotapprovalagent Jul 21, 2020
7eff47b
chore: add npm package image status
bchew Oct 1, 2020
a8f2d45
Merge pull request #8 from Adslot/update-readme
adityasabnis Oct 1, 2020
4bab541
ci: migrate to github actions
bchew Nov 6, 2020
91eea30
Merge pull request #9 from Adslot/github-actions
adityasabnis Nov 6, 2020
d7aa57b
ci: drop node12 from ci
bchew Nov 25, 2020
c9c5192
Merge pull request #10 from Adslot/bchew-patch-1
xiaofan2406 Nov 25, 2020
2bd64b8
ci: update actions ci to use setup-node@v2
bchew Sep 30, 2021
75af9b9
Merge pull request #11 from Adslot/actions-ci
xiaofan2406 Sep 30, 2021
e6dd30b
chore: enable dependabot for version updates
bchew Oct 1, 2021
5c070fd
Merge pull request #12 from Adslot/dependabot
xiaofan2406 Oct 1, 2021
1808567
build(deps-dev): bump mocha from 6.2.1 to 9.1.2
dependabot[bot] Oct 1, 2021
cd877fd
Merge pull request #13 from Adslot/dependabot/npm_and_yarn/mocha-9.1.2
bchew Oct 1, 2021
e995f31
build(deps): bump async from 2.6.0 to 3.2.1
dependabot[bot] Oct 1, 2021
f1cf4f1
Merge pull request #14 from Adslot/dependabot/npm_and_yarn/async-3.2.1
bchew Oct 4, 2021
9c35ae6
ci: add dependabot reviewers list
pphminions Oct 11, 2021
39fe181
Merge pull request #15 from Adslot/dependabot-reviewers
bchew Oct 11, 2021
71cbbdf
build(deps-dev): bump mocha from 9.1.2 to 9.1.3
dependabot[bot] Oct 18, 2021
a264478
Merge pull request #16 from Adslot/dependabot/npm_and_yarn/mocha-9.1.3
bchew Oct 19, 2021
b45c82a
build(deps): bump async from 3.2.1 to 3.2.2
dependabot[bot] Nov 1, 2021
01c3212
Merge pull request #17 from Adslot/dependabot/npm_and_yarn/async-3.2.2
bchew Nov 1, 2021
f63441d
build: release patch version 1.0.1
adslotbuildagent Nov 1, 2021
2092650
Merge pull request #18 from Adslot/auto-release-20211101-021355
adslotapprovalagent Nov 1, 2021
2c88508
chore: update dependabot schedule interval to monthly
bchew Nov 3, 2021
e8882d3
Merge pull request #19 from Adslot/bchew-patch-1
xiaofan2406 Nov 3, 2021
39fd717
chore: upgrade nodejs to 16
bchew Nov 4, 2021
d332fc4
chore: upgrade lockfile to version 2
bchew Nov 4, 2021
24388be
Merge pull request #20 from Adslot/node16
xiaofan2406 Nov 4, 2021
6874762
build: release minor version 1.1.0
adslotbuildagent Nov 4, 2021
3948ad3
Merge pull request #21 from Adslot/auto-release-20211104-033710
adslotapprovalagent Nov 4, 2021
af966c9
chore(deps): add renovate.json
renovate-bot Nov 15, 2021
e8aeb5b
chore: update renovate config to adslot preset and drop dependabot
bchew Nov 15, 2021
4f07402
Merge pull request #22 from Adslot/renovate/configure
bchew Nov 15, 2021
9b8a8f4
fix(deps): update dependency async to ^3.2.2
renovate-bot Nov 15, 2021
dd6f379
Merge pull request #25 from Adslot/renovate-async-3.x
bchew Nov 15, 2021
79eb07f
chore(deps): update dependency mocha to ^9.1.3
renovate-bot Nov 15, 2021
71d3283
Merge pull request #24 from Adslot/renovate-mocha-9.x
bchew Nov 15, 2021
852febc
fix(deps): update dependency async to ^3.2.4
renovate[bot] Jun 29, 2022
358a6d0
chore(deps): update actions/checkout action to v3
renovate[bot] Jun 29, 2022
0aff9cd
chore(deps): update actions/setup-node action to v3
renovate[bot] Jun 29, 2022
1351716
chore(deps): update dependency mocha to v10
renovate[bot] Jun 29, 2022
a1dbc8c
Merge pull request #26 from Adslot/renovate-async-3.x
bchew Jun 29, 2022
48221a9
Merge pull request #29 from Adslot/renovate-mocha-10.x
bchew Jun 29, 2022
6972978
Merge pull request #27 from Adslot/renovate-actions-checkout-3.x
bchew Jun 29, 2022
7822767
Merge pull request #28 from Adslot/renovate-actions-setup-node-3.x
bchew Jun 29, 2022
c2c273e
ci: add codeql action workflow
bchew Aug 9, 2022
e6539c7
Merge pull request #30 from Adslot/codeql
bchew Aug 9, 2022
bce5536
ci: add dependency review github action
bchew Oct 2, 2022
20be1fc
Merge pull request #31 from Adslot/dependency-review
chaofan232 Oct 2, 2022
bc3a9ba
chore: upgrade node to 18
bchew Oct 26, 2022
2e30e55
chore: npm audit fix
bchew Oct 26, 2022
d100398
Merge pull request #32 from Adslot/node18
xiaofan2406 Oct 27, 2022
2a921ed
build: release minor version 1.2.0
adslotbuildagent Oct 27, 2022
842eb37
Merge pull request #33 from Adslot/auto-release-20221027-003601
adslotapprovalagent Oct 27, 2022
580cb08
chore(deps): update dependency mocha to ^10.1.0
renovate[bot] Dec 8, 2022
520f179
chore(deps): update actions/dependency-review-action action to v3
renovate[bot] Dec 8, 2022
11142e6
Merge pull request #35 from Adslot/renovate-actions-dependency-review…
bchew Dec 8, 2022
5d9570b
Merge pull request #34 from Adslot/renovate-mocha-10.x
bchew Dec 8, 2022
d017433
chore(deps): update dependency mocha to ^10.2.0
renovate[bot] Apr 9, 2023
0833de7
Merge pull request #36 from Adslot/renovate-mocha-10.x
bchew Apr 9, 2023
709f62c
chore: add db transaction
glenkurniawan-adslot Nov 4, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ "master" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master" ]
schedule:
- cron: '39 13 * * 3'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'javascript' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
14 changes: 14 additions & 0 deletions .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: 'Dependency Review'
on: [pull_request]

permissions:
contents: read

jobs:
dependency-review:
runs-on: ubuntu-latest
steps:
- name: 'Checkout Repository'
uses: actions/checkout@v3
- name: 'Dependency Review'
uses: actions/dependency-review-action@v3
23 changes: 23 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Node.js CI

on:
push:
branches: [master]
pull_request:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node: [18.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
dynamo-table-extensions
=======================

[![Build Status](https://secure.travis-ci.org/xarvh/dynamo-table-extensions.png?branch=master)](http://travis-ci.org/xarvh/dynamo-table-extensions)
[![npm version](https://badge.fury.io/js/dynamo-table-extensions.svg)](https://badge.fury.io/js/dynamo-table-extensions)
![Build Status](https://github.com/Adslot/dynamo-table-extensions/actions/workflows/node.js.yml/badge.svg)

Adds higher-level methods to [dynamo-table](https://github.com/mhart/dynamo-table).

Expand Down
145 changes: 145 additions & 0 deletions dynamo-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
const {promisify} = require('util');
const {DynamoTable} = require('./slim');

const isCallback = (fn) => typeof fn === 'function';

const validateCall = (methodName, args, minLength) => {
if (args.length < minLength)
throw new Error(`Incorrect args supplied to method ${methodName}: expected: ${minLength} got: ${args.length}`);
}

/**
Provides an adapter for the DynamoTable class to allow for promisified
api usage when omitting the cb parameter.
*/
class DynamoAdapter {
constructor(name, options) {
this._delegate = new DynamoTable(name, options);
this.hooks = options.hooks || {};

// Delegate externally accessed properties
Object.keys(this._delegate).forEach(key => {
this[key] = this._delegate[key];
});

// NOTE: Rebind all methods so they can be passed without the class instance
// Since we only instantiate once per table and maintain the instances,
// the performance should be a big concern
Object.getOwnPropertyNames(DynamoAdapter.prototype).forEach(key => {
if (typeof this[key] === 'function') this[key] = this[key].bind(this);
});

// NOTE: This function needs to be rebound to the delegate for its use case
this['mapToDb'] = this._delegate.mapToDb.bind(this._delegate);
}

// Proxies execution to delegate
_call(methodName, minLength, ...args) {
validateCall(methodName, args, minLength);

const cb = args[args.length - 1];

let method = this._delegate[methodName];

// TODO: No support for async 'hook'
this._runHook(methodName);

if (!isCallback(cb)) method = promisify(method);

return method.apply(this._delegate, args);
}

_runHook(methodName) {
const hook = this.hooks[methodName];

return hook ? hook() : null;
}

// Allows additional hook for execution BEFORE function
addHook(methodName, fn) {
this.hooks[methodName] = fn;
}

// Provides instance copy functionality
copy(options) {
const overrides = Object.assign({}, this, options);

return new DynamoAdapter(this.name, overrides);
}

// Accepts minimum 1 parameter
addNew(...args) {
return this._call('addNew', 1, ...args);
}

// Accepts minimum 0 parameters
count(...args) {
return this._call('count', 0, ...args);
}

// Accepts minimum 1 parameter
get(...args) {
return this._call('get', 1, ...args);
}

// Accepts minimum 1 parameter
put(...args) {
return this._call('put', 1, ...args);
}

// Accept minimum 1 parameter
delete(...args) {
return this._call('delete', 1, ...args);
}

update(...args) {
return this._call('update', 1, ...args);
}

// Accept minimum 1 parameter
query(...args) {
return this._call('query', 1, ...args);
}

// Accept minimum 0 parameters
scan(...args) {
return this._call('scan', 0, ...args);
}

// Accept minimum 1 parameter
batchGet(...args) {
return this._call('batchGet', 1, ...args);
}

// Accept minimum 1 parameter
batchWrite(...args) {
return this._call('batchWrite', 1, ...args);
}

// Accept minimum 0 parameters
createTable(...args) {
return this._call('createTable', 0, ...args);
}

// Accept minimum 0 parameters
describeTable(...args) {
return this._call('describeTable', 0, ...args);
}

// Accept minimum 0 parameters
deleteTable(...args) {
return this._call('deleteTable', 0, ...args);
}

// Accept minimum 2 parameters
throttledBatchWrite(...args) {
return this._call('throttledBatchWrite', 2, ...args);
}

// Accept minimum 0 parameters
truncate(...args) {
return this._call('truncate', 0, ...args);
}
}

module.exports = DynamoAdapter;
62 changes: 6 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,9 @@
var async = require('async')
var dynamoTable = require('./slim')
const {DynamoTable} = require('./slim');
const DynamoAdapter = require('./dynamo-adapter');

module.exports = dynamoTable
// Library is a factory function which also provides the DynamoTable class
const dynamo = (...args) => new DynamoAdapter(...args);

var proto = dynamoTable.DynamoTable.prototype
dynamo.DynamoTable = DynamoTable;

// Ensures that no more than capacityRatio * writeCapacity items are written per second
proto.throttledBatchWrite = function(capacityRatio, items, cb) {
if (!(capacityRatio > 0)) return cb(new Error('non-positive capacityRatio'))

var self = this
self.describeTable(function(err, info) {
if (err) return cb(err)

var itemsPerSecond = Math.ceil(info.ProvisionedThroughput.WriteCapacityUnits * capacityRatio);
var written = 0;
var ready = true;

var waitAndWrite = function(cb) {
async.until(function() {return ready}, function(cb) {setTimeout(cb, 10)}, function(err) {
if (err) return cb(err)
ready = false
setTimeout(function() {ready = true}, 1000)

var write = items.slice(written, written + itemsPerSecond)
self.batchWrite(write, function(err) {
if (err) return cb(err)
written += write.length;
cb();
})
})
}

async.whilst(function() {return written < items.length}, waitAndWrite, cb)
})
}


proto.truncate = function(cb) {
async.series([this.deleteTable.bind(this), this.createTable.bind(this)], cb)
}


proto.addNew = function(record, cb) {
var key = Array.isArray(this.key) ? this.key[0] : this.key;

if (record[key])
return this.put(record, cb);

var self = this;

// this.nextId is added by `dynamo-table-id`, which must be loaded
this.nextId(function(err, id) {
if (err) return cb(err);
record[key] = id;
self.put(record, cb);
});
}
module.exports = dynamo;
Loading