Skip to content

Commit cef0aa1

Browse files
committed
Add all send to parameters and add a simple doc
1 parent 0e202b1 commit cef0aa1

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

__tests__/neovis.tests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -603,19 +603,19 @@ describe('Neovis', () => {
603603
yield record;
604604
}
605605
},
606-
} as Partial<NonFlatNeovisConfig>], ['Async Iterable', {
606+
} as Partial<NeovisConfig>], ['Async Iterable', {
607607
...dataFunctionSharedConfig,
608608
async dataFunction() {
609609
return retData;
610610
},
611-
} as Partial<NonFlatNeovisConfig>], ['ASync Generator', {
611+
} as Partial<NeovisConfig>], ['ASync Generator', {
612612
...dataFunctionSharedConfig,
613613
async *dataFunction() {
614614
for (const record of retData) {
615615
yield record;
616616
}
617617
},
618-
} as Partial<NonFlatNeovisConfig>]])('neovis dataFunction %s test', (configName: string, config) => {
618+
} as Partial<NeovisConfig>]])('neovis dataFunction %s test', (configName: string, config) => {
619619
beforeEach(() => {
620620
neovis = new Neovis(config as NonFlatNeovisConfig | NeovisConfig);
621621
});

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "neovis.js",
3-
"version": "2.1.0-alpha7",
3+
"version": "2.1.0-alpha8",
44
"description": "Graph visualizations powered by vis.js with data from Neo4j.",
55
"main": "./dist/neovis.js",
66
"module": "./dist/neovis-without-dependencies.js",
@@ -11,7 +11,9 @@
1111
"test": "`__tests__"
1212
},
1313
"scripts": {
14-
"test": "jest __tests__/neovis.tests.ts",
14+
"test": "run-p test:*",
15+
"test:jest": "jest __tests__/neovis.tests.ts",
16+
"test:types": "tsc --esModuleInterop --allowSyntheticDefaultImports --moduleResolution Node --target esnext --noemit ./__tests__/neovis.tests.ts",
1517
"eslint": "eslint .",
1618
"prepublishOnly": "run-s typedoc build",
1719
"clean": "rimraf ./dist",

src/neovis.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import deepmerge from 'deepmerge';
1111
import type * as VisNetwork from 'vis-network';
1212
import {
1313
Cypher,
14+
DataFunctionType,
1415
Edge,
1516
LabelConfig,
1617
Neo4jConfig,
@@ -560,19 +561,19 @@ export class NeoVis {
560561
/**
561562
* Renders the network
562563
*/
563-
render(query?: Cypher, parameters?: unknown): void {
564-
if (this.#config.dataFunction) {
565-
this.#runFunctionDataGetter(parameters);
564+
render(query_or_function?: Cypher | DataFunctionType, parameters?: unknown): void {
565+
if (this.#config.dataFunction || typeof query_or_function === 'function') {
566+
this.#runFunctionDataGetter(typeof query_or_function === 'function' ? query_or_function : this.#config.dataFunction, parameters);
566567
} else {
567-
this.#runNeo4jDataGetter(query, parameters);
568+
this.#runNeo4jDataGetter(query_or_function as Cypher, parameters);
568569
}
569570
}
570571

571-
async #runFunctionDataGetter(parameters?: unknown) {
572+
async #runFunctionDataGetter(func: DataFunctionType, parameters: unknown) {
572573
let recordCount = 0;
573574
try {
574575
const dataBuildPromises: Promise<unknown>[] = [];
575-
for await (const record of await this.#config.dataFunction(parameters)) {
576+
for await (const record of await func(parameters)) {
576577
dataBuildPromises.push(this.#createSingleRecord(record));
577578
recordCount++;
578579
}
@@ -733,17 +734,17 @@ export class NeoVis {
733734
* Reset the config object and reload data
734735
* @param config
735736
*/
736-
reinit(config: NeovisConfig | NonFlatNeovisConfig): void {
737+
reinit(config: NeovisConfig | NonFlatNeovisConfig, parameter?: unknown): void {
737738
this.#init(config);
738-
this.render();
739+
this.render(undefined, parameter);
739740
}
740741

741742
/**
742743
* Clear the network and fetch live data form the server and reload the visualization
743744
*/
744-
reload(): void {
745+
reload(parameter?: unknown): void {
745746
this.clearNetwork();
746-
this.render();
747+
this.render(undefined, parameter);
747748
}
748749

749750
/**
@@ -757,21 +758,42 @@ export class NeoVis {
757758
/**
758759
* Execute an arbitrary Cypher query and re-render the visualization
759760
* @param query
761+
* @param parameters - parameters to send to the cypher
760762
*/
761-
renderWithCypher(query: Cypher): void {
763+
renderWithCypher(query: Cypher, parameters?: unknown): void {
762764
// this._config.initialCypher = query;
763765
this.clearNetwork();
764766
this.#query = query;
765-
this.render();
767+
this.render(undefined, parameters);
768+
}
769+
770+
/**
771+
* Execute an arbitrary function and re-render the visualization
772+
* @param func
773+
* @param parameters - parameters to send to the function
774+
*/
775+
renderWithFunction(func: DataFunctionType, parameters?: unknown): void {
776+
this.clearNetwork();
777+
this.render(func, parameters);
766778
}
767779

768780
/**
769781
* Execute an arbitrary Cypher query and update the current visualization, retaning current nodes
770-
* This function will not change the original query given by renderWithCypher or the inital cypher.
771-
* @param query
782+
* This function will not change the original query given by renderWithCypher or the inital cypher.
783+
* @param parameters - parameters to send to the cypher
784+
*
785+
*/
786+
updateWithCypher(query: Cypher, parameters?: unknown): void {
787+
this.render(query, parameters);
788+
}
789+
790+
/**
791+
* Execute an arbitrary function and update the visualization
792+
* @param func
793+
* @param parameters - parameters to send to the function
772794
*/
773-
updateWithCypher(query: Cypher): void {
774-
this.render(query);
795+
updateWithFunction(func: DataFunctionType, parameters?: unknown): void {
796+
this.render(func, parameters);
775797
}
776798
}
777799

src/types.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const NEOVIS_ADVANCED_CONFIG = Symbol();
77
export type NumberOrInteger = number | Neo4jTypes.Integer;
88

99
export type RecursiveMapToDist<T, New> = T extends object ? RecursiveMapTo<T, New> : New
10+
export type DataFunctionType = (any?: unknown) => AsyncIterable<Neo4jTypes.Record> | Promise<Iterable<Neo4jTypes.Record>> | Iterable<Neo4jTypes.Record>;
1011

1112
/**
1213
* Maps a type recursively and replace each non object type with the new type
@@ -115,10 +116,13 @@ export interface BaseNeovisConfig {
115116

116117

117118
/**
118-
* function to get the data instead of neo4j driver
119-
* @returns list of neo4j data
119+
* function to get fetch data instead of neo4j driver
120+
*
121+
* it needs to return a list of records
122+
*
123+
* example in examples/simple-dataFunction-example.html
120124
*/
121-
dataFunction?: (any?: unknown) => AsyncIterable<Neo4jTypes.Record> | Promise<Iterable<Neo4jTypes.Record>> | Iterable<Neo4jTypes.Record>
125+
dataFunction?: DataFunctionType
122126

123127
/**
124128
* The Cypher query that will get the data

0 commit comments

Comments
 (0)