Skip to content

Commit

Permalink
Fixing lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fishcharlie committed May 23, 2020
1 parent e2983e0 commit 5beea96
Show file tree
Hide file tree
Showing 29 changed files with 203 additions and 175 deletions.
26 changes: 15 additions & 11 deletions lib/Condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import CustomError = require("./Error");
import utils = require("./utils");
const OR = Symbol("OR");
import {DynamoDB} from "aws-sdk";
import { ObjectType } from "./General";
import {ObjectType} from "./General";

const isRawConditionObject = (object): boolean => Object.keys(object).length === 3 && ["ExpressionAttributeValues", "ExpressionAttributeNames"].every((item) => Boolean(object[item]) && typeof object[item] === "object");

Expand Down Expand Up @@ -95,7 +95,7 @@ export class Condition {

requestObject: (settings?: ConditionRequestObjectSettings) => ConditionRequestObjectResult;

constructor(object?: ConditionInitalizer) {
constructor (object?: ConditionInitalizer) {
if (object instanceof Condition) {
Object.entries(object).forEach((entry) => {
const [key, value] = entry;
Expand Down Expand Up @@ -141,7 +141,7 @@ interface ConditionsConditionStorageObject {
value: any;
}

function finalizePending(instance: Condition): void {
function finalizePending (instance: Condition): void {
const pending = instance.settings.pending;

let dynamoNameType: ConditionComparisonComparatorDynamoName;
Expand Down Expand Up @@ -169,22 +169,24 @@ Condition.prototype.parenthesis = Condition.prototype.group = function (this: Co
this.settings.conditions.push(value.settings.conditions);
return this;
};
Condition.prototype.or = function(this: Condition): Condition {
Condition.prototype.or = function (this: Condition): Condition {
this.settings.conditions.push(OR);
return this;
};
Condition.prototype.and = function(this: Condition): Condition { return this; };
Condition.prototype.not = function(this: Condition): Condition {
Condition.prototype.and = function (this: Condition): Condition {
return this;
};
Condition.prototype.not = function (this: Condition): Condition {
this.settings.pending.not = !this.settings.pending.not;
return this;
};
Condition.prototype.where = Condition.prototype.filter = Condition.prototype.attribute = function(this: Condition, key: string): Condition {
Condition.prototype.where = Condition.prototype.filter = Condition.prototype.attribute = function (this: Condition, key: string): Condition {
this.settings.pending = {key};
return this;
};
// TODO: I don't think this prototypes are being exposed which is gonna cause a lot of problems with our type definition file. Need to figure out a better way to do this since they aren't defined and are dynamic.
types.forEach((type) => {
Condition.prototype[type.name] = function(this: Condition, ...args: any[]): Condition {
Condition.prototype[type.name] = function (this: Condition, ...args: any[]): Condition {
this.settings.pending.value = type.multipleArguments ? args : args[0];
this.settings.pending.type = type;
finalizePending(this);
Expand All @@ -200,7 +202,7 @@ interface ConditionRequestObjectSettings {
};
conditionStringType: "array" | "string";
}
Condition.prototype.requestObject = function(this: Condition, settings: ConditionRequestObjectSettings = {"conditionString": "ConditionExpression", "conditionStringType": "string"}): ConditionRequestObjectResult {
Condition.prototype.requestObject = function (this: Condition, settings: ConditionRequestObjectSettings = {"conditionString": "ConditionExpression", "conditionStringType": "string"}): ConditionRequestObjectResult {
if (this.settings.raw && utils.object.equals(Object.keys(this.settings.raw).sort(), [settings.conditionString, "ExpressionAttributeValues", "ExpressionAttributeNames"].sort())) {
return Object.entries((this.settings.raw as ObjectType).ExpressionAttributeValues).reduce((obj, entry) => {
const [key, value] = entry;
Expand All @@ -215,8 +217,10 @@ Condition.prototype.requestObject = function(this: Condition, settings: Conditio
}

let index = (settings.index || {}).start || 0;
const setIndex = (i: number): void => {index = i; (settings.index || {"set": utils.empty_function}).set(i);};
function main(input: ConditionStorageSettingsConditions): ConditionRequestObjectResult {
const setIndex = (i: number): void => {
index = i; (settings.index || {"set": utils.empty_function}).set(i);
};
function main (input: ConditionStorageSettingsConditions): ConditionRequestObjectResult {
return input.reduce((object: ConditionRequestObjectResult, entry: ConditionStorageTypeNested, i: number, arr: any[]) => {
let expression = "";
if (Array.isArray(entry)) {
Expand Down
42 changes: 22 additions & 20 deletions lib/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import aws = require("./aws");
import ddb = require("./aws/ddb/internal");
import utils = require("./utils");
import Error = require("./Error");
import Internal = require("./Internal");
import Internal = require("./Internal");
import {Model, ModelExpiresSettings} from "./Model";
import {DynamoDBTypeResult, DynamoDBSetTypeResult} from "./Schema";
const {internalProperties} = Internal.General;
const dynamooseUndefined = Internal.Public.undefined;

import { DynamoDB, AWSError } from "aws-sdk";
import { ValueType } from "./Schema";
import { CallbackType, ObjectType } from "./General";
import {DynamoDB, AWSError} from "aws-sdk";
import {ValueType} from "./Schema";
import {CallbackType, ObjectType} from "./General";

export interface DocumentSaveSettings {
overwrite?: boolean;
Expand All @@ -22,7 +22,7 @@ export interface DocumentSettings {

// Document represents an item in a Model that is either pending (not saved) or saved
export class Document {
constructor(model: Model<Document>, object?: DynamoDB.AttributeMap | ObjectType, settings?: DocumentSettings) {
constructor (model: Model<Document>, object?: DynamoDB.AttributeMap | ObjectType, settings?: DocumentSettings) {
const documentObject = Document.isDynamoObject(object) ? aws.converter().unmarshall(object) : object;
Object.keys(documentObject).forEach((key) => this[key] = documentObject[key]);
Object.defineProperty(this, internalProperties, {
Expand All @@ -47,21 +47,21 @@ export class Document {
static objectToDynamo(object: ObjectType): DynamoDB.AttributeMap;
static objectToDynamo(object: any, settings: {type: "value"}): DynamoDB.AttributeValue;
static objectToDynamo(object: ObjectType, settings: {type: "object"}): DynamoDB.AttributeMap;
static objectToDynamo(object: any, settings: {type: "object" | "value"} = {"type": "object"}): DynamoDB.AttributeValue | DynamoDB.AttributeMap {
static objectToDynamo (object: any, settings: {type: "object" | "value"} = {"type": "object"}): DynamoDB.AttributeValue | DynamoDB.AttributeMap {
return (settings.type === "value" ? aws.converter().input : aws.converter().marshall)(object);
}
static fromDynamo(object: DynamoDB.AttributeMap): ObjectType {
static fromDynamo (object: DynamoDB.AttributeMap): ObjectType {
return aws.converter().unmarshall(object);
}
// This function will return null if it's unknown if it is a Dynamo object (ex. empty object). It will return true if it is a Dynamo object and false if it's not.
static isDynamoObject(object: ObjectType, recurrsive?: boolean): boolean | null {
function isValid(value): boolean {
static isDynamoObject (object: ObjectType, recurrsive?: boolean): boolean | null {
function isValid (value): boolean {
if (typeof value === "undefined" || value === null) {
return false;
}
const keys = Object.keys(value);
const key = keys[0];
const nestedResult = (typeof value[key] === "object" && !(value[key] instanceof Buffer) ? (Array.isArray(value[key]) ? value[key].every((value) => Document.isDynamoObject(value, true)) : Document.isDynamoObject(value[key])) : true);
const nestedResult = typeof value[key] === "object" && !(value[key] instanceof Buffer) ? Array.isArray(value[key]) ? value[key].every((value) => Document.isDynamoObject(value, true)) : Document.isDynamoObject(value[key]) : true;
const {Schema} = require("./Schema");
const attributeType = Schema.attributeTypes.findDynamoDBType(key);
return typeof value === "object" && keys.length === 1 && attributeType && (nestedResult || Object.keys(value[key]).length === 0 || attributeType.isSet);
Expand Down Expand Up @@ -90,7 +90,7 @@ export class Document {
// Delete
delete(this: Document): Promise<void>;
delete(this: Document, callback: CallbackType<void, AWSError>): void;
delete(this: Document, callback?: CallbackType<void, AWSError>): Promise<void> | void {
delete (this: Document, callback?: CallbackType<void, AWSError>): Promise<void> | void {
return this.model.delete({
[this.model.schema.getHashKey()]: this[this.model.schema.getHashKey()]
}, callback);
Expand All @@ -103,7 +103,7 @@ export class Document {
save(this: Document, settings: DocumentSaveSettings & {return: "request"}, callback: CallbackType<DynamoDB.PutItemInput, AWSError>): void;
save(this: Document, settings: DocumentSaveSettings & {return: "document"}): Promise<Document>;
save(this: Document, settings: DocumentSaveSettings & {return: "document"}, callback: CallbackType<Document, AWSError>): void;
save(this: Document, settings?: DocumentSaveSettings | CallbackType<Document, AWSError> | CallbackType<DynamoDB.PutItemInput, AWSError>, callback?: CallbackType<Document, AWSError> | CallbackType<DynamoDB.PutItemInput, AWSError>): void | Promise<Document | DynamoDB.PutItemInput> {
save (this: Document, settings?: DocumentSaveSettings | CallbackType<Document, AWSError> | CallbackType<DynamoDB.PutItemInput, AWSError>, callback?: CallbackType<Document, AWSError> | CallbackType<DynamoDB.PutItemInput, AWSError>): void | Promise<Document | DynamoDB.PutItemInput> {
if (typeof settings !== "object" && typeof settings !== "undefined") {
callback = settings;
settings = {};
Expand Down Expand Up @@ -142,7 +142,9 @@ export class Document {

if (callback) {
const localCallback: CallbackType<Document, AWSError> = callback as CallbackType<Document, AWSError>;
promise.then(() => {this[internalProperties].storedInDynamo = true; localCallback(null, this);}).catch((error) => callback(error));
promise.then(() => {
this[internalProperties].storedInDynamo = true; localCallback(null, this);
}).catch((error) => callback(error));
} else {
return (async (): Promise<Document> => {
await promise;
Expand Down Expand Up @@ -172,7 +174,7 @@ Document.prepareForObjectFromSchema = function<T>(object: T, model: Model<Docume
// This function will return a list of attributes combining both the schema attributes with the document attributes. This also takes into account all attributes that could exist (ex. properties in sets that don't exist in document), adding the indexes for each item in the document set.
// https://stackoverflow.com/a/59928314/894067
const attributesWithSchemaCache: ObjectType = {};
Document.attributesWithSchema = function(document: Document, model: Model<Document>): string[] {
Document.attributesWithSchema = function (document: Document, model: Model<Document>): string[] {
const attributes = model.schema.attributes();
const documentID = utils.object.keys(document as any).join("");
if (attributesWithSchemaCache[documentID] && attributesWithSchemaCache[documentID][attributes.join()]) {
Expand Down Expand Up @@ -234,8 +236,8 @@ export interface DocumentObjectFromSchemaSettings {
updateTimestamps?: boolean | {updatedAt?: boolean; createdAt?: boolean};
}
// This function will return an object that conforms to the schema (removing any properties that don't exist, using default values, etc.) & throws an error if there is a typemismatch.
Document.objectFromSchema = async function(object: any, model: Model<Document>, settings: DocumentObjectFromSchemaSettings = {"type": "toDynamo"}): Promise<ObjectType> {
if (settings.checkExpiredItem && model.options.expires && ((model.options.expires as ModelExpiresSettings).items || {}).returnExpired === false && object[(model.options.expires as ModelExpiresSettings).attribute] && (object[(model.options.expires as ModelExpiresSettings).attribute] * 1000) < Date.now()) {
Document.objectFromSchema = async function (object: any, model: Model<Document>, settings: DocumentObjectFromSchemaSettings = {"type": "toDynamo"}): Promise<ObjectType> {
if (settings.checkExpiredItem && model.options.expires && ((model.options.expires as ModelExpiresSettings).items || {}).returnExpired === false && object[(model.options.expires as ModelExpiresSettings).attribute] && object[(model.options.expires as ModelExpiresSettings).attribute] * 1000 < Date.now()) {
return undefined;
}

Expand Down Expand Up @@ -309,7 +311,7 @@ Document.objectFromSchema = async function(object: any, model: Model<Document>,
if (!isValueUndefined) {
const typeDetails = model.schema.getAttributeTypeDetails(key) as DynamoDBTypeResult;
const {customType} = typeDetails;
const {type: typeInfo} = typeDetails.isOfType(value as ValueType);
const {"type": typeInfo} = typeDetails.isOfType(value as ValueType);
const isCorrectTypeAlready = typeInfo === (settings.type === "toDynamo" ? "underlying" : "main");
if (customType && !isCorrectTypeAlready) {
const customValue = customType.functions[settings.type](value);
Expand Down Expand Up @@ -376,7 +378,7 @@ Document.objectFromSchema = async function(object: any, model: Model<Document>,
await Promise.all(attributesToCheck.map(async (key) => {
const check = async (): Promise<void> => {
const value = utils.object.get(returnObject, key);
await model.schema.requiredCheck(key, (value as ValueType));
await model.schema.requiredCheck(key, value as ValueType);
};

const keyParts = key.split(".");
Expand Down Expand Up @@ -407,7 +409,7 @@ Document.objectFromSchema = async function(object: any, model: Model<Document>,

return returnObject;
};
Document.prototype.toDynamo = async function(this: Document, settings: Partial<DocumentObjectFromSchemaSettings> = {}): Promise<any> {
Document.prototype.toDynamo = async function (this: Document, settings: Partial<DocumentObjectFromSchemaSettings> = {}): Promise<any> {
const newSettings: DocumentObjectFromSchemaSettings = {
...settings,
"type": "toDynamo"
Expand All @@ -417,7 +419,7 @@ Document.prototype.toDynamo = async function(this: Document, settings: Partial<D
return Document.objectToDynamo(object);
};
// This function will modify the document to conform to the Schema
Document.prototype.conformToSchema = async function(this: Document, settings: DocumentObjectFromSchemaSettings = {"type": "fromDynamo"}): Promise<Document> {
Document.prototype.conformToSchema = async function (this: Document, settings: DocumentObjectFromSchemaSettings = {"type": "fromDynamo"}): Promise<Document> {
Document.prepareForObjectFromSchema(this, this.model, settings);
const expectedObject = await Document.objectFromSchema(this, this.model, settings);
if (!expectedObject) {
Expand Down

0 comments on commit 5beea96

Please sign in to comment.