Skip to content

Commit

Permalink
feat: add recordcounts command
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreth committed Apr 20, 2021
1 parent 0051795 commit 7628eaa
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 11 deletions.
22 changes: 11 additions & 11 deletions command-snapshot.json
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"]
}
]
8 changes: 8 additions & 0 deletions messages/recordcounts.json
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"
}
61 changes: 61 additions & 0 deletions src/commands/force/limits/recordcounts/display.ts
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);
}
}
}
43 changes: 43 additions & 0 deletions test/commands/recordcounts.test.ts
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);
});
});
});

0 comments on commit 7628eaa

Please sign in to comment.