Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
service:jsondb [2022/11/28 22:15] – Added information about requiring json-client.js in a JS script in order to use JSONClient, how to create a JSONClient object, and calling disconnect() Nightfox | service:jsondb [2022/12/08 14:01] (current) – [Best Practices] Nightfox | ||
---|---|---|---|
Line 4: | Line 4: | ||
\\ | \\ | ||
===== Updating services.ini and json-service.ini ===== | ===== Updating services.ini and json-service.ini ===== | ||
- | Synchronet provides this via the script json-service.js (in the exec directory). | + | Synchronet provides this via the script json-service.js (in the exec directory). |
< | < | ||
[JSON] | [JSON] | ||
Line 13: | Line 13: | ||
Note the port number is 10088. | Note the port number is 10088. | ||
\\ | \\ | ||
- | For storing data to be accessed this way, Synchronet uses the concept of JSON " | + | For storing data to be accessed this way, Synchronet uses the concept of JSON " |
< | < | ||
[oneliners] | [oneliners] | ||
Line 23: | Line 23: | ||
===== Reading from & writing to a JSON database===== | ===== Reading from & writing to a JSON database===== | ||
- | In a JavaScript mod/ | + | In a JavaScript mod/ |
require(" | require(" | ||
Then, create a JSONClient object. | Then, create a JSONClient object. | ||
Line 61: | Line 61: | ||
===== Best Practices ===== | ===== Best Practices ===== | ||
- | When there could be concurrent access to the same data (i.e., multiple clients that might need to write to the same location in the JSON data), it would be a good practice to lock that location for writing, | + | When there could be concurrent access to the same data (i.e., multiple clients that might need to write to the same location in the JSON data), it is a good practice to lock that location for writing |
+ | \\ | ||
+ | Note that the write function has a lock parameter: | ||
+ | JSONCLient.write(scope, | ||
+ | If you specify the lock parameter (such as 2, the write lock), it will lock, write, and unlock all at once. | ||
+ | var JSON_DB_LOCK_WRITE = 2; | ||
+ | jsonClient.write(" | ||
+ | \\ | ||
+ | There are also a couple of functions, lock() and unlock() that you can use for locking and unlocking: | ||
jsonClient.lock(scope, | jsonClient.lock(scope, | ||
jsonClient.unlock(scope, | jsonClient.unlock(scope, | ||
+ | When you use those, then you would //not// specify the lock parameter for write(). | ||
+ | \\ | ||
For instance: | For instance: | ||
var JSON_DB_LOCK_READ = 1; | var JSON_DB_LOCK_READ = 1; | ||
Line 70: | Line 79: | ||
var jsonClient = new JSONClient(" | var jsonClient = new JSONClient(" | ||
jsonClient.lock(" | jsonClient.lock(" | ||
- | var scoresData = jsonClient.read(" | + | var scoresData = jsonClient.read(" |
// Change scoresData ... | // Change scoresData ... | ||
- | jsonClient.write(" | + | jsonClient.write(" |
jsonClient.unlock(" | jsonClient.unlock(" | ||
| | ||
+ | Note that you need to either specify the lock parameter for write() OR call lock() & unlock() without specifying a lock parameter for write(). | ||
===== Special files: commands.js and service.js ===== | ===== Special files: commands.js and service.js ===== | ||
There are a couple of additional JavaScript files you can create for your JSON database:\\ | There are a couple of additional JavaScript files you can create for your JSON database:\\ |