-
Notifications
You must be signed in to change notification settings - Fork 30
feat: Collection aliasing #316
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
17c5be2
chore: refresh OpenAPI schema
bevzzz 1c23c5f
feat: add collection aliasing
bevzzz 2eb64d7
feat: add alias permissions to RBAC
bevzzz d5309c6
chore: lint & format
bevzzz 2564f0c
docs: write docs for alias API
bevzzz 67313f7
fix: add 'collection' parameter to AliasPermission
bevzzz d992f45
fix: add missing parameter in response parsing
bevzzz 1fd81d9
chore: lint & format
bevzzz 6446dfe
refactor: name required arguments 'args' and optional 'opts'
bevzzz 9880f1e
test: pass opts object to listAll
bevzzz f2ba029
chore: rename AliasListAllOpts -> AliasListAllOptions
bevzzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,77 @@ | ||
| import { ConnectionREST } from '../index.js'; | ||
| import { WeaviateAlias, WeaviateAliasResponse } from '../openapi/types.js'; | ||
| import { Alias, CreateAliasInput, UpdateAliasInput } from './types.js'; | ||
|
|
||
| export interface Aliases { | ||
| /** | ||
| * Create alias for a collection. | ||
| * | ||
| * The collection must exist prior to aliasing it. | ||
| * One alias cannot be created for multiple collections simultaneously. | ||
| * | ||
| * @param {string} [opt.collection] Original collection name. | ||
| * @param {string} [opt.alias] Alias for collection. | ||
| * @returns {Promise<void>} Awaitable promise. | ||
| * */ | ||
| create: (opt: CreateAliasInput) => Promise<void>; | ||
|
|
||
| /** | ||
| * List all aliases defined in the schema. | ||
| * | ||
| * @param {string | undefined} collection Get all aliases defined for this collection. | ||
bevzzz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @returns {Promise<Alias[] | undefined>} An array of aliases. | ||
| */ | ||
| listAll: (collection?: string) => Promise<Alias[] | undefined>; | ||
bevzzz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Get information about an alias. | ||
| * | ||
| * @param {string} alias Alias to fetch. | ||
| * @return {Promise<Alias>} Alias definition. | ||
| */ | ||
| get: (alias: string) => Promise<Alias>; | ||
|
|
||
| /** | ||
| * Replace target collection the alias points to. | ||
| * | ||
| * To change the alias that points to the collection, | ||
| * delete the alias and create a new one. | ||
| * | ||
| * @param {string} [opt.alias] Alias to update. | ||
| * @param {string} [opt.collection] New collection the alias should point to. | ||
| * @return {Promise<void>} Awaitable promise. | ||
| */ | ||
| update: (opt: UpdateAliasInput) => Promise<void>; | ||
bevzzz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Delete a collection alias. | ||
| * | ||
| * @param {string} alias Alias definition to delete. | ||
| * @return {Promise<void>} Awaitable promise. | ||
| */ | ||
| delete: (alias: string) => Promise<void>; | ||
| } | ||
|
|
||
| const alias = (connection: ConnectionREST): Aliases => { | ||
| return { | ||
| create: (opt: CreateAliasInput) => | ||
| connection.postReturn<WeaviateAlias, void>(`/aliases/`, { ...opt, class: opt.collection }), | ||
| listAll: (collection?: string) => | ||
| connection | ||
| .get<WeaviateAliasResponse>(`/aliases${collection !== undefined ? '/?class=' + collection : ''}`) | ||
| .then((aliases) => | ||
| aliases.aliases !== undefined | ||
| ? aliases.aliases.map((alias) => ({ alias: alias.alias, collection: alias.class })) | ||
| : [] | ||
| ), | ||
| get: (alias: string) => | ||
| connection | ||
| .get<WeaviateAlias>(`/aliases/${alias}`) | ||
| .then((alias) => ({ alias: alias.alias!, collection: alias.class! })), | ||
| update: (opt: UpdateAliasInput) => | ||
| connection.put(`/aliases/${opt.alias}`, { class: opt.newTargetCollection }), | ||
| delete: (alias: string) => connection.delete(`/aliases/${alias}`, null), | ||
| }; | ||
| }; | ||
|
|
||
| export default alias; | ||
This file contains hidden or 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,53 @@ | ||
| import weaviate, { WeaviateClient } from '..'; | ||
| import { requireAtLeast } from '../../test/version'; | ||
| import { Alias } from './types'; | ||
|
|
||
| requireAtLeast(1, 32, 0).describe('manages collection aliases', () => { | ||
| let client: WeaviateClient; | ||
| const collectionsWithAliases = ['PaulHewson', 'GeorgeBarnes', 'ColsonBaker']; | ||
|
|
||
| beforeAll(async () => { | ||
| client = await weaviate.connectToLocal(); | ||
| await Promise.all(collectionsWithAliases.map(client.collections.delete)); | ||
| await Promise.all(collectionsWithAliases.map((name) => client.collections.create({ name }))); | ||
| }); | ||
|
|
||
| it('should create alias', () => { | ||
| return Promise.all([ | ||
| client.alias.create({ collection: 'PaulHewson', alias: 'Bono' }), | ||
| client.alias.create({ collection: 'GeorgeBarnes', alias: 'MachineGunKelly' }), | ||
| ]) | ||
| .then(() => client.alias.listAll()) | ||
| .then((aliases) => { | ||
| expect(aliases).not.toBeUndefined(); | ||
| expect(aliases).toHaveLength(2); | ||
| expect(aliases).toEqual<Alias[]>([ | ||
| { collection: 'PaulHewson', alias: 'Bono' }, | ||
| { collection: 'GeorgeBarnes', alias: 'MachineGunKelly' }, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| it('should update alias', () => { | ||
| return client.alias | ||
| .update({ alias: 'MachineGunKelly', newTargetCollection: 'ColsonBaker' }) | ||
| .then(() => client.alias.get('MachineGunKelly')) | ||
| .then((alias) => { | ||
| expect(alias.collection).toEqual('ColsonBaker'); | ||
| }); | ||
| }); | ||
|
|
||
| it('should delete alias Bono', () => { | ||
| return client.alias | ||
| .delete('Bono') | ||
| .then(() => client.alias.listAll('PaulHewson')) | ||
| .then((aliases) => expect(aliases).toEqual([])); | ||
| }); | ||
|
|
||
| it('should delete alias MachineGunKelly', () => { | ||
| return client.alias | ||
| .delete('MachineGunKelly') | ||
| .then(() => client.alias.listAll('ColsonBaker')) | ||
| .then((aliases) => expect(aliases).toEqual([])); | ||
| }); | ||
| }); |
This file contains hidden or 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,14 @@ | ||
| export type Alias = { | ||
| collection: string; | ||
| alias: string; | ||
| }; | ||
|
|
||
| export type CreateAliasInput = { | ||
| collection: string; | ||
| alias: string; | ||
| }; | ||
|
|
||
| export type UpdateAliasInput = { | ||
| newTargetCollection: string; | ||
| alias: string; | ||
| }; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.