Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

serverless-seed-plugin

Package Overview
Dependencies
Maintainers
7
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serverless-seed-plugin - npm Package Compare versions

Comparing version 0.1.0 to 0.3.0

22

CHANGELOG.md

@@ -5,2 +5,24 @@ # Changelog

## [0.3.0](https://github.com/purple-technology/serverless-seed-plugin/compare/v0.1.0...v0.3.0) (2022-01-11)
### ⚠ BREAKING CHANGES
* node 14
* use standard-version as release standard
### Features
* **cognito:** support for groups ([8bbfa66](https://github.com/purple-technology/serverless-seed-plugin/commit/8bbfa66385719201084ef49d0cd7314d7701dadd))
* **dynamodb:** support for table cloning and Jest upgrade ([e1710ee](https://github.com/purple-technology/serverless-seed-plugin/commit/e1710ee3fcdf360ebf6cf94ce8a5ab42fc196d81))
### Bug Fixes
* **dynamodb:** fixed missing required region for default config ([e95098a](https://github.com/purple-technology/serverless-seed-plugin/commit/e95098a7e4ff6d0e1da49a0e0c65dcf6cebf9043))
* node 14 ([e0d08cb](https://github.com/purple-technology/serverless-seed-plugin/commit/e0d08cb659bf7c3fd67e8476b4dba90ddeae9539))
* use standard-version as release standard ([fe23c2f](https://github.com/purple-technology/serverless-seed-plugin/commit/fe23c2fa72342d0f6aedc31752b84ad5e61d4c38))
## 0.1.0 (2021-06-24)

@@ -7,0 +29,0 @@

9

package.json
{
"name": "serverless-seed-plugin",
"version": "0.1.0",
"version": "0.3.0",
"engines": {

@@ -13,3 +13,3 @@ "node": ">=14"

"test": "jest",
"test:coverage": "jest --coverage --collectCoverageFrom=src/**/*.js test",
"test:coverage": "npm run test -- --coverage --collectCoverageFrom=src/**/*.js test",
"lint": "eslint ./src/**/*.js",

@@ -37,3 +37,3 @@ "lint:fix": "npm run lint -- --fix",

"husky": "^6.0.0",
"jest": "^27.0.6",
"jest": "^27.4.5",
"lint-staged": "^11.0.0",

@@ -49,4 +49,5 @@ "prettier": "^2.3.2",

"dependencies": {
"bluebird": "^3.7.2"
"bluebird": "^3.7.2",
"copy-dynamodb-table": "^2.2.1"
}
}

@@ -41,6 +41,15 @@ # serverless-seed-plugin

TableId:
- id: 'abc1'
name: 'myRecordName1'
- id: 'abc2'
name: 'myRecordName2'
truncate: true # default false
clone:
table: source-table-name # Clone data from table
recreate: true # default false
config: # empty by default, for more options see https://github.com/enGMzizo/copy-dynamodb-table#aws-config-for-each-table--cross-region--
accessKeyId: AKID
secretAccessKey: SECRET
region: eu-west-1
data:
- id: 'abc1'
name: 'myRecordName1'
- id: 'abc2'
name: 'myRecordName2'
```

@@ -47,0 +56,0 @@

@@ -40,3 +40,3 @@ 'use strict'

}
for (const { username, password, attributes } of users) {
for (const { username, password, attributes, groups } of users) {
try {

@@ -74,2 +74,3 @@ await this.identityProvider

}
await this.identityProvider

@@ -83,2 +84,12 @@ .adminSetUserPassword({

.promise()
for (const group of groups || []) {
await this.identityProvider
.adminAddUserToGroup({
GroupName: group,
UserPoolId: userPoolId,
Username: username
})
.promise()
}
}

@@ -85,0 +96,0 @@

'use strict'
const { copy } = require('copy-dynamodb-table')
const DYNAMODB_BATCH_LIMIT = 25
const getPutRequest = (item) => ({
PutRequest: {
Item: item
}
})
const getDeleteRequest = (item) => ({
DeleteRequest: {
Key: {
id: item.id
}
}
})
class DynamoDbResource {

@@ -18,9 +36,15 @@ constructor({ options, log, serverless }) {

this.region = provider.getRegion()
const credentials = {
...provider.getCredentials(),
region: provider.getRegion()
region: this.region
}
this.dynamodb = new provider.sdk.DynamoDB.DocumentClient({
service: new provider.sdk.DynamoDB(credentials)
const { DynamoDB } = provider.sdk
this.dynamoDb = new DynamoDB(credentials)
this.documentClient = new DynamoDB.DocumentClient({
service: this.dynamoDb
})

@@ -32,28 +56,123 @@ }

for (const [tableId, data] of Object.entries(this.options)) {
for (const [tableId, options] of Object.entries(this.options)) {
const { data, clone, truncate } = options
const tableName = this.tables[tableId]
if (!data.length) continue
this.log(`Table '${tableName}' - writing ${data.length} items..`)
if (!tableName) {
throw new Error(`Table name not found for table id ${tableId}`)
}
let recordGroup = data.splice(0, 25)
while (recordGroup.length > 0) {
await this.dynamodb
.batchWrite({
RequestItems: {
[tableName]: recordGroup.map((record) => ({
PutRequest: {
Item: record
}
}))
}
if (typeof data !== 'object' && !clone) {
this.log(`Option data or clone is required for table ${tableName}`)
continue
}
if (truncate && (!clone || !clone.recreate)) {
this.log(`Table '${tableName}' - truncate table..`)
const items = await this._getAllItems(tableName)
await this._batchWrite(tableName, items, getDeleteRequest)
}
if (clone) {
await this._clone(tableName, clone)
}
if (data) {
this.log(`Table '${tableName}' - writing ${data.length} items..`)
await this._batchWrite(tableName, data, getPutRequest)
}
this.log(`Table '${tableName}' - finished!`)
}
}
async _clone(targetTable, options) {
const { table, recreate, config } = options
try {
await this.dynamoDb
.describeTable({
TableName: table
})
.promise()
} catch (err) {
if (err.code === 'ResourceNotFoundException') {
throw new Error(`Cloned table '${table}' not found`)
}
throw err
}
if (recreate) {
this.log(`Table '${targetTable}' - delete table..`)
try {
await this.dynamoDb
.describeTable({
TableName: targetTable
})
.promise()
recordGroup = data.splice(0, 25)
} catch (err) {
if (err.code !== 'ResourceNotFoundException') {
throw err
}
}
}
this.log(`Table '${tableName}' - finished!`)
this.log(`Table '${targetTable}' - clone from table ${table}..`)
await this._cloneTable({
config: {
region: this.region
},
source: {
tableName: table,
config: config || {
region: this.region
}
},
destination: {
tableName: targetTable
},
create: recreate
})
}
async _batchWrite(tableName, data, mapCb) {
let recordGroup = data.splice(0, DYNAMODB_BATCH_LIMIT)
while (recordGroup.length > 0) {
await this.documentClient
.batchWrite({
RequestItems: {
[tableName]: recordGroup.map((record) => mapCb(record))
}
})
.promise()
recordGroup = data.splice(0, DYNAMODB_BATCH_LIMIT)
}
}
async _getAllItems(tableName, params = {}) {
const scanResults = []
let items
do {
items = await this.documentClient.scan({ TableName: tableName }).promise()
items.Items.forEach((item) => scanResults.push(item))
params.ExclusiveStartKey = items.LastEvaluatedKey
} while (typeof items.LastEvaluatedKey !== 'undefined')
return scanResults
}
_cloneTable(opt) {
return new Promise((resolve, reject) => {
copy(opt, function (err, result) {
if (err) {
return reject(err)
}
resolve(result)
})
})
}
}
module.exports = DynamoDbResource
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc