npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fetchxml4js

v1.0.5

Published

A declarative JavaScript library for creating Dataverse fetchxml queries

Downloads

5

Readme

fetchxml4js

A declarative JavaScript library for creating Dataverse fetchxml queries. The package simplifies the creation of complex queries by allowing you to build them using intuitive JavaScript functions and chaining methods.

Table of Contents

Installation

Npm:

npm install fetchxml4js --save

Npm TypeScript definitions:

npm install @types/fetchxml4js --save-dev

Usage

In browser:

<script type="text/javascript" src="//fetchxml4js.js"></script>

In node:

var caml4js = require('fetchxml4js');

ES6 modules:

import {LinkType, attributes, choiceColumn} from 'fetchxml4js';

Basic Concepts

Before diving into usage, it's important to understand some basic components:

  1. Entity: The primary object in the query.
  2. Attributes: The fields or columns of the entity you wish to retrieve.
  3. Filter: Conditions applied to restrict the data returned.
  4. Link Entity: Used to join another related entity.
  5. Order: Determines the sorting of the results.

Example Query

Here's an example of a FetchXML query created using this package:

fetchxml({ entity: "contact" },
    attributes("fullname"),
    filterAnd(
        idColumn('leadid').equalTo('8788facf-828e-4333-8405-b825b0f29ea0'),
        choiceColumn('statecode').equalTo(0)
    ),
    linkEntitySimple("lead", LinkType.LEFT, "emailaddress1", "emailaddress1", '',       
        attributes("fullname")
    ),
    orderBy({ logicalName: "createdon", desc: true })
);

This code generates the following FetchXML:

<fetch>
    <entity name='contact'>
        <attribute name='fullname'/>
        <filter type='and'>
            <condition attribute='leadid' operator='eq' value='8788facf-828e-4333-8405-b825b0f29ea0' />
            <condition attribute='statecode' operator='eq' value='0' />
        </filter>
        <link-entity name='lead' link-type='outer' to='emailaddress1' from='emailaddress1'>
            <attribute name='fullname'/>
        </link-entity>
        <order attribute='createdon' descending='true'/>
    </entity>
</fetch>

API Overview

Below are the key functions provided by the package:

  1. fetchxml(entity, ...elements): Main function to start building a FetchXML query.

    • entity: IFetchOption - Specifies the options for the query.
    • ...elements: Accepts attributes, filters, link entities, and orders.
  2. attributes(...names|...IAttribute): Specifies the attributes (fields) to be retrieved.

    • Example: attributes("fullname", "emailaddress1",{name:"telephone",alias:"phone", distint:true}).
  3. filterAnd(...conditions): Combines multiple conditions using an AND logical operator.

    • Example: filterAnd(idColumn('leadid').equalTo('1234'), textColumn('name').like('John%')).
  4. filterOr(...conditions): Combines multiple conditions using an OR logical operator.

    • Example: filterOr(textColumn('firstname').like('A%'), textColumn('lastname').like('B%')).
  5. idColumn(name): Creates an operator for filtering by ID.

    • Example: idColumn('leadid').equalTo('1234').
  6. textColumn(name): Creates an operator for filtering text fields.

    • Example: textColumn('fullname').like('John%').
  7. numberColumn(name): Creates an operator for filtering numeric fields.

    • Example: numberColumn('age').greaterThan(25).
  8. choiceColumn(name): Creates an operator for filtering choice fields.

    • Example: choiceColumn('statecode').equalTo(0).
  9. booleanColumn(name): Creates an operator for filtering boolean fields.

    • Example: booleanColumn('isActive').isTrue().
  10. linkEntitySimple(name, linkType, to, from, alias, ...components): Simplifies linking entities.

    • Example: linkEntitySimple("lead", LinkType.LEFT, "emailaddress1", "emailaddress1", '', attributes("fullname")).
  11. linkEntity(linkEntity, ...elements): Main function to start building a FetchXML query.

    • linkEntity: ILinkEntity - link entity attributes object
    • ...elements: Elements to include within the link-entity.
  12. orderBy(options): Orders the results based on a specified attribute.

    • options: { logicalName: string, desc?: boolean }.
    • Example: orderBy({ logicalName: "createdon", desc: true }).

Examples

Simple Query:

fetchxml({ entity: "account" },
    attributes("name", "accountnumber"),
    filterAnd(textColumn('name').beginsWith('A'))
);

Complex Query:

fetchxml({ entity: "order" },
    attributes("ordernumber", "totalamount"),
    filterOr(
        idColumn('customerid').in(['1111', '2222']),
        dateColumn('orderdate').thisYear()
    ),
    orderBy({ logicalName: "totalamount", desc: true })
);

Linked Entities::

fetchxml({ entity: "invoice" },
    attributes("invoicenumber"),
    linkEntitySimple("customer", LinkType.INNER, "customerid", "customerid", 'cust',
        attributes("fullname"))
);