notherbase-fs 4.3.6 → 4.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,6 +21,7 @@ export default class SpiritWorld {
21
21
  this.router = express.Router();
22
22
  this.router.post("/loadAll", this.loadAll);
23
23
  this.router.post("/load", this.load);
24
+ this.router.post("/save", this.save);
24
25
  this.router.post("/serve", this.serve);
25
26
  this.router.use("/user", this.user.router);
26
27
  }
@@ -125,7 +126,6 @@ export default class SpiritWorld {
125
126
  * This API route requests a spirit of a kind from the database.
126
127
  * @param {Object} req
127
128
  * @param {Object} res
128
- * @returns {Object} The requested spirit.
129
129
  */
130
130
  load = async (req, res) => {
131
131
  try {
@@ -153,6 +153,46 @@ export default class SpiritWorld {
153
153
  }
154
154
  }
155
155
 
156
+ /**
157
+ * This API route saves a spirit to the database.
158
+ * @param {Object} req
159
+ * @param {Object} res
160
+ */
161
+ save = async (req, res) => {
162
+ try {
163
+ let parent = null;
164
+ let spiritData = req.body.data ? req.body.data : {};
165
+ let id = req.body.id ? req.body.id : null;
166
+
167
+ // if the scope is local, the parent is the user's id
168
+ if (req.body.scope === "local") {
169
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
170
+ if (user?.memory?._id) parent = user.memory._id;
171
+ else {
172
+ fail(res, "User had no id on load()");
173
+ return;
174
+ }
175
+ }
176
+
177
+ // save the spirit with the given service name and parent
178
+ let spirit = await req.db.Spirit.recallOne(req.body.service, parent, {}, id);
179
+
180
+ if (spirit) {
181
+ // update existing spirit
182
+ await spirit.commit({ ...spirit.memory.data, ...spiritData });
183
+ }
184
+ else {
185
+ // create new spirit
186
+ spirit = await req.db.Spirit.create(req.body.service, spiritData, parent);
187
+ }
188
+
189
+ res.send("spirit saved");
190
+ } catch (error) {
191
+ console.log(error);
192
+ fail(res, "Server error");
193
+ }
194
+ }
195
+
156
196
  /**
157
197
  * This API route runs a script on the server. Responds with the result.
158
198
  * @param {Object} req
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "4.3.6",
3
+ "version": "4.4.1",
4
4
  "description": "Functions to help make developing for NotherBase easier.",
5
5
  "exports": "./notherbase-fs.js",
6
6
  "scripts": {
package/public/js/base.js CHANGED
@@ -128,6 +128,19 @@ class Base {
128
128
  return response;
129
129
  }
130
130
 
131
+ /**
132
+ * Saves a spirit of a certain service.
133
+ * @param {String} service The name of the spirit to save.
134
+ * @param {String} scope Defaults to local, else global.
135
+ * @param {Object} data Data to save the spirit with.
136
+ * @param {ObjectID} id The id of the spirit to save.
137
+ * @returns Spirit world response.
138
+ */
139
+ save = async (service, scope = "local", data = {}, id = null) => {
140
+ let response = await $.post("/s/save", JSON.stringify({ service, scope, data, id }));
141
+ return response;
142
+ }
143
+
131
144
  /**
132
145
  * Appends html to the head.
133
146
  * @param {String} html The html to append.
@@ -15,7 +15,7 @@
15
15
 
16
16
  <input type="number" id="token">
17
17
  <button onclick="test.continueTest()">Continue Test</button>
18
- <button onclick="base.do('add-gold', { route: '/the-front' })">add gold</button>
18
+ <button onclick="myGold = myGold + 1; base.save('front-gold', 'local', { amount: myGold })">add gold</button>
19
19
  <!-- download you data -->
20
20
  <button onclick="downloadData()">Download Data</button>
21
21
  <!-- delete all your data -->
@@ -40,6 +40,8 @@
40
40
  </a>
41
41
 
42
42
  <script>
43
+ let myGold = 0;
44
+
43
45
  class Test {
44
46
  constructor() {
45
47
  this.$info = $("main #info");
@@ -65,11 +67,10 @@
65
67
  this.$info.append(this.newUsername + "<br>");
66
68
  this.$info.append(this.newPassword + "<br>");
67
69
 
68
- let loaded = await base.load("gold", "global");
69
- console.log("global gold ", loaded);
70
- this.$gold.text(`?/${loaded?.memory?.data?.amount !== null ? loaded.memory.data.amount : 0} Gold Added`);
71
- loaded = await base.loadAll("gold");
72
- console.log("local gold ", loaded);
70
+ let loaded = await base.load("front-gold");
71
+ console.log("gold ", loaded);
72
+ myGold = loaded?.memory?.data?.amount || 0;
73
+ this.$gold.text(`${myGold} Gold Added`);
73
74
  }
74
75
 
75
76
  runTest = async () => {