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.
Files changed (2) hide show
  1. package/lib/service.js +13 -10
  2. 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 ensureItemsHaveIds(items) {
82
- return items.map((item) => {
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
- return { ...item, id: randomId() };
87
+ item['id'] = randomId();
85
88
  }
86
- return item;
87
89
  });
88
90
  }
89
91
  // Ensure all items have an id
90
- function ensureAllItemsHaveIds(data) {
91
- return Object.entries(data).reduce((acc, [key, value]) => ({
92
- ...acc,
93
- [key]: Array.isArray(value) ? ensureItemsHaveIds(value) : value,
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
- db.data = ensureAllItemsHaveIds(db.data);
102
+ fixAllItemsIds(db.data);
100
103
  this.#db = db;
101
104
  }
102
105
  #get(name) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-server",
3
- "version": "1.0.0-alpha.19",
3
+ "version": "1.0.0-alpha.20",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "bin": {