Skip to content

Commit

Permalink
Merge pull request #46 from ainblockchain/feature/liayoo/get-options
Browse files Browse the repository at this point in the history
Feature/liayoo/get options
  • Loading branch information
liayoo authored Dec 6, 2021
2 parents 5c2605a + d6f050c commit d238802
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 29 deletions.
39 changes: 24 additions & 15 deletions __tests__/ain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('ain-js', function() {

it('should set provider', async function() {
ain.setProvider(test_node_2);
expect(await ain.net.getNetworkId()).toBe('Testnet');
expect(await ain.net.getNetworkId()).toBe(0);
expect(await ain.net.isListening()).toMatchSnapshot();
expect(await ain.net.getPeerCount()).toBeGreaterThan(0);
expect(await ain.net.isSyncing()).toBe(false);
Expand Down Expand Up @@ -612,8 +612,7 @@ describe('ain-js', function() {
value: {
".function": {
'0xFUNCTION_HASH': {
service_name: "functions.ainetwork.ai",
event_listener: "https://events.ainetwork.ai/trigger",
function_url: "https://events.ainetwork.ai/trigger",
function_id: '0xFUNCTION_HASH',
function_type: "REST"
}
Expand Down Expand Up @@ -666,18 +665,6 @@ describe('ain-js', function() {
});
});

it('deleteValue', function(done) {
ain.db.ref(allowed_path).deleteValue()
.then(res => {
expect(res.result.code).toBe(0);
done();
})
.catch((error) => {
console.log("deleteValue error:",error);
done();
});
});

it('getValue', async function() {
expect(await ain.db.ref(allowed_path).getValue()).toMatchSnapshot();
});
Expand Down Expand Up @@ -712,6 +699,28 @@ describe('ain-js', function() {
)).toMatchSnapshot();
});

it('get with options', async function() {
expect(await ain.db.ref().getValue(allowed_path, { is_final: true })).toMatchSnapshot();
expect(await ain.db.ref().getValue(allowed_path, { is_global: true })).toMatchSnapshot();
expect(await ain.db.ref().getValue(allowed_path, { is_shallow: true })).toMatchSnapshot();
expect(await ain.db.ref().getValue(allowed_path, { include_proof: true })).toMatchSnapshot();
expect(await ain.db.ref().getValue(allowed_path, { include_tree_info: true })).toMatchSnapshot();
const getWithVersion = await ain.db.ref().getValue(allowed_path, { include_version: true });
expect(getWithVersion['#version']).not.toBeNull();
});

it('deleteValue', function(done) {
ain.db.ref(allowed_path).deleteValue()
.then(res => {
expect(res.result.code).toBe(0);
done();
})
.catch((error) => {
console.log("deleteValue error:",error);
done();
});
});

it('evalRule: true', function(done) {
ain.db.ref(allowed_path).evalRule({ ref: '/can/write', value: 'hi' })
.then(res => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ainblockchain/ain-js",
"version": "1.1.8",
"version": "1.1.9",
"description": "",
"main": "lib/ain.js",
"scripts": {
Expand Down
27 changes: 16 additions & 11 deletions src/ain-db/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
SetMultiTransactionInput,
EvalRuleInput,
EvalOwnerInput,
MatchInput
MatchInput,
GetOptions,
} from '../types';
import Ain from '../ain';
import { PushId } from './push-id';
Expand Down Expand Up @@ -76,35 +77,35 @@ export default class Reference {
* Returns the value at the path.
* @param path
*/
getValue(path?: string): Promise<any> {
const req = Reference.buildGetRequest('GET_VALUE', Reference.extendPath(this.path, path));
getValue(path?: string, options?: GetOptions): Promise<any> {
const req = Reference.buildGetRequest('GET_VALUE', Reference.extendPath(this.path, path), options);
return this._ain.provider.send('ain_get', req);
}

/**
* Returns the rule at the path.
* @param path
*/
getRule(path?: string): Promise<any> {
const req = Reference.buildGetRequest('GET_RULE', Reference.extendPath(this.path, path));
getRule(path?: string, options?: GetOptions): Promise<any> {
const req = Reference.buildGetRequest('GET_RULE', Reference.extendPath(this.path, path), options);
return this._ain.provider.send('ain_get', req);
}

/**
* Returns the owner config at the path.
* @param path
*/
getOwner(path?: string): Promise<any> {
const req = Reference.buildGetRequest('GET_OWNER', Reference.extendPath(this.path, path));
getOwner(path?: string, options?: GetOptions): Promise<any> {
const req = Reference.buildGetRequest('GET_OWNER', Reference.extendPath(this.path, path), options);
return this._ain.provider.send('ain_get', req);
}

/**
* Returns the function config at the path.
* @param path
*/
getFunction(path?: string): Promise<any> {
const req = Reference.buildGetRequest('GET_FUNCTION', Reference.extendPath(this.path, path));
getFunction(path?: string, options?: GetOptions): Promise<any> {
const req = Reference.buildGetRequest('GET_FUNCTION', Reference.extendPath(this.path, path), options);
return this._ain.provider.send('ain_get', req);
}

Expand Down Expand Up @@ -363,8 +364,12 @@ export default class Reference {
* @param type
* @param ref
*/
static buildGetRequest(type: GetOperationType, ref: string) {
return { type, ref: Reference.sanitizeRef(ref) };
static buildGetRequest(type: GetOperationType, ref: string, options?: GetOptions) {
const request = { type, ref: Reference.sanitizeRef(ref) };
if (options) {
Object.assign(request, options);
}
return request;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ export type GetOperationType = "GET_VALUE" | "GET_RULE" | "GET_OWNER" | "GET_FUN

export type OwnerPermission = "branch_owner" | "write_function" | "write_owner" | "write_rule";

export type GetOptions = {
is_global?: boolean,
is_final?: boolean,
is_shallow?: boolean,
include_version?: boolean,
include_tree_info?: boolean,
include_proof?: boolean,
}

export interface SetOperation {
type: SetOperationType;
ref: string;
Expand All @@ -70,10 +79,9 @@ export interface SetMultiOperation {
op_list: SetOperation[];
}

export interface GetOperation {
export interface GetOperation extends GetOptions {
type: GetOperationType;
ref?: string;
is_global?: boolean;
}

export interface GetMultiOperation {
Expand Down

0 comments on commit d238802

Please sign in to comment.