Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
service:jsondb [2022/11/28 22:18] – Fixed links to other pages on the wiki Nightfox | service:jsondb [2022/12/08 14:01] (current) – [Best Practices] Nightfox | ||
---|---|---|---|
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:\\ |