UNPKG

@flexvertex/flexvertex-driver

Version:

The official FlexVertex Node.js driver

214 lines (180 loc) 6.45 kB
import axios from 'axios'; import { Buffer } from 'node:buffer'; import { JSONParse, JSONStringify } from 'json-with-bigint'; import { v4 as uuidv4 } from 'uuid'; //import { FlexConnection } from '../connections/FlexConnection.js'; /** * Represents a FlexVertex FlexObject. */ export class FlexObject { #flexSchema; #className; #objectKey = undefined; #attributes = { }; #properties = { }; #uniqueID = uuidv4(); /** * * @param {!FlexSchema} flexSchema - The FlexSchema this FlexObject belongs * @param {!string} className - The name of the class of this FlexObject */ constructor(flexSchema, className) { this.#flexSchema = flexSchema; this.#className = className; } static createFromObject(schema, o) { if(schema === undefined) throw "FlexObject.createFromObject() schema is undefined!"; if(o === undefined) throw "FlexObject.createFromObject() object is undefined!"; if(o.Metadata === undefined) throw "FlexObject.createFromObject() Metadata is undefined!"; if(o.Metadata.Class === undefined) throw "FlexObject.createFromObject() Metadata.Class is undefined!"; let obj = new FlexObject(schema, o.Metadata.Class); // if(!(o.attributes === undefined)) obj.attributes = o.Attributes; // if(!(o.properties === undefined)) obj.properties = o.Properties; // if(!(o.objectKey === undefined)) obj.objectKey = o.Metadata.ObjectKey; return obj; } /** * @property {Object} attributes - Returns this FlexObject's attributes */ get attributes() { return this.#attributes; } set attributes(jsonProps) { if(!(jsonProps === undefined)) Object.assign(this.#attributes, jsonProps); } /** * @property {string} className - Returns the name of this FlexObject's class */ get className() { return this.#className; } set className(name) { this.#className = name; } /** * @property {string} objectKey - Returns this FlexObject's ObjectKey */ get objectKey() { return this.#objectKey; } set objectKey(key) { if(!(key === undefined)) this.#objectKey = key; } /** * @property {Object} properties - Returns this FlexObject's properties */ get properties() { return this.#properties; } set properties(jsonProps) { if(!(jsonProps === undefined)) Object.assign(this.#properties, jsonProps); } get uniqueID() { return this.#uniqueID; } /* set uniqueID(id) { if(!(id === undefined)) this.#uniqueID = id; }*/ static printObject(obj) { if(!(obj === undefined)) { for(let [name, value] of Object.entries(obj)) { console.log("[" + name + "] = " + value); } } } printAttributes() { FlexObject.printObject(this.#attributes); } printProperties() { FlexObject.printObject(this.#properties); } /** * * @returns {boolean} True if this FlexObject has no ObjectKey defined yet */ isNew() { return this.#objectKey === undefined; } /** * * @param {!string} key * @returns This FlexObject's property named key if it exists */ getProperty(key) { return Object.getOwnPropertyDescriptor(this.#properties, key).value; } /** * Sets a property on this FlexObject * @param {!string} key * @param {!*} value * @returns {FlexObject} This FlexObject */ setProperty(key, value) { // console.log("-- setProperty() key = " + key + ", value = " + value); Object.defineProperty(this.#properties, key, { value: value, enumerable: true, writable: true }); // console.log("-- setProperty() get value = " + Object.getOwnPropertyDescriptor(this.#properties, key).value); /* for(const [key, value] of Object.entries(this.#properties)) { console.log('entry: ${key}: ${value}'); } */ // console.log("-- setProperty() getProperties = " + JSONStringify(this.#properties)); // console.log("-- setProperty() getProperties = " + JSONStringify(this.properties)); return this; } /** * Creates a {@link FlexConnection} from the source FlexObject to this FlexObject * @param {!FlexObject} source * @param {Object} options * @param {string} [options.class] - The connection class to use for the {@link FlexConnection} * @param {string} [options.name] - The name to associate with the created connection (cannot be used with toName or fromName) * @param {string} [options.toName] - The to name to associate with the created connection (cannot be used with name) * @param {string} [options.fromName] - The from name to associate with the created connection (cannot be used with name) */ connectFrom(source, options) { let cx = this.#flexSchema.createConnection(source.objectKey, this.#objectKey, options); // let cx = new FlexConnection(this.#flexSchema, source.objectKey(), objectKey(), options); // await cx.save(); return cx; } /** * Creates a {@link FlexConnection} from this FlexObject to the destination FlexObject * @param {!FlexObject} destination * @param {Object} options * @param {string} [options.class] - The connection class to use for the {@link FlexConnection} * @param {string} [options.name] - The name to associate with the created connection (cannot be used with toName or fromName) * @param {string} [options.toName] - The to name to associate with the created connection (cannot be used with name) * @param {string} [options.fromName] - The from name to associate with the created connection (cannot be used with name) */ connectTo(destination, options) { // let cx = await this.#flexSchema.createConnection(this.#objectKey, destination.objectKey, options); let cx = this.#flexSchema.createConnection(this.#objectKey, destination.objectKey, options); // let cx = new FlexConnection(this.#flexSchema, objectKey(), destination.objectKey(), options); // await cx.save(); return cx; } getObject() { // console.log("FlexObject getObject() calling isNew().."); if(this.isNew()) { return { Class: this.#className, UniqueID: this.#uniqueID, Attributes: this.#attributes, Properties: this.#properties }; } else { return { ObjectKey: this.#objectKey, UniqueID: this.#uniqueID, Attributes: this.#attributes, Properties: this.#properties }; } } async save() { if(this.#flexSchema === undefined) throw "FlexObject.save() The FlexSchema is undefined!"; await this.#flexSchema.saveObject(this); } }