notherbase-fs 3.0.7 → 3.0.9

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.
@@ -85,7 +85,7 @@ export default class Creation {
85
85
  page = async (req, res, next) => {
86
86
  let main = `${req.contentPath}`;
87
87
 
88
- main += `/${req.params.page}/index.ejs`;
88
+ main += `/pages/${req.params.page}/index.ejs`;
89
89
 
90
90
  try {
91
91
  if (fs.existsSync(main)) {
@@ -47,24 +47,14 @@ export default class SpiritWorld {
47
47
  }
48
48
 
49
49
  load = async (req, res) => {
50
- let user = await req.db.User.recallOne(req.session.currentUser);
51
-
52
50
  let parent = null;
53
- if (req.query.scope === "local" && user) parent = user.id;
54
51
 
55
- let spirit = await req.db.Spirit.recallOne({
56
- route: req.query.route,
57
- service: req.query.service,
58
- scope: req.query.scope,
59
- parent: parent
60
- });
52
+ if (req.query.scope === "local") {
53
+ let user = await req.db.User.recallOne(req.session.currentUser);
54
+ parent = user.id;
55
+ }
61
56
 
62
- if (!spirit) spirit = await req.db.Spirit.create({
63
- route: req.query.route,
64
- service: req.query.service,
65
- scope: req.query.scope,
66
- parent: parent
67
- }, {});
57
+ let spirit = await req.db.Spirit.recallOne(req.query.service, parent);
68
58
 
69
59
  if (!spirit.memory.data) spirit.memory.data = {};
70
60
 
@@ -73,7 +63,7 @@ export default class SpiritWorld {
73
63
 
74
64
  serve = async (req, res) => {
75
65
  try {
76
- let scriptPath = `${req.contentPath}${req.body.route}/${req.body.data.script}.js`;
66
+ let scriptPath = `${req.contentPath}${req.body.data.route}/${req.body.data.script}.js`;
77
67
 
78
68
  let script, result = null;
79
69
 
package/models/item.js CHANGED
@@ -5,10 +5,8 @@ export default class Item extends Spirit {
5
5
  let spirit = new Item(name);
6
6
 
7
7
  spirit.memory = await Spirit.db.create({
8
- route: "/",
9
8
  service: "items",
10
9
  parent: null,
11
- scope: "global",
12
10
  _lastUpdate: Date.now(),
13
11
  data: {
14
12
  name,
@@ -24,10 +22,8 @@ export default class Item extends Spirit {
24
22
  let spirit = new Item();
25
23
 
26
24
  let found = await Spirit.db.find({
27
- route: "/",
28
25
  service: "items",
29
- parent: null,
30
- scope: "global"
26
+ parent: null
31
27
  });
32
28
 
33
29
  if (found) {
@@ -41,14 +37,7 @@ export default class Item extends Spirit {
41
37
  static recallOne = async (name) => {
42
38
  let spirit = new Item(name);
43
39
 
44
- let query = Spirit.buildQuery({
45
- route: "/",
46
- service: "items",
47
- parent: null,
48
- scope: "global"
49
- }, {
50
- name: name
51
- });
40
+ let query = Spirit.buildQuery("items", { name });
52
41
 
53
42
  let found = await Spirit.db.findOne(query);
54
43
 
@@ -61,14 +50,7 @@ export default class Item extends Spirit {
61
50
  }
62
51
 
63
52
  static delete = async (name) => {
64
- let found = await Spirit.db.findAndDelete(Spirit.buildQuery({
65
- route: "/",
66
- service: "items",
67
- parent: null,
68
- scope: "global"
69
- }, {
70
- name: name
71
- }));
53
+ let found = await Spirit.db.findAndDelete(Spirit.buildQuery("items", { name }));
72
54
 
73
55
  return found.deletedCount;
74
56
  }
package/models/spirit.js CHANGED
@@ -3,9 +3,7 @@ import mongoose from "mongoose";
3
3
  export default class Spirit {
4
4
  static db = mongoose.model('spirits', new mongoose.Schema({
5
5
  _lastUpdate: Number,
6
- route: String,
7
6
  service: String,
8
- scope: String,
9
7
  parent: {
10
8
  type: mongoose.Schema.Types.ObjectId,
11
9
  ref: "spirits",
@@ -14,16 +12,14 @@ export default class Spirit {
14
12
  data: {}
15
13
  }));
16
14
 
17
- static buildQuery = (options = {}, data = {}, id = null) => {
18
- let query = {};
19
-
20
- if (id) query = {
21
- _id: id
22
- };
23
- if (options) query = {
24
- ...query,
25
- ...options
15
+ static buildQuery = (service, data = null, parent = null, id = null) => {
16
+ let query = {
17
+ service: service,
18
+ parent: parent
26
19
  };
20
+
21
+ if (id) query._id = id;
22
+
27
23
  if (data){
28
24
  let keys = Object.keys(data);
29
25
  for (let i = 0; i < keys.length; i++) {
@@ -34,11 +30,12 @@ export default class Spirit {
34
30
  return query;
35
31
  }
36
32
 
37
- static create = async (options, data) => {
33
+ static create = async (service, data = {}, parent = null) => {
38
34
  let spirit = new Spirit();
39
35
 
40
36
  spirit.memory = await Spirit.db.create({
41
- ...options,
37
+ service,
38
+ parent,
42
39
  _lastUpdate: Date.now(),
43
40
  data: data
44
41
  });
@@ -46,54 +43,36 @@ export default class Spirit {
46
43
  return spirit;
47
44
  }
48
45
 
49
- static recall = async (options = {}, data = {}, id = null) => {
46
+ static recallAll = async (service) => {
50
47
  let spirit = new Spirit();
51
48
 
52
- let query = Spirit.buildQuery(options, data, id);
53
-
54
- let found = await Spirit.db.find(query);
49
+ let query = Spirit.buildQuery(service);
55
50
 
56
- if (found) {
57
- spirit.memory = found;
58
- if (!spirit.memory.data) spirit.memory.data = {};
59
-
60
- return spirit;
61
- }
62
- else return null;
63
- }
64
-
65
- static recallOne = async (options = {}, data = {}, id = null) => {
66
- let spirit = new Spirit();
67
-
68
- let query = Spirit.buildQuery(options, data, id);
69
-
70
- let found = await Spirit.db.findOne(query);
51
+ let found = await Spirit.db.find({
52
+ service: service
53
+ });
71
54
 
72
55
  if (found) {
73
56
  spirit.memory = found;
74
- if (!spirit.memory.data) spirit.memory.data = {};
75
57
 
76
58
  return spirit;
77
59
  }
78
60
  else return null;
79
61
  }
80
62
 
81
- static recallOrCreate = async (options = {}, queryData = {}, initdata = {}) => {
63
+ static recallOne = async (service, parent = null, data = {}, id = null) => {
82
64
  let spirit = new Spirit();
83
65
 
84
- let query = Spirit.buildQuery(options, queryData);
66
+ let query = Spirit.buildQuery(service, data, parent, id);
85
67
 
86
68
  let found = await Spirit.db.findOne(query);
87
69
 
88
70
  if (found) {
89
71
  spirit.memory = found;
90
- if (!spirit.memory.data) spirit.memory.data = {};
91
-
92
72
  return spirit;
93
73
  }
94
74
  else {
95
- options._lastUpdate = Date.now();
96
- let newSpirit = await Spirit.create(options, initdata);
75
+ let newSpirit = await Spirit.create(service, {}, parent);
97
76
  return newSpirit;
98
77
  }
99
78
  }
package/models/user.js CHANGED
@@ -3,34 +3,19 @@ import Item from "./item.js";
3
3
  import bcrypt from "bcrypt";
4
4
 
5
5
  export default class User extends Spirit {
6
- static recallOne = async (target = null, username = null, id = null) => {
7
- let spirit = new User(target, id);
6
+ static recallOne = async (email = null, username = null, id = null) => {
7
+ let spirit = new User(email, id);
8
8
 
9
9
  let query = null;
10
10
 
11
- if (target) {
12
- query = Spirit.buildQuery({
13
- route: "/",
14
- service: "user",
15
- scope: "global",
16
- parent: null
17
- }, { email: target });
11
+ if (email) {
12
+ query = Spirit.buildQuery("user", { email: email });
18
13
  }
19
14
  else if (username) {
20
- query = Spirit.buildQuery({
21
- route: "/",
22
- service: "user",
23
- scope: "global",
24
- parent: null
25
- }, { username: username });
15
+ query = Spirit.buildQuery("user", { username: username });
26
16
  }
27
17
  else if (id) {
28
- query = Spirit.buildQuery({
29
- route: "/",
30
- service: "user",
31
- scope: "global",
32
- parent: null
33
- }, null, id);
18
+ query = Spirit.buildQuery("user", null, null, id);
34
19
  }
35
20
 
36
21
  let found = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "3.0.7",
3
+ "version": "3.0.9",
4
4
  "description": "Functions to help make developing for NotherBase easier.",
5
5
  "exports": "./notherbase-fs.js",
6
6
  "scripts": {
@@ -5,9 +5,26 @@
5
5
  <button onclick="base.do('add-more-gold')">+3</button>
6
6
  <button onclick="base.do('subtract-gold')">-30</button>
7
7
  <button onclick="base.do('emailTime')">email</button>
8
+ <input type="text" id="test" placeholder="Loading...">
9
+ <button onclick="saveInput()">Save</button>
8
10
 
9
11
  <hr>
10
12
 
11
13
  <a href="/the-front">
12
14
  Go to The Front
13
- </a>
15
+ </a>
16
+
17
+ <script>
18
+ const $inp = $("input#test");
19
+
20
+ base.load("test-save3").then((res) => {
21
+ if (res.text) $inp.attr("placeholder", res.text);
22
+ else $inp.attr("placeholder", "No Input Saved");
23
+ })
24
+
25
+ const saveInput = function saveInput() {
26
+ let inp = $inp.val();
27
+
28
+ base.do("save-input", { text: inp });
29
+ }
30
+ </script>
@@ -0,0 +1,7 @@
1
+ export default async (req, user) => {
2
+ let spirit = await req.db.Spirit.recallOne("test-save3", user.id);
3
+
4
+ await spirit.commit({
5
+ text: req.body.data.text
6
+ });
7
+ }
@@ -288,23 +288,22 @@ class Base {
288
288
  return response;
289
289
  }
290
290
 
291
- do = async (what, data = null, route = window.location.pathname) => {
292
- let response = Base.commune("/s/serve", {
291
+ do = async (what, data = null) => {
292
+ console.log(window.location.pathname);
293
+ let response = await Base.commune("/s/serve", {
293
294
  script: what,
294
- ...data
295
- }, {
296
- route
295
+ ...data,
296
+ route: window.location.pathname
297
297
  });
298
298
 
299
299
  this.playerInventory.refresh();
300
300
  this.playerAttributes.refresh();
301
-
302
- await response;
301
+
303
302
  return response;
304
303
  }
305
304
 
306
305
  load = async (service, scope = "local") => {
307
- let response = await $.get("/s/load", { service, scope, route: window.location.pathname });
306
+ let response = await $.get("/s/load", { service, scope });
308
307
 
309
308
  return response;
310
309
  }
File without changes