blob: c6b98677d465ec7ff09b9bb102b7508a30dbe41b [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
//tag::example-block[]
const IgniteClient = require('apache-ignite-client');
const IgniteClientConfiguration = IgniteClient.IgniteClientConfiguration;
const ObjectType = IgniteClient.ObjectType;
const CacheEntry = IgniteClient.CacheEntry;
const ComplexObjectType = IgniteClient.ComplexObjectType;
class Person {
constructor(id = null, name = null, salary = null) {
this.id = id;
this.name = name;
this.salary = salary;
}
}
async function putGetComplexAndBinaryObjects() {
const igniteClient = new IgniteClient();
try {
await igniteClient.connect(new IgniteClientConfiguration('127.0.0.1:10800'));
const cache = await igniteClient.getOrCreateCache('myPersonCache');
// Complex Object type for JavaScript Person class instances
const personComplexObjectType = new ComplexObjectType(new Person(0, '', 0)).
setFieldType('id', ObjectType.PRIMITIVE_TYPE.INTEGER);
// Set cache key and value types
cache.setKeyType(ObjectType.PRIMITIVE_TYPE.INTEGER).
setValueType(personComplexObjectType);
// Put Complex Objects to the cache
await cache.put(1, new Person(1, 'John Doe', 1000));
await cache.put(2, new Person(2, 'Jane Roe', 2000));
// Get Complex Object; returned value is an instance of Person class
const person = await cache.get(1);
console.log(person);
// New CacheClient instance of the same cache to operate with BinaryObjects
const binaryCache = igniteClient.getCache('myPersonCache').
setKeyType(ObjectType.PRIMITIVE_TYPE.INTEGER);
// Get Complex Object from the cache in a binary form, returned value is an instance of BinaryObject class
let binaryPerson = await binaryCache.get(2);
console.log('Binary form of Person:');
for (let fieldName of binaryPerson.getFieldNames()) {
let fieldValue = await binaryPerson.getField(fieldName);
console.log(fieldName + ' : ' + fieldValue);
}
// Modify Binary Object and put it to the cache
binaryPerson.setField('id', 3, ObjectType.PRIMITIVE_TYPE.INTEGER).
setField('name', 'Mary Major');
await binaryCache.put(3, binaryPerson);
// Get Binary Object from the cache and convert it to JavaScript Object
binaryPerson = await binaryCache.get(3);
console.log(await binaryPerson.toObject(personComplexObjectType));
await igniteClient.destroyCache('myPersonCache');
}
catch (err) {
console.log(err.message);
}
finally {
igniteClient.disconnect();
}
}
putGetComplexAndBinaryObjects();
//end::example-block[]