clarkie / dynogels

DynamoDB data mapper for node.js. Originally forked from https://github.com/ryanfitz/vogels
Other
490 stars 110 forks source link

dynamic create table how to get model #140

Closed dduo518 closed 6 years ago

dduo518 commented 6 years ago

hi! I use api dynamic create table;

function _define(params) {
    params.schema = _defineSchema(params.schema);
    dynogels.define(params.name, {
        hashKey: params.hashkey,
        timestamps: true,
        createdAt: true,
        updatedAt: 'updateTimestamp',
        schema: params.schema
    });
    return new Promise((resolve, reject) => {
        dynogels.createTables(function(err) {
            if (err) {
                reject(err);
            } else {
                resolve({ success: true });
            }
        })
    })
}

one api want to query the table ,then how to get the model; or change expression: can I not defined the model and save or query the table thanks

cdhowie commented 6 years ago

dynogels.define() returns the table model. Perhaps you could return this from your promise?

let model = dynogels.define(...);

// In callback:
resolve({ success: true, model });

You can also access any model by its name as an attribute of dynogels.models. For example, in your code you could fetch the model as dynogels.models[params.name] after the call to dynogels.define().