Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates to address docs for entity parsers and fix missing export in … #1035

Merged
merged 3 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fixes #X, mentioned in #Y
*You can delete this section when you are submitting the pull request.*

* Please update this PR information accordingly. We'll use this as part of our release notes in monthly communications.
* Please target your PR to "dev-v2" branch for v2 changes and "version-1" branch for v1 changes
* Please target your PR to "version-2" branch for v2 changes and "version-1" branch for v1 changes
* Please ensure you have updated any associated docs files based on your code changes
* Please ensure you have updated/added tests to cover your change.
* If you are fixing a bug include a test that would have caught the bug you are fixing
Expand Down
2 changes: 1 addition & 1 deletion docs/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The following packages comprise the Patterns and Practices client side libraries. All of the packages are published as a set and depend on their peers within
the @pnp scope.

The latest published version is **{{version}}**.
The latest published version is [![npm version](https://badge.fury.io/js/%40pnp%2Fcommon.svg)](https://badge.fury.io/js/%40pnp%2Fcommon).

| || |
| ---| -------------|-------------|
Expand Down
28 changes: 24 additions & 4 deletions docs/sp/entity-merging.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@

Sometimes when we make a query entity's data we would like then to immediately run other commands on the returned entity. To have data returned as its representing type we make use of the _spODataEntity_ and _spODataEntityArray_ parsers. The below approach works for all instance types such as List, Web, Item, or Field as examples.

## Importing spODataEntity and spODataEntityArray

You can import spODataEntity and spODataEntityArray in two ways, depending on your use case. The simplest way is to use the presets/all import as shown in the examples. The downside of this approach is that you can't take advantage of selective imports.

If you want to take advantage of selective imports while using either of the entity parsers you can use:

```TypeScript
import { spODataEntity, spODataEntityArray } from "@pnp/sp/odata";
```

The full selective import for the first sample would be:

```TypeScript
import { sp } from "@pnp/sp";
import { spODataEntity } from "@pnp/sp/odata";
import { Item, IItem } from "@pnp/sp/items";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
```

## Request a single entity

If we are loading a single entity we use the _spODataEntity_ method. Here we show loading a list item using the Item class and a simple get query.

```TypeScript
import { sp, spODataEntity, Item } from "@pnp/sp/presets/all";
import { sp, spODataEntity, Item, IItem } from "@pnp/sp/presets/all";

// interface defining the returned properties
interface MyProps {
Expand All @@ -17,7 +37,7 @@ interface MyProps {
try {

// get a list item loaded with data and merged into an instance of Item
const item = await sp.web.lists.getByTitle("ListTitle").items.getById(1).get(spODataEntity<Item, MyProps>(Item));
const item = await sp.web.lists.getByTitle("ListTitle").items.getById(1).usingParser(spODataEntity<IItem, MyProps>(Item))();

// log the item id, all properties specified in MyProps will be type checked
Logger.write(`Item id: ${item.Id}`);
Expand All @@ -37,7 +57,7 @@ try {
The same pattern works when requesting a collection of objects with the exception of using the _spODataEntityArray_ method.

```TypeScript
import { sp, spODataEntityArray, Item } from "@pnp/sp/presets/all";
import { sp, spODataEntityArray, Item, IItem } from "@pnp/sp/presets/all";

// interface defining the returned properties
interface MyProps {
Expand All @@ -48,7 +68,7 @@ interface MyProps {
try {

// get a list item loaded with data and merged into an instance of Item
const items = await sp.web.lists.getByTitle("ListTitle").items.select("Id", "Title").get(spODataEntityArray<Item, MyProps>(Item));
const items = await sp.web.lists.getByTitle("OrderByList").items.select("Id", "Title").usingParser(spODataEntityArray<IItem, MyProps>(Item))();

Logger.write(`Item id: ${items.length}`);

Expand Down
1 change: 1 addition & 0 deletions packages/sp/presets/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export * from "../hubsites";
export * from "../items";
export * from "../lists";
export * from "../navigation";
export { spODataEntity, spODataEntityArray } from "../odata";
export * from "../profiles";
export * from "../regional-settings";
export * from "../related-items";
Expand Down