node-oracledb/lib/dbObject.js

151 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-04-01 12:48:37 +08:00
// Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
//
//----------------------------------------------------------------------------
//
// You may not use the identified files except in compliance with the Apache
// License, Version 2.0 (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.
//
//-----------------------------------------------------------------------------
'use strict';
const util = require('util');
// define base database object class; instances of this class are never
// instantiated; instead, classes subclassed from this one will be
// instantiated; a cache of these classes are maintained on each connection
class BaseDbObject {
2019-11-05 07:54:10 +08:00
// extend class with promisified functions
_extend(oracledb) {
this._oracledb = oracledb;
}
// initialize object with value
_initialize(initialValue) {
if (this.isCollection) {
2019-11-05 07:54:10 +08:00
for (let i = 0; i < initialValue.length; i++) {
this.append(initialValue[i]);
}
2019-11-05 07:54:10 +08:00
} else {
Object.assign(this, initialValue);
}
}
// return as a plain object
_toPojo() {
if (this.isCollection) {
2020-11-19 10:06:47 +08:00
const result = this.getValues();
if (this.elementType == this._oracledb.DB_TYPE_OBJECT) {
for (let i = 0; i < result.length; i++) {
result[i] = result[i]._toPojo();
}
}
2021-04-01 12:48:37 +08:00
return (result);
}
const result = {};
for (let name in this.attributes) {
let value = this[name];
if (value instanceof BaseDbObject) {
value = value._toPojo();
}
result[name] = value;
}
2021-04-01 12:48:37 +08:00
return (result);
}
// custom inspection routine
[util.inspect.custom](depth, options) {
2021-04-01 12:48:37 +08:00
return ('[' + this.fqn + '] ' + util.inspect(this._toPojo(), options));
}
[Symbol.iterator]() {
if (this.isCollection) {
const values = this.getValues();
2021-04-01 12:48:37 +08:00
return (values[Symbol.iterator]());
}
throw TypeError("obj is not iterable");
}
2019-06-28 08:29:16 +08:00
[Symbol.toPrimitive](hint) {
switch (hint) {
case 'number':
2021-04-01 12:48:37 +08:00
return (NaN);
2019-06-28 08:29:16 +08:00
default:
2021-04-01 12:48:37 +08:00
return ('[' + this.fqn + '] ' + util.inspect(this._toPojo(), {}));
2019-06-28 08:29:16 +08:00
}
}
2019-11-05 07:54:10 +08:00
get [Symbol.toStringTag]() {
2021-04-01 12:48:37 +08:00
return (this.fqn);
2019-11-05 07:54:10 +08:00
}
toJSON() {
2021-04-01 12:48:37 +08:00
return (this._toPojo());
}
}
2019-11-05 07:54:10 +08:00
// define proxy handler used for collections
BaseDbObject._collectionProxyHandler = {
deleteProperty(target, prop) {
if (typeof prop === 'string') {
const index = +prop;
if (!isNaN(index)) {
2021-04-01 12:48:37 +08:00
return (target.deleteElement(index));
2019-11-05 07:54:10 +08:00
}
}
2021-04-01 12:48:37 +08:00
return (delete target[prop]);
2019-11-05 07:54:10 +08:00
},
get(target, prop) {
if (typeof prop === 'string') {
// when binding collections, we must be consistent in getting the target
// of the proxy, since napi_wrap() called on the proxy will not be
// available when calling napi_unwrap() on the target; this property
// forces the target to get returned
if (prop === '_target') {
2021-04-01 12:48:37 +08:00
return (target);
2019-11-05 07:54:10 +08:00
}
const index = +prop;
if (!isNaN(index)) {
2021-04-01 12:48:37 +08:00
return (target.getElement(index));
2019-11-05 07:54:10 +08:00
}
}
const value = target[prop];
if (typeof value === 'function') {
2021-04-01 12:48:37 +08:00
return (value.bind(target));
2019-11-05 07:54:10 +08:00
}
2021-04-01 12:48:37 +08:00
return (value);
2019-11-05 07:54:10 +08:00
},
set(target, prop, value) {
if (typeof prop === 'string') {
const index = +prop;
if (!isNaN(index)) {
target.setElement(index, value);
2021-04-01 12:48:37 +08:00
return (true);
2019-11-05 07:54:10 +08:00
}
}
target[prop] = value;
2021-04-01 12:48:37 +08:00
return (true);
2019-11-05 07:54:10 +08:00
}
};
module.exports = BaseDbObject;