techveda-lib-dynamodb
Version:
A clean DynamoDB abstraction library with schema and model for Node.js and TypeScript
86 lines (76 loc) • 2.97 kB
text/typescript
import * as _aws_sdk_lib_dynamodb from '@aws-sdk/lib-dynamodb';
import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
import * as joi from 'joi';
declare class Model {
[key: string]: any;
modelName: string;
schema: ReturnType<any["getSchema"]>;
PKPrefix: string;
SKPrefix?: string;
constructor(modelName: string, schemaInstance: any, PKPrefix: string, SKPrefix?: string);
buildJoiSchema: () => joi.ObjectSchema<any>;
validate: (data: Record<string, any>) => Record<string, any>;
create: (data: Record<string, any>) => Promise<any>;
save: () => Promise<any>;
find: (filters?: Record<string, any>, limit?: number) => Promise<{
data: any[];
LastEvaluatedKey: Record<string, any> | undefined;
}>;
list: (filters?: Record<string, any>, limit?: number) => Promise<{
data: any[];
LastEvaluatedKey: Record<string, any> | undefined;
}>;
findAndUpdate: (updates: Record<string, any>) => Promise<any>;
scan: (filters?: Record<string, any>, limit?: number) => Promise<_aws_sdk_lib_dynamodb.ScanCommandOutput>;
}
type SchemaFieldConfig = {
type?: any;
required?: boolean;
enum?: any[];
default?: any;
minLength?: number;
maxLength?: number;
min?: number;
max?: number;
isConditionalRequired?: boolean;
conditionKey?: string;
compareValue?: any;
conditionType?: "equalTo" | "contains" | "isValuePresent";
ref?: string;
};
type InternalField = {
key: string;
type?: string;
required?: boolean;
enum?: any[];
default?: any;
minLength?: number;
maxLength?: number;
min?: number;
max?: number;
isConditionalRequired?: boolean;
conditionKey?: string;
compareValue?: any;
conditionType?: "equalTo" | "contains" | "isValuePresent";
ref?: string;
};
declare class Schema {
fields: InternalField[];
constructor(definition?: Record<string, SchemaFieldConfig>);
private parseType;
getSchema(): InternalField[];
}
declare const generateKeys: (modelName: string, id: string, skType?: string, skId?: string) => {
PK: string;
SK: string;
};
declare const removeExtraFields: (data?: Record<string, any>, validKeys?: string[]) => {
[k: string]: any;
};
declare const putDataCommand: (item: Record<string, any>) => Promise<Record<string, any>>;
declare const allQueryByParams: (params: any) => Promise<_aws_sdk_lib_dynamodb.QueryCommandOutput>;
declare const callUpdateByParams: (params: any) => Promise<Record<string, any> | undefined>;
declare const getDataInScanCommand: (expressions: any) => Promise<_aws_sdk_lib_dynamodb.ScanCommandOutput>;
declare let docClient: DynamoDBDocumentClient;
declare const initDynamoDB: () => DynamoDBDocumentClient;
export { type InternalField, Model, Schema, type SchemaFieldConfig, allQueryByParams, callUpdateByParams, docClient, generateKeys, getDataInScanCommand, initDynamoDB, putDataCommand, removeExtraFields };