-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #765 from tchak/add-query-expression-options
Add options to QueryExpression and Operation
- Loading branch information
Showing
19 changed files
with
914 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { deepMerge } from '@orbit/utils'; | ||
import { | ||
Operation, | ||
AddRecordOperation, | ||
UpdateRecordOperation, | ||
RemoveRecordOperation, | ||
ReplaceAttributeOperation, | ||
ReplaceKeyOperation, | ||
AddToRelatedRecordsOperation, | ||
RemoveFromRelatedRecordsOperation, | ||
ReplaceRelatedRecordsOperation, | ||
ReplaceRelatedRecordOperation | ||
} from './operation'; | ||
import { RequestOptions } from './request'; | ||
import { RecordIdentity } from './record'; | ||
|
||
/** | ||
* Operation terms are used by transform builders to allow for the construction of | ||
* operations in composable patterns. | ||
*/ | ||
export class OperationTerm<T extends Operation = Operation> { | ||
operation: T; | ||
|
||
constructor(operation?: T) { | ||
this.operation = operation; | ||
} | ||
|
||
toOperation(): T { | ||
return this.operation; | ||
} | ||
|
||
options(options: RequestOptions): this { | ||
this.operation.options = deepMerge(this.operation.options || {}, options); | ||
return this; | ||
} | ||
} | ||
|
||
export class AddRecordTerm extends OperationTerm<AddRecordOperation> { | ||
constructor(record: RecordIdentity) { | ||
const operation: AddRecordOperation = { | ||
op: 'addRecord', | ||
record | ||
}; | ||
|
||
super(operation); | ||
} | ||
} | ||
|
||
export class UpdateRecordTerm extends OperationTerm<UpdateRecordOperation> { | ||
constructor(record: RecordIdentity) { | ||
const operation: UpdateRecordOperation = { | ||
op: 'updateRecord', | ||
record | ||
}; | ||
|
||
super(operation); | ||
} | ||
} | ||
|
||
export class RemoveRecordTerm extends OperationTerm<RemoveRecordOperation> { | ||
constructor(record: RecordIdentity) { | ||
const operation: RemoveRecordOperation = { | ||
op: 'removeRecord', | ||
record | ||
}; | ||
|
||
super(operation); | ||
} | ||
} | ||
|
||
export class ReplaceAttributeTerm extends OperationTerm< | ||
ReplaceAttributeOperation | ||
> { | ||
constructor(record: RecordIdentity, attribute: string, value: unknown) { | ||
const operation: ReplaceAttributeOperation = { | ||
op: 'replaceAttribute', | ||
record, | ||
attribute, | ||
value | ||
}; | ||
|
||
super(operation); | ||
} | ||
} | ||
|
||
export class ReplaceKeyTerm extends OperationTerm<ReplaceKeyOperation> { | ||
constructor(record: RecordIdentity, key: string, value: string) { | ||
const operation: ReplaceKeyOperation = { | ||
op: 'replaceKey', | ||
record, | ||
key, | ||
value | ||
}; | ||
|
||
super(operation); | ||
} | ||
} | ||
|
||
export class AddToRelatedRecordsTerm extends OperationTerm< | ||
AddToRelatedRecordsOperation | ||
> { | ||
constructor( | ||
record: RecordIdentity, | ||
relationship: string, | ||
relatedRecord: RecordIdentity | ||
) { | ||
const operation: AddToRelatedRecordsOperation = { | ||
op: 'addToRelatedRecords', | ||
record, | ||
relationship, | ||
relatedRecord | ||
}; | ||
|
||
super(operation); | ||
} | ||
} | ||
|
||
export class RemoveFromRelatedRecordsTerm extends OperationTerm< | ||
RemoveFromRelatedRecordsOperation | ||
> { | ||
constructor( | ||
record: RecordIdentity, | ||
relationship: string, | ||
relatedRecord: RecordIdentity | ||
) { | ||
const operation: RemoveFromRelatedRecordsOperation = { | ||
op: 'removeFromRelatedRecords', | ||
record, | ||
relationship, | ||
relatedRecord | ||
}; | ||
|
||
super(operation); | ||
} | ||
} | ||
|
||
export class ReplaceRelatedRecordsTerm extends OperationTerm< | ||
ReplaceRelatedRecordsOperation | ||
> { | ||
constructor( | ||
record: RecordIdentity, | ||
relationship: string, | ||
relatedRecords: RecordIdentity[] | ||
) { | ||
const operation: ReplaceRelatedRecordsOperation = { | ||
op: 'replaceRelatedRecords', | ||
record, | ||
relationship, | ||
relatedRecords | ||
}; | ||
|
||
super(operation); | ||
} | ||
} | ||
|
||
export class ReplaceRelatedRecordTerm extends OperationTerm< | ||
ReplaceRelatedRecordOperation | ||
> { | ||
constructor( | ||
record: RecordIdentity, | ||
relationship: string, | ||
relatedRecord: RecordIdentity | null | ||
) { | ||
const operation: ReplaceRelatedRecordOperation = { | ||
op: 'replaceRelatedRecord', | ||
record, | ||
relationship, | ||
relatedRecord | ||
}; | ||
|
||
super(operation); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.