UNPKG

6.32 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | ewd-document-store: Persistent JavaScript Objects and Document Database |
5 | using Global Storage |
6 | |
7 | Copyright (c) 2017-18 M/Gateway Developments Ltd, |
8 | Redhill, 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 25 April 2018
29
30*/
31
32'use strict';
33
34var DocumentStore = require('ewd-document-store');
35var Gtm = require('nodem').Gtm;
36var db = new Gtm();
37
38// Change these parameters to match your GlobalsDB or Cache system:
39var ok = db.open();
40
41console.log('ok: ' + JSON.stringify(ok));
42console.log('version: ' + db.version());
43
44var documentStore = new DocumentStore(db);
45var rob = new documentStore.DocumentNode('rob');
46
47var temp = new documentStore.DocumentNode('temp', [1]);
48console.log('exists: ' + temp.exists);
49console.log('hasValue: ' + temp.hasValue);
50console.log('hasChildren: ' + temp.hasChildren);
51console.log('value: ' + temp.value);
52
53console.log(JSON.stringify(temp.getDocument(), null, 2));
54
55documentStore.on('afterSet', function (node) {
56 console.log('afterSet: ' + JSON.stringify(node));
57});
58
59rob.$('x').value = 'hello';
60rob.$('y').value = 'world';
61rob.$('a').increment();
62
63var z = {
64 a: 'this is a',
65 b: 'this is b',
66 Barton: 'J',
67 Briggs: 'A',
68 Davies: 'D',
69 Davis: 'T',
70 Douglas: 'N',
71 c: ['a', 's', 'd'],
72 d: {
73 a: 'a',
74 b: 'b'
75 }
76};
77
78rob.$('z').setDocument(z);
79
80console.log(JSON.stringify(rob.getDocument(), null, 2));
81
82console.log('forEachChild through rob document:');
83rob.forEachChild(function (nodeName) {
84 console.log(nodeName);
85});
86
87console.log('forEachChild through rob document, stopping early:');
88rob.forEachChild(function (nodeName) {
89 console.log(nodeName);
90 if (nodeName === 'x') {
91 return true;
92 }
93});
94
95console.log('forEachChild through rob document, in reverse:');
96rob.forEachChild({
97 direction: 'reverse'
98}, function (nodeName) {
99 console.log(nodeName);
100});
101
102console.log('forPrefix through rob global starting x:');
103rob.forEachChild({
104 prefix: 'x'
105}, function (subscript) {
106 console.log(subscript);
107});
108
109console.log('forEachLeafNode through rob global:');
110rob.forEachLeafNode(function (value) {
111 console.log(value);
112});
113
114console.log('Number of children: ' + rob.countChildren());
115
116var robx = rob.$('x', true);
117console.log('robx: ' + robx.value);
118console.log(JSON.stringify(rob, null, 2));
119console.log('===============');
120console.log(JSON.stringify(robx, null, 2));
121
122var roby = rob.$x.$('y');
123console.log('parent: ' + roby.parent.value);
124
125var first = rob.firstChild;
126console.log('first: ' + first.name);
127console.log('next = ' + first.nextSibling.name);
128
129var last = rob.lastChild;
130console.log('last: ' + last.name);
131console.log('previous = ' + last.previousSibling.name);
132
133var z = rob.$z;
134console.log('Names from Br to Da');
135z.forEachChild({
136 range: {
137 from: 'Br',
138 to: 'Da'
139 }
140}, function (lastName, node) {
141 console.log('LastName: ' + lastName + '; firstName: ' + node.value);
142});
143console.log('------------');
144console.log('Names from Br to Db');
145z.forEachChild({
146 range: {
147 from: 'Br',
148 to: 'Db'
149 }
150}, function (lastName, node) {
151 console.log('LastName: ' + lastName + '; firstName: ' + node.value);
152});
153console.log('------------');
154console.log('Names from Briggs to Davis');
155z.forEachChild({
156 range: {
157 from: 'Briggs',
158 to: 'Davis'
159 }
160}, function (lastName, node) {
161 console.log('LastName: ' + lastName + '; firstName: ' + node.value);
162});
163console.log('------------');
164console.log('Names from B to D');
165z.forEachChild({
166 range: {
167 from: 'B',
168 to: 'D'
169 }
170}, function (lastName, node) {
171 console.log('LastName: ' + lastName + '; firstName: ' + node.value);
172});
173console.log('------------');
174console.log('Names from B');
175z.forEachChild({
176 range: {
177 from: 'B'
178 }
179}, function (lastName, node) {
180 //jshint unused:false
181 console.log('LastName: ' + lastName);
182});
183console.log('------------');
184console.log('Names from D');
185z.forEachChild({
186 range: {
187 from: 'D'
188 }
189}, function (lastName, node) {
190 //jshint unused:false
191 console.log('LastName: ' + lastName);
192});
193console.log('------------');
194console.log('Names to D');
195z.forEachChild({
196 range: {
197 to: 'D'
198 }
199}, function (lastName, node) {
200 //jshint unused:false
201 console.log('LastName: ' + lastName);
202});
203console.log('------------');
204
205console.log('temp before: ' + temp.value);
206temp.value = 1234;
207console.log('temp after: ' + temp.value);
208
209temp.delete();
210console.log('temp after delete: ' + temp.value);
211
212var list = documentStore.list();
213console.log(JSON.stringify(list));
214
215db.close();