Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| service:jsondb [2022/11/27 15:40] – created: A basic start of a guide for using the JSON database service Nightfox | service:jsondb [2022/12/08 14:01] (current) – [Best Practices] Nightfox | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| For instance, a BBS might provide game scores for a particular game; that particular door game could be installed on multiple BBSes and post their user scores to the host BBS, which would provide a central place to host scores for the door, so that there could be a multi-BBS top scores list. Another example is a global one-liners wall where people from multiple BBSes could post a one-liner, and the one-liners could be hosted by a BBS and accessed from any other BBS.\\ | For instance, a BBS might provide game scores for a particular game; that particular door game could be installed on multiple BBSes and post their user scores to the host BBS, which would provide a central place to host scores for the door, so that there could be a multi-BBS top scores list. Another example is a global one-liners wall where people from multiple BBSes could post a one-liner, and the one-liners could be hosted by a BBS and accessed from any other BBS.\\ | ||
| \\ | \\ | ||
| - | Synchronet provides this via the script json-service.js (in the exec directory). | + | ===== Updating services.ini and json-service.ini ===== |
| + | Synchronet provides this via the script json-service.js (in the exec directory). | ||
| < | < | ||
| [JSON] | [JSON] | ||
| Line 12: | 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 21: | Line 22: | ||
| - | Reading from & writing to a JSON database | + | ===== Reading from & writing to a JSON database===== |
| - | ----------------------------------------- | + | In a JavaScript mod/ |
| - | Most basically, all that is really required to interact with a JSON database is writing (adding/ | + | 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 35: | Line 39: | ||
| } | } | ||
| } | } | ||
| - | A location of scores.player1.TestGame specifies the value of the TestGame property under player1 under scores in the JSON object. | + | A location of scores.player1.TestGame specifies the value of the TestGame property under player1 under scores in the JSON object.\\ |
| + | \\ | ||
| For the ' | For the ' | ||
| LOCK_READ = 1 | LOCK_READ = 1 | ||
| Line 45: | Line 49: | ||
| The subscribe function can be used to subscribe to get updates when data at a given JSON location changes: | The subscribe function can be used to subscribe to get updates when data at a given JSON location changes: | ||
| - | | + | |
| When you subscribe to a certain data location, it's important to specify a callback function. | When you subscribe to a certain data location, it's important to specify a callback function. | ||
| Line 53: | Line 57: | ||
| } | } | ||
| + | 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 ===== | |
| - | Special files: commands.js and service.js | + | 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 when you're writing an update. |
| - | ----------------------------------------- | + | \\ |
| + | 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. For instance: | ||
| + | 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.unlock(scope, | ||
| + | When you use those, then you would //not// specify the lock parameter for write(). | ||
| + | \\ | ||
| + | For instance: | ||
| + | var JSON_DB_LOCK_READ = 1; | ||
| + | var JSON_DB_LOCK_WRITE = 2; | ||
| + | var jsonClient = new JSONClient(" | ||
| + | jsonClient.lock(" | ||
| + | var scoresData = jsonClient.read(" | ||
| + | // Change scoresData ... | ||
| + | jsonClient.write(" | ||
| + | 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 | ||
| 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:\\ | ||
| * commands.js: | * commands.js: | ||