UNPKG

5.04 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | ewd-document-store: Persistent JavaScript Objects and Document Database |
5 | using Global Storage |
6 | |
7 | Copyright (c) 2016-19 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 20 November 2019
29
30*/
31
32var events = require("events");
33
34var DocumentNode = function(documentStore, documentName, path) {
35 if (!documentName) return;
36 path = path || [];
37 var pathType = typeof path;
38 if (pathType === 'string' || pathType === 'number') path = [path];
39 if (!Array.isArray(path)) return;
40 this.key = documentName + ';' + path.join();
41 this.documentStore = documentStore;
42 this.name = documentName;
43 this.isDocumentNode = true;
44 // this.path returns a clone of the path array
45 this.path = path.slice(0);
46 if (path.length > 0) {
47 this.name = path[path.length - 1];
48 this.isDocumentNode = false
49 }
50 this.documentName = documentName;
51 // internal globalStore.db node reference
52 this._node = {global: documentName, subscripts: this.path};
53
54};
55
56// Now define all its instance methods
57
58var proto = DocumentNode.prototype;
59proto._keys = Object.keys(proto).slice(0);
60
61Object.defineProperty(proto, '_defined', require('./proto/defined'));
62Object.defineProperty(proto, 'exists', require('./proto/exists'));
63Object.defineProperty(proto, 'hasValue', require('./proto/hasValue'));
64Object.defineProperty(proto, 'hasChildren', require('./proto/hasChildren'));
65Object.defineProperty(proto, 'value', require('./proto/value'));
66Object.defineProperty(proto, 'parent', require('./proto/parent'));
67Object.defineProperty(proto, 'firstChild', require('./proto/firstChild'));
68Object.defineProperty(proto, 'lastChild', require('./proto/lastChild'));
69Object.defineProperty(proto, 'nextSibling', require('./proto/nextSibling'));
70Object.defineProperty(proto, 'previousSibling', require('./proto/previousSibling'));
71
72proto.delete = require('./proto/delete');
73proto.$ = require('./proto/dollar');
74proto.increment = require('./proto/increment');
75proto.countChildren = require('./proto/count');
76proto.forEachChild = require('./proto/forEach');
77proto.getDocument = require('./proto/getDocument');
78proto.setDocument = require('./proto/setDocument');
79proto.forEachLeafNode = require('./proto/forEachLeafNode');
80proto.lock = require('./proto/lock');
81proto.unlock = require('./proto/unlock');
82
83proto._set = require('./proto/set');
84proto.enable_kvs = require('./proto/enable_kvs');
85proto.enable_list = require('./proto/enable_list');
86proto.enable_dom = require('./proto/enable_dom');
87proto.enable_rdb = require('./proto/enable_rdb');
88
89
90// ============= Top-level Constructor =============
91
92var build = require('./build');
93
94var DocumentStore = function(db) {
95 this.db = db;
96 this.build = build;
97 events.EventEmitter.call(this);
98 this.DocumentNode = DocumentNode.bind(undefined, this);
99 var dn = this.DocumentNode;
100 this.dbx_reg = {};
101
102 this.use = function(documentName, ...subscripts) {
103 if (subscripts.length === 1 && Array.isArray(subscripts[0])) {
104 subscripts = subscripts[0];
105 }
106 return new dn(documentName, subscripts);
107 };
108
109 this.sql = require('./proto/rdb/sql');
110
111};
112
113proto = DocumentStore.prototype;
114proto.__proto__ = events.EventEmitter.prototype;
115proto.list = require('./proto/list');
116
117
118module.exports = DocumentStore;