json-server 1.0.0-alpha.19 → 1.0.0-alpha.20
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/lib/service.js +13 -10
- package/package.json +1 -1
package/lib/service.js
CHANGED
|
@@ -78,25 +78,28 @@ function deleteDependents(db, name, dependents) {
|
|
|
78
78
|
function randomId() {
|
|
79
79
|
return randomBytes(2).toString('hex');
|
|
80
80
|
}
|
|
81
|
-
function
|
|
82
|
-
|
|
81
|
+
function fixItemsIds(items) {
|
|
82
|
+
items.forEach((item) => {
|
|
83
|
+
if (typeof item['id'] === 'number') {
|
|
84
|
+
item['id'] = item['id'].toString();
|
|
85
|
+
}
|
|
83
86
|
if (item['id'] === undefined) {
|
|
84
|
-
|
|
87
|
+
item['id'] = randomId();
|
|
85
88
|
}
|
|
86
|
-
return item;
|
|
87
89
|
});
|
|
88
90
|
}
|
|
89
91
|
// Ensure all items have an id
|
|
90
|
-
function
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
function fixAllItemsIds(data) {
|
|
93
|
+
Object.values(data).forEach((value) => {
|
|
94
|
+
if (Array.isArray(value)) {
|
|
95
|
+
fixItemsIds(value);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
95
98
|
}
|
|
96
99
|
export class Service {
|
|
97
100
|
#db;
|
|
98
101
|
constructor(db) {
|
|
99
|
-
|
|
102
|
+
fixAllItemsIds(db.data);
|
|
100
103
|
this.#db = db;
|
|
101
104
|
}
|
|
102
105
|
#get(name) {
|