brackets-memory-db 1.0.4 → 1.0.5

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.
package/dist/index.d.ts CHANGED
@@ -70,4 +70,20 @@ export declare class InMemoryDatabase implements CrudInterface {
70
70
  * @param filter An object to filter data.
71
71
  */
72
72
  delete<T>(table: Table, filter: Partial<T>): Promise<boolean>;
73
+ /**
74
+ * Find the index of a table entity by its id
75
+ *
76
+ * @param table
77
+ * @param id
78
+ * @returns
79
+ */
80
+ getEntityIndexById(table: Table, id: number): number;
81
+ /**
82
+ * Set a table entity value by its index
83
+ *
84
+ * @param table
85
+ * @param index
86
+ * @param value
87
+ */
88
+ setEntityByIndex<T>(table: Table, index: number, value: T): void;
73
89
  }
package/dist/index.js CHANGED
@@ -50,6 +50,7 @@ class InMemoryDatabase {
50
50
  */
51
51
  insert(table, values) {
52
52
  let id = this.data[table].length > 0
53
+ // @ts-ignore
53
54
  ? (Math.max(...this.data[table].map(d => d.id)) + 1)
54
55
  : 0;
55
56
  if (!Array.isArray(values)) {
@@ -120,8 +121,8 @@ class InMemoryDatabase {
120
121
  update(table, arg, value) {
121
122
  if (typeof arg === 'number') {
122
123
  try {
123
- // @ts-ignore
124
- this.data[table][arg] = value;
124
+ const index = this.getEntityIndexById(table, arg);
125
+ this.setEntityByIndex(table, index, value);
125
126
  return new Promise((resolve) => {
126
127
  resolve(true);
127
128
  });
@@ -140,7 +141,8 @@ class InMemoryDatabase {
140
141
  });
141
142
  }
142
143
  values.forEach((v) => {
143
- const existing = this.data[table][v.id];
144
+ const index = this.getEntityIndexById(table, v.id);
145
+ const existing = this.data[table][index];
144
146
  for (const key in value) {
145
147
  // @ts-ignore
146
148
  if (existing[key] && typeof existing[key] === 'object' && typeof value[key] === 'object') {
@@ -152,7 +154,7 @@ class InMemoryDatabase {
152
154
  existing[key] = value[key]; // Otherwise, do a simple value assignment.
153
155
  }
154
156
  }
155
- this.data[table][v.id] = existing;
157
+ this.setEntityByIndex(table, index, existing);
156
158
  });
157
159
  return new Promise((resolve) => {
158
160
  resolve(true);
@@ -185,5 +187,30 @@ class InMemoryDatabase {
185
187
  resolve(true);
186
188
  });
187
189
  }
190
+ /**
191
+ * Find the index of a table entity by its id
192
+ *
193
+ * @param table
194
+ * @param id
195
+ * @returns
196
+ */
197
+ getEntityIndexById(table, id) {
198
+ const index = this.data[table].findIndex(e => e.id === id);
199
+ if (index === -1) {
200
+ throw new Error(`Entity in ${table} with id ${id} not found.`);
201
+ }
202
+ return index;
203
+ }
204
+ /**
205
+ * Set a table entity value by its index
206
+ *
207
+ * @param table
208
+ * @param index
209
+ * @param value
210
+ */
211
+ setEntityByIndex(table, index, value) {
212
+ // @ts-ignore
213
+ this.data[table][index] = value;
214
+ }
188
215
  }
189
216
  exports.InMemoryDatabase = InMemoryDatabase;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brackets-memory-db",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "An in-memory database for brackets-manager.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -40,4 +40,4 @@
40
40
  "dependencies": {
41
41
  "rfdc": "^1.3.0"
42
42
  }
43
- }
43
+ }