generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
[ | ||
{ | ||
"command": "force:limits:api:display", | ||
"plugin": "@salesforce/plugin-limits", | ||
"flags": [ | ||
"apiversion", | ||
"json", | ||
"loglevel", | ||
"targetusername" | ||
] | ||
} | ||
] | ||
{ | ||
"command": "force:limits:api:display", | ||
"plugin": "@salesforce/plugin-limits", | ||
"flags": ["apiversion", "json", "loglevel", "targetusername"] | ||
}, | ||
{ | ||
"command": "force:limits:recordcounts:display", | ||
"plugin": "@salesforce/plugin-limits", | ||
"flags": ["apiversion", "json", "loglevel", "targetusername", "objectnames"] | ||
} | ||
] |
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,8 @@ | ||
{ | ||
"commandDescription": "print record counts for the given objects", | ||
"examples": [ | ||
"sfdx force:limits:recordcounts:display -o Account,Contact,Lead,Opportunity", | ||
"sfdx force:limits:recordcounts:display -o Account,Contact -u [email protected]" | ||
], | ||
"objectnamesFlagDescription": "comma-separated list of object names for which to print record counts" | ||
} |
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,61 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import * as os from 'os'; | ||
import { flags, FlagsConfig, SfdxCommand, SfdxResult } from '@salesforce/command'; | ||
import { Messages, SfdxError } from '@salesforce/core'; | ||
|
||
Messages.importMessagesDirectory(__dirname); | ||
const messages = Messages.loadMessages('@salesforce/plugin-limits', 'recordcounts'); | ||
|
||
interface RecordCount { | ||
name: string; | ||
count: number; | ||
} | ||
|
||
interface Result { | ||
sObjects: RecordCount[]; | ||
} | ||
|
||
export class LimitsRecordCountsDisplayCommand extends SfdxCommand { | ||
public static readonly description = messages.getMessage('commandDescription'); | ||
public static readonly examples = messages.getMessage('examples').split(os.EOL); | ||
public static readonly requiresUsername = true; | ||
public static readonly result: SfdxResult = { | ||
tableColumnData: { | ||
columns: [ | ||
{ key: 'name', label: 'sObject' }, | ||
{ key: 'count', label: 'Record Count' }, | ||
], | ||
}, | ||
display() { | ||
if (Array.isArray(this.data) && this.data.length) { | ||
this.ux.table(this.data, this.tableColumnData); | ||
} | ||
}, | ||
}; | ||
|
||
protected static readonly flagsConfig: FlagsConfig = { | ||
objectnames: flags.array({ | ||
char: 'o', | ||
required: true, | ||
description: messages.getMessage('objectnamesFlagDescription'), | ||
}), | ||
}; | ||
|
||
public async run(): Promise<RecordCount[]> { | ||
try { | ||
const objectnamesString = (this.flags.objectnames as string[]).join(); | ||
const conn = this.org.getConnection(); | ||
const geturl = `${conn.instanceUrl}/services/data/v${conn.version}/limits/recordCount?sObjects=${objectnamesString}`; | ||
const result = ((await conn.request(geturl)) as unknown) as Result; | ||
|
||
return result.sObjects; | ||
} catch (err) { | ||
throw SfdxError.wrap(err); | ||
} | ||
} | ||
} |
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,43 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import { Connection, Org } from '@salesforce/core'; | ||
import { $$, test, expect } from '@salesforce/command/lib/test'; | ||
|
||
describe('force:limits:recordcounts:display', () => { | ||
async function prepareStubs() { | ||
$$.SANDBOX.stub(Org.prototype, 'getConnection').callsFake(() => Connection.prototype); | ||
$$.SANDBOX.stub(Connection.prototype, 'request').resolves({ | ||
sObjects: [ | ||
{ count: 34, name: 'Account' }, | ||
{ count: 116, name: 'Contact' }, | ||
], | ||
}); | ||
} | ||
|
||
it('queries and aggregates data correctly', () => { | ||
test | ||
.do(() => prepareStubs()) | ||
.stdout() | ||
.command(['force:limits:recordcounts:display', '--objectnames', 'Account,Contact,Lead', '--json']) | ||
.it('displays the expected results correctly', (ctx) => { | ||
const expected = [ | ||
{ | ||
name: 'Account', | ||
count: 34, | ||
}, | ||
{ | ||
name: 'Contact', | ||
count: 116, | ||
}, | ||
]; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access | ||
const result = JSON.parse(ctx.stdout).result; | ||
expect(result).to.deep.equal(expected); | ||
}); | ||
}); | ||
}); |