json-server 1.0.0-beta.10 → 1.0.0-beta.12
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.
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { randomId } from "../random-id.js";
|
|
2
|
+
export const DEFAULT_SCHEMA_PATH = './node_modules/json-server/schema.json';
|
|
3
|
+
export class NormalizedAdapter {
|
|
4
|
+
#adapter;
|
|
5
|
+
constructor(adapter) {
|
|
6
|
+
this.#adapter = adapter;
|
|
7
|
+
}
|
|
8
|
+
async read() {
|
|
9
|
+
const data = await this.#adapter.read();
|
|
10
|
+
if (data === null) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
delete data['$schema'];
|
|
14
|
+
for (const value of Object.values(data)) {
|
|
15
|
+
if (Array.isArray(value)) {
|
|
16
|
+
for (const item of value) {
|
|
17
|
+
if (typeof item['id'] === 'number') {
|
|
18
|
+
item['id'] = item['id'].toString();
|
|
19
|
+
}
|
|
20
|
+
if (item['id'] === undefined) {
|
|
21
|
+
item['id'] = randomId();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
async write(data) {
|
|
29
|
+
await this.#adapter.write({ ...data, $schema: DEFAULT_SCHEMA_PATH });
|
|
30
|
+
}
|
|
31
|
+
}
|
package/lib/bin.js
CHANGED
|
@@ -8,8 +8,9 @@ import JSON5 from "json5";
|
|
|
8
8
|
import { Low } from "lowdb";
|
|
9
9
|
import { DataFile, JSONFile } from "lowdb/node";
|
|
10
10
|
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { NormalizedAdapter } from "./adapters/normalized-adapter.js";
|
|
12
|
+
import { Observer } from "./adapters/observer.js";
|
|
11
13
|
import { createApp } from "./app.js";
|
|
12
|
-
import { Observer } from "./observer.js";
|
|
13
14
|
function help() {
|
|
14
15
|
console.log(`Usage: json-server [options] <file>
|
|
15
16
|
|
|
@@ -109,7 +110,7 @@ if (extname(file) === ".json5") {
|
|
|
109
110
|
else {
|
|
110
111
|
adapter = new JSONFile(file);
|
|
111
112
|
}
|
|
112
|
-
const observer = new Observer(adapter);
|
|
113
|
+
const observer = new Observer(new NormalizedAdapter(adapter));
|
|
113
114
|
const db = new Low(observer, {});
|
|
114
115
|
await db.read();
|
|
115
116
|
// Create app
|
package/lib/random-id.js
ADDED
package/lib/service.js
CHANGED
|
@@ -1,19 +1,12 @@
|
|
|
1
|
-
import { randomBytes } from 'node:crypto';
|
|
2
1
|
import inflection from 'inflection';
|
|
3
2
|
import { Low } from 'lowdb';
|
|
4
3
|
import sortOn from 'sort-on';
|
|
5
4
|
import { matchesWhere } from "./matches-where.js";
|
|
6
5
|
import { paginate } from "./paginate.js";
|
|
6
|
+
import { randomId } from "./random-id.js";
|
|
7
7
|
export function isItem(obj) {
|
|
8
8
|
return typeof obj === 'object' && obj !== null && !Array.isArray(obj);
|
|
9
9
|
}
|
|
10
|
-
export function isData(obj) {
|
|
11
|
-
if (typeof obj !== 'object' || obj === null) {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
const data = obj;
|
|
15
|
-
return Object.values(data).every((value) => Array.isArray(value) ? value.every(isItem) : isItem(value));
|
|
16
|
-
}
|
|
17
10
|
function ensureArray(arg = []) {
|
|
18
11
|
return Array.isArray(arg) ? arg : [arg];
|
|
19
12
|
}
|
|
@@ -65,31 +58,9 @@ function deleteDependents(db, name, dependents) {
|
|
|
65
58
|
}
|
|
66
59
|
});
|
|
67
60
|
}
|
|
68
|
-
function randomId() {
|
|
69
|
-
return randomBytes(2).toString('hex');
|
|
70
|
-
}
|
|
71
|
-
function fixItemsIds(items) {
|
|
72
|
-
items.forEach((item) => {
|
|
73
|
-
if (typeof item['id'] === 'number') {
|
|
74
|
-
item['id'] = item['id'].toString();
|
|
75
|
-
}
|
|
76
|
-
if (item['id'] === undefined) {
|
|
77
|
-
item['id'] = randomId();
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
// Ensure all items have an id
|
|
82
|
-
function fixAllItemsIds(data) {
|
|
83
|
-
Object.values(data).forEach((value) => {
|
|
84
|
-
if (Array.isArray(value)) {
|
|
85
|
-
fixItemsIds(value);
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
61
|
export class Service {
|
|
90
62
|
#db;
|
|
91
63
|
constructor(db) {
|
|
92
|
-
fixAllItemsIds(db.data);
|
|
93
64
|
this.#db = db;
|
|
94
65
|
}
|
|
95
66
|
#get(name) {
|
package/package.json
CHANGED
|
File without changes
|