-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlShortener.js
More file actions
38 lines (32 loc) · 897 Bytes
/
Copy pathurlShortener.js
File metadata and controls
38 lines (32 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require('dotenv').config()
const DynamoDB = require('aws-sdk/clients/dynamodb')
const nanoID = require('nanoid')
const tableName = process.env.AWS_DYNAMODB_TABLE
const region = process.env.AWS_BUCKET_REGION
const dynoDb = new DynamoDB({
region
})
// It creates a new shortUrl if not exist
async function urlShortener(){
let breakOrContinue = false
let result
do{
const newShortURL = nanoID.nanoid(6)
const params = {
TableName : tableName,
Key: {
"shortURL": {
S:newShortURL
}
}
}
result = await dynoDb.getItem(params).promise()
if(JSON.stringify(result)==='{}'){
return newShortURL
}
else{
breakOrContinue = true
}
}while(breakOrContinue)
}
exports.urlShortener = urlShortener