peak-ai / jedlik

DynamoDB ODM for Node
MIT License
106 stars 4 forks source link

Ability to add methods to Documents and Models #14

Open harrim91 opened 4 years ago

harrim91 commented 4 years ago

Is your feature request related to a problem? Please describe. We should be able to add methods to customise the behaviour models and documents.

Something like this...

interface UserProps {
  id: number;
  name: string;
}

interface UserMethods {
  sayHello: () => string;
}

interface UserStatics {
  count: () => number;
}

const schema = Joi.object(...);

const methods: UserMethods = {
  sayHello(): string {
    return `Hi, I'm ${this.name}`;
  }
};

const statics: UserStatics = {
  count(): number {
    const n = countTheItemsInTheTableSomehow()
    return n;
  }
};

const Users = new jedlik.Model<UserProps, UserStatics, UserMethods>({ table: 'users', schema, statics, methods });

Users.count(); // returns 0;

const user = User.create({ id: 1, name: 'Michael' });

await user.save()

Users.count(); // returns 1;

user.sayHello(); // returns 'Hi, I'm Michael'
issue-label-bot[bot] commented 4 years ago

Issue-Label Bot is automatically applying the label feature_request to this issue, with a confidence of 0.98. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.