Skip to content

Commit

Permalink
@aws-sdk/[client|lib]-dynamodb / aws-sdk/clients/dynamodb → `@aws…
Browse files Browse the repository at this point in the history
…-lite/dynamodb`
  • Loading branch information
ryanblock committed Oct 12, 2023
1 parent 3a72eba commit eba7bc3
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 232 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"license": "Apache-2.0",
"dependencies": {
"@aws-lite/client": "^0.8.1",
"@aws-lite/dynamodb": "^0.2.1",
"@aws-lite/ssm": "^0.1.0",
"cookie": "^0.5.0",
"cookie-signature": "^1.2.1",
Expand Down
1 change: 1 addition & 0 deletions src/discovery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = function lookup (callback) {
port,
protocol: 'http',
region: AWS_REGION || 'us-west-2',
plugins: [ '@aws-lite/ssm' ],
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/tables/dynamo.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TODO: figure out what to do with instantiating DDB + DocumentClients

let { getPorts, isNode18, useAWS } = require('../lib')

/**
Expand Down
199 changes: 93 additions & 106 deletions src/tables/factory.js
Original file line number Diff line number Diff line change
@@ -1,124 +1,111 @@
let dynamo = require('./dynamo')
let parallel = require('run-parallel')
let { getAwsClient, getPorts } = require('../lib')
let paginate = true

/**
* returns a data client
*/
module.exports = function reflectFactory (tables, callback) {
let local = process.env.ARC_ENV === 'testing'
let { ARC_ENV, AWS_REGION } = process.env
let local = ARC_ENV === 'testing'

parallel(dynamo, function done (err, { db, doc }) {
if (err) return callback(err)
getPorts((err, ports) => {
if (err) callback(err)
else {
let port = ports.tables
if (!port) {
return callback(ReferenceError('Sandbox tables port not found'))
}
let config = {
host: `localhost`,
port,
protocol: 'http',
region: AWS_REGION || 'us-west-2',
plugins: [ '@aws-lite/dynamodb' ],
}
getAwsClient(config, (err, aws) => {
if (err) callback(err)
else {
let data = Object.keys(tables)
.filter(name => {
if (local && !name.includes('-production-')) return name
return name
})
.reduce((client, fullName) => {
let name = local ? fullName.replace(/.+-staging-/, '') : fullName
client[name] = factory(tables[name])
return client
}, {})

let data = Object.keys(tables)
.filter(name => {
if (local && !name.includes('-production-')) return name
return name
})
.reduce((client, fullName) => {
let name = local ? fullName.replace(/.+-staging-/, '') : fullName
client[name] = factory(tables[name])
return client
}, {})
data.reflect = async () => tables
let _name = name => tables[name]
data.name = _name
data._name = _name

function go (method, params, callback) {
if (callback) method(params)
.then(result => callback(null, result))
.catch(err => callback(err))
else return method(params)
}

function factory (TableName) {
return {
delete (Key, callback) {
if (callback) aws.dynamodb.DeleteItem({ TableName, Key })
.then(result => callback(null, result))
.catch(err => callback(err))

else return new Promise((res, rej) => {
aws.dynamodb.DeleteItem({ TableName, Key })
.then(result => res(result))
.catch(rej)
})
},

get (Key, callback) {
if (callback) aws.dynamodb.GetItem({ TableName, Key })
.then(({ Item }) => callback(null, Item))
.catch(err => callback(err))

else return new Promise((res, rej) => {
aws.dynamodb.GetItem({ TableName, Key })
.then(({ Item }) => res(Item))
.catch(rej)
})
},

put (Item, callback) {
return go(aws.dynamodb.PutItem, { TableName, Item }, callback)
},

query (params = {}, callback) {
return go(aws.dynamodb.Query, { ...params, TableName }, callback)
},

let enumerable = false
Object.defineProperty(data, '_db', { enumerable, value: db })
Object.defineProperty(data, '_doc', { enumerable, value: doc })
scan (params = {}, callback) {
return go(aws.dynamodb.Scan, { ...params, TableName }, callback)
},

// async jic for later
// eslint-disable-next-line
data.reflect = async () => tables
scanAll (params = {}, callback) {
if (callback) aws.dynamodb.Scan({ ...params, TableName, paginate })
.then(({ Items }) => callback(null, Items))
.catch(err => callback(err))

let _name = name => tables[name]
data.name = _name
data._name = _name
else return new Promise((res, rej) => {
aws.dynamodb.Scan({ ...params, TableName, paginate })
.then(({ Items }) => res(Items))
.catch(rej)
})
},

function factory (TableName) {
return promisify({
delete (key, callback) {
let params = {}
params.TableName = TableName
params.Key = key
doc.delete(params, callback)
},
get (key, callback) {
let params = {}
params.TableName = TableName
params.Key = key
doc.get(params, function _get (err, result) {
if (err) callback(err)
else callback(null, result.Item)
})
},
put (item, callback) {
let params = {}
params.TableName = TableName
params.Item = item
doc.put(params, function _put (err) {
if (err) callback(err)
else callback(null, item)
})
},
query (params, callback) {
params.TableName = TableName
doc.query(params, callback)
},
scan (params = {}, callback) {
params.TableName = TableName
doc.scan(params, callback)
},
scanAll (params = {}, callback) {
let records = []
params.TableName = TableName
function getRecords () {
db.scan(params, (err, data) => {
if (err) callback(err)
else {
data.Items.forEach(d => records.push(d))
if (data.LastEvaluatedKey) {
params.ExclusiveStartKey = data.LastEvaluatedKey
getRecords()
}
else {
callback(null, records)
}
update (params, callback) {
return go(aws.dynamodb.UpdateItem, { ...params, TableName }, callback)
}
})
}
}
getRecords()
},
update (params, callback) {
params.TableName = TableName
doc.update(params, callback)
callback(null, data)
}
})
}

callback(null, data)
})
}

// accepts an object and promisifies all keys
function promisify (obj) {
let copy = {}
Object.keys(obj).forEach(k => {
copy[k] = promised(obj[k])
})
return copy
}

// Accepts an errback style fn and returns a promisified fn
function promised (fn) {
return function _promisified (params, callback) {
if (!callback) {
return new Promise(function (res, rej) {
fn(params, function (err, result) {
err ? rej(err) : res(result)
})
})
}
else {
fn(params, callback)
}
}
}
9 changes: 4 additions & 5 deletions test/integration/tables-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ test('tables get()', async t => {
t.ok(result, 'got accounts table result')
t.ok(result.baz.doe, 'result.baz.doe deserialized')
result = null
console.log(data['accounts-messages'].get)
result = await data['accounts-messages'].get({
accountID: 'fake',
msgID: 'alsofake'
Expand All @@ -107,7 +106,7 @@ test('tables delete()', async t => {
let result = await data.accounts.get({
accountID: 'fake'
})
t.equals(result, undefined, 'could not get deleted accounts item')
t.equal(result, undefined, 'could not get deleted accounts item')
await data['accounts-messages'].delete({
accountID: 'fake',
msgID: 'alsofake'
Expand All @@ -117,7 +116,7 @@ test('tables delete()', async t => {
accountID: 'fake',
msgID: 'alsofake'
})
t.equals(otherResult, undefined, 'could not get deleted accounts-messages item')
t.equal(otherResult, undefined, 'could not get deleted accounts-messages item')
})

test('tables query()', async t => {
Expand All @@ -138,7 +137,7 @@ test('tables query()', async t => {
})

t.ok(result, 'got a result')
t.equals(result.Count, 1, 'got count of one')
t.equal(result.Count, 1, 'got count of one')
})

test('tables scan()', async t => {
Expand Down Expand Up @@ -181,7 +180,7 @@ test('tables update()', async t => {
})

t.ok(result, 'got result')
t.equals(result.hits, 20, 'property updated')
t.equal(result.hits, 20, 'property updated')
})

test('server closes', t => {
Expand Down
Loading

0 comments on commit eba7bc3

Please sign in to comment.