Skip to content

Commit

Permalink
chore: update tsconfig and package.json to configure as npm library
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-williams committed Mar 16, 2024
1 parent 1ed2958 commit 7fad300
Show file tree
Hide file tree
Showing 32 changed files with 953 additions and 10 deletions.
7 changes: 7 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export { default as Table } from './src/table';
export { default as Entity } from "./src/entity";
export { default as Model } from './src/model';
export { default as DynamormFactory, DynamoRM } from './src/dynamorm';
export * from './src/decorators';
export * as types from './src/types';
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions dist/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.types = exports.DynamoRM = exports.DynamormFactory = exports.Model = exports.Entity = exports.Table = void 0;
var table_1 = require("./src/table");
Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return __importDefault(table_1).default; } });
var entity_1 = require("./src/entity");
Object.defineProperty(exports, "Entity", { enumerable: true, get: function () { return __importDefault(entity_1).default; } });
var model_1 = require("./src/model");
Object.defineProperty(exports, "Model", { enumerable: true, get: function () { return __importDefault(model_1).default; } });
var dynamorm_1 = require("./src/dynamorm");
Object.defineProperty(exports, "DynamormFactory", { enumerable: true, get: function () { return __importDefault(dynamorm_1).default; } });
Object.defineProperty(exports, "DynamoRM", { enumerable: true, get: function () { return dynamorm_1.DynamoRM; } });
__exportStar(require("./src/decorators"), exports);
exports.types = __importStar(require("./src/types"));
9 changes: 9 additions & 0 deletions dist/src/decorators.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'reflect-metadata';
import { DynamoRMOptions, ModelOptions, TableOptions } from "./types";
import { AttributeType } from "./types";
export declare function table(options: TableOptions): (constructor: any) => void;
export declare function model(options: ModelOptions): (constructor: any) => void;
export declare function dynamorm(options: DynamoRMOptions): (constructor: any) => void;
export declare function attribute(type?: AttributeType, required?: boolean): (constructor: any, key: any) => void;
export declare function Entity(constructor: any): void;
//# sourceMappingURL=decorators.d.ts.map
1 change: 1 addition & 0 deletions dist/src/decorators.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions dist/src/decorators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Entity = exports.attribute = exports.dynamorm = exports.model = exports.table = void 0;
require("reflect-metadata");
const types_1 = require("./types");
function table(options) {
return function (constructor) {
Reflect.defineMetadata("name", options.name, constructor);
Reflect.defineMetadata("primaryKey", options.primaryKey, constructor);
Reflect.defineMetadata("entity", options.entity, constructor);
};
}
exports.table = table;
function model(options) {
return function (constructor) {
Reflect.defineMetadata('table', options.table, constructor);
};
}
exports.model = model;
function dynamorm(options) {
return function (constructor) {
Reflect.defineMetadata("client", options.client, constructor);
Reflect.defineMetadata("tables", options.tables, constructor);
Reflect.defineMetadata("models", options.models, constructor);
};
}
exports.dynamorm = dynamorm;
function attribute(type = types_1.AttributeType.String, required = false) {
return function (constructor, key) {
let attributes = Reflect.getMetadata('attributes', constructor);
if (attributes) {
attributes[key] = { type, required };
}
else {
attributes = {
[key]: { type, required }
};
}
Reflect.defineMetadata('attributes', attributes, constructor);
};
}
exports.attribute = attribute;
function Entity(constructor) {
Reflect.defineMetadata('entity', constructor.name, constructor);
}
exports.Entity = Entity;
31 changes: 31 additions & 0 deletions dist/src/dynamorm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'reflect-metadata';
import { CreateTableCommandOutput } from "@aws-sdk/client-dynamodb";
import Model from "./model";
import { IDynamoRM, ModelConstructor, TableConstructor } from "./types";
/**
* @todo throw error if tables or client is undefined
*/
export declare class DynamoRM {
private readonly tables;
private readonly models;
private readonly client;
constructor(DB: Function);
getTables(): any[];
getTable(tableName: string): TableConstructor;
createTables(): Promise<CreateTableCommandOutput[]>;
getModels(): Array<ModelConstructor>;
getModel(modelName: string): ModelConstructor;
private tableToModel;
/**
* @description Gets instance of model
* @param modelName
* @param attributes
*/
model(modelName: string, attributes?: Record<string, any>): Model;
}
export declare const create: (App: Function) => IDynamoRM;
declare const _default: {
create: (App: Function) => IDynamoRM;
};
export default _default;
//# sourceMappingURL=dynamorm.d.ts.map
1 change: 1 addition & 0 deletions dist/src/dynamorm.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions dist/src/dynamorm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.create = exports.DynamoRM = void 0;
require("reflect-metadata");
const model_1 = __importDefault(require("./model"));
const index_1 = require("../index");
/**
* @todo throw error if tables or client is undefined
*/
class DynamoRM {
tables;
models;
client;
constructor(DB) {
this.tables = Reflect.getMetadata('tables', DB);
this.models = Reflect.getMetadata('models', DB);
this.client = Reflect.getMetadata('client', DB);
if (!this.tables || !this.client) {
throw new Error('A dynamodb client and tables are required');
}
}
getTables() {
return this.tables;
}
getTable(tableName) {
for (let table of this.tables) {
if (tableName === table.name) {
return table;
}
}
}
async createTables() {
const results = [];
for (let Constructor of this.tables) {
const table = new Constructor(this.client);
const result = await table.create(Constructor);
results.push(result);
}
return results;
}
getModels() {
return this.models;
}
getModel(modelName) {
for (let model of this.models) {
if (modelName == model.name) {
return model;
}
}
}
tableToModel(table) {
const attributes = table.getEntity(true).getAttributeDefinitions();
class DynamicModel extends model_1.default {
attributes = attributes;
}
;
Reflect.defineMetadata('table', table, DynamicModel);
Object.assign(DynamicModel.prototype, index_1.Entity);
return new DynamicModel(this.client);
}
/**
* @description Gets instance of model
* @param modelName
* @param attributes
*/
model(modelName, attributes) {
let Constructor = this.getModel(modelName);
let model;
if (Constructor) {
model = new Constructor(this.client);
}
else {
for (let table of this.tables) {
const entityConstructor = table.getEntity();
if (entityConstructor.name == modelName) {
model = this.tableToModel(table);
break;
}
}
}
if (!model)
return;
if (attributes) {
model.fill(attributes);
}
return model;
}
}
exports.DynamoRM = DynamoRM;
const create = (App) => {
return new DynamoRM(App);
};
exports.create = create;
exports.default = {
create: exports.create
};
6 changes: 6 additions & 0 deletions dist/src/entity.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AttributeDefinitions } from "./types";
declare class Entity {
getAttributeDefinitions(): AttributeDefinitions;
}
export default Entity;
//# sourceMappingURL=entity.d.ts.map
1 change: 1 addition & 0 deletions dist/src/entity.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions dist/src/entity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Entity {
getAttributeDefinitions() {
const properties = Reflect.ownKeys(this);
const attributes = Reflect.getMetadata('attributes', this);
const reducer = (attributeDefinitions, name) => {
if (!attributes.hasOwnProperty(name))
return attributeDefinitions;
const { type, required } = attributes[name];
attributeDefinitions[name] = { type, required, value: this[name] };
return attributeDefinitions;
};
return properties.reduce(reducer, {});
}
}
exports.default = Entity;
13 changes: 13 additions & 0 deletions dist/src/exceptions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export declare class DynamormException extends Error {
constructor(message: any);
}
export declare class TableNotFoundException extends DynamormException {
constructor(message: any);
}
export declare class ServiceUnavailableException extends DynamormException {
constructor(message: any);
}
export declare class PrimaryKeyException extends DynamormException {
constructor(message: any);
}
//# sourceMappingURL=exceptions.d.ts.map
1 change: 1 addition & 0 deletions dist/src/exceptions.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions dist/src/exceptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrimaryKeyException = exports.ServiceUnavailableException = exports.TableNotFoundException = exports.DynamormException = void 0;
class DynamormException extends Error {
constructor(message) {
super(message);
}
}
exports.DynamormException = DynamormException;
class TableNotFoundException extends DynamormException {
constructor(message) {
super(message);
}
}
exports.TableNotFoundException = TableNotFoundException;
class ServiceUnavailableException extends DynamormException {
constructor(message) {
super(message);
}
}
exports.ServiceUnavailableException = ServiceUnavailableException;
class PrimaryKeyException extends DynamormException {
constructor(message) {
super(message);
}
}
exports.PrimaryKeyException = PrimaryKeyException;
44 changes: 44 additions & 0 deletions dist/src/model.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { AttributeDefinitions, Attributes } from "./types";
import { DynamoDBClient, PutItemCommandOutput } from "@aws-sdk/client-dynamodb";
import Table from "./table";
declare class Model {
private client;
name: string;
protected table: Table;
protected attributes: AttributeDefinitions;
constructor(client: DynamoDBClient);
fill(attribute: Attributes | string, value?: any): void;
/**
* @description Sets named attribute value
* @param attributeName
* @param value
*/
set(attributeName: string, value: any): void;
/**
* @description Sets named attribute value. If attribute not set and defaultValue is not provided an error is thrown
* @param attributeName
*/
get(attributeName: string): any;
getAttributes(): AttributeDefinitions;
getAttributeValues(): {};
static getEntity(): any;
getEntity(instance?: boolean): unknown;
save(): Promise<PutItemCommandOutput>;
find(pk?: string, sk?: string): Promise<Model>;
/**
* @description Delete an item by primary key
* @param pk - Partition key
* @param sk - (optional) Sort Key
* @return boolean - True if an item was deleted. False if primary key did not match an item and nothing was deleted
*/
delete(pk?: string, sk?: string): Promise<any>;
clear(): void;
validate(): {
valid: boolean;
errors: string[];
};
private validateAttribute;
private toPutCommandInput;
}
export default Model;
//# sourceMappingURL=model.d.ts.map
1 change: 1 addition & 0 deletions dist/src/model.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7fad300

Please sign in to comment.