brackets-memory-db 0.1.0 → 1.0.2

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.
Files changed (3) hide show
  1. package/README.md +9 -0
  2. package/dist/index.js +13 -14
  3. package/package.json +4 -1
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # brackets-memory-db
2
+
3
+ This implementation of the [`CrudInterface`](https://drarig29.github.io/brackets-docs/reference/manager/interfaces/CrudInterface.html) allows you to store the database directly in memory.
4
+
5
+ This is a good choice if you want to have both the [manager](https://github.com/Drarig29/brackets-manager.js) and the [viewer](https://github.com/Drarig29/brackets-viewer.js) to run in the browser.
6
+
7
+ You can also use it to do all the logic in memory and persist the new state at the end of an update, for example.
8
+
9
+ It is used in the documentation here: https://drarig29.github.io/brackets-docs/getting-started/#using-the-viewer
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InMemoryDatabase = void 0;
4
+ const rfdc = require("rfdc");
5
+ const clone = rfdc();
4
6
  class InMemoryDatabase {
5
7
  data = {
6
8
  participant: [],
@@ -51,7 +53,6 @@ class InMemoryDatabase {
51
53
  id = this.data[table].length;
52
54
  if (!Array.isArray(values)) {
53
55
  try {
54
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
55
56
  // @ts-ignore
56
57
  this.data[table].push({ id, ...values });
57
58
  }
@@ -66,7 +67,6 @@ class InMemoryDatabase {
66
67
  }
67
68
  try {
68
69
  values.map((object) => {
69
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
70
70
  // @ts-ignore
71
71
  this.data[table].push({ id: id++, ...object });
72
72
  });
@@ -88,22 +88,19 @@ class InMemoryDatabase {
88
88
  try {
89
89
  if (arg === undefined) {
90
90
  return new Promise((resolve) => {
91
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
92
91
  // @ts-ignore
93
- resolve(this.data[table]);
92
+ resolve(this.data[table].map(clone));
94
93
  });
95
94
  }
96
95
  if (typeof arg === 'number') {
97
96
  return new Promise((resolve) => {
98
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
99
97
  // @ts-ignore
100
- resolve(this.data[table][arg]);
98
+ resolve(clone(this.data[table][arg]));
101
99
  });
102
100
  }
103
101
  return new Promise((resolve) => {
104
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
105
102
  // @ts-ignore
106
- resolve(this.data[table].filter(this.makeFilter(arg)) || null);
103
+ resolve(this.data[table].filter(this.makeFilter(arg))).map(clone);
107
104
  });
108
105
  }
109
106
  catch (error) {
@@ -122,7 +119,6 @@ class InMemoryDatabase {
122
119
  update(table, arg, value) {
123
120
  if (typeof arg === 'number') {
124
121
  try {
125
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
126
122
  // @ts-ignore
127
123
  this.data[table][arg] = value;
128
124
  return new Promise((resolve) => {
@@ -135,7 +131,6 @@ class InMemoryDatabase {
135
131
  });
136
132
  }
137
133
  }
138
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
139
134
  // @ts-ignore
140
135
  const values = this.data[table].filter(this.makeFilter(arg));
141
136
  if (!values) {
@@ -146,9 +141,14 @@ class InMemoryDatabase {
146
141
  values.forEach((v) => {
147
142
  const existing = this.data[table][v.id];
148
143
  for (const key in value) {
149
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
150
- // @ts-ignore
151
- existing[key] = value[key];
144
+ if (typeof value[key] === 'object') {
145
+ // @ts-ignore
146
+ Object.assign(existing[key], value[key]); // For opponent objects, this does a deep merge of level 2.
147
+ }
148
+ else {
149
+ // @ts-ignore
150
+ existing[key] = value[key]; // Otherwise, do a simple value assignment.
151
+ }
152
152
  }
153
153
  this.data[table][v.id] = existing;
154
154
  });
@@ -177,7 +177,6 @@ class InMemoryDatabase {
177
177
  }
178
178
  const predicate = this.makeFilter(filter);
179
179
  const negativeFilter = (value) => !predicate(value);
180
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
181
180
  // @ts-ignore
182
181
  this.data[table] = values.filter(negativeFilter);
183
182
  return new Promise((resolve) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brackets-memory-db",
3
- "version": "0.1.0",
3
+ "version": "1.0.2",
4
4
  "description": "An in-memory database for brackets-manager.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -36,5 +36,8 @@
36
36
  },
37
37
  "peerDependencies": {
38
38
  "brackets-manager": "^1.3.9"
39
+ },
40
+ "dependencies": {
41
+ "rfdc": "^1.3.0"
39
42
  }
40
43
  }