I need to create IDs to use as key on DynamoDB. Checked https://github.com/uuidjs/uuid but the ids generated are too long to be remembered. how can I generate shorter ones like the ones from youtube video Id.
Also do I really need to specify a Key or Dynamo can create one automatically for me just like firebase firestore for example?
My code:
var db = new AWS.DynamoDB.DocumentClient();
var student = {
TableName: "students",
Item: {
id: "abc123",
name: "Mia",
},
};
db.put(student)
.promise()
.then((response) => console.log(response))
.catch((err) => console.log(err));
It sounds like you want to build a URL Shortener. There are loads of tutorials online showcasing how to do this in AWS using DynamoDB and API Gateway, so you might want to search online for a more detailed walkthrough.
how can I generate shorter ones like the ones from youtube video Id
You'll need to generate this yourself in your application code. If you're using javascript, you could use something like this, which creates a random string:
let short_id = (Math.random() + 1).toString(36).substring(7);
Keep in mind that you'll need to include retry logic in the event the generated ID is not unique in the database.
do I really need to specify a Key or Dynamo can create one automatically for me just like firebase firestore for example?
DynamoDB will not automatically create or manage ID's for your application. You'll need to do this yourself.
You can use nanoid which is similar to uuid, u can use specify length of ID u want
const { nanoid } = require('nanoid')
var db = new AWS.DynamoDB.DocumentClient();
var student = {
TableName: "students",
Item: {
id: nanoid(), // or nanoid(10) for 10 char id
name: "Mia",
},
};
db.put(student)
.promise()
.then((response) => console.log(response))
.catch((err) => console.log(err));