UNPKG

6.5 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | ewd-document-store: Persistent JavaScript Objects and Document Database |
5 | using Global Storage |
6 | |
7 | Copyright (c) 2017 M/Gateway Developments Ltd, |
8 | Reigate, Surrey UK. |
9 | All rights reserved. |
10 | |
11 | http://www.mgateway.com |
12 | Email: rtweed@mgateway.com |
13 | |
14 | |
15 | Licensed under the Apache License, Version 2.0 (the "License"); |
16 | you may not use this file except in compliance with the License. |
17 | You may obtain a copy of the License at |
18 | |
19 | http://www.apache.org/licenses/LICENSE-2.0 |
20 | |
21 | Unless required by applicable law or agreed to in writing, software |
22 | distributed under the License is distributed on an "AS IS" BASIS, |
23 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
24 | See the License for the specific language governing permissions and |
25 | limitations under the License. |
26 ----------------------------------------------------------------------------
27
28 7 September 2017
29
30*/
31
32'use strict';
33
34// Standalone example demonstrating use of ewd-document-store with Cache database
35// You may need to run this as sudo because of permissions
36
37require('dotenv').config();
38
39var DocumentStore = require('ewd-document-store');
40var Cache = require('cache').Cache;
41var db = new Cache();
42
43// Change these parameters to match your GlobalsDB or Cache system:
44var ok = db.open({
45 path: process.env.CACHE_MGR_PATH || '/opt/cache/mgr',
46 username: process.env.CACHE_USERNAME || '_SYSTEM',
47 password: process.env.CACHE_PASSWORD || 'SYS',
48 namespace: process.env.CACHE_NAMESPACE || 'USER'
49});
50
51console.log('ok: ' + JSON.stringify(ok));
52console.log('version: ' + db.version());
53
54var documentStore = new DocumentStore(db);
55var rob = new documentStore.DocumentNode('rob');
56
57var temp = new documentStore.DocumentNode('temp', [1]);
58console.log('exists: ' + temp.exists);
59console.log('hasValue: ' + temp.hasValue);
60console.log('hasChildren: ' + temp.hasChildren);
61console.log('value: ' + temp.value);
62
63console.log(JSON.stringify(temp.getDocument(), null, 2));
64
65documentStore.on('afterSet', function (node) {
66 console.log('afterSet: ' + JSON.stringify(node));
67});
68
69rob.$('x').value = 'hello';
70rob.$('y').value = 'world';
71rob.$('a').increment();
72
73var z = {
74 a: 'this is a',
75 b: 'this is b',
76 Barton: 'J',
77 Briggs: 'A',
78 Davies: 'D',
79 Davis: 'T',
80 Douglas: 'N',
81 c: ['a', 's', 'd'],
82 d: {
83 a: 'a',
84 b: 'b'
85 }
86};
87
88rob.$('z').setDocument(z);
89
90console.log(JSON.stringify(rob.getDocument(), null, 2));
91
92console.log('forEachChild through rob document:');
93rob.forEachChild(function (nodeName) {
94 console.log(nodeName);
95});
96
97console.log('forEachChild through rob document, stopping early:');
98rob.forEachChild(function (nodeName) {
99 console.log(nodeName);
100 if (nodeName === 'x') {
101 return true;
102 }
103});
104
105console.log('forEachChild through rob document, in reverse:');
106rob.forEachChild({
107 direction: 'reverse'
108}, function (nodeName) {
109 console.log(nodeName);
110});
111
112console.log('forPrefix through rob global starting x:');
113rob.forEachChild({
114 prefix: 'x'
115}, function (subscript) {
116 console.log(subscript);
117});
118
119console.log('forEachLeafNode through rob global:');
120rob.forEachLeafNode(function (value) {
121 console.log(value);
122});
123
124console.log('Number of children: ' + rob.countChildren());
125
126var robx = rob.$('x', true);
127console.log('robx: ' + robx.value);
128console.log(JSON.stringify(rob, null, 2));
129console.log('===============');
130console.log(JSON.stringify(robx, null, 2));
131
132var roby = rob.$x.$('y');
133console.log('parent: ' + roby.parent.value);
134
135var first = rob.firstChild;
136console.log('first: ' + first.name);
137console.log('next = ' + first.nextSibling.name);
138
139var last = rob.lastChild;
140console.log('last: ' + last.name);
141console.log('previous = ' + last.previousSibling.name);
142
143var z = rob.$z;
144console.log('Names from Br to Da');
145z.forEachChild({
146 range: {
147 from: 'Br',
148 to: 'Da'
149 }
150}, function (lastName, node) {
151 console.log('LastName: ' + lastName + '; firstName: ' + node.value);
152});
153console.log('------------');
154console.log('Names from Br to Db');
155z.forEachChild({
156 range: {
157 from: 'Br',
158 to: 'Db'
159 }
160}, function (lastName, node) {
161 console.log('LastName: ' + lastName + '; firstName: ' + node.value);
162});
163console.log('------------');
164console.log('Names from Briggs to Davis');
165z.forEachChild({
166 range: {
167 from: 'Briggs',
168 to: 'Davis'
169 }
170}, function (lastName, node) {
171 console.log('LastName: ' + lastName + '; firstName: ' + node.value);
172});
173console.log('------------');
174console.log('Names from B to D');
175z.forEachChild({
176 range: {
177 from: 'B',
178 to: 'D'
179 }
180}, function (lastName, node) {
181 console.log('LastName: ' + lastName + '; firstName: ' + node.value);
182});
183console.log('------------');
184console.log('Names from B');
185z.forEachChild({
186 range: {
187 from: 'B'
188 }
189}, function (lastName, node) {
190 //jshint unused:false
191 console.log('LastName: ' + lastName);
192});
193console.log('------------');
194console.log('Names from D');
195z.forEachChild({
196 range: {
197 from: 'D'
198 }
199}, function (lastName, node) {
200 //jshint unused:false
201 console.log('LastName: ' + lastName);
202});
203console.log('------------');
204console.log('Names to D');
205z.forEachChild({
206 range: {
207 to: 'D'
208 }
209}, function (lastName, node) {
210 //jshint unused:false
211 console.log('LastName: ' + lastName);
212});
213console.log('------------');
214
215console.log('temp before: ' + temp.value);
216temp.value = 1234;
217console.log('temp after: ' + temp.value);
218
219temp.delete();
220console.log('temp after delete: ' + temp.value);
221
222var list = documentStore.list();
223console.log(JSON.stringify(list));
224
225db.close();