Skip to content

Commit 2e5fae4

Browse files
authored
Remove bluebird and lodash (#21)
1 parent 95d57d9 commit 2e5fae4

File tree

4 files changed

+44
-88
lines changed

4 files changed

+44
-88
lines changed

index.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"use strict";
2-
const _ = require("lodash");
32
const AWS = require("aws-sdk");
43
const dynamodbLocal = require("aws-dynamodb-local");
54
const seeder = require("./src/seeder");
@@ -11,11 +10,10 @@ class ServerlessDynamodbLocal {
1110
this.service = serverless.service;
1211
this.serverlessLog = serverless.cli.log.bind(serverless.cli);
1312
this.config = this.service.custom && this.service.custom['serverless-dynamodb'] || this.service.custom.dynamodb || {};
14-
this.options = _.merge({
15-
localPath: serverless.config && path.join(serverless.config.servicePath, '.dynamodb')
16-
},
17-
options
18-
);
13+
this.options = {
14+
localPath: serverless.config && path.join(serverless.config.servicePath, '.dynamodb'),
15+
...options,
16+
};
1917
this.provider = "aws";
2018
this.commands = {
2119
dynamodb: {
@@ -156,13 +154,11 @@ class ServerlessDynamodbLocal {
156154
}
157155

158156
get port() {
159-
const port = _.get(this.config, "start.port", 8000);
160-
return port;
157+
return this.config?.start?.port ?? 8000;
161158
}
162159

163160
get host() {
164-
const host = _.get(this.config, "start.host", "localhost");
165-
return host;
161+
return this.config?.start?.host ?? "localhost";
166162
}
167163

168164
/**
@@ -254,13 +250,12 @@ class ServerlessDynamodbLocal {
254250

255251
startHandler() {
256252
if (this.shouldExecute()) {
257-
const options = _.merge({
258-
sharedDb: this.options.sharedDb || true,
259-
installPath: this.options.localPath
260-
},
261-
this.config.start,
262-
this.options
263-
);
253+
const options = {
254+
sharedDb: this.options.sharedDb ?? true,
255+
installPath: this.options.localPath,
256+
...this.config.start,
257+
...this.options
258+
}
264259

265260
// otherwise endHandler will be mis-informed
266261
this.options = options;
@@ -288,19 +283,19 @@ class ServerlessDynamodbLocal {
288283
}
289284

290285
getDefaultStack() {
291-
return _.get(this.service, "resources");
286+
return this.service.resources;
292287
}
293288

294289
getAdditionalStacks() {
295-
return _.values(_.get(this.service, "custom.additionalStacks", {}));
290+
return Object.values(this.service.custom?.additionalStacks ?? {});
296291
}
297292

298293
hasAdditionalStacksPlugin() {
299-
return _.get(this.service, "plugins", []).includes("serverless-plugin-additional-stacks");
294+
return (this.service.plugins ?? []).includes("serverless-plugin-additional-stacks");
300295
}
301296

302297
getTableDefinitionsFromStack(stack) {
303-
const resources = _.get(stack, "Resources", []);
298+
const resources = stack.Resources ?? [];
304299
return Object.keys(resources).map((key) => {
305300
if (resources[key].Type === "AWS::DynamoDB::Table") {
306301
return resources[key].Properties;
@@ -320,7 +315,7 @@ class ServerlessDynamodbLocal {
320315
}
321316

322317
if (this.hasAdditionalStacksPlugin()) {
323-
stacks = stacks.concat(this.getAdditionalStacks());
318+
stacks.push(...this.getAdditionalStacks());
324319
}
325320

326321
return stacks.map((stack) => this.getTableDefinitionsFromStack(stack)).reduce((tables, tablesInStack) => tables.concat(tablesInStack), []);
@@ -330,7 +325,7 @@ class ServerlessDynamodbLocal {
330325
* Gets the seeding sources
331326
*/
332327
get seedSources() {
333-
const seedConfig = _.get(this.config, "seed", {});
328+
const seedConfig = this.config.seed ?? {};
334329
const seed = this.options.seed || this.config.start.seed || seedConfig;
335330
let categories;
336331
if (typeof seed === "string") {

package-lock.json

Lines changed: 3 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
},
1515
"dependencies": {
1616
"aws-dynamodb-local": "^0.0.11",
17-
"aws-sdk": "^2.7.0",
18-
"bluebird": "^3.4.6",
19-
"lodash": "^4.17.0"
17+
"aws-sdk": "^2.7.0"
2018
},
2119
"devDependencies": {
2220
"chai": "^4.1.1",

src/seeder.js

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
"use strict";
2-
const BbPromise = require("bluebird");
3-
const _ = require("lodash");
4-
const path = require("path");
5-
const fs = require("fs");
2+
const path = require("node:path");
3+
const fs = require("node:fs");
64

75
// DynamoDB has a 25 item limit in batch requests
86
// https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
97
const MAX_MIGRATION_CHUNK = 25;
108

11-
// TODO: let this be configurable
12-
const MIGRATION_SEED_CONCURRENCY = 5;
13-
149
/**
1510
* Writes a batch chunk of migration seeds to DynamoDB. DynamoDB has a limit on the number of
1611
* items that may be written in a batch operation.
@@ -67,59 +62,36 @@ function writeSeeds(dynamodbWriteFunction, tableName, seeds) {
6762
}
6863

6964
if (seeds.length > 0) {
70-
const seedChunks = _.chunk(seeds, MAX_MIGRATION_CHUNK);
71-
return BbPromise.map(
72-
seedChunks,
73-
(chunk) => writeSeedBatch(dynamodbWriteFunction, tableName, chunk),
74-
{ concurrency: MIGRATION_SEED_CONCURRENCY }
75-
)
65+
const seedChunks = chunk(seeds, MAX_MIGRATION_CHUNK);
66+
return Promise.all(seedChunks.map((chunk) => writeSeedBatch(dynamodbWriteFunction, tableName, chunk)))
7667
.then(() => console.log("Seed running complete for table: " + tableName));
7768
}
7869
}
7970

80-
/**
81-
* A promise-based function that determines if a file exists
82-
* @param {string} fileName The path to the file
83-
*/
84-
function fileExists(fileName) {
85-
return new Promise((resolve) => {
86-
fs.exists(fileName, (exists) => resolve(exists));
87-
});
88-
}
89-
90-
/**
91-
* Transform all selerialized Buffer value in a Buffer value inside a json object
92-
*
93-
* @param {json} json with serialized Buffer value.
94-
* @return {json} json with Buffer object.
95-
*/
96-
function unmarshalBuffer(json) {
97-
_.forEach(json, function(value, key) {
98-
// Null check to prevent creation of Buffer when value is null
99-
if (value !== null && value.type==="Buffer") {
100-
json[key]= new Buffer(value.data);
101-
}
102-
});
103-
return json;
104-
}
71+
const chunk = (input, size) => {
72+
return input.reduce((arr, item, idx) => {
73+
return idx % size === 0
74+
? [...arr, [item]]
75+
: [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];
76+
}, []);
77+
};
10578

10679
/**
10780
* Scrapes seed files out of a given location. This file may contain
10881
* either a simple json object, or an array of simple json objects. An array
10982
* of json objects is returned.
11083
*
111-
* @param {any} location the filename to read seeds from.
84+
* @param {string} location the filename to read seeds from.
85+
* @returns {object[]} json
11286
*/
11387
function getSeedsAtLocation(location) {
11488
// load the file as JSON
11589
const result = require(location);
11690

11791
// Ensure the output is an array
118-
if (Array.isArray(result)) {
119-
return _.forEach(result, unmarshalBuffer);
120-
} else {
121-
return [ unmarshalBuffer(result) ];
122-
}
92+
const array = Array.isArray(result) ? result : [result];
93+
94+
return array;
12395
}
12496

12597
/**
@@ -131,15 +103,12 @@ function locateSeeds(sources, cwd) {
131103
cwd = cwd || process.cwd();
132104

133105
const locations = sources.map((source) => path.join(cwd, source));
134-
return BbPromise.map(locations, (location) => {
135-
return fileExists(location).then((exists) => {
136-
if(!exists) {
137-
throw new Error("source file " + location + " does not exist");
138-
}
139-
return getSeedsAtLocation(location);
140-
});
141-
// Smash the arrays together
142-
}).then((seedArrays) => [].concat.apply([], seedArrays));
106+
return locations.map((location) => {
107+
if(!fs.existsSync(location)) {
108+
throw new Error("source file " + location + " does not exist");
109+
}
110+
return getSeedsAtLocation(location);
111+
}).flat(1);
143112
}
144113

145114
module.exports = { writeSeeds, locateSeeds };

0 commit comments

Comments
 (0)