@micro-cms/mock-db 1.0.13 → 1.0.16

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.mts CHANGED
@@ -5,6 +5,8 @@ declare class MockDataProvider implements DataProvider {
5
5
  find(entity: string, query?: {
6
6
  page?: number;
7
7
  limit?: number;
8
+ q?: string;
9
+ sort?: string;
8
10
  }): Promise<any>;
9
11
  findById(entity: string, id: any): Promise<any>;
10
12
  create(entity: string, data: any): Promise<any>;
package/dist/index.d.ts CHANGED
@@ -5,6 +5,8 @@ declare class MockDataProvider implements DataProvider {
5
5
  find(entity: string, query?: {
6
6
  page?: number;
7
7
  limit?: number;
8
+ q?: string;
9
+ sort?: string;
8
10
  }): Promise<any>;
9
11
  findById(entity: string, id: any): Promise<any>;
10
12
  create(entity: string, data: any): Promise<any>;
package/dist/index.js CHANGED
@@ -77,9 +77,25 @@ var MockDataProvider = class {
77
77
  return Promise.resolve(MOCK_SCHEMA);
78
78
  }
79
79
  async find(entity, query) {
80
- const table = MOCK_DATA[entity] || [];
80
+ let table = [...MOCK_DATA[entity] || []];
81
81
  const page = query?.page || 1;
82
82
  const limit = query?.limit || 10;
83
+ if (query?.q) {
84
+ const q = query.q.toLowerCase();
85
+ table = table.filter(
86
+ (item) => Object.values(item).some((val) => String(val).toLowerCase().includes(q))
87
+ );
88
+ }
89
+ if (query?.sort) {
90
+ const [field, direction] = query.sort.split(":");
91
+ table.sort((a, b) => {
92
+ const valA = a[field];
93
+ const valB = b[field];
94
+ if (valA < valB) return direction === "desc" ? 1 : -1;
95
+ if (valA > valB) return direction === "desc" ? -1 : 1;
96
+ return 0;
97
+ });
98
+ }
83
99
  const start = (page - 1) * limit;
84
100
  const end = start + limit;
85
101
  return Promise.resolve({
package/dist/index.mjs CHANGED
@@ -51,9 +51,25 @@ var MockDataProvider = class {
51
51
  return Promise.resolve(MOCK_SCHEMA);
52
52
  }
53
53
  async find(entity, query) {
54
- const table = MOCK_DATA[entity] || [];
54
+ let table = [...MOCK_DATA[entity] || []];
55
55
  const page = query?.page || 1;
56
56
  const limit = query?.limit || 10;
57
+ if (query?.q) {
58
+ const q = query.q.toLowerCase();
59
+ table = table.filter(
60
+ (item) => Object.values(item).some((val) => String(val).toLowerCase().includes(q))
61
+ );
62
+ }
63
+ if (query?.sort) {
64
+ const [field, direction] = query.sort.split(":");
65
+ table.sort((a, b) => {
66
+ const valA = a[field];
67
+ const valB = b[field];
68
+ if (valA < valB) return direction === "desc" ? 1 : -1;
69
+ if (valA > valB) return direction === "desc" ? -1 : 1;
70
+ return 0;
71
+ });
72
+ }
57
73
  const start = (page - 1) * limit;
58
74
  const end = start + limit;
59
75
  return Promise.resolve({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micro-cms/mock-db",
3
- "version": "1.0.13",
3
+ "version": "1.0.16",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
@@ -16,7 +16,7 @@
16
16
  }
17
17
  },
18
18
  "dependencies": {
19
- "@micro-cms/types": "^1.0.13"
19
+ "@micro-cms/types": "^1.0.16"
20
20
  },
21
21
  "scripts": {
22
22
  "build": "tsup src/index.ts --format cjs,esm --dts",