-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.ts
79 lines (68 loc) · 2.57 KB
/
query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { flags, SfdxCommand } from '@salesforce/command';
import * as chalk from 'chalk';
export default class SOSLQuery extends SfdxCommand {
public static description = `Runs a sosl query. SOSL Reference: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_syntax.htm`;
public static aliases = [
'shane:data:sosl:query',
'force:data:sosl:query',
'shane:data:search',
'force:data:search',
'force:data:sosl',
'shane:data:sosl'
];
public static examples = [
`sfdx force:data:sosl:query -q "find {something}"
`,
`sfdx force:data:sosl:query -q "find {Jack} returning User(Name), Account(Name),Contact(FirstName,LastName,Department)" -u platformers
// search across several objects with different results fields on a specified org
`
];
protected static flagsConfig = {
query: flags.string({
char: 'q',
description: 'SOSL query',
required: true
})
};
protected static requiresUsername = true;
public async run(): Promise<any> {
const conn = this.org.getConnection();
const result = ((await conn.request({
url: `${conn.baseUrl()}/search/?q=${encodeURI(this.flags.query)}`
})) as unknown) as SOSLResponse;
const types = [...new Set(result.searchRecords.map((row) => row.attributes.type))];
// iterate the types, one table for each
for (const objType of types) {
this.ux.log();
this.ux.log(chalk.blueBright(objType));
const fields = [
...new Set(...result.searchRecords.filter((row) => row.attributes.type === objType).map((row) => Object.keys(row)))
].filter((property) => property !== 'attributes');
this.ux.table(
result.searchRecords.filter((row) => row.attributes.type === objType),
{
columns: [
{
key: 'attributes.url',
label: 'ID',
get: (row: any) => row.attributes.url.substr(row.attributes.url.lastIndexOf('/') + 1)
},
...fields.map((field) => ({ key: field }))
]
}
);
this.ux.log();
}
return result;
}
}
interface SOSLRecord {
attributes: {
type: string;
url: string;
};
[key: string]: unknown;
}
interface SOSLResponse {
searchRecords: SOSLRecord[];
}