Skip to content
Open
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
6 changes: 5 additions & 1 deletion lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,11 @@ class Query {
propertyOptions: optionsObject
) {
if (!Object.prototype.hasOwnProperty.call(queryObject, propertyName)) {
queryObject[propertyName] = propertyOptions.default;
if (typeof propertyOptions.default === 'function') {
queryObject[propertyName] = propertyOptions.default();
} else {
queryObject[propertyName] = propertyOptions.default;
}
}
return true;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/test_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ describe('test Query methods', () => {
required: false,
default: null,
},
createdAt: {
type: 'date',
required: false,
default: () => new Date()
}
};

const collectionName = 'testCollection';
Expand Down Expand Up @@ -101,6 +106,14 @@ describe('test Query methods', () => {
assertStrictEquals(queryObject[propertyName], null);
});

it('will set property value in queryObject by calling default if it is a function', () => {
queryObject = { name: 'Mr. D' };
propertyName = 'createdAt'
propertyOptions = testSchema.schemaMap[propertyName];
query.setDefault(queryObject, propertyName, propertyOptions);
assertInstanceOf(queryObject[propertyName], Date);
})

it('will not assign a default value to a property in queryObject if it already has an assigned value', () => {
queryObject = { name: 'Mr. D', occupation: 'baker' };
query.setDefault(queryObject, propertyName, propertyOptions);
Expand Down