brackets-memory-db 1.0.3 → 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 +16 -0
- package/dist/index.js +36 -8
- package/package.json +2 -2
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
|
@@ -49,8 +49,10 @@ class InMemoryDatabase {
|
|
|
49
49
|
* @param values What to insert.
|
|
50
50
|
*/
|
|
51
51
|
insert(table, values) {
|
|
52
|
-
let id
|
|
53
|
-
|
|
52
|
+
let id = this.data[table].length > 0
|
|
53
|
+
// @ts-ignore
|
|
54
|
+
? (Math.max(...this.data[table].map(d => d.id)) + 1)
|
|
55
|
+
: 0;
|
|
54
56
|
if (!Array.isArray(values)) {
|
|
55
57
|
try {
|
|
56
58
|
// @ts-ignore
|
|
@@ -95,12 +97,12 @@ class InMemoryDatabase {
|
|
|
95
97
|
if (typeof arg === 'number') {
|
|
96
98
|
return new Promise((resolve) => {
|
|
97
99
|
// @ts-ignore
|
|
98
|
-
resolve(clone(this.data[table]
|
|
100
|
+
resolve(clone(this.data[table].find(d => d.id === arg)));
|
|
99
101
|
});
|
|
100
102
|
}
|
|
101
103
|
return new Promise((resolve) => {
|
|
102
104
|
// @ts-ignore
|
|
103
|
-
resolve(this.data[table].filter(this.makeFilter(arg))
|
|
105
|
+
resolve(this.data[table].filter(this.makeFilter(arg)).map(clone));
|
|
104
106
|
});
|
|
105
107
|
}
|
|
106
108
|
catch (error) {
|
|
@@ -119,8 +121,8 @@ class InMemoryDatabase {
|
|
|
119
121
|
update(table, arg, value) {
|
|
120
122
|
if (typeof arg === 'number') {
|
|
121
123
|
try {
|
|
122
|
-
|
|
123
|
-
this.
|
|
124
|
+
const index = this.getEntityIndexById(table, arg);
|
|
125
|
+
this.setEntityByIndex(table, index, value);
|
|
124
126
|
return new Promise((resolve) => {
|
|
125
127
|
resolve(true);
|
|
126
128
|
});
|
|
@@ -139,7 +141,8 @@ class InMemoryDatabase {
|
|
|
139
141
|
});
|
|
140
142
|
}
|
|
141
143
|
values.forEach((v) => {
|
|
142
|
-
const
|
|
144
|
+
const index = this.getEntityIndexById(table, v.id);
|
|
145
|
+
const existing = this.data[table][index];
|
|
143
146
|
for (const key in value) {
|
|
144
147
|
// @ts-ignore
|
|
145
148
|
if (existing[key] && typeof existing[key] === 'object' && typeof value[key] === 'object') {
|
|
@@ -151,7 +154,7 @@ class InMemoryDatabase {
|
|
|
151
154
|
existing[key] = value[key]; // Otherwise, do a simple value assignment.
|
|
152
155
|
}
|
|
153
156
|
}
|
|
154
|
-
this.
|
|
157
|
+
this.setEntityByIndex(table, index, existing);
|
|
155
158
|
});
|
|
156
159
|
return new Promise((resolve) => {
|
|
157
160
|
resolve(true);
|
|
@@ -184,5 +187,30 @@ class InMemoryDatabase {
|
|
|
184
187
|
resolve(true);
|
|
185
188
|
});
|
|
186
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
|
+
}
|
|
187
215
|
}
|
|
188
216
|
exports.InMemoryDatabase = InMemoryDatabase;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brackets-memory-db",
|
|
3
|
-
"version": "1.0.
|
|
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
|
+
}
|