@trellisjs/plugin-datasource 0.1.0-alpha.1 → 1.0.0
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/LICENSE +21 -0
- package/README.md +102 -0
- package/package.json +18 -13
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 TrellisJS Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# @trellisjs/plugin-datasource
|
|
2
|
+
|
|
3
|
+
Datasource abstraction for Trellis data tables, with built-in static (local) and remote (HTTP) implementations.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @trellisjs/plugin-datasource
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Exports
|
|
12
|
+
|
|
13
|
+
| Export | Description |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `createStaticDatasource<T>(data, options?)` | Local datasource that performs sorting, filtering, and pagination in memory. |
|
|
16
|
+
| `createRemoteDatasource<T>(options)` | HTTP datasource that sends standardized queries to a backend API. |
|
|
17
|
+
| `buildQueryFromState(api)` | Builds a `TrellisQuery` from the current Trellis state (sorting, filtering, pagination). |
|
|
18
|
+
| `TrellisQuery` | Query shape sent to `datasource.fetch()`. |
|
|
19
|
+
| `TrellisResponse<T>` | Expected response shape: `{ data, total, filtered }`. |
|
|
20
|
+
| `TrellisDatasource<T>` | Interface: `{ fetch(query): Promise<TrellisResponse<T>> }`. |
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Static Datasource (Client-Side)
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { createStaticDatasource } from '@trellisjs/plugin-datasource';
|
|
28
|
+
|
|
29
|
+
const ds = createStaticDatasource([
|
|
30
|
+
{ name: 'Alice', age: 30 },
|
|
31
|
+
{ name: 'Bob', age: 25 },
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
const result = await ds.fetch({
|
|
35
|
+
page: 1,
|
|
36
|
+
pageSize: 10,
|
|
37
|
+
sort: [{ columnId: 'age', direction: 'asc' }],
|
|
38
|
+
filter: { global: 'ali' },
|
|
39
|
+
});
|
|
40
|
+
// result.data --> [{ name: 'Alice', age: 30 }]
|
|
41
|
+
// result.total --> 2
|
|
42
|
+
// result.filtered --> 1
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Remote Datasource (Server-Side)
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { createRemoteDatasource } from '@trellisjs/plugin-datasource';
|
|
49
|
+
|
|
50
|
+
const ds = createRemoteDatasource({
|
|
51
|
+
url: '/api/users',
|
|
52
|
+
method: 'POST', // default
|
|
53
|
+
headers: { Authorization: 'Bearer ...' },
|
|
54
|
+
transformQuery: (q) => ({ // adapt to your API contract
|
|
55
|
+
page: q.page,
|
|
56
|
+
per_page: q.pageSize,
|
|
57
|
+
sort_by: q.sort?.[0]?.columnId,
|
|
58
|
+
sort_dir: q.sort?.[0]?.direction,
|
|
59
|
+
}),
|
|
60
|
+
transformResponse: (raw) => ({ // adapt response to TrellisResponse
|
|
61
|
+
data: raw.items,
|
|
62
|
+
total: raw.total_count,
|
|
63
|
+
filtered: raw.filtered_count,
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const result = await ds.fetch({ page: 1, pageSize: 20 });
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Build Query from Trellis State
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { buildQueryFromState } from '@trellisjs/plugin-datasource';
|
|
74
|
+
|
|
75
|
+
const query = buildQueryFromState(api);
|
|
76
|
+
// { page, pageSize, sort?, filter? }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## TrellisQuery Shape
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
interface TrellisQuery {
|
|
83
|
+
page: number; // 1-based
|
|
84
|
+
pageSize: number;
|
|
85
|
+
sort?: { columnId: string; direction: 'asc' | 'desc' }[];
|
|
86
|
+
filter?: { global?: string; columns?: Record<string, unknown> };
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## TrellisResponse Shape
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
interface TrellisResponse<T> {
|
|
94
|
+
data: T[]; // rows for the current page
|
|
95
|
+
total: number; // total rows (unfiltered)
|
|
96
|
+
filtered: number; // total rows after filtering
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trellisjs/plugin-datasource",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "資料源插件 — 支援靜態資料與遠端 API",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -20,22 +20,27 @@
|
|
|
20
20
|
"files": [
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
|
-
"scripts": {
|
|
24
|
-
"build": "tsup",
|
|
25
|
-
"dev": "tsup --watch",
|
|
26
|
-
"test": "vitest run",
|
|
27
|
-
"test:watch": "vitest",
|
|
28
|
-
"clean": "rm -rf dist"
|
|
29
|
-
},
|
|
30
23
|
"peerDependencies": {
|
|
31
|
-
"@trellisjs/core": "
|
|
24
|
+
"@trellisjs/core": "0.1.0"
|
|
32
25
|
},
|
|
33
26
|
"devDependencies": {
|
|
34
|
-
"@trellisjs/core": "workspace:*",
|
|
35
27
|
"tsup": "^8.4.0",
|
|
36
28
|
"typescript": "^5.7.0",
|
|
37
|
-
"vitest": "^3.1.0"
|
|
29
|
+
"vitest": "^3.1.0",
|
|
30
|
+
"@trellisjs/core": "0.1.0"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/cluion/trellis.git",
|
|
35
|
+
"directory": "packages/plugin-datasource"
|
|
38
36
|
},
|
|
39
37
|
"license": "MIT",
|
|
40
|
-
"sideEffects": false
|
|
41
|
-
|
|
38
|
+
"sideEffects": false,
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsup",
|
|
41
|
+
"dev": "tsup --watch",
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"test:watch": "vitest",
|
|
44
|
+
"clean": "rm -rf dist"
|
|
45
|
+
}
|
|
46
|
+
}
|