-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http-client): createQueryIndex and updateQueryIndex
- Loading branch information
1 parent
7a64a85
commit 0c0fed9
Showing
9 changed files
with
293 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/http-client/src/services/query/createQueryIndex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2023-Present Jonathan MASSUCHETTI <jonathan.massuchetti@dappit.fr>. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Keyspace, keyspacePath, quoteIdentifier } from '@cbjsdev/shared'; | ||
|
||
import { CouchbaseHttpApiConfig } from '../../types.js'; | ||
import { ApiQueryResponseBody } from '../../types/Api/index.js'; | ||
import { createHttpError } from '../../utils/createHttpError.js'; | ||
import { requestExecuteStatement } from './requests/requestExecuteStatement.js'; | ||
|
||
/** | ||
* Create a query index. | ||
*/ | ||
export async function createQueryIndex( | ||
params: CouchbaseHttpApiConfig, | ||
indexName: string, | ||
keyspace: Keyspace, | ||
config: { | ||
/** | ||
* The document keys to index. | ||
*/ | ||
keys: string[]; | ||
/** | ||
* Only index documents that match this where clause. | ||
*/ | ||
where?: string; | ||
/** | ||
* The number of replicas of this index that should be created. | ||
*/ | ||
numReplicas?: number; | ||
}, | ||
options?: CreateQueryIndexOptions | ||
): Promise<CreateQueryIndexResponse['results']> { | ||
const { keys, where, numReplicas } = config; | ||
const { deferred = false } = options ?? {}; | ||
|
||
let query = `CREATE INDEX ${quoteIdentifier(indexName)} | ||
ON ${keyspacePath(keyspace)} | ||
(${keys.join(',')})`; | ||
|
||
if (where) { | ||
query += ` WHERE ${where} `; | ||
} | ||
|
||
const withConfig: { num_replica?: number; defer_build?: boolean } = {}; | ||
|
||
if (numReplicas) { | ||
withConfig.num_replica = numReplicas; | ||
} | ||
|
||
if (deferred) { | ||
withConfig.defer_build = deferred; | ||
} | ||
|
||
if (Object.keys(withConfig).length > 0) { | ||
query += ` WITH ${JSON.stringify(withConfig)} `; | ||
} | ||
|
||
const response = await requestExecuteStatement(params, query); | ||
|
||
if (response.status !== 200) { | ||
throw await createHttpError('GET', response); | ||
} | ||
|
||
const body = (await response.json()) as CreateQueryIndexResponse; | ||
return body.results; | ||
} | ||
|
||
export type CreateQueryIndexOptions = { | ||
/** | ||
* Specifies whether this index creation should be deferred until a later | ||
* point in time when they can be explicitly built together. | ||
*/ | ||
deferred?: boolean; | ||
}; | ||
|
||
export type CreateQueryIndexResponse = ApiQueryResponseBody< | ||
Array<{ id: string; name: string; state: string }>, | ||
null | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/http-client/src/services/query/updateQueryIndex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) 2023-Present Jonathan MASSUCHETTI <jonathan.massuchetti@dappit.fr>. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Keyspace, keyspacePath, quoteIdentifier } from '@cbjsdev/shared'; | ||
|
||
import { CouchbaseHttpApiConfig } from '../../types.js'; | ||
import { ApiQueryResponseBody } from '../../types/Api/index.js'; | ||
import { createHttpError } from '../../utils/createHttpError.js'; | ||
import { requestExecuteStatement } from './requests/requestExecuteStatement.js'; | ||
|
||
/** | ||
* Create a query index. | ||
*/ | ||
export async function updateQueryIndex( | ||
params: CouchbaseHttpApiConfig, | ||
indexName: string, | ||
keyspace: Keyspace, | ||
config: { | ||
action: 'move' | 'replica_count' | 'drop_replica'; | ||
num_replica?: number; | ||
nodes?: string[]; | ||
replicaId?: string; | ||
} | ||
): Promise<[]> { | ||
const query = `ALTER INDEX ${quoteIdentifier(indexName)} | ||
ON ${keyspacePath(keyspace)} | ||
WITH ${JSON.stringify(config)} | ||
`; | ||
|
||
const response = await requestExecuteStatement(params, query); | ||
|
||
if (response.status !== 200) { | ||
throw await createHttpError('GET', response); | ||
} | ||
|
||
const body = (await response.json()) as ApiQueryResponseBody<[], null>; | ||
return body.results; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2023-Present Jonathan MASSUCHETTI. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { describe } from 'vitest'; | ||
|
||
import { createQueryIndex } from '@cbjsdev/http-client'; | ||
import { createCouchbaseTest, getRandomId } from '@cbjsdev/vitest'; | ||
|
||
describe('createQueryIndex', { timeout: 40_000 }, async () => { | ||
const test = await createCouchbaseTest(); | ||
|
||
test('create a collection index', async ({ expect, serverTestContext, apiConfig }) => { | ||
const indexName = `cbjs_${getRandomId()}`; | ||
const ks = { | ||
bucket: serverTestContext.bucket.name, | ||
scope: serverTestContext.scope.name, | ||
collection: serverTestContext.defaultCollection.name, | ||
}; | ||
|
||
await expect( | ||
createQueryIndex(apiConfig, indexName, ks, { keys: ['name'] }) | ||
).resolves.toEqual( | ||
expect.arrayContaining([ | ||
{ | ||
id: expect.any(String), | ||
name: indexName, | ||
state: 'online', | ||
}, | ||
]) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2023-Present Jonathan MASSUCHETTI. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { describe } from 'vitest'; | ||
|
||
import { createQueryIndex, updateQueryIndex } from '@cbjsdev/http-client'; | ||
import { createCouchbaseTest, getRandomId } from '@cbjsdev/vitest'; | ||
|
||
// requires a multi-node cluster | ||
describe.skip('updateQueryIndex', { timeout: 80_000 }, async () => { | ||
const test = await createCouchbaseTest(); | ||
|
||
test('update a collection index', async ({ expect, serverTestContext, apiConfig }) => { | ||
const indexName = `cbjs_${getRandomId()}`; | ||
const ks = { | ||
bucket: serverTestContext.bucket.name, | ||
scope: serverTestContext.scope.name, | ||
collection: serverTestContext.defaultCollection.name, | ||
}; | ||
|
||
await createQueryIndex(apiConfig, indexName, ks, { keys: ['name'] }); | ||
|
||
await expect( | ||
updateQueryIndex(apiConfig, indexName, ks, { | ||
action: 'replica_count', | ||
num_replica: 1, | ||
}) | ||
).resolves.toEqual([]); | ||
}); | ||
}); |