Skip to content

Commit

Permalink
fix: update unique property name to uniqueQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
code-xhyun committed Apr 14, 2024
1 parent bbb259f commit 8e44261
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/job/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface JobAttributes<T extends JobAttributesData = JobAttributesData>
*/
data: T | any;

unique?: any;
uniqueQuery?: any;
uniqueOpts?: {
insertOnly: boolean;
};
Expand Down
12 changes: 8 additions & 4 deletions src/job/unique.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Filter } from 'mongodb';
import { Job } from '.';

export type UniqueMethod = (unique: any, options?: { insertOnly: boolean }) => Job;
export type UniqueMethod<TSchema extends Document = Document> = (
filter: Filter<TSchema>,
options?: { insertOnly: boolean }
) => Job;
/**
* Data to ensure is unique for job to be created
* @name Job#unique
* @function
* @param unique mongo data query for unique
* @param filter mongo data query for unique
* @param options unique options
*/
export const unique: UniqueMethod = function (this: Job, unique, options?) {
this.attrs.unique = unique;
export const unique: UniqueMethod = function (this: Job, filter, options?) {
this.attrs.uniqueQuery = filter;
this.attrs.uniqueOpts = options;
return this;
};
6 changes: 3 additions & 3 deletions src/pulse/save-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ export const saveJob: SaveJobMethod = async function (this: Pulse, job) {
// Grab information needed to save job but that we don't want to persist in MongoDB
const id = job.attrs._id;

const { unique, uniqueOpts } = job.attrs;
const { uniqueQuery: unique, uniqueOpts } = job.attrs;

// Store job as JSON and remove props we don't want to store from object
const props = job.toJSON();
delete props._id;
delete props.unique;
delete props.uniqueQuery;
delete props.uniqueOpts;

// Store name of pulse queue as last modifier in job data
Expand Down Expand Up @@ -143,7 +143,7 @@ export const saveJob: SaveJobMethod = async function (this: Pulse, job) {

if (unique) {
// If we want the job to be unique, then we can upsert based on the 'unique' query object that was passed in
const query = job.attrs.unique;
const query = job.attrs.uniqueQuery;
query.name = props.name;
if (uniqueOpts?.insertOnly) {
// @ts-expect-error
Expand Down

0 comments on commit 8e44261

Please sign in to comment.