brackets-memory-db 1.0.5 → 1.0.6

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
@@ -1,4 +1,5 @@
1
- import { CrudInterface, OmitId, Table, Database } from 'brackets-manager';
1
+ import { CrudInterface, OmitId, Table, Database } from 'brackets-model';
2
+ type Filter = Record<string, any>;
2
3
  export declare class InMemoryDatabase implements CrudInterface {
3
4
  protected data: Database;
4
5
  /**
@@ -8,7 +9,7 @@ export declare class InMemoryDatabase implements CrudInterface {
8
9
  /**
9
10
  * @param partial Filter
10
11
  */
11
- makeFilter(partial: any): (entry: any) => boolean;
12
+ makeFilter(partial: Filter): (entry: Filter) => boolean;
12
13
  /**
13
14
  * Clearing all of the data
14
15
  */
@@ -86,4 +87,8 @@ export declare class InMemoryDatabase implements CrudInterface {
86
87
  * @param value
87
88
  */
88
89
  setEntityByIndex<T>(table: Table, index: number, value: T): void;
90
+ private getTable;
91
+ private setTable;
92
+ private isObject;
89
93
  }
94
+ export {};
package/dist/index.js CHANGED
@@ -49,14 +49,13 @@ class InMemoryDatabase {
49
49
  * @param values What to insert.
50
50
  */
51
51
  insert(table, values) {
52
- let id = this.data[table].length > 0
53
- // @ts-ignore
54
- ? (Math.max(...this.data[table].map(d => d.id)) + 1)
52
+ const rows = this.getTable(table);
53
+ let id = rows.length > 0
54
+ ? (Math.max(...rows.map(d => d.id)) + 1)
55
55
  : 0;
56
56
  if (!Array.isArray(values)) {
57
57
  try {
58
- // @ts-ignore
59
- this.data[table].push({ id, ...values });
58
+ rows.push({ id, ...values });
60
59
  }
61
60
  catch (error) {
62
61
  return new Promise((resolve) => {
@@ -69,8 +68,7 @@ class InMemoryDatabase {
69
68
  }
70
69
  try {
71
70
  values.map((object) => {
72
- // @ts-ignore
73
- this.data[table].push({ id: id++, ...object });
71
+ rows.push({ id: id++, ...object });
74
72
  });
75
73
  }
76
74
  catch (error) {
@@ -90,19 +88,17 @@ class InMemoryDatabase {
90
88
  try {
91
89
  if (arg === undefined) {
92
90
  return new Promise((resolve) => {
93
- // @ts-ignore
94
- resolve(this.data[table].map(clone));
91
+ resolve(this.getTable(table).map(clone));
95
92
  });
96
93
  }
97
94
  if (typeof arg === 'number') {
98
95
  return new Promise((resolve) => {
99
- // @ts-ignore
100
- resolve(clone(this.data[table].find(d => d.id === arg)));
96
+ const found = this.getTable(table).find(d => d.id === arg);
97
+ resolve(found ? clone(found) : null);
101
98
  });
102
99
  }
103
100
  return new Promise((resolve) => {
104
- // @ts-ignore
105
- resolve(this.data[table].filter(this.makeFilter(arg)).map(clone));
101
+ resolve(this.getTable(table).filter(this.makeFilter(arg)).map(clone));
106
102
  });
107
103
  }
108
104
  catch (error) {
@@ -133,8 +129,7 @@ class InMemoryDatabase {
133
129
  });
134
130
  }
135
131
  }
136
- // @ts-ignore
137
- const values = this.data[table].filter(this.makeFilter(arg));
132
+ const values = this.getTable(table).filter(this.makeFilter(arg));
138
133
  if (!values) {
139
134
  return new Promise((resolve) => {
140
135
  resolve(false);
@@ -142,16 +137,14 @@ class InMemoryDatabase {
142
137
  }
143
138
  values.forEach((v) => {
144
139
  const index = this.getEntityIndexById(table, v.id);
145
- const existing = this.data[table][index];
146
- for (const key in value) {
147
- // @ts-ignore
148
- if (existing[key] && typeof existing[key] === 'object' && typeof value[key] === 'object') {
149
- // @ts-ignore
150
- Object.assign(existing[key], value[key]); // For opponent objects, this does a deep merge of level 2.
140
+ const existing = this.getTable(table)[index];
141
+ const updateValue = value;
142
+ for (const key in updateValue) {
143
+ if (this.isObject(existing[key]) && this.isObject(updateValue[key])) {
144
+ Object.assign(existing[key], updateValue[key]); // For opponent objects, this does a deep merge of level 2.
151
145
  }
152
146
  else {
153
- // @ts-ignore
154
- existing[key] = value[key]; // Otherwise, do a simple value assignment.
147
+ existing[key] = updateValue[key]; // Otherwise, do a simple value assignment.
155
148
  }
156
149
  }
157
150
  this.setEntityByIndex(table, index, existing);
@@ -181,8 +174,7 @@ class InMemoryDatabase {
181
174
  }
182
175
  const predicate = this.makeFilter(filter);
183
176
  const negativeFilter = (value) => !predicate(value);
184
- // @ts-ignore
185
- this.data[table] = values.filter(negativeFilter);
177
+ this.setTable(table, this.getTable(table).filter(negativeFilter));
186
178
  return new Promise((resolve) => {
187
179
  resolve(true);
188
180
  });
@@ -195,7 +187,7 @@ class InMemoryDatabase {
195
187
  * @returns
196
188
  */
197
189
  getEntityIndexById(table, id) {
198
- const index = this.data[table].findIndex(e => e.id === id);
190
+ const index = this.getTable(table).findIndex(e => e.id === id);
199
191
  if (index === -1) {
200
192
  throw new Error(`Entity in ${table} with id ${id} not found.`);
201
193
  }
@@ -209,8 +201,16 @@ class InMemoryDatabase {
209
201
  * @param value
210
202
  */
211
203
  setEntityByIndex(table, index, value) {
212
- // @ts-ignore
213
- this.data[table][index] = value;
204
+ this.getTable(table)[index] = value;
205
+ }
206
+ getTable(table) {
207
+ return this.data[table];
208
+ }
209
+ setTable(table, values) {
210
+ this.data[table] = values;
211
+ }
212
+ isObject(value) {
213
+ return typeof value === 'object' && value !== null;
214
214
  }
215
215
  }
216
216
  exports.InMemoryDatabase = InMemoryDatabase;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brackets-memory-db",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "An in-memory database for brackets-manager.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -18,6 +18,7 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "build": "tsc",
21
+ "test": "node --test ../test/implementations/memory.test.js",
21
22
  "prepare": "npm run build",
22
23
  "prepublishOnly": "npm run build"
23
24
  },
@@ -35,9 +36,9 @@
35
36
  "typescript": "^4.4.4"
36
37
  },
37
38
  "peerDependencies": {
38
- "brackets-manager": "^1.3.9"
39
+ "brackets-model": "^1.7.0"
39
40
  },
40
41
  "dependencies": {
41
42
  "rfdc": "^1.3.0"
42
43
  }
43
- }
44
+ }