Skip to content

Commit

Permalink
Merge pull request #160 from skyflowapi/SK-1621-node-sdk-v2
Browse files Browse the repository at this point in the history
SK-1621: node sdk v2
  • Loading branch information
amith-skyflow authored Oct 25, 2024
2 parents f9119d6 + fb3bf16 commit edb25d3
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 32 deletions.
5 changes: 3 additions & 2 deletions src/error/codes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ const SKYFLOW_ERROR_CODE = {
EMPTY_CONNECTION_ID_VALIDATION: { http_code: 400, message: errorMessages.EMPTY_CONNECTION_ID_VALIDATION },
EMPTY_CONNECTION_URL: { http_code: 400, message: errorMessages.EMPTY_CONNECTION_URL },
INVALID_CONNECTION_URL: { http_code: 400, message: errorMessages.INVALID_CONNECTION_URL },



VAULT_ID_EXITS_IN_CONFIG_LIST: { http_code: 400, message: errorMessages.VAULT_ID_EXITS_IN_CONFIG_LIST },
CONNECTION_ID_EXITS_IN_CONFIG_LIST: { http_code: 400, message: errorMessages.CONNECTION_ID_EXITS_IN_CONFIG_LIST },
VAULT_ID_NOT_IN_CONFIG_LIST: { http_code: 400, message: errorMessages.VAULT_ID_NOT_IN_CONFIG_LIST },
CONNECTION_ID_NOT_IN_CONFIG_LIST: { http_code: 400, message: errorMessages.CONNECTION_ID_NOT_IN_CONFIG_LIST },

Expand Down
2 changes: 2 additions & 0 deletions src/error/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const errorMessages = {
EMPTY_CONNECTION_URL: `${errorPrefix} Initialization failed. Invalid connection URL. Specify a valid connection Url.`,
INVALID_CONNECTION_URL: `${errorPrefix} Initialization failed. Invalid connection URL. Specify connection Url as a valid url.`,

VAULT_ID_EXITS_IN_CONFIG_LIST: `${errorPrefix} Validation error. %s1 already exists in the config list. Specify a new vaultId.`,
CONNECTION_ID_EXITS_IN_CONFIG_LIST: `${errorPrefix} Validation error. %s1 already exists in the config list. Specify a new vaultId.`,
VAULT_ID_NOT_IN_CONFIG_LIST: `${errorPrefix} Validation error. %s1 is missing from the config. Specify the vaultId's from config.`,
CONNECTION_ID_NOT_IN_CONFIG_LIST: `${errorPrefix} Validation error. %s1 is missing from the config. Specify the connectionIds from config.`,

Expand Down
8 changes: 2 additions & 6 deletions src/utils/validations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,14 @@ export const validateInsertOptions = (insertOptions?: InsertOptions) => {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_RETURN_TOKEN, [typeof insertOptions?.getReturnTokens()]);
}

if (insertOptions?.getUpsert() && typeof insertOptions.getUpsert() !== 'string') {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_UPSERT, [typeof insertOptions?.getUpsert()]);
if (insertOptions?.getUpsertColumn() && typeof insertOptions.getUpsertColumn() !== 'string') {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_UPSERT, [typeof insertOptions?.getUpsertColumn()]);
}

if (insertOptions?.getContinueOnError() && typeof insertOptions.getContinueOnError() !== 'boolean') {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_CONTINUE_ON_ERROR, [typeof insertOptions?.getContinueOnError()]);
}

if (insertOptions?.getTokenStrict() && typeof insertOptions.getTokenStrict() !== 'boolean') {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_TOKEN_STRICT, [typeof insertOptions?.getTokenStrict()]);
}

if (insertOptions?.getHomogeneous() && typeof insertOptions.getHomogeneous() !== 'boolean') {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_HOMOGENEOUS, [typeof insertOptions?.getHomogeneous()]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/vault/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class VaultClient {
}

updateClientConfig(clusterID: string, vaultId: string, individualCredentials?: Credentials, skyflowCredentials?: Credentials, logLevel?: LogLevel) {
this.updateTriggered = true
this.updateTriggered = true;
this.initializeClient(clusterID, vaultId, individualCredentials, skyflowCredentials, logLevel);
}

Expand Down Expand Up @@ -104,6 +104,7 @@ class VaultClient {
}

updateSkyflowCredentials(credentials?: Credentials) {
this.updateTriggered = true;
this.skyflowCredentials = credentials;
}

Expand Down Expand Up @@ -153,7 +154,6 @@ class VaultClient {
grpcCode?: number,
details?: any
) {
printLog(description, MessageType.ERROR, this.getLogLevel());
reject(new SkyflowError({
http_code: err?.response?.status || 400,
message: description,
Expand Down
4 changes: 2 additions & 2 deletions src/vault/controller/vault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class VaultController {
tokenization: options?.getReturnTokens() || false,
method: BatchRecordMethod.Post,
tokens: this.getTokens(index, options?.getTokens()),
upsert: options?.getUpsert(),
upsert: options?.getUpsertColumn(),
}));
return {
records,
Expand All @@ -191,7 +191,7 @@ class VaultController {
return {
records,
tokenization: options?.getReturnTokens(),
upsert: options?.getUpsert(),
upsert: options?.getUpsertColumn(),
homogeneous: options?.getHomogeneous(),
byot: options?.getTokenMode()
};
Expand Down
13 changes: 2 additions & 11 deletions src/vault/model/options/insert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class InsertOptions {
private tokens?: Array<object>;
private homogeneous?: boolean;
private tokenMode?: BYOT;
private tokenStrict?: boolean;
private continueOnError?: boolean;

// Constructor
Expand All @@ -19,7 +18,7 @@ class InsertOptions {
this.returnTokens = returnTokens;
}

setUpsert(upsert: string) {
setUpsertColumn(upsert: string) {
this.upsert = upsert;
}

Expand All @@ -35,10 +34,6 @@ class InsertOptions {
this.tokenMode = tokenMode;
}

setTokenStrict(tokenStrict: boolean) {
this.tokenStrict = tokenStrict;
}

setContinueOnError(continueOnError: boolean) {
this.continueOnError = continueOnError;
}
Expand All @@ -48,7 +43,7 @@ class InsertOptions {
return this.returnTokens;
}

getUpsert(): string | undefined {
getUpsertColumn(): string | undefined {
return this.upsert;
}

Expand All @@ -64,10 +59,6 @@ class InsertOptions {
return this.tokenMode;
}

getTokenStrict(): boolean | undefined {
return this.tokenStrict;
}

getContinueOnError(): boolean | undefined {
return this.continueOnError;
}
Expand Down
12 changes: 12 additions & 0 deletions src/vault/skyflow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ class Skyflow {

addVaultConfig(config: VaultConfig) {
validateVaultConfig(config);
this.throwErrorIfIdExits(config?.vaultId, this.vaultClients, VAULT_ID);
this.addVaultClient(config, this.vaultClients);
}

addConnectionConfig(config: ConnectionConfig) {
validateConnectionConfig(config);
this.throwErrorIfIdExits(config?.connectionId, this.connectionClients, CONNECTION_ID);
this.addConnectionClient(config, this.connectionClients);
}

Expand Down Expand Up @@ -122,6 +124,16 @@ class Skyflow {
}
}

private throwErrorIfIdExits(id: string, clients: ClientObj, idKey: string) {
const errorMapping = {
[VAULT_ID]: SKYFLOW_ERROR_CODE.VAULT_ID_EXITS_IN_CONFIG_LIST,
[CONNECTION_ID]: SKYFLOW_ERROR_CODE.CONNECTION_ID_EXITS_IN_CONFIG_LIST,
};
if(Object.keys(clients).includes(id)){
this.throwSkyflowError(idKey, errorMapping, [id]);
}
}

private throwErrorForUnknownId(id: string, idKey: string) {
const errorMapping = {
[VAULT_ID]: SKYFLOW_ERROR_CODE.VAULT_ID_NOT_IN_CONFIG_LIST,
Expand Down
18 changes: 9 additions & 9 deletions test/vault/controller/vault.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('VaultController insert method', () => {
const mockOptions = {
getContinueOnError: jest.fn().mockReturnValue(false),
getReturnTokens: jest.fn().mockReturnValue(true),
getUpsert: jest.fn().mockReturnValue(''),
getUpsertColumn: jest.fn().mockReturnValue(''),
getHomogeneous: jest.fn().mockReturnValue(false),
getTokenMode: jest.fn().mockReturnValue(''),
getTokens: jest.fn().mockReturnValue([{}])
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('VaultController insert method', () => {
const mockOptions = {
getContinueOnError: jest.fn().mockReturnValue(false),
getReturnTokens: jest.fn().mockReturnValue(true),
getUpsert: jest.fn().mockReturnValue(''),
getUpsertColumn: jest.fn().mockReturnValue(''),
getHomogeneous: jest.fn().mockReturnValue(false),
getTokenMode: jest.fn().mockReturnValue(''),
getTokens: jest.fn().mockReturnValue([{}])
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('VaultController insert method', () => {
const mockOptions = {
getContinueOnError: jest.fn().mockReturnValue(false),
getReturnTokens: jest.fn().mockReturnValue(true),
getUpsert: jest.fn().mockReturnValue(''),
getUpsertColumn: jest.fn().mockReturnValue(''),
getHomogeneous: jest.fn().mockReturnValue(false),
getTokenMode: jest.fn().mockReturnValue(''),
getTokens: jest.fn().mockReturnValue([{}])
Expand Down Expand Up @@ -238,7 +238,7 @@ describe('VaultController insert method', () => {
const mockOptions = {
getContinueOnError: jest.fn().mockReturnValue(true),
getReturnTokens: jest.fn().mockReturnValue(false),
getUpsert: jest.fn().mockReturnValue(''),
getUpsertColumn: jest.fn().mockReturnValue(''),
getHomogeneous: jest.fn().mockReturnValue(false),
getTokenMode: jest.fn().mockReturnValue(''),
getTokens: jest.fn().mockReturnValue([])
Expand All @@ -261,7 +261,7 @@ describe('VaultController insert method', () => {
const mockOptions = {
getContinueOnError: jest.fn().mockReturnValue(true),
getReturnTokens: jest.fn().mockReturnValue(false),
getUpsert: jest.fn().mockReturnValue(''),
getUpsertColumn: jest.fn().mockReturnValue(''),
getHomogeneous: jest.fn().mockReturnValue(false),
getTokenMode: jest.fn().mockReturnValue(''),
getTokens: jest.fn().mockReturnValue([])
Expand All @@ -284,7 +284,7 @@ describe('VaultController insert method', () => {
const mockOptions = {
getContinueOnError: jest.fn().mockReturnValue(true),
getReturnTokens: jest.fn().mockReturnValue(false),
getUpsert: jest.fn().mockReturnValue(''),
getUpsertColumn: jest.fn().mockReturnValue(''),
getHomogeneous: jest.fn().mockReturnValue(false),
getTokenMode: jest.fn().mockReturnValue(''),
getTokens: jest.fn().mockReturnValue([])
Expand All @@ -307,7 +307,7 @@ describe('VaultController insert method', () => {
const mockOptions = {
getContinueOnError: jest.fn().mockReturnValue(true),
getReturnTokens: jest.fn().mockReturnValue(false),
getUpsert: jest.fn().mockReturnValue(''),
getUpsertColumn: jest.fn().mockReturnValue(''),
getHomogeneous: jest.fn().mockReturnValue(false),
getTokenMode: jest.fn().mockReturnValue(''),
getTokens: jest.fn().mockReturnValue([])
Expand All @@ -330,7 +330,7 @@ describe('VaultController insert method', () => {
const mockOptions = {
getContinueOnError: jest.fn().mockReturnValue(false),
getReturnTokens: jest.fn().mockReturnValue(true),
getUpsert: jest.fn().mockReturnValue(''),
getUpsertColumn: jest.fn().mockReturnValue(''),
getHomogeneous: jest.fn().mockReturnValue(false),
getTokenMode: jest.fn().mockReturnValue('')
};
Expand All @@ -352,7 +352,7 @@ describe('VaultController insert method', () => {
const mockOptions = {
getContinueOnError: jest.fn().mockReturnValue(false),
getReturnTokens: jest.fn().mockReturnValue(true),
getUpsert: jest.fn().mockReturnValue(''),
getUpsertColumn: jest.fn().mockReturnValue(''),
getHomogeneous: jest.fn().mockReturnValue(false),
getTokenMode: jest.fn().mockReturnValue('')
};
Expand Down
23 changes: 23 additions & 0 deletions test/vault/skyflow/skyflow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ describe('Skyflow initialization', () => {
const missingVaultConfigError = "VAULT_ID is missing from the config.";
const missingIdConfigError = "ID is missing from the config.";
const noConfigFound = "No vault config found.";
const idExits = "already exists in the config list.";

const validVaultConfig = {
vaultId: "VAULT_ID",
Expand Down Expand Up @@ -257,6 +258,28 @@ describe('Skyflow initialization', () => {
expect(() => skyflow.removeVaultConfig("ID")).toThrowError(missingIdConfigError);
});

test('should throw error when calling a non-existing vault config', () => {
const skyflow = new Skyflow({
vaultConfigs: [validVaultConfig],
});
expect(() => skyflow.vault("ID")).toThrowError(missingIdConfigError);
});

test('should throw error when no vault configs exits', () => {
const skyflow = new Skyflow({
vaultConfigs: [validVaultConfig],
});
skyflow.removeVaultConfig("VAULT_ID");
expect(() => skyflow.vault("ID")).toThrowError(noConfigFound);
});

test('should throw error when adding vault config', () => {
const skyflow = new Skyflow({
vaultConfigs: [validVaultConfig],
});
expect(() => skyflow.addVaultConfig(validVaultConfig)).toThrowError(idExits);
});

test('should throw error when updating a non-existing vault config', () => {
const skyflow = new Skyflow({
vaultConfigs: [validVaultConfig],
Expand Down

0 comments on commit edb25d3

Please sign in to comment.