dynamoose
Version:
Dynamoose is a modeling tool for Amazon's DynamoDB (inspired by Mongoose)
77 lines • 2.92 kB
JavaScript
;
require("source-map-support/register");
const Model_1 = require("./Model");
const Schema_1 = require("./Schema");
const Condition_1 = require("./Condition");
const Transaction_1 = require("./Transaction");
const aws = require("./aws");
const Internal = require("./Internal");
const utils = require("./utils");
const logger = require("./logger");
const ModelStore = require("./ModelStore");
const model = (name, schema, options = {}) => {
let model;
let storedSchema;
if (name) {
storedSchema = ModelStore(name);
}
// TODO: this is something I'd like to do. But is a breaking change. Need to enable this and uncomment it in a breaking release. Also will need to fix the tests as well.
/* if (schema && storedSchema) {
throw new CustomError.InvalidParameter(`Model with name ${name} has already been registered.`);
} else */
if (!schema && storedSchema) {
model = storedSchema;
}
else {
model = new Model_1.Model(name, schema, options);
}
const returnObject = model.Document;
const keys = utils.array_flatten([
Object.keys(model),
Object.keys(Object.getPrototypeOf(model)),
Object.getOwnPropertyNames(Object.getPrototypeOf(model))
]).filter((key) => !["constructor", "name"].includes(key));
keys.forEach((key) => {
if (typeof model[key] === "object") {
const main = (key) => {
utils.object.set(returnObject, key, {});
const value = utils.object.get(model, key);
if (value === null || value.constructor !== Object && value.constructor !== Array) {
utils.object.set(returnObject, key, value);
}
else {
Object.keys(value).forEach((subKey) => {
const newKey = `${key}.${subKey}`;
const subValue = utils.object.get(model, newKey);
if (typeof subValue === "object") {
main(newKey);
}
else {
utils.object.set(returnObject, newKey, typeof subValue === "function" ? subValue.bind(model) : subValue);
}
});
}
};
main(key);
}
else if (typeof model[key] === "function") {
returnObject[key] = model[key].bind(model);
}
else {
returnObject[key] = model[key];
}
});
return returnObject;
};
model.defaults = Object.assign({}, require("./Model/defaults").custom);
module.exports = {
model,
Schema: Schema_1.Schema,
Condition: Condition_1.Condition,
transaction: Transaction_1.default,
aws,
logger,
"UNDEFINED": Internal.Public.undefined,
"THIS": Internal.Public.this
};
//# sourceMappingURL=index.js.map