serverless-seed-plugin
Advanced tools
Comparing version 0.1.0 to 0.3.0
@@ -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 @@ |
{ | ||
"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 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
94
0
17273
2
8
380
2
+ Addedcopy-dynamodb-table@^2.2.1
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedaws-sdk@2.1692.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@4.9.2(transitive)
+ Addedcall-bind@1.0.8(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addedcopy-dynamodb-table@2.2.1(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedevents@1.1.1(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedieee754@1.1.13(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-arguments@1.2.0(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-generator-function@1.1.0(transitive)
+ Addedis-regex@1.2.1(transitive)
+ Addedis-typed-array@1.1.15(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedjmespath@0.16.0(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedpunycode@1.3.2(transitive)
+ Addedquerystring@0.2.0(transitive)
+ Addedreadline@1.3.0(transitive)
+ Addedsafe-regex-test@1.1.0(transitive)
+ Addedsax@1.2.1(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedurl@0.10.3(transitive)
+ Addedutil@0.12.5(transitive)
+ Addeduuid@8.0.0(transitive)
+ Addedwhich-typed-array@1.1.18(transitive)
+ Addedxml2js@0.6.2(transitive)
+ Addedxmlbuilder@11.0.1(transitive)