@micro-cms/mock-db 0.0.1
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.ts +10 -0
- package/dist/index.js +70 -0
- package/package.json +26 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CmsModule, DataProvider, Schema } from '@micro-cms/types';
|
|
2
|
+
export declare class MockDataProvider implements DataProvider {
|
|
3
|
+
introspect(): Promise<Schema>;
|
|
4
|
+
find(entity: string): Promise<any[]>;
|
|
5
|
+
create(entity: string, data: any): Promise<any>;
|
|
6
|
+
update(entity: string, id: any, data: any): Promise<any>;
|
|
7
|
+
delete(entity: string, id: any): Promise<any>;
|
|
8
|
+
}
|
|
9
|
+
declare const mockDbModule: CmsModule;
|
|
10
|
+
export default mockDbModule;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const MOCK_SCHEMA = {
|
|
2
|
+
entities: [
|
|
3
|
+
{
|
|
4
|
+
name: 'users',
|
|
5
|
+
fields: [
|
|
6
|
+
{ name: 'id', type: 'number', constraints: { required: true } },
|
|
7
|
+
{ name: 'name', type: 'text', label: 'Full Name', constraints: { required: true, minLength: 2 } },
|
|
8
|
+
{ name: 'email', type: 'text', label: 'Email Address', constraints: { required: true } },
|
|
9
|
+
{ name: 'isActive', type: 'boolean', label: 'Active User' },
|
|
10
|
+
{ name: 'role', type: 'select', constraints: { options: ['admin', 'editor', 'viewer'] } }
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
name: 'posts',
|
|
15
|
+
fields: [
|
|
16
|
+
{ name: 'id', type: 'number', constraints: { required: true } },
|
|
17
|
+
{ name: 'title', type: 'text', constraints: { required: true } },
|
|
18
|
+
{ name: 'content', type: 'text' },
|
|
19
|
+
{ name: 'publishedAt', type: 'date' },
|
|
20
|
+
{ name: 'authorId', type: 'relation', relation: { targetEntity: 'users', displayField: 'name' } }
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
};
|
|
25
|
+
const MOCK_DATA = {
|
|
26
|
+
users: [
|
|
27
|
+
{ id: 1, name: 'Alice Admin', email: 'alice@example.com', isActive: true, role: 'admin' },
|
|
28
|
+
{ id: 2, name: 'Bob Builder', email: 'bob@example.com', isActive: false, role: 'editor' }
|
|
29
|
+
],
|
|
30
|
+
posts: [
|
|
31
|
+
{ id: 1, title: 'Hello World', content: 'First post', publishedAt: '2023-01-01', authorId: 1 }
|
|
32
|
+
]
|
|
33
|
+
};
|
|
34
|
+
export class MockDataProvider {
|
|
35
|
+
async introspect() {
|
|
36
|
+
return Promise.resolve(MOCK_SCHEMA);
|
|
37
|
+
}
|
|
38
|
+
async find(entity) {
|
|
39
|
+
return Promise.resolve(MOCK_DATA[entity] || []);
|
|
40
|
+
}
|
|
41
|
+
async create(entity, data) {
|
|
42
|
+
const table = MOCK_DATA[entity] || [];
|
|
43
|
+
const newItem = { ...data, id: table.length + 1 };
|
|
44
|
+
MOCK_DATA[entity] = [...table, newItem];
|
|
45
|
+
return Promise.resolve(newItem);
|
|
46
|
+
}
|
|
47
|
+
async update(entity, id, data) {
|
|
48
|
+
return Promise.resolve(data);
|
|
49
|
+
}
|
|
50
|
+
async delete(entity, id) {
|
|
51
|
+
return Promise.resolve({ success: true });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const mockDbModule = {
|
|
55
|
+
manifest: {
|
|
56
|
+
name: '@micro-cms/mock-db',
|
|
57
|
+
version: '0.0.1',
|
|
58
|
+
provides: ['database-adapter', 'introspection'],
|
|
59
|
+
publishes: {
|
|
60
|
+
'database.schema': 'The current database schema'
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
async load({ runtime, context }) {
|
|
64
|
+
const provider = new MockDataProvider();
|
|
65
|
+
runtime.register('database-adapter', provider);
|
|
66
|
+
const schema = await provider.introspect();
|
|
67
|
+
context.publish('database.schema', schema);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
export default mockDbModule;
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@micro-cms/mock-db",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": ["dist"],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@micro-cms/types": "workspace:*"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"devDependencies": {},
|
|
21
|
+
"typescript": "^5.0.0",
|
|
22
|
+
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"prepare": "pnpm build"
|
|
25
|
+
}
|
|
26
|
+
}
|