Skip to content

Commit

Permalink
implement user grpc api
Browse files Browse the repository at this point in the history
Signed-off-by: nameczz <[email protected]>
  • Loading branch information
nameczz committed Apr 20, 2022
1 parent c5cf5bc commit d0e8225
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 60 deletions.
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "avoid"
}
25 changes: 13 additions & 12 deletions example/User.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { MilvusClient } from "../milvus/index";
import { IP } from "../const";
const NAME = 'nameczz'
const PASSWORD = '123456'
// const milvusClient = new MilvusClient(IP, false, NAME, PASSWORD); // Auth Client
const milvusClient = new MilvusClient(IP); // Normal client

import { MilvusClient } from '../milvus/index';
import { IP } from '../const';
const NAME = 'nameczz';
const PASSWORD = '123456';
const milvusClient = new MilvusClient(IP, false, NAME, PASSWORD); // After create user we can use this Auth Client
// const milvusClient = new MilvusClient(IP); // Normal client

// when test_1 collection includes some data.
const user = async () => {
const createUserRes = await milvusClient.userManager.createUser({
username: NAME,
password: "123456"
})
username: 'test2',
password: '123456',
});
console.log('--- create user ---', createUserRes);

const usersRes = await milvusClient.userManager.listUsers();
console.log('--- list user ---', usersRes);

// const deleteUserRes = await milvusClient.userManager.deleteUser({username:NAME});
// console.log('--- delete user ---',deleteUserRes);
const deleteUserRes = await milvusClient.userManager.deleteUser({
username: NAME,
});
console.log('--- delete user ---', deleteUserRes);
};

user();
56 changes: 35 additions & 21 deletions milvus/User.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
import protobuf from "protobufjs";
import { promisify } from "../utils";
import { Client } from "./Client";
import { ERROR_REASONS } from "./const/ErrorReason";

import {

ListCredUsersResponse,

} from "./types/Response";
import { DeleteUserReq, UpdateUserReq } from "./types/User";

import protobuf from 'protobufjs';
import { promisify } from '../utils';
import { Client } from './Client';
import { ERROR_REASONS } from './const/ErrorReason';

import { ListCredUsersResponse } from './types/Response';
import { DeleteUserReq, UpdateUserReq } from './types/User';
import { stringToBase64 } from './utils/Format';

/**
* See all [collection operation examples](https://github.com/milvus-io/milvus-sdk-node/blob/main/example/Collection.ts).
*/
export class User extends Client {
private async createOrUpdateUser(
data: UpdateUserReq,
type: 'create' | 'update'
) {
if (!data.username || !data.password) {
throw new Error(ERROR_REASONS.USERNAME_PWD_ARE_REQUIRED);
}
const encryptedPassword = stringToBase64(data.password);
const promise = await promisify(
this.client,
type === 'create' ? 'CreateCredential' : 'UpdateCredential',
{
username: data.username,
password: encryptedPassword,
}
);
return promise;
}

async createUser(data: UpdateUserReq) {
const encryptedPassword = Buffer.from(data.password, 'utf-8').toString('base64')
console.log(data, encryptedPassword)
const promise = await promisify(this.client, "CreateCredential", {
username: data.username,
passwrod: encryptedPassword
});
return promise;
return await this.createOrUpdateUser(data, 'create');
}

async updateUser(data: UpdateUserReq) {
return await this.createOrUpdateUser(data, 'update');
}

async deleteUser(data: DeleteUserReq) {
const promise = await promisify(this.client, "DeleteCredential", {
if (!data.username) {
throw new Error(ERROR_REASONS.USERNAME_IS_REQUIRED);
}
const promise = await promisify(this.client, 'DeleteCredential', {
username: data.username,
});
return promise;
}

async listUsers(): Promise<ListCredUsersResponse> {
const promise = await promisify(this.client, "ListCredUsers", {});
const promise = await promisify(this.client, 'ListCredUsers', {});
return promise;
}
}
57 changes: 30 additions & 27 deletions milvus/const/ErrorReason.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
export enum ERROR_REASONS {
MILVUS_ADDRESS_IS_REQUIRED = "Milvus addres is required.",
MILVUS_ADDRESS_IS_REQUIRED = 'Milvus addres is required.',

CREATE_COLLECTION_CHECK_PARAMS = "fields and collection_name is needed",
CREATE_COLLECTION_CHECK_PRIMARY_KEY = "Fields must contain one data_type = int64 and is_primary_key = true",
CREATE_COLLECTION_CHECK_VECTOR_FIELD_EXIST = "Fields must contain one vector field column",
CREATE_COLLECTION_CHECK_MISS_DIM = "Vector field need dim in type params",
CREATE_COLLECTION_CHECK_BINARY_DIM = "Binary vector field dim need mutiples of 8",
CREATE_COLLECTION_CHECK_PARAMS = 'fields and collection_name is needed',
CREATE_COLLECTION_CHECK_PRIMARY_KEY = 'Fields must contain one data_type = int64 and is_primary_key = true',
CREATE_COLLECTION_CHECK_VECTOR_FIELD_EXIST = 'Fields must contain one vector field column',
CREATE_COLLECTION_CHECK_MISS_DIM = 'Vector field need dim in type params',
CREATE_COLLECTION_CHECK_BINARY_DIM = 'Binary vector field dim need mutiples of 8',

COLLECTION_NAME_IS_REQUIRED = "Collection name is required",
COLLECTION_PARTITION_NAME_ARE_REQUIRED = "Collection and partition name are required",
COLLECTION_NAME_IS_REQUIRED = 'Collection name is required',
COLLECTION_PARTITION_NAME_ARE_REQUIRED = 'Collection and partition name are required',

INSERT_CHECK_MISS_FIELD = "Insert fail: missing some field for this collection in line ",
INSERT_CHECK_FILEDS_DATA_IS_REQUIRED = "fields_data is required as array.",
INSERT_CHECK_WRONG_FIELD = "Insert fail: some field is not exist for this collection in line",
INSERT_CHECK_WRONG_DIM = "Insert fail: Binary vector data length need to equal (dimension / 8) ",
INSERT_CHECK_WRONG_DATA_TYPE = "Some field type in collection schema is not belong to DataType. ",
INSERT_CHECK_MISS_FIELD = 'Insert fail: missing some field for this collection in line ',
INSERT_CHECK_FILEDS_DATA_IS_REQUIRED = 'fields_data is required as array.',
INSERT_CHECK_WRONG_FIELD = 'Insert fail: some field is not exist for this collection in line',
INSERT_CHECK_WRONG_DIM = 'Insert fail: Binary vector data length need to equal (dimension / 8) ',
INSERT_CHECK_WRONG_DATA_TYPE = 'Some field type in collection schema is not belong to DataType. ',

SEARCH_MISS_VECTOR_TYPE = "Miss vector_type, need to be binary or float vector field type.",
SEARCH_NOT_FIND_VECTOR_FIELD = "Your anns_field cannot find in this collection.",
SEARCH_DIM_NOT_MATCH = "Your vector dimension is not match your anns_field dimension",
SEARCH_PARAMS_IS_REQUIRED = "search_params must contains anns_field, metric_type, topk and params.",
SEARCH_MISS_VECTOR_TYPE = 'Miss vector_type, need to be binary or float vector field type.',
SEARCH_NOT_FIND_VECTOR_FIELD = 'Your anns_field cannot find in this collection.',
SEARCH_DIM_NOT_MATCH = 'Your vector dimension is not match your anns_field dimension',
SEARCH_PARAMS_IS_REQUIRED = 'search_params must contains anns_field, metric_type, topk and params.',

DELETE_PARAMS_CHECK = "Collection name and expr is required.",
DELETE_PARAMS_CHECK = 'Collection name and expr is required.',

GET_METRIC_CHECK_PARAMS = "request.metric_type is required.",
GET_METRIC_CHECK_PARAMS = 'request.metric_type is required.',

GET_FLUSH_STATE_CHECK_PARAMS = "segmentIDs is required as array",
GET_FLUSH_STATE_CHECK_PARAMS = 'segmentIDs is required as array',

LOAD_BALANCE_CHECK_PARAMS = "src_nodeID is required",
LOAD_BALANCE_CHECK_PARAMS = 'src_nodeID is required',

CREATE_INDEX_PARAMS_REQUIRED = "field_name and extra_params are required",
CREATE_INDEX_PARAMS_REQUIRED = 'field_name and extra_params are required',

PARTITION_NAMES_IS_REQUIRED = "partition_names is required",
PARTITION_NAMES_IS_REQUIRED = 'partition_names is required',

ALIAS_NAME_IS_REQUIRED = "alias is required",
ALIAS_NAME_IS_REQUIRED = 'alias is required',

COMPACTIONID_IS_REQUIRED = "compactionID is required",
COMPACTIONID_IS_REQUIRED = 'compactionID is required',

TIMESTAMP_PARAM_CHECK = "type should be string (only contains number) or bigint",
USERNAME_PWD_ARE_REQUIRED = 'username and password are required',
USERNAME_IS_REQUIRED = 'username is required',

DATE_TYPE_CHECK = "type should be Date",
TIMESTAMP_PARAM_CHECK = 'type should be string (only contains number) or bigint',

DATE_TYPE_CHECK = 'type should be Date',
}
2 changes: 2 additions & 0 deletions milvus/utils/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,5 @@ export const datetimeToHybrids = (datetime: Date) => {
}
return unixtimeToHybridts((datetime.getTime() / 1000).toString());
};

export const stringToBase64 = (str: string) => Buffer.from(str, 'utf-8').toString('base64')

0 comments on commit d0e8225

Please sign in to comment.