How to make a UUID in DynamoDB?

后端 未结 10 1311
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 18:17

In my db scheme, I need a autoincrement primary key. How I can realize this feature?

PS For access to DynamoDB, I use dynode, module for Node.js.

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 18:34

    Incase you are using NoSQL DynamoDB then using Dynamoose ORM, you can easily set default unique id. Here is the simple user creation example

    // User.modal.js

    const dynamoose = require("dynamoose");
    
    const userSchema = new dynamoose.Schema(
      {
        id: {
          type: String,
          hashKey: true,
        },
        displayName: String,
        firstName: String,
        lastName: String,
      },
      { timestamps: true },
    );
    
    const User = dynamoose.model("User", userSchema);
    
    module.exports = User;
    

    // User.controller.js

    const { v4: uuidv4 } = require("uuid");    
    const User = require("./user.model");
    
    exports.create = async (req, res) => {
      const user = new User({ id: uuidv4(), ...req.body }); // set unique id
      const [err, response] = await to(user.save());
      if (err) {
        return badRes(res, err);
      }
      return goodRes(res, reponse);
    };
    

提交回复
热议问题