use-neo4j
Version:
<div style="text-align:center"> <h1>React Hooks For Neo4j</h1>
97 lines (96 loc) • 3.85 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDatabases = exports.useSchema = void 0;
var react_1 = require("react");
var cypher_1 = require("./cypher");
function toPropertySchema(properties) {
var ordered = Object.entries(properties)
.sort(function (_a, _b) {
var a = _a[0];
var b = _b[0];
return a < b ? -1 : 1;
})
.map(function (_a) {
var key = _a[0], value = _a[1];
return [key, value];
});
return Object.fromEntries(ordered);
}
function toLabelSchema(label, input) {
return __assign(__assign({}, input), { label: label, relationships: Object.entries(input.relationships)
.map(function (_a) {
var key = _a[0], value = _a[1];
return toRelationshipTypeSchema(key, value);
})
.sort(function (a, b) { return a.type < b.type ? -1 : 1; }), properties: toPropertySchema(input.properties), count: input === null || input === void 0 ? void 0 : input.count.toNumber() });
}
function toRelationshipTypeSchema(type, input) {
var _a;
return __assign(__assign({}, input), { type: type, count: (_a = input.count) === null || _a === void 0 ? void 0 : _a.toNumber(), properties: toPropertySchema(input.properties) });
}
function useSchema(specificDatabase) {
var _a = cypher_1.useReadCypher('call apoc.meta.schema', {}, specificDatabase), loading = _a.loading, error = _a.error, first = _a.first, database = _a.database;
var _b = react_1.useState([]), labels = _b[0], setLabels = _b[1];
var _c = react_1.useState([]), types = _c[0], setTypes = _c[1];
react_1.useEffect(function () {
if (first) {
setLabels(Object.entries(first.get('value'))
.filter(function (_a) {
var key = _a[0], value = _a[1];
return value.type === 'node';
})
.map(function (_a) {
var key = _a[0], value = _a[1];
return toLabelSchema(key, value);
})
.sort(function (a, b) { return a.label < b.label ? -1 : 1; }));
setTypes(Object.entries(first.get('value'))
.filter(function (_a) {
var key = _a[0], value = _a[1];
return value.type === 'relationship';
})
.map(function (_a) {
var key = _a[0], value = _a[1];
return toRelationshipTypeSchema(key, value);
})
.sort(function (a, b) { return a.type < b.type ? -1 : 1; }));
}
}, [first]);
return {
loading: loading,
error: error,
database: database,
labels: labels,
types: types,
};
}
exports.useSchema = useSchema;
function useDatabases() {
var _a = cypher_1.useReadCypher('SHOW DATABASES', {}, 'system'), loading = _a.loading, error = _a.error, records = _a.records;
var databases = records === null || records === void 0 ? void 0 : records.map(function (row) { return ({
name: row.get('name'),
address: row.get('address'),
role: row.get('role'),
requestedStatus: row.get('requestedStatus'),
currentStatus: row.get('currentStatus'),
error: row.get('error'),
default: row.get('default'),
}); });
return {
loading: loading,
error: error,
databases: databases,
};
}
exports.useDatabases = useDatabases;