我想在 JavaScript 解析器中使用 dynamoose,使其能够与 GraphQL 和 DynamoDB 配合使用。
我尝试在无服务器框架中创建的函数中导入 dynamoose。但是当我运行serverless部署
时出现错误创建用户.js
'use strict';
const { getSuccessResponse } = require('../../helper/success');
const { getErrorResponse } = require('../../helper/error');
const { v4: uuidv4 } = require('uuid');
const { UsersModel } = require('../../Models/UserModel');
module.exports.main = async (event) => {
try {
const request = JSON.parse(event.body);
const { name, email } = request;
const result = await UsersModel.create({
id: uuidv4(),
name,
email,
});
return getSuccessResponse(result);
} catch (error) {
console.log(error);
return getErrorResponse(error);
}
};
UserModel.js
const dynamoose = require('dynamoose');
const schema = new dynamoose.Schema(
{
id: {
type: String,
hashKey: true,
},
name: String,
age: Number,
},
{
timestamps: true,
}
);
const UsersModel = dynamoose.model('aws-appsync-dynamoose-dev-users', schema, {
create: false,
throughput: {
read: 5,
write: 5,
},
});
module.exports = { UsersModel };
您不能在 AppSync 的 JS 解析器内部使用外部库。以下是支持的运行时功能列表:https://docs.aws.amazon.com/appsync/latest/devguide/resolver-util-reference-js.html
执行此操作的一种方法是创建 Lambda 函数(您可以在其中安装 dynamoose),然后使用 AppSync JS 解析器调用该函数。请参阅此处:https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers-js.html