Skip to content

Commit

Permalink
Added Hierarchical Partition Key Support (#23919)
Browse files Browse the repository at this point in the history
* Added Hierarchical Partition Key Support

* Added Hierarchical Partition Key Support

* Fixed lint errors

* Fixed lint errors

* Refactored PartitionKey Value. Fixed Tests.

* 1. renamed stripUndefined method.
2. refactored constuctor of Item.

* Fixing build errors.

* Making Null and None parititonKey type naming consistent.

* Fixing formatting errors.

---------

Co-authored-by: FAREAST\vikassingh <vikassingh@microsoft.com>
  • Loading branch information
v1k1 and vikask00 authored Feb 2, 2023
1 parent 4e3ac05 commit ec75d88
Show file tree
Hide file tree
Showing 27 changed files with 1,772 additions and 873 deletions.
61 changes: 0 additions & 61 deletions sdk/cosmosdb/cosmos/.vscode/launch.json

This file was deleted.

35 changes: 21 additions & 14 deletions sdk/cosmosdb/cosmos/review/cosmos.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class ClientContext {
batch<T>({ body, path, partitionKey, resourceId, options, }: {
body: T;
path: string;
partitionKey: string;
partitionKey: PartitionKey;
resourceId: string;
options?: RequestOptions;
}): Promise<Response_2<any>>;
Expand Down Expand Up @@ -584,7 +584,7 @@ export interface CreateOperationInput {
// (undocumented)
operationType: typeof BulkOperationType.Create;
// (undocumented)
partitionKey?: string | number | null | Record<string, unknown> | undefined;
partitionKey?: PartitionKey;
// (undocumented)
resourceBody: JSONObject;
}
Expand Down Expand Up @@ -694,7 +694,7 @@ export interface DeleteOperationInput {
// (undocumented)
operationType: typeof BulkOperationType.Delete;
// (undocumented)
partitionKey?: string | number | null | Record<string, unknown> | undefined;
partitionKey?: PartitionKey;
}

// @public (undocumented)
Expand Down Expand Up @@ -734,8 +734,10 @@ export type ExistingKeyOperation = {
path: string;
};

// @public (undocumented)
export function extractPartitionKey(document: unknown, partitionKeyDefinition: PartitionKeyDefinition): PartitionKey[];
// Warning: (ae-forgotten-export) The symbol "PartitionKeyInternal" needs to be exported by the entry point index.d.ts
//
// @public
export function extractPartitionKey(document: unknown, partitionKeyDefinition?: PartitionKeyDefinition): PartitionKeyInternal | undefined;

// @public
export interface FeedOptions extends SharedOptions {
Expand All @@ -752,7 +754,7 @@ export interface FeedOptions extends SharedOptions {
forceQueryPlan?: boolean;
maxDegreeOfParallelism?: number;
maxItemCount?: number;
partitionKey?: any;
partitionKey?: PartitionKey;
populateQueryMetrics?: boolean;
useIncrementalFeed?: boolean;
}
Expand Down Expand Up @@ -870,7 +872,7 @@ export enum IndexKind {

// @public
export class Item {
constructor(container: Container, id: string, partitionKey: PartitionKey, clientContext: ClientContext);
constructor(container: Container, id: string, clientContext: ClientContext, partitionKey?: PartitionKey);
// (undocumented)
readonly container: Container;
delete<T extends ItemDefinition = any>(options?: RequestOptions): Promise<ItemResponse<T>>;
Expand Down Expand Up @@ -900,7 +902,7 @@ export class ItemResponse<T extends ItemDefinition> extends ResourceResponse<T &
// @public
export class Items {
constructor(container: Container, clientContext: ClientContext);
batch(operations: OperationInput[], partitionKey?: string, options?: RequestOptions): Promise<Response_2<OperationResponse[]>>;
batch(operations: OperationInput[], partitionKey?: PartitionKey, options?: RequestOptions): Promise<Response_2<OperationResponse[]>>;
bulk(operations: OperationInput[], bulkOptions?: BulkOptions, options?: RequestOptions): Promise<OperationResponse[]>;
changeFeed(partitionKey: string | number | boolean, changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<any>;
changeFeed(changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<any>;
Expand Down Expand Up @@ -1074,15 +1076,20 @@ export interface PartitionedQueryExecutionInfo {
queryRanges: QueryRange[];
}

// Warning: (ae-forgotten-export) The symbol "PrimitivePartitionKeyValue" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export type PartitionKey = PartitionKeyDefinition | string | number | unknown;
export type PartitionKey = PrimitivePartitionKeyValue | PrimitivePartitionKeyValue[];

// @public (undocumented)
export interface PartitionKeyDefinition {
// Warning: (ae-forgotten-export) The symbol "PartitionKeyKind" needs to be exported by the entry point index.d.ts
kind?: PartitionKeyKind;
paths: string[];
// (undocumented)
systemKey?: boolean;
version?: number;
// Warning: (ae-forgotten-export) The symbol "PartitionKeyDefinitionVersion" needs to be exported by the entry point index.d.ts
version?: PartitionKeyDefinitionVersion;
}

// @public (undocumented)
Expand Down Expand Up @@ -1127,7 +1134,7 @@ export interface PatchOperationInput {
// (undocumented)
operationType: typeof BulkOperationType.Patch;
// (undocumented)
partitionKey?: string | number | null | Record<string, unknown> | undefined;
partitionKey?: PartitionKey;
// (undocumented)
resourceBody: PatchRequestBody;
}
Expand Down Expand Up @@ -1381,7 +1388,7 @@ export interface ReadOperationInput {
// (undocumented)
operationType: typeof BulkOperationType.Read;
// (undocumented)
partitionKey?: string | number | boolean | null | Record<string, unknown> | undefined;
partitionKey?: PartitionKey;
}

// @public (undocumented)
Expand All @@ -1407,7 +1414,7 @@ export interface ReplaceOperationInput {
// (undocumented)
operationType: typeof BulkOperationType.Replace;
// (undocumented)
partitionKey?: string | number | null | Record<string, unknown> | undefined;
partitionKey?: PartitionKey;
// (undocumented)
resourceBody: JSONObject;
}
Expand Down Expand Up @@ -2014,7 +2021,7 @@ export interface UpsertOperationInput {
// (undocumented)
operationType: typeof BulkOperationType.Upsert;
// (undocumented)
partitionKey?: string | number | null | Record<string, unknown> | undefined;
partitionKey?: PartitionKey;
// (undocumented)
resourceBody: JSONObject;
}
Expand Down
18 changes: 14 additions & 4 deletions sdk/cosmosdb/cosmos/src/ClientContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import { Constants, HTTPMethod, OperationType, ResourceType } from "./common/con
import { getIdFromLink, getPathFromLink, parseLink } from "./common/helper";
import { StatusCodes, SubStatusCodes } from "./common/statusCodes";
import { Agent, CosmosClientOptions } from "./CosmosClientOptions";
import { ConnectionPolicy, ConsistencyLevel, DatabaseAccount, PartitionKey } from "./documents";
import {
ConnectionPolicy,
ConsistencyLevel,
DatabaseAccount,
PartitionKey,
convertToInternalPartitionKey,
} from "./documents";
import { GlobalEndpointManager } from "./globalEndpointManager";
import { PluginConfig, PluginOn, executePlugins } from "./plugins/Plugin";
import { FetchFunctionCallback, SqlQuerySpec } from "./queryExecutionContext";
Expand Down Expand Up @@ -600,7 +606,7 @@ export class ClientContext {
}: {
body: T;
path: string;
partitionKey: string;
partitionKey: PartitionKey;
resourceId: string;
options?: RequestOptions;
}): Promise<Response<any>> {
Expand Down Expand Up @@ -757,12 +763,16 @@ export class ClientContext {
options: requestContext.options,
partitionKeyRangeId: requestContext.partitionKeyRangeId,
useMultipleWriteLocations: this.connectionPolicy.useMultipleWriteLocations,
partitionKey: requestContext.partitionKey,
partitionKey:
requestContext.partitionKey !== undefined
? convertToInternalPartitionKey(requestContext.partitionKey)
: undefined, // TODO: Move this check from here to PartitionKey
});
}

/**
* Returns collection of properties which are derived from the context for Request Creation
* Returns collection of properties which are derived from the context for Request Creation.
* These properties have client wide scope, as opposed to request specific scope.
* @returns
*/
private getContextDerivedPropsForRequestCreation(): {
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/src/client/Container/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class Container {
* `const {body: replacedItem} = await container.item("<item id>", "<partition key value>").replace({id: "<item id>", title: "Updated post", authorID: 5});`
*/
public item(id: string, partitionKeyValue?: PartitionKey): Item {
return new Item(this, id, partitionKeyValue, this.clientContext);
return new Item(this, id, this.clientContext, partitionKeyValue);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions sdk/cosmosdb/cosmos/src/client/Item/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ResourceType,
StatusCodes,
} from "../../common";
import { PartitionKey } from "../../documents";
import { PartitionKey, PartitionKeyInternal, convertToInternalPartitionKey } from "../../documents";
import { extractPartitionKey, undefinedPartitionKey } from "../../extractPartitionKey";
import { RequestOptions, Response } from "../../request";
import { PatchRequestBody } from "../../utils/patch";
Expand All @@ -24,7 +24,7 @@ import { ItemResponse } from "./ItemResponse";
* @see {@link Items} for operations on all items; see `container.items`.
*/
export class Item {
private partitionKey: PartitionKey;
private partitionKey: PartitionKeyInternal;
/**
* Returns a reference URL to the resource. Used for linking in Permissions.
*/
Expand All @@ -41,10 +41,11 @@ export class Item {
constructor(
public readonly container: Container,
public readonly id: string,
partitionKey: PartitionKey,
private readonly clientContext: ClientContext
private readonly clientContext: ClientContext,
partitionKey?: PartitionKey
) {
this.partitionKey = partitionKey;
this.partitionKey =
partitionKey === undefined ? undefined : convertToInternalPartitionKey(partitionKey);
}

/**
Expand Down
Loading

0 comments on commit ec75d88

Please sign in to comment.