json-server 1.0.0-alpha.18 → 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.d.ts CHANGED
@@ -17,7 +17,7 @@ export declare class Service {
17
17
  constructor(db: Low<Data>);
18
18
  has(name: string): boolean;
19
19
  findById(name: string, id: string, query: {
20
- _embed?: string[];
20
+ _embed?: string[] | string;
21
21
  }): Item | undefined;
22
22
  find(name: string, query?: {
23
23
  [key: string]: unknown;
package/lib/service.js CHANGED
@@ -24,6 +24,9 @@ var Condition;
24
24
  function isCondition(value) {
25
25
  return Object.values(Condition).includes(value);
26
26
  }
27
+ function ensureArray(arg = []) {
28
+ return Array.isArray(arg) ? arg : [arg];
29
+ }
27
30
  function embed(db, name, item, related) {
28
31
  if (inflection.singularize(related) === related) {
29
32
  const relatedData = db.data[inflection.pluralize(related)];
@@ -75,25 +78,28 @@ function deleteDependents(db, name, dependents) {
75
78
  function randomId() {
76
79
  return randomBytes(2).toString('hex');
77
80
  }
78
- function ensureItemsHaveIds(items) {
79
- 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
+ }
80
86
  if (item['id'] === undefined) {
81
- return { ...item, id: randomId() };
87
+ item['id'] = randomId();
82
88
  }
83
- return item;
84
89
  });
85
90
  }
86
91
  // Ensure all items have an id
87
- function ensureAllItemsHaveIds(data) {
88
- return Object.entries(data).reduce((acc, [key, value]) => ({
89
- ...acc,
90
- [key]: Array.isArray(value) ? ensureItemsHaveIds(value) : value,
91
- }), {});
92
+ function fixAllItemsIds(data) {
93
+ Object.values(data).forEach((value) => {
94
+ if (Array.isArray(value)) {
95
+ fixItemsIds(value);
96
+ }
97
+ });
92
98
  }
93
99
  export class Service {
94
100
  #db;
95
101
  constructor(db) {
96
- db.data = ensureAllItemsHaveIds(db.data);
102
+ fixAllItemsIds(db.data);
97
103
  this.#db = db;
98
104
  }
99
105
  #get(name) {
@@ -106,7 +112,7 @@ export class Service {
106
112
  const value = this.#get(name);
107
113
  if (Array.isArray(value)) {
108
114
  let item = value.find((item) => item['id'] === id);
109
- query._embed?.forEach((related) => {
115
+ ensureArray(query._embed).forEach((related) => {
110
116
  if (item !== undefined)
111
117
  item = embed(this.#db, name, item, related);
112
118
  });
@@ -120,9 +126,10 @@ export class Service {
120
126
  return items;
121
127
  }
122
128
  // Include
123
- query._embed?.forEach((related) => {
124
- if (items !== undefined && Array.isArray(items))
129
+ ensureArray(query._embed).forEach((related) => {
130
+ if (items !== undefined && Array.isArray(items)) {
125
131
  items = items.map((item) => embed(this.#db, name, item, related));
132
+ }
126
133
  });
127
134
  // Return list if no query params
128
135
  if (Object.keys(query).length === 0) {
@@ -142,8 +149,17 @@ export class Service {
142
149
  conds[field] = [op, value];
143
150
  continue;
144
151
  }
145
- if (['_sort', '_start', '_end', '_limit', '_page', '_per_page'].includes(key))
152
+ if ([
153
+ '_embed',
154
+ '_sort',
155
+ '_start',
156
+ '_end',
157
+ '_limit',
158
+ '_page',
159
+ '_per_page',
160
+ ].includes(key)) {
146
161
  continue;
162
+ }
147
163
  conds[key] = [Condition.default, value];
148
164
  }
149
165
  // Loop through conditions and filter items
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-server",
3
- "version": "1.0.0-alpha.18",
3
+ "version": "1.0.0-alpha.20",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "bin": {