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 18:06] – Changed section formatting for table of contents; added a Best Practices section 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===== | ||
- | Most basically, all that is really required to interact with a JSON database is writing (adding/ | + | In a JavaScript mod/ |
+ | require(" | ||
+ | Then, create a JSONClient object. | ||
+ | var jsonClient = new JSONClient(" | ||
+ | Most basically, all that is really required to interact with a JSON database is writing (adding/ | ||
jsonClient.read(scope, | jsonClient.read(scope, | ||
Line 52: | Line 56: | ||
{ | { | ||
} | } | ||
+ | |||
+ | Also, when you are done using your JSONClient object, it is generally a good practice to explicitly have it disconnect from the server, by calling the ' | ||
+ | jsonClient.disconnect(); | ||
===== 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; | ||
var JSON_DB_LOCK_WRITE = 2; | var JSON_DB_LOCK_WRITE = 2; | ||
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:\\ |