Skip to content

Commit

Permalink
🎉Support composite index in CosmosDB🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
infhyroyage committed Sep 4, 2024
1 parent 3bbe134 commit c5d1b68
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
31 changes: 14 additions & 17 deletions cosmosdb/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,20 @@ export const createDatabasesAndContainers = async (
await databaseRes.database.containers.createIfNotExists({
id: "Test",
partitionKey: "/id",
// Azure上では複合インデックスを作成するインデックスポリシーを定義しているが、
// 2023/07/16現在、Azure SDK for JavaScriptでは未サポートのためlocalhost環境上では定義しない
// https://github.com/Azure/azure-sdk-for-js/issues/21115
// indexingPolicy: {
// compositeIndexes: [
// [
// {
// path: "/courseName",
// order: "ascending",
// },
// {
// path: "/testName",
// order: "ascending",
// },
// ],
// ],
// },
indexingPolicy: {
compositeIndexes: [
[
{
path: "/courseName",
order: "ascending",
},
{
path: "/testName",
order: "ascending",
},
],
],
},
});

// UsersデータベースのQuestionコンテナー
Expand Down
12 changes: 2 additions & 10 deletions functions/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
InvocationContext,
} from "@azure/functions";
import { Test } from "../cosmosDB";
import { getReadOnlyContainer } from "./cosmosDBWrapper";
import { GetTests } from "../functions";
import { getReadOnlyContainer } from "./cosmosDBWrapper";

const COSMOS_DB_DATABASE_NAME = "Users";
const COSMOS_DB_CONTAINER_NAME = "Test";
Expand All @@ -18,15 +18,7 @@ export default async function (
try {
// Cosmos DBのUsersデータベースのTestコンテナーから全項目取得
const query: SqlQuerySpec = {
query: `SELECT c.id, c.courseName, c.testName, c.length FROM c${
// Azure上では複合インデックスを作成するインデックスポリシーを定義しているが、
// 2023/07/16現在、Azure SDK for JavaScriptでは未サポートでありlocalhost環境上では定義していないため、
// Azure上のみORDER BY句を設定する
// https://github.com/Azure/azure-sdk-for-js/issues/21115
process.env["COSMOSDB_URI"] === "https://localhost:8081"
? ""
: " ORDER BY c.courseName ASC, c.testName ASC"
}`,
query: `SELECT c.id, c.courseName, c.testName, c.length FROM c ORDER BY c.courseName ASC, c.testName ASC`,
};
const response: FeedResponse<Test> = await getReadOnlyContainer(
COSMOS_DB_DATABASE_NAME,
Expand Down

0 comments on commit c5d1b68

Please sign in to comment.