brackets-memory-db 1.0.4 → 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
  */
@@ -70,4 +71,24 @@ export declare class InMemoryDatabase implements CrudInterface {
70
71
  * @param filter An object to filter data.
71
72
  */
72
73
  delete<T>(table: Table, filter: Partial<T>): Promise<boolean>;
74
+ /**
75
+ * Find the index of a table entity by its id
76
+ *
77
+ * @param table
78
+ * @param id
79
+ * @returns
80
+ */
81
+ getEntityIndexById(table: Table, id: number): number;
82
+ /**
83
+ * Set a table entity value by its index
84
+ *
85
+ * @param table
86
+ * @param index
87
+ * @param value
88
+ */
89
+ setEntityByIndex<T>(table: Table, index: number, value: T): void;
90
+ private getTable;
91
+ private setTable;
92
+ private isObject;
73
93
  }
94
+ export {};
package/dist/index.js CHANGED
@@ -49,13 +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
- ? (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)
54
55
  : 0;
55
56
  if (!Array.isArray(values)) {
56
57
  try {
57
- // @ts-ignore
58
- this.data[table].push({ id, ...values });
58
+ rows.push({ id, ...values });
59
59
  }
60
60
  catch (error) {
61
61
  return new Promise((resolve) => {
@@ -68,8 +68,7 @@ class InMemoryDatabase {
68
68
  }
69
69
  try {
70
70
  values.map((object) => {
71
- // @ts-ignore
72
- this.data[table].push({ id: id++, ...object });
71
+ rows.push({ id: id++, ...object });
73
72
  });
74
73
  }
75
74
  catch (error) {
@@ -89,19 +88,17 @@ class InMemoryDatabase {
89
88
  try {
90
89
  if (arg === undefined) {
91
90
  return new Promise((resolve) => {
92
- // @ts-ignore
93
- resolve(this.data[table].map(clone));
91
+ resolve(this.getTable(table).map(clone));
94
92
  });
95
93
  }
96
94
  if (typeof arg === 'number') {
97
95
  return new Promise((resolve) => {
98
- // @ts-ignore
99
- 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);
100
98
  });
101
99
  }
102
100
  return new Promise((resolve) => {
103
- // @ts-ignore
104
- resolve(this.data[table].filter(this.makeFilter(arg)).map(clone));
101
+ resolve(this.getTable(table).filter(this.makeFilter(arg)).map(clone));
105
102
  });
106
103
  }
107
104
  catch (error) {
@@ -120,8 +117,8 @@ class InMemoryDatabase {
120
117
  update(table, arg, value) {
121
118
  if (typeof arg === 'number') {
122
119
  try {
123
- // @ts-ignore
124
- this.data[table][arg] = value;
120
+ const index = this.getEntityIndexById(table, arg);
121
+ this.setEntityByIndex(table, index, value);
125
122
  return new Promise((resolve) => {
126
123
  resolve(true);
127
124
  });
@@ -132,27 +129,25 @@ class InMemoryDatabase {
132
129
  });
133
130
  }
134
131
  }
135
- // @ts-ignore
136
- const values = this.data[table].filter(this.makeFilter(arg));
132
+ const values = this.getTable(table).filter(this.makeFilter(arg));
137
133
  if (!values) {
138
134
  return new Promise((resolve) => {
139
135
  resolve(false);
140
136
  });
141
137
  }
142
138
  values.forEach((v) => {
143
- const existing = this.data[table][v.id];
144
- for (const key in value) {
145
- // @ts-ignore
146
- if (existing[key] && typeof existing[key] === 'object' && typeof value[key] === 'object') {
147
- // @ts-ignore
148
- Object.assign(existing[key], value[key]); // For opponent objects, this does a deep merge of level 2.
139
+ const index = this.getEntityIndexById(table, v.id);
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.
149
145
  }
150
146
  else {
151
- // @ts-ignore
152
- existing[key] = value[key]; // Otherwise, do a simple value assignment.
147
+ existing[key] = updateValue[key]; // Otherwise, do a simple value assignment.
153
148
  }
154
149
  }
155
- this.data[table][v.id] = existing;
150
+ this.setEntityByIndex(table, index, existing);
156
151
  });
157
152
  return new Promise((resolve) => {
158
153
  resolve(true);
@@ -179,11 +174,43 @@ class InMemoryDatabase {
179
174
  }
180
175
  const predicate = this.makeFilter(filter);
181
176
  const negativeFilter = (value) => !predicate(value);
182
- // @ts-ignore
183
- this.data[table] = values.filter(negativeFilter);
177
+ this.setTable(table, this.getTable(table).filter(negativeFilter));
184
178
  return new Promise((resolve) => {
185
179
  resolve(true);
186
180
  });
187
181
  }
182
+ /**
183
+ * Find the index of a table entity by its id
184
+ *
185
+ * @param table
186
+ * @param id
187
+ * @returns
188
+ */
189
+ getEntityIndexById(table, id) {
190
+ const index = this.getTable(table).findIndex(e => e.id === id);
191
+ if (index === -1) {
192
+ throw new Error(`Entity in ${table} with id ${id} not found.`);
193
+ }
194
+ return index;
195
+ }
196
+ /**
197
+ * Set a table entity value by its index
198
+ *
199
+ * @param table
200
+ * @param index
201
+ * @param value
202
+ */
203
+ setEntityByIndex(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
+ }
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.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,7 +36,7 @@
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"