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
97const 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 */
11387function 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
145114module . exports = { writeSeeds, locateSeeds } ;
0 commit comments