notherbase-fs 4.3.5 → 4.4.0

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.
@@ -3,6 +3,7 @@ import { stripHtml } from "string-strip-html";
3
3
  import { success, fail } from "./util.js";
4
4
  import User from "./user.js";
5
5
  import fs from 'fs';
6
+ import { log } from "console";
6
7
 
7
8
  /**
8
9
  * The spirit world is the API of a base.
@@ -21,6 +22,7 @@ export default class SpiritWorld {
21
22
  this.router = express.Router();
22
23
  this.router.post("/loadAll", this.loadAll);
23
24
  this.router.post("/load", this.load);
25
+ this.router.post("/save", this.save);
24
26
  this.router.post("/serve", this.serve);
25
27
  this.router.use("/user", this.user.router);
26
28
  }
@@ -125,7 +127,6 @@ export default class SpiritWorld {
125
127
  * This API route requests a spirit of a kind from the database.
126
128
  * @param {Object} req
127
129
  * @param {Object} res
128
- * @returns {Object} The requested spirit.
129
130
  */
130
131
  load = async (req, res) => {
131
132
  try {
@@ -145,6 +146,7 @@ export default class SpiritWorld {
145
146
 
146
147
  // recall all spirits with the given service name and parent
147
148
  let spirit = await req.db.Spirit.recallOne(req.body.service, parent, data, id);
149
+ console.log("loaded spirit ", spirit.memory);
148
150
 
149
151
  res.send(spirit);
150
152
  } catch (error) {
@@ -153,6 +155,48 @@ export default class SpiritWorld {
153
155
  }
154
156
  }
155
157
 
158
+ /**
159
+ * This API route saves a spirit to the database.
160
+ * @param {Object} req
161
+ * @param {Object} res
162
+ */
163
+ save = async (req, res) => {
164
+ console.log("saving spirit ");
165
+ try {
166
+ let parent = null;
167
+ let spiritData = req.body.data ? req.body.data : {};
168
+ let id = req.body.id ? req.body.id : null;
169
+
170
+ // if the scope is local, the parent is the user's id
171
+ if (req.body.scope === "local") {
172
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
173
+ if (user?.memory?._id) parent = user.memory._id;
174
+ else {
175
+ fail(res, "User had no id on load()");
176
+ return;
177
+ }
178
+ }
179
+
180
+ // save the spirit with the given service name and parent
181
+ let spirit = await req.db.Spirit.recallOne(req.body.service, parent, {}, id);
182
+
183
+ if (spirit) {
184
+ console.log("saving spirit ", spirit.memory.data, " with new data ", spiritData);
185
+ // update existing spirit
186
+ await spirit.commit({ ...spirit.memory.data, ...spiritData });
187
+ }
188
+ else {
189
+ // create new spirit
190
+ spirit = await req.db.Spirit.create(req.body.service, spiritData, parent);
191
+ }
192
+
193
+ res.send("spirit saved");
194
+ } catch (error) {
195
+ console.log(error);
196
+ fail(res, "Server error");
197
+ }
198
+ }
199
+
156
200
  /**
157
201
  * This API route runs a script on the server. Responds with the result.
158
202
  * @param {Object} req
@@ -17,8 +17,6 @@ export default class User {
17
17
  this.router.post("/login", this.login);
18
18
  this.router.post("/deletePermanently", this.deletePermanently);
19
19
  this.router.post("/getInfo", this.getInfo);
20
- this.router.post("/getView", this.getView);
21
- this.router.post("/setView", this.setView);
22
20
  this.router.post("/downloadData", this.downloadData);
23
21
  this.router.post("/deleteAlldata", this.deleteAlldata);
24
22
  this.router.post("/importData", this.importData);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "4.3.5",
3
+ "version": "4.4.0",
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 () => {