· 5 years ago · Mar 23, 2020, 02:23 PM
1var methods = {
2 fetch: require('../lib/fetch.js'),
3 set: require('../lib/set.js'),
4 delete: require('../lib/delete.js'),
5 push: require('../lib/push.js'),
6 add: require('../lib/add.js'),
7 subtract: require('../lib/subtract.js'),
8 fetchAll: require('../lib/fetchAll.js'),
9 startsWith: require('../lib/startsWith.js'),
10 createWebview: require('../lib/createWebview.js'),
11 table: require('../lib/table.js'),
12 setupServer: require('../lib/setupServer.js'),
13 login: require('../lib/login.js')
14}
15
16if (typeof keys === "undefined" || keys === null) {
17 var keys = {}
18}
19
20module.exports = {
21
22 version : '0.0.4-dev.59',
23
24 /**
25 * This function fetches data from a key in the database. (alias: .get())
26 * @param {key} input any string as a key. Also allows for dot notation following the key.
27 * @param {options} [input={ target: null }] Any options to be added to the request.
28 * @returns {data} the data requested.
29 */
30
31 fetch: function(key, ops) {
32 if (!key) throw new TypeError('No key specified. Need Help? DM DarkenLight Mage#2401');
33 return arbitrate('fetch', {id: key, ops: ops || {}});
34 },
35
36 get: function(key, ops) {
37 if (!key) throw new TypeError('No key specified. Need Help? Check Out: discord.gg/plexidev');
38 return arbitrate('fetch', {id: key, ops: ops || {}});
39 },
40
41 /**
42 * This function sets new data based on a key in the database.
43 * @param {key} input any string as a key. Also allows for dot notation following the key.
44 * @param {options} [input={ target: null }] Any options to be added to the request.
45 * @returns {data} the updated data.
46 */
47
48 set: function(key, value, ops) {
49 if (!key) throw new TypeError('No key specified. Need Help? DM DarkenLight Mage#2401');
50 if (value === undefined) throw new TypeError('No value specified. Need Help? DM DarkenLight Mage#2401');
51 return arbitrate('set', {stringify: true, id: key, data: value, ops: ops || {}});
52 },
53
54 /**
55 * This function adds a number to a key in the database. (If no existing number, it will add to 0)
56 * @param {key} input any string as a key. Also allows for dot notation following the key.
57 * @param {options} [input={ target: null }] Any options to be added to the request.
58 * @returns {data} the updated data.
59 */
60
61 add: function(key, value, ops) {
62 if (!key) throw new TypeError('No key specified. Need Help? DM DarkenLight Mage#2401');
63 if (isNaN(value)) throw new TypeError('Must specify value to add. Need Help? DM DarkenLight Mage#2401');
64 return arbitrate('add', {id: key, data: value, ops: ops || {}});
65 },
66
67 /**
68 * This function subtracts a number to a key in the database. (If no existing number, it will subtract from 0)
69 * @param {key} input any string as a key. Also allows for dot notation following the key.
70 * @param {options} [input={ target: null }] Any options to be added to the request.
71 * @returns {data} the updated data.
72 */
73
74 subtract: function(key, value, ops) {
75 if (!key) throw new TypeError('No key specified. Need Help? DM DarkenLight Mage#2401');
76 if (isNaN(value)) throw new TypeError('Must specify value to add. Need Help? DM DarkenLight Mage#2401');
77 return arbitrate('subtract', {id: key, data: value, ops: ops || {}});
78 },
79
80 /**
81 * This function will push into an array in the database based on the key. (If no existing array, it will create one)
82 * @param {key} input any string as a key. Also allows for dot notation following the key.
83 * @param {options} [input={ target: null }] Any options to be added to the request.
84 * @returns {data} the updated data.
85 */
86
87 push: function(key, value, ops) {
88 if (!key) throw new TypeError('No key specified. Need Help? DM DarkenLight Mage#2401');
89 if (!value && value != 0) throw new TypeError('Must specify value to push. Need Help? DM DarkenLight Mage#2401');
90 return arbitrate('push', {stringify: true, id: key, data: value, ops: ops || {}});
91 },
92
93 /**
94 * This function will delete an object (or property) in the database.
95 * @param {key} input any string as a key. Also allows for dot notation following the key, this will delete the prop in the object.
96 * @param {options} [input={ target: null }] Any options to be added to the request.
97 * @returns {boolean} if it was a success or not.
98 */
99
100 delete: function(key, ops) {
101 if (!key) throw new TypeError('No key specified. Need Help? DM DarkenLight Mage#2401');
102 return arbitrate('delete', {id: key, ops: ops || {}});
103 },
104
105 /**
106 * This function fetches the entire active table
107 * @param {options} [input={ target: null }] Any options to be added to the request.
108 * @returns {boolean} if it exists.
109 */
110
111 all: function(ops) {
112 return arbitrate('all', {ops: ops || {}});
113 },
114
115 fetchAll: function(ops) {
116 return arbitrate('all', {ops: ops || {}});
117 },
118
119 /**
120 * Using 'new' on this function creates a new instance of a table.
121 * @param {name} input any string as the name of the table.
122 * @param {options} options.
123 */
124
125 table: function(tableName) {
126
127 // Set Name
128 if (typeof tableName !== 'string') throw new TypeError('Table name has to be a string. Need Help? DM DarkenLight Mage#2401');
129 else if (tableName.includes(' ')) throw new TypeError('Table name cannot include spaces. Need Help? DM DarkenLight Mage#2401');
130 return arbitrate('table', {tableName: tableName})
131 },
132
133 /**
134 * This function will try to login into a database with the domain and the password specified.
135 * @param {domain} input any string as a domain. Specify the page you want it to connect. Example: 'https://yourdomain.com/' or 'https://yourdomain.com/page' or 'ip:port'
136 * @param {password} The password to connect to the database.
137 */
138 login: function(domain, password) {
139 if (typeof domain !== 'string') throw new TypeError('Domain has to be a string. Need Help? DM DarkenLight Mage#2401');
140 else if (domain.includes(' ')) throw new TypeError('Domain cannot include spaces. Need Help? DM DarkenLight Mage#2401');
141
142 if (typeof password !== 'string') throw new TypeError('Password has to be a string. Need Help? DM DarkenLight Mage#2401');
143 else if (password.includes(' ')) throw new TypeError('Password cannot include spaces. Need Help? DM DarkenLight Mage#2401');
144
145 return arbitrate('login', {domain: domain, password: password, ops: {}})
146 },
147
148 /**
149 * This function will setup a server to the database with the domain and the password specified.
150 * @param {URL path} input any string as a URL path you want the server to listen. Leave blank to listen to the main URL. Example: 'https://yourdomain.com/page'
151 * @param {password} The password to connect to the database.
152 */
153 setupServer: function(urlpath, lport, password) {
154 if (urlpath && password && lport) {
155 if (typeof urlpath !== 'string') throw new TypeError('URL path has to be a string. Need Help? DM DarkenLight Mage#2401');
156 else if (urlpath.includes(' ')) throw new TypeError('URL path cannot include spaces. Need Help? DM DarkenLight Mage#2401');
157
158 if (typeof password !== 'string') throw new TypeError('Password has to be a string. Need Help? DM DarkenLight Mage#2401');
159 else if (password.includes(' ')) throw new TypeError('Password cannot include spaces. Need Help? DM DarkenLight Mage#2401');
160
161 if (typeof lport !== 'number') throw new TypeError('Listener port has to be a number. Need Help? DM DarkenLight Mage#2401');
162 else if (lport.includes(' ')) throw new TypeError('Listener port cannot include spaces. Need Help? DM DarkenLight Mage#2401');
163
164 return arbitrate('setupServer', {path: urlpath, password: password, port: lport, ops: {}})
165 }
166
167 if (typeof urlpath !== 'number') throw new TypeError('Listener port has to be a number. Need Help? DM DarkenLight Mage#2401');
168 //else if (urlpath.includes(' ')) throw new TypeError('Listener port cannot include spaces. Need Help? DM DarkenLight Mage#2401');
169
170 if (typeof lport !== 'string') throw new TypeError('Password has to be a string. Need Help? DM DarkenLight Mage#2401');
171 else if (lport.includes(' ')) throw new TypeError('Password cannot include spaces. Need Help? DM DarkenLight Mage#2401');
172
173 return arbitrate('setupServer', {password: lport, port: urlpath, ops: {}})
174 },
175 }
176
177
178
179 function arbitrate(method, params) {
180 if (keys) {
181
182 // Verify Options
183 if (params.ops.target && params.ops.target[0] === '.') params.ops.target = params.ops.target.slice(1); // Remove prefix if necessary
184 if (params.data && params.data === Infinity) throw new TypeError(`You cannot set Infinity into the database @ ID: ${params.id}`)
185
186 // Stringify
187 if (params.stringify) {
188 try { params.data = JSON.stringify(params.data); } catch (e)
189 { throw new TypeError(`Please supply a valid input @ ID: ${params.id}\nError: ${e.message}`); }
190 }
191
192 // Translate dot notation from keys
193 if (params.id && params.id.includes('.')) {
194 let unparsed = params.id.split('.');
195 params.id = unparsed.shift();
196 params.ops.target = unparsed.join('.');
197 }
198
199
200 // Run & Return Method
201 return methods[method](params, keys);
202 } else {
203 if (method === 'login') {
204 return methods['login'](params, keys)
205 } else throw new TypeError('No domain/password specified. Please use the login function to set the domain and the password.');
206 }
207}