notherbase-fs 3.2.21 → 3.3.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.
@@ -19,7 +19,7 @@ export default class SpiritWorld {
19
19
  this.router = express.Router();
20
20
  this.user = new User();
21
21
 
22
- this.router.post("/load", this.load);
22
+ this.router.post("/loadAll", this.loadAll);
23
23
  this.router.post("/serve", this.serve);
24
24
  this.router.use("/user", this.user.router);
25
25
 
@@ -85,14 +85,18 @@ export default class SpiritWorld {
85
85
  }
86
86
 
87
87
  /**
88
- * This API route requests a spirit from the database.
89
- * @param {Object} req
90
- * @param {Object} res
88
+ * This API route requests all spirits of a kind from the database.
89
+ * @param {Object} req
90
+ * @param {Object} res
91
+ * @returns {Object} The requested spirits.
91
92
  */
92
- load = async (req, res) => {
93
+ loadAll = async (req, res) => {
93
94
  try {
94
95
  let parent = null;
96
+ let data = req.body.data ? req.body.data : {};
97
+ let id = req.body.id ? req.body.id : null;
95
98
 
99
+ // if the scope is local, the parent is the user's id
96
100
  if (req.body.scope === "local") {
97
101
  let user = await req.db.User.recallOne(req.session.currentUser);
98
102
  if (user?.id) parent = user.id;
@@ -102,9 +106,22 @@ export default class SpiritWorld {
102
106
  }
103
107
  }
104
108
 
105
- let spirit = await req.db.Spirit.recallOne(req.body.service, parent);
109
+ // recall all spirits with the given service name and parent
110
+ let spirit = await req.db.Spirit.recallAll(req.body.service, parent, data, id);
106
111
 
107
- res.send(spirit.memory.data ? spirit.memory.data : {});
112
+ // if the spirit is not an array, it's a single spirit
113
+ if (!Array.isArray(spirit.memory)) {
114
+ let togo = spirit.memory;
115
+ res.send(togo);
116
+ }
117
+ // if the spirit is an array, it's multiple spirits
118
+ else {
119
+ let togo = [];
120
+ for (let i = 0; i < spirit.memory.length; i++) {
121
+ togo.push(spirit.memory[i]);
122
+ }
123
+ res.send(togo);
124
+ }
108
125
  } catch (error) {
109
126
  console.log(error);
110
127
  fail(res, "Server error");
@@ -124,6 +124,14 @@ export default class User {
124
124
  }
125
125
  }
126
126
 
127
+ /**
128
+ * Checks if a user is logged in.
129
+ */
130
+ loggedIn = () => {
131
+ if (this.id) return true;
132
+ else return false;
133
+ }
134
+
127
135
  /**
128
136
  * Changes a user's email address on file.
129
137
  * @param {Object} req An Express.js request.
package/models/spirit.js CHANGED
@@ -66,14 +66,32 @@ export default class Spirit {
66
66
  * @param {String} service The name of the spirit.
67
67
  * @returns All spirits of the given name.
68
68
  */
69
- static recallAll = async (service, extraQuery) => {
69
+ static recallAll = async (service, parent = null, data = {}, id = null) => {
70
70
  let spirit = new Spirit();
71
71
 
72
- let found = await Spirit.db.find({ service: service, ...extraQuery });
72
+ let query = Spirit.buildQuery(service, data, parent, id);
73
+
74
+ let found = await Spirit.db.find(query);
75
+
76
+ if (found) {
77
+ spirit.memory = found;
78
+ return spirit;
79
+ }
80
+ else return null;
81
+ }
82
+
83
+ /**
84
+ * Recalls any spirit from the database.
85
+ * @param {String} service The name of the spirit.
86
+ * @returns Any spirit found.
87
+ */
88
+ static recallAny = async (service) => {
89
+ let spirit = new Spirit();
90
+
91
+ let found = await Spirit.db.findOne({ service: service });
73
92
 
74
93
  if (found) {
75
94
  spirit.memory = found;
76
-
77
95
  return spirit;
78
96
  }
79
97
  else return null;
@@ -147,4 +165,36 @@ export default class Spirit {
147
165
 
148
166
  return "Updated";
149
167
  }
168
+
169
+ /**
170
+ * Normalizes the data property to an array.
171
+ */
172
+ normalizeDataToArray = () => {
173
+ if (!Array.isArray(this.memory.data)) this.memory.data = [];
174
+ }
175
+
176
+ /**
177
+ * Adds a new backup to the spirit's data.
178
+ * If backups have not already been enabled, the old dat will be moved to a backup.
179
+ * @param {Object} data Data to add to the backup.
180
+ */
181
+ addBackup = async (data, max = 5) => {
182
+ if (!this.memory.data._backupsEnabled) {
183
+ let oldData = this.memory.data;
184
+ this.memory.data = {
185
+ _backupsEnabled: true,
186
+ backups: [ oldData ]
187
+ };
188
+ }
189
+ else {
190
+ this.memory.data.backups.unshift({
191
+ _lastUpdate: Date.now(),
192
+ data: data
193
+ });
194
+
195
+ if (max > 1) {
196
+ while (this.memory.data.backups.length > max) this.memory.data.backups.pop();
197
+ }
198
+ }
199
+ }
150
200
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "3.2.21",
3
+ "version": "3.3.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
@@ -161,13 +161,13 @@ class Base {
161
161
  }
162
162
 
163
163
  /**
164
- * Loads a certain spirit.
165
- * @param {String} service The name of the spirit to load.
164
+ * Loads all spirits of a certain service.
165
+ * @param {String} service The name of the spirits to load.
166
166
  * @param {String} scope Defaults to local, else global.
167
167
  * @returns Spirit world response.
168
168
  */
169
- load = async (service, scope = "local") => {
170
- let response = await $.post("/s/load", JSON.stringify({ service, scope }));
169
+ loadAll = async (service, scope = "local", data = {}, id = null) => {
170
+ let response = await $.post("/s/loadAll", JSON.stringify({ service, scope, data, id }));
171
171
 
172
172
  return response;
173
173
  }