From 7628eaab70a6b1fc9b015f22b4040de198d53745 Mon Sep 17 00:00:00 2001 From: Magnus Kreth Date: Tue, 20 Apr 2021 15:31:41 +0200 Subject: [PATCH] feat: add recordcounts command --- command-snapshot.json | 22 +++---- messages/recordcounts.json | 8 +++ .../force/limits/recordcounts/display.ts | 61 +++++++++++++++++++ test/commands/recordcounts.test.ts | 43 +++++++++++++ 4 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 messages/recordcounts.json create mode 100644 src/commands/force/limits/recordcounts/display.ts create mode 100644 test/commands/recordcounts.test.ts diff --git a/command-snapshot.json b/command-snapshot.json index 0217ef61..ae0284cb 100644 --- a/command-snapshot.json +++ b/command-snapshot.json @@ -1,12 +1,12 @@ [ - { - "command": "force:limits:api:display", - "plugin": "@salesforce/plugin-limits", - "flags": [ - "apiversion", - "json", - "loglevel", - "targetusername" - ] - } -] \ No newline at end of file + { + "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"] + } +] diff --git a/messages/recordcounts.json b/messages/recordcounts.json new file mode 100644 index 00000000..487c7bc6 --- /dev/null +++ b/messages/recordcounts.json @@ -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 me@my.org" + ], + "objectnamesFlagDescription": "comma-separated list of object names for which to print record counts" +} diff --git a/src/commands/force/limits/recordcounts/display.ts b/src/commands/force/limits/recordcounts/display.ts new file mode 100644 index 00000000..79e9c535 --- /dev/null +++ b/src/commands/force/limits/recordcounts/display.ts @@ -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 { + 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); + } + } +} diff --git a/test/commands/recordcounts.test.ts b/test/commands/recordcounts.test.ts new file mode 100644 index 00000000..cf659c1f --- /dev/null +++ b/test/commands/recordcounts.test.ts @@ -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); + }); + }); +});