notherbase-fs 3.4.1 → 3.4.3

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.
@@ -20,6 +20,7 @@ export default class SpiritWorld {
20
20
  this.user = new User();
21
21
 
22
22
  this.router.post("/loadAll", this.loadAll);
23
+ this.router.post("/load", this.load);
23
24
  this.router.post("/serve", this.serve);
24
25
  this.router.use("/user", this.user.router);
25
26
 
@@ -99,7 +100,7 @@ export default class SpiritWorld {
99
100
  // if the scope is local, the parent is the user's id
100
101
  if (req.body.scope === "local") {
101
102
  let user = await req.db.Spirit.recallOne("user", null, { email: req.session.currentUser });
102
- if (user?.id) parent = user.id;
103
+ if (user?.memory?._id) parent = user.memory._id;
103
104
  else {
104
105
  fail(res, "User had no id on load()");
105
106
  return;
@@ -116,6 +117,38 @@ export default class SpiritWorld {
116
117
  }
117
118
  }
118
119
 
120
+ /**
121
+ * This API route requests a spirit of a kind from the database.
122
+ * @param {Object} req
123
+ * @param {Object} res
124
+ * @returns {Object} The requested spirit.
125
+ */
126
+ load = async (req, res) => {
127
+ try {
128
+ let parent = null;
129
+ let data = req.body.data ? req.body.data : {};
130
+ let id = req.body.id ? req.body.id : null;
131
+
132
+ // if the scope is local, the parent is the user's id
133
+ if (req.body.scope === "local") {
134
+ let user = await req.db.Spirit.recallOne("user", null, { email: req.session.currentUser });
135
+ if (user?.memory?._id) parent = user.memory._id;
136
+ else {
137
+ fail(res, "User had no id on load()");
138
+ return;
139
+ }
140
+ }
141
+
142
+ // recall all spirits with the given service name and parent
143
+ let spirit = await req.db.Spirit.recallOne(req.body.service, parent, data, id);
144
+
145
+ res.send(spirit);
146
+ } catch (error) {
147
+ console.log(error);
148
+ fail(res, "Server error");
149
+ }
150
+ }
151
+
119
152
  /**
120
153
  * This API route runs a script on the server. Responds with the result.
121
154
  * @param {Object} req
package/models/spirit.js CHANGED
@@ -14,10 +14,7 @@ export default class Spirit {
14
14
  required: false
15
15
  },
16
16
  data: {},
17
- backups: [{
18
- _lastUpdate: Number,
19
- data: {}
20
- }]
17
+ backups: []
21
18
  }));
22
19
 
23
20
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "3.4.1",
3
+ "version": "3.4.3",
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
@@ -114,6 +114,8 @@ class Base {
114
114
  * Loads all spirits of a certain service.
115
115
  * @param {String} service The name of the spirits to load.
116
116
  * @param {String} scope Defaults to local, else global.
117
+ * @param {Object} data Data to filter the spirits by.
118
+ * @param {ObjectID} id The id of the spirit to load.
117
119
  * @returns Spirit world response.
118
120
  */
119
121
  loadAll = async (service, scope = "local", data = {}, id = null) => {
@@ -122,6 +124,20 @@ class Base {
122
124
  return response;
123
125
  }
124
126
 
127
+ /**
128
+ * Loads a single spirit of a certain service.
129
+ * @param {String} service The name of the spirit to load.
130
+ * @param {String} scope Defaults to local, else global.
131
+ * @param {Object} data Data to filter the spirits by.
132
+ * @param {ObjectID} id The id of the spirit to load.
133
+ * @returns Spirit world response.
134
+ */
135
+ load = async (service, scope = "local", data = {}, id = null) => {
136
+ let response = await $.post("/s/load", JSON.stringify({ service, scope, data, id }));
137
+
138
+ return response;
139
+ }
140
+
125
141
  /**
126
142
  * Creates the toggle view button.
127
143
  */
@@ -2,7 +2,13 @@ export default async (req, user, io) => {
2
2
  // let deleted = await req.db.Spirit.delete("gold");
3
3
  let spirit = await req.db.Spirit.recallOrCreateOne("gold");
4
4
  spirit.addBackup({
5
- amount: spirit.memory?.data?.amount != null ? spirit.memory.data.amount + 1 : 0
5
+ amount: spirit.memory?.data?.amount != null ? spirit.memory.data.amount + 1 : 1
6
+ });
7
+ await spirit.commit();
8
+
9
+ spirit = await req.db.Spirit.recallOrCreateOne("gold", user.memory._id);
10
+ spirit.addBackup({
11
+ amount: spirit.memory?.data?.amount != null ? spirit.memory.data.amount + 1 : 1
6
12
  });
7
13
  await spirit.commit();
8
14
  }
@@ -57,9 +57,11 @@
57
57
  this.$info.append(this.newEmail + "<br>");
58
58
  this.$info.append(this.newPassword + "<br>");
59
59
 
60
- let loaded = await base.loadAll("gold", "global");
60
+ let loaded = await base.load("gold", "global");
61
+ console.log(loaded);
62
+ this.$gold.text(`?/${loaded?.memory?.data?.amount !== null ? loaded.memory.data.amount : 0} Gold Added`);
63
+ loaded = await base.loadAll("gold");
61
64
  console.log(loaded);
62
- this.$gold.text(`?/${loaded[0]?.memory?.data?.amount !== null ? loaded[0].memory.data.amount : 0} Gold Added`);
63
65
  }
64
66
 
65
67
  runTest = async () => {