Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#50220 Add 'search' method to connection.js…
Browse files Browse the repository at this point in the history
… by @douglascayers

* add 'search' method

* add 'search' tests

Co-authored-by: trailhead-content-bot <trailhead_content_publish_toolchain@salesforce.com>
  • Loading branch information
douglascayers and trailhead-content-bot authored Dec 22, 2020
1 parent 7d42392 commit 9dc5415
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions types/jsforce/connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ export interface ExecuteAnonymousResult {

export type ConnectionEvent = "refresh";

export interface SearchResult<T> {
searchRecords: Record<T>[];
}

/**
* the methods exposed here are done so that a client can use 'declaration augmentation' to get intellisense on their own projects.
* for example, given a type
Expand Down Expand Up @@ -217,6 +221,7 @@ export abstract class BaseConnection extends EventEmitter {
recent(callback?: (err: Error, result: RecordResult[]) => void): Promise<(RecordResult[])>;
recent(param: number | string, callback?: (err: Error, result: RecordResult[]) => void): Promise<(RecordResult[])>;
recent(type: string, limit: number, callback?: (err: Error, result: RecordResult[]) => void): Promise<(RecordResult[])>;
search<T>(sosl: string, callback?: (err: Error, result: SearchResult<T>) => void): Promise<(SearchResult<T>)>;
}

export class Connection extends BaseConnection {
Expand Down
19 changes: 19 additions & 0 deletions types/jsforce/jsforce-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RecordReference, Record } from 'jsforce/record';
import { SObject } from 'jsforce/salesforce-object';
import { RecordResult } from 'jsforce/record-result';
import { BatchDescribeSObjectOptions, DescribeSObjectOptions, DescribeSObjectResult } from 'jsforce/describe-result';
import { SearchResult } from 'jsforce/connection';

const salesforceConnection: sf.Connection = new sf.Connection({
instanceUrl: '',
Expand Down Expand Up @@ -467,6 +468,10 @@ const requestInfo: sf.RequestInfo = {
interface MyFoo {
anything: string;
}
interface MyBar {
something: string;
}

salesforceConnection.request<MyFoo>(requestInfo).then((myFoo: MyFoo) => {
console.log(myFoo.anything)
});
Expand Down Expand Up @@ -501,6 +506,20 @@ salesforceConnection.query('SELECT Id FROM Account')
})
.run({ autoFetch: true, maxFetch: 25 });

salesforceConnection.search<MyFoo|MyBar>('FIND {my} IN ALL FIELDS RETURNING Foo__c(Id, Name), Bar__c(Id, Name)', (err: Error, result: SearchResult<MyFoo|MyBar>) => {
console.error(err);
for (const record of result.searchRecords) {
if (record && record.attributes && record.attributes.type === 'Foo__c') {
const foo = record as MyFoo;
console.log(foo.anything);
}
if (record && record.attributes && record.attributes.type === 'Bar__c') {
const bar = record as MyBar;
console.log(bar.something);
}
}
});

salesforceConnection.sobject<any>('Coverage__c')
.select(['Id', 'Name']).del(() => { });

Expand Down
10 changes: 9 additions & 1 deletion types/jsforce/record.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ export class RecordReference<T = any> {
update(record: Partial<T>, options?: Object, callback?: (err: Error, result: RecordResult) => void): Promise<RecordResult>;
}

export type Record<T = any> = { Id?: SalesforceId } & T;
export interface RecordAttributes {
type: string;
url: string;
}

export type Record<T = any> = {
Id?: SalesforceId;
attributes?: RecordAttributes
} & T;

0 comments on commit 9dc5415

Please sign in to comment.