Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Add missing "get" and "set" property options #260

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface BasePropOptions {
sparse?: boolean;
expires?: string | number;
_id?: boolean;
get?: (value: any) => any;
set?: (value: any) => any;
}

export interface PropOptions extends BasePropOptions {
Expand Down
8 changes: 5 additions & 3 deletions src/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Typegoose', () => {
price: mongoose.Types.Decimal128.fromString('28189.25'),
}, {
model: 'Zastava',
price: mongoose.Types.Decimal128.fromString('1234.25'),
price: mongoose.Types.Decimal128.fromString('1234.25'),
}]);

const user = await User.create({
Expand All @@ -56,6 +56,7 @@ describe('Typegoose', () => {
title: 'Manager',
}],
previousCars: [trabant.id, zastava.id],
encodedKey: 'This an sample string',
});

{
Expand Down Expand Up @@ -89,6 +90,7 @@ describe('Typegoose', () => {

expect(foundUser).to.have.property('fullName', 'John Doe');

expect(foundUser).to.have.property('encodedKey', 'This an sample string');

const [janitor, manager] = foundUser.previousJobs;
expect(janitor).to.have.property('title', 'Janitor');
Expand Down Expand Up @@ -169,7 +171,7 @@ describe('Typegoose', () => {

expect(savedUser.languages).to.include('Hungarian');
expect(savedUser.previousJobs.length).to.be.above(0);
savedUser.previousJobs.map((prevJob) => {
savedUser.previousJobs.map((prevJob) => {
expect(prevJob.startedAt).to.be.ok;
});
});
Expand Down Expand Up @@ -288,7 +290,7 @@ describe('getClassForDocument()', () => {
it('Should validate email', async () => {
try {
await Person.create({
email: 'email',
email: 'email',
});
fail('Validation must fail.');
} catch (e) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ export class User extends Typegoose {
@arrayProp({ itemsRef: Car })
previousCars?: Ref<Car>[];

@prop({
set: (value) => value ? Buffer.from(value).toString('base64') : value,
get: (value: any) => value ? Buffer.from(value, 'base64').toString('ascii') : value,
})
encodedKey?: string;

@staticMethod
static findByAge(this: ModelType<User> & typeof User, age: number) {
return this.findOne({ age });
Expand Down