Skip to content

Commit

Permalink
feat: improved retrieveMultiple
Browse files Browse the repository at this point in the history
  • Loading branch information
BetimBeja committed Dec 19, 2023
1 parent 3e204e1 commit 2b7bd6e
Show file tree
Hide file tree
Showing 9 changed files with 1,154 additions and 414 deletions.
51 changes: 51 additions & 0 deletions __sample-components__/WebAPIControl/css/WebAPIControl.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
This file is part of the Microsoft PowerApps code samples.
Copyright (C) Microsoft Corporation. All rights reserved.
This source code is intended only as a supplement to Microsoft Development Tools and/or
on-line documentation. See these other materials for detailed information regarding
Microsoft code samples.
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*/

.SampleNamespace\.WebAPIControl
{
font-family: 'SegoeUI-Semibold', 'Segoe UI Semibold', 'Segoe UI Regular', 'Segoe UI';
color: #1160B7;
}

.SampleNamespace\.WebAPIControl .WebAPIControl_Container
{
overflow-x: auto;
}

.SampleNamespace\.WebAPIControl .SampleControl_WebAPIControl_Header
{
color: rgb(51, 51, 51);
font-size: 1rem;
padding-top: 20px;
}

.SampleNamespace\.WebAPIControl .result_container
{
padding-bottom: 20px;
}

.SampleNamespace\.WebAPIControl .SampleControl_WebAPIControl_ButtonClass
{
text-decoration: none;
display: inline-block;
font-size: 14px;
cursor: pointer;
color: #1160B7;
background-color: #FFFFFF;
border: 1px solid black;
padding: 5px;
text-align: center;
min-width: 300px;
margin-top: 10px;
margin-bottom: 5px;
display: block;
}
19 changes: 19 additions & 0 deletions __sample-components__/WebAPIControl/generated/ManifestTypes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
This file is part of the Microsoft PowerApps code samples.
Copyright (C) Microsoft Corporation. All rights reserved.
This source code is intended only as a supplement to Microsoft Development Tools and/or
on-line documentation. See these other materials for detailed information regarding
Microsoft code samples.
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*/

// Define IInputs and IOutputs Type. They should match with ControlManifest.
export interface IInputs {
stringProperty: ComponentFramework.PropertyTypes.StringProperty;
}
export interface IOutputs {
stringProperty?: string;
}
499 changes: 499 additions & 0 deletions __sample-components__/WebAPIControl/index.ts

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions __tests__/WebApiMock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,24 @@ describe('WebApiMock', () => {
});
});
});

describe('OData', ()=>{
it('Should retrieve multiple records using OData', async () => {
const result = await mockGenerator.context.webAPI.retrieveMultipleRecords(
'systemuser',
'?$select=systemuserid,firstname,lastname',
);

const betimBeja = result.entities.find(
(e) => e['systemuserid'] === '682d1eb3-0ba4-ed11-aad1-000d3add5311',
);
expect(betimBeja).toEqual({
systemuserid: '682d1eb3-0ba4-ed11-aad1-000d3add5311',
firstname: 'Betim',
lastname: 'Beja',
});
});
});
});

it('Should update record', async () => {
Expand Down
50 changes: 47 additions & 3 deletions src/ComponentFramework-Mock-Generator/Metadata.db/Metadata.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getSqlTypeForAttribute } from './getSQLTypeForAttribute';
import { AttributeType, OptionSetType } from '../../ComponentFramework-Mock';
import { SavedQueryMetadata } from './PlatformMetadata/SavedQuery.Metadata';
import { UserQueryMetadata } from './PlatformMetadata/UserQuery.Metadata';
import { ODataQuery } from '@shko.online/dataverse-odata';

export class MetadataDB {
/**
Expand Down Expand Up @@ -732,7 +733,7 @@ export class MetadataDB {
return result;
}
if (result.entityMetadata && result.entityMetadata.Attributes && result.row) {
result.entityMetadata.Attributes?.filter((attr) => attr.AttributeType === AttributeType.Lookup).forEach(
result.entityMetadata.Attributes.filter((attr) => attr.AttributeType === AttributeType.Lookup).forEach(
(lookupAttribute) => {
const key = lookupAttribute.LogicalName;
const lookupValue = result.row[key] as ComponentFramework.LookupValue;
Expand Down Expand Up @@ -856,9 +857,52 @@ export class MetadataDB {

var attributesX = entityNode.getElementsByTagName('attribute');
const attributes: string[] = [];
for (let i = 0; i < attributesX.length; i++) {
attributes.push(attributesX[i].getAttribute('name') as string);


if(fetchNode.getAttribute('aggregate') === 'true'){
for (let i = 0; i < attributesX.length; i++) {
const attribute = attributesX[i].getAttribute('name') as string;
const aggregate = attributesX[i].getAttribute('aggregate') as string;
const alias = attributesX[i].getAttribute('alias') as string;
attributes.push(`${aggregate}(${attribute}) as [${alias}]`);
}
}else{
for (let i = 0; i < attributesX.length; i++) {
attributes.push(attributesX[i].getAttribute('name') as string);
}
}

return this.db.exec(`SELECT ${attributes.join(',')} FROM ${entityNode.getAttribute('name')}`);
}

SelectUsingOData(tableMetadata: ShkoOnline.EntityMetadata, query: ODataQuery) {
const safeTableName = tableMetadata.LogicalName.toLowerCase().replace(/\!/g, '_').replace(/\@/g, '_');

const attributes: string[] = [];

if (query.$select && tableMetadata.Attributes) {
tableMetadata.Attributes.forEach((attribute) => {
if (
attribute.AttributeType === AttributeType.Lookup &&
query.$select?.find((a) => a === `_${attribute.LogicalName}_value`)
) {
attributes.push(attribute.LogicalName);
attributes.push(attribute.LogicalName + 'name');
attributes.push(attribute.LogicalName + 'type');
} else if (query.$select?.find((a) => a === attribute.LogicalName)) {
attributes.push(attribute.LogicalName);
if (
attribute.AttributeType === AttributeType.Boolean ||
attribute.AttributeType === AttributeType.Picklist ||
attribute.AttributeType === AttributeType.State ||
attribute.AttributeType === AttributeType.Status
) {
attributes.push(attribute.LogicalName + 'name');
}
}
});
}

return this.db.exec(`SELECT ${attributes.join(',')} FROM ${safeTableName}`);
}
}
Loading

0 comments on commit 2b7bd6e

Please sign in to comment.