Skip to content

Commit

Permalink
wrap redis client
Browse files Browse the repository at this point in the history
  • Loading branch information
gkorland committed Mar 18, 2024
1 parent bbd4339 commit 0f41c74
Show file tree
Hide file tree
Showing 45 changed files with 4,668 additions and 1,069 deletions.
3 changes: 3 additions & 0 deletions examples/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ const result = await graph.query('MATCH (n) RETURN n')

console.log(result)

console.log(await db.list())
console.log(await db.info())

db.close()
7 changes: 0 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
export { default as Node } from './src/node';

export { default as Edge } from './src/edge';

export { default as Path } from './src/path';

export { default as Graph } from './src/graph';

export { default as FalkorDB } from './src/falkordb';


Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"typescript": "^5.4.2"
},
"dependencies": {
"redis": "^4.6.13"
"redis": "^4.6.13",
"@redis/client": "*"
}
}
22 changes: 22 additions & 0 deletions src/commands/CONFIG_GET.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './CONFIG_GET';

describe('CONFIG GET', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('TIMEOUT'),
['GRAPH.CONFIG', 'GET', 'TIMEOUT']
);
});

testUtils.testWithClient('client.graph.configGet', async client => {
assert.deepEqual(
await client.graph.configGet('TIMEOUT'),
[
'TIMEOUT',
0
]
);
}, GLOBAL.SERVERS.OPEN);
});
12 changes: 12 additions & 0 deletions src/commands/CONFIG_GET.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const IS_READ_ONLY = true;

export function transformArguments(configKey: string): Array<string> {
return ['GRAPH.CONFIG', 'GET', configKey];
}

type ConfigItem = [
configKey: string,
value: number
];

export declare function transformReply(): ConfigItem | Array<ConfigItem>;
19 changes: 19 additions & 0 deletions src/commands/CONFIG_SET.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './CONFIG_SET';

describe('CONFIG SET', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('TIMEOUT', 0),
['GRAPH.CONFIG', 'SET', 'TIMEOUT', '0']
);
});

testUtils.testWithClient('client.graph.configSet', async client => {
assert.equal(
await client.graph.configSet('TIMEOUT', 0),
'OK'
);
}, GLOBAL.SERVERS.OPEN);
});
10 changes: 10 additions & 0 deletions src/commands/CONFIG_SET.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function transformArguments(configKey: string, value: number): Array<string> {
return [
'GRAPH.CONFIG',
'SET',
configKey,
value.toString()
];
}

export declare function transformReply(): 'OK';
21 changes: 21 additions & 0 deletions src/commands/DELETE.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './DELETE';

describe('', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key'),
['GRAPH.DELETE', 'key']
);
});

testUtils.testWithClient('client.graph.delete', async client => {
await client.graph.query('key', 'RETURN 1');

assert.equal(
typeof await client.graph.delete('key'),
'string'
);
}, GLOBAL.SERVERS.OPEN);
});
7 changes: 7 additions & 0 deletions src/commands/DELETE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const FIRST_KEY_INDEX = 1;

export function transformArguments(key: string): Array<string> {
return ['GRAPH.DELETE', key];
}

export declare function transformReply(): string;
21 changes: 21 additions & 0 deletions src/commands/EXPLAIN.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './EXPLAIN';

describe('EXPLAIN', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'RETURN 0'),
['GRAPH.EXPLAIN', 'key', 'RETURN 0']
);
});

testUtils.testWithClient('client.graph.explain', async client => {
const [, reply] = await Promise.all([
client.graph.query('key', 'RETURN 0'), // make sure to create a graph first
client.graph.explain('key', 'RETURN 0')
]);
assert.ok(Array.isArray(reply));
assert.ok(!reply.find(x => typeof x !== 'string'));
}, GLOBAL.SERVERS.OPEN);
});
9 changes: 9 additions & 0 deletions src/commands/EXPLAIN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const FIRST_KEY_INDEX = 1;

export const IS_READ_ONLY = true;

export function transformArguments(key: string, query: string): Array<string> {
return ['GRAPH.EXPLAIN', key, query];
}

export declare function transfromReply(): Array<string>;
25 changes: 25 additions & 0 deletions src/commands/INFO.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './INFO';

describe('INFO', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments(),
['GRAPH.INFO']
);
});

testUtils.testWithClient('client.graph.info', async client => {
assert.deepEqual(
await client.graph.info(),
[
"# Running queries",
[],
"# Waiting queries",
[],

]
);
}, GLOBAL.SERVERS.OPEN);
});
13 changes: 13 additions & 0 deletions src/commands/INFO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const IS_READ_ONLY = true;

export function transformArguments(section?: string): Array<string> {
const args = ['GRAPH.INFO'];

if (section) {
args.push(section);
}

return args;
}

export declare function transformReply(): Array<string | Array<string>>;
19 changes: 19 additions & 0 deletions src/commands/LIST.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './LIST';

describe('LIST', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments(),
['GRAPH.LIST']
);
});

testUtils.testWithClient('client.graph.list', async client => {
assert.deepEqual(
await client.graph.list(),
[]
);
}, GLOBAL.SERVERS.OPEN);
});
7 changes: 7 additions & 0 deletions src/commands/LIST.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const IS_READ_ONLY = true;

export function transformArguments(): Array<string> {
return ['GRAPH.LIST'];
}

export declare function transformReply(): Array<string>;
18 changes: 18 additions & 0 deletions src/commands/PROFILE.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './PROFILE';

describe('PROFILE', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'RETURN 0'),
['GRAPH.PROFILE', 'key', 'RETURN 0']
);
});

testUtils.testWithClient('client.graph.profile', async client => {
const reply = await client.graph.profile('key', 'RETURN 0');
assert.ok(Array.isArray(reply));
assert.ok(!reply.find(x => typeof x !== 'string'));
}, GLOBAL.SERVERS.OPEN);
});
9 changes: 9 additions & 0 deletions src/commands/PROFILE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const FIRST_KEY_INDEX = 1;

export const IS_READ_ONLY = true;

export function transformArguments(key: string, query: string): Array<string> {
return ['GRAPH.PROFILE', key, query];
}

export declare function transfromReply(): Array<string>;
17 changes: 17 additions & 0 deletions src/commands/QUERY.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './QUERY';

describe('QUERY', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'query'),
['GRAPH.QUERY', 'key', 'query']
);
});

testUtils.testWithClient('client.graph.query', async client => {
const { data } = await client.graph.query('key', 'RETURN 0');
assert.deepEqual(data, [[0]]);
}, GLOBAL.SERVERS.OPEN);
});
55 changes: 55 additions & 0 deletions src/commands/QUERY.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
import { pushQueryArguments, QueryOptionsBackwardCompatible } from '.';

export const FIRST_KEY_INDEX = 1;

export function transformArguments(
graph: RedisCommandArgument,
query: RedisCommandArgument,
options?: QueryOptionsBackwardCompatible,
compact?: boolean
): RedisCommandArguments {
return pushQueryArguments(
['GRAPH.QUERY'],
graph,
query,
options,
compact
);
}

type Headers = Array<string>;

type Data = Array<string | number | null | Data>;

type Metadata = Array<string>;

type QueryRawReply = [
headers: Headers,
data: Data,
metadata: Metadata
] | [
metadata: Metadata
];

export type QueryReply = {
headers: undefined;
data: undefined;
metadata: Metadata;
} | {
headers: Headers;
data: Data;
metadata: Metadata;
};

export function transformReply(reply: QueryRawReply): QueryReply {
return reply.length === 1 ? {
headers: undefined,
data: undefined,
metadata: reply[0]
} : {
headers: reply[0],
data: reply[1],
metadata: reply[2]
};
}
20 changes: 20 additions & 0 deletions src/commands/RO_QUERY.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './RO_QUERY';

describe('RO_QUERY', () => {
it('transformArguments', () => {
assert.deepEqual(
transformArguments('key', 'query'),
['GRAPH.RO_QUERY', 'key', 'query']
);
});

testUtils.testWithClient('client.graph.roQuery', async client => {
const [, { data }] = await Promise.all([
client.graph.query('key', 'RETURN 0'), // make sure to create a graph first
client.graph.roQuery('key', 'RETURN 0')
]);
assert.deepEqual(data, [[0]]);
}, GLOBAL.SERVERS.OPEN);
});
23 changes: 23 additions & 0 deletions src/commands/RO_QUERY.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RedisCommandArgument, RedisCommandArguments } from '@redis/client/dist/lib/commands';
import { pushQueryArguments, QueryOptionsBackwardCompatible } from '.';

export { FIRST_KEY_INDEX } from './QUERY';

export const IS_READ_ONLY = true;

export function transformArguments(
graph: RedisCommandArgument,
query: RedisCommandArgument,
options?: QueryOptionsBackwardCompatible,
compact?: boolean
): RedisCommandArguments {
return pushQueryArguments(
['GRAPH.RO_QUERY'],
graph,
query,
options,
compact
);
}

export { transformReply } from './QUERY';
Loading

0 comments on commit 0f41c74

Please sign in to comment.