如何使用dynamoose删除表

时间:2018-02-07 22:34:25

标签: node.js amazon-dynamodb dynamoose

如何删除dynamoose中的表?

例如,是否有Model.deleteTable()函数?

1 个答案:

答案 0 :(得分:0)

我建议您创建一个小模块,为您的dynamoose导入添加其他配置。

const dynamo = require('dynamoose')
const constants = require('../constants')

let oldModelFunction = dynamo.model

dynamo.model = function () {
    let compiledModel = oldModelFunction.apply(this, arguments)
    let name = compiledModel.$__.name
    compiledModel.modelName = name.toLowerCase()

    // Add deleteTable method
    compiledModel.deleteTable = () => {
        return new Promise((resolve, reject) => {
            dynamo.ddb().deleteTable({ TableName: compiledModel.$__.name }, (err, resp) => {
                if (err) reject(err)
                else resolve(resp)
            })
        })
    }

    return compiledModel
}

module.exports = dynamo

然后您可以创建一个模型并像这样使用它:

const dynamo = require('./mydynamoose')

const Schema = dynamo.Schema

let UserSchema = new Schema({
    username: String,
    name: String,
    age: Number
})

const User = dynamo.model("User", UserSchema)

User.deleteTable().then(res => {
    // Check response
}).catch(err => {
    console.log(err)
})