UNPKG

neo4j-driver-core

Version:
324 lines (323 loc) 11.9 kB
"use strict"; /** * Copyright (c) "Neo4j" * Neo4j Sweden AB [https://neo4j.com] * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); var error_1 = require("./error"); function generateFieldLookup(keys) { var lookup = {}; keys.forEach(function (name, idx) { lookup[name] = idx; }); return lookup; } /** * Records make up the contents of the {@link Result}, and is how you access * the output of a query. A simple query might yield a result stream * with a single record, for instance: * * MATCH (u:User) RETURN u.name, u.age * * This returns a stream of records with two fields, named `u.name` and `u.age`, * each record represents one user found by the query above. You can access * the values of each field either by name: * * record.get("u.name") * * Or by it's position: * * record.get(0) * * @access public */ var Record = /** @class */ (function () { /** * Create a new record object. * @constructor * @protected * @param {string[]} keys An array of field keys, in the order the fields appear in the record * @param {Array} fields An array of field values * @param {Object} fieldLookup An object of fieldName -> value index, used to map * field names to values. If this is null, one will be * generated. */ function Record(keys, fields, fieldLookup) { /** * Field keys, in the order the fields appear in the record. * @type {string[]} */ this.keys = keys; /** * Number of fields * @type {Number} */ this.length = keys.length; this._fields = fields; this._fieldLookup = fieldLookup !== null && fieldLookup !== void 0 ? fieldLookup : generateFieldLookup(keys); } /** * Run the given function for each field in this record. The function * will get three arguments - the value, the key and this record, in that * order. * * @param {function(value: Object, key: string, record: Record)} visitor the function to apply to each field. * @returns {void} Nothing */ Record.prototype.forEach = function (visitor) { var e_1, _a; try { for (var _b = __values(this.entries()), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 2), key = _d[0], value = _d[1]; visitor(value, key, this); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } }; /** * Run the given function for each field in this record. The function * will get three arguments - the value, the key and this record, in that * order. * * @param {function(value: Object, key: string, record: Record)} visitor the function to apply on each field * and return a value that is saved to the returned Array. * * @returns {Array} */ Record.prototype.map = function (visitor) { var e_2, _a; var resultArray = []; try { for (var _b = __values(this.entries()), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 2), key = _d[0], value = _d[1]; resultArray.push(visitor(value, key, this)); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } return resultArray; }; /** * Iterate over results. Each iteration will yield an array * of exactly two items - the key, and the value (in order). * * @generator * @returns {IterableIterator<Array>} */ Record.prototype.entries = function () { var i; return __generator(this, function (_a) { switch (_a.label) { case 0: i = 0; _a.label = 1; case 1: if (!(i < this.keys.length)) return [3 /*break*/, 4]; return [4 /*yield*/, [this.keys[i], this._fields[i]]]; case 2: _a.sent(); _a.label = 3; case 3: i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; } }); }; /** * Iterate over values. * * @generator * @returns {IterableIterator<Object>} */ Record.prototype.values = function () { var i; return __generator(this, function (_a) { switch (_a.label) { case 0: i = 0; _a.label = 1; case 1: if (!(i < this.keys.length)) return [3 /*break*/, 4]; return [4 /*yield*/, this._fields[i]]; case 2: _a.sent(); _a.label = 3; case 3: i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; } }); }; /** * Iterate over values. Delegates to {@link Record#values} * * @generator * @returns {IterableIterator<Object>} */ Record.prototype[Symbol.iterator] = function () { var i; return __generator(this, function (_a) { switch (_a.label) { case 0: i = 0; _a.label = 1; case 1: if (!(i < this.keys.length)) return [3 /*break*/, 4]; return [4 /*yield*/, this._fields[i]]; case 2: _a.sent(); _a.label = 3; case 3: i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; } }); }; /** * Generates an object out of the current Record * * @returns {Object} */ Record.prototype.toObject = function () { var e_3, _a; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions var obj = {}; try { for (var _b = __values(this.entries()), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 2), key = _d[0], value = _d[1]; obj[key] = value; } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_3) throw e_3.error; } } return obj; }; /** * Get a value from this record, either by index or by field key. * * @param {string|Number} key Field key, or the index of the field. * @returns {*} */ Record.prototype.get = function (key) { var index; if (!(typeof key === 'number')) { index = this._fieldLookup[key]; if (index === undefined) { throw (0, error_1.newError)("This record has no field with key '".concat(key.toString(), "', available keys are: [") + this.keys.toString() + '].'); } } else { index = key; } if (index > this._fields.length - 1 || index < 0) { throw (0, error_1.newError)("This record has no field with index '" + index.toString() + "'. Remember that indexes start at `0`, " + 'and make sure your query returns records in the shape you meant it to.'); } return this._fields[index]; }; /** * Check if a value from this record, either by index or by field key, exists. * * @param {string|Number} key Field key, or the index of the field. * @returns {boolean} */ Record.prototype.has = function (key) { // if key is a number, we check if it is in the _fields array if (typeof key === 'number') { return key >= 0 && key < this._fields.length; } // if it's not a number, we check _fieldLookup dictionary directly return this._fieldLookup[key] !== undefined; }; return Record; }()); exports.default = Record;