@tablecraft/adapter-express 0.1.0-beta.1 → 0.1.0-beta.2
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/README.md +107 -5
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,20 +1,41 @@
|
|
|
1
1
|
# @tablecraft/adapter-express
|
|
2
2
|
|
|
3
|
-
Express adapter for
|
|
3
|
+
Express adapter for TableCraft — build powerful data APIs in your Express application.
|
|
4
|
+
|
|
5
|
+
## Links
|
|
6
|
+
|
|
7
|
+
- [GitHub](https://github.com/jacksonkasi1/TableCraft)
|
|
8
|
+
- [Documentation](https://jacksonkasi.gitbook.io/tablecraft/)
|
|
4
9
|
|
|
5
10
|
## Install
|
|
6
11
|
|
|
7
12
|
```bash
|
|
8
|
-
bun add @tablecraft/engine @tablecraft/adapter-express
|
|
13
|
+
bun add @tablecraft/engine @tablecraft/adapter-express express
|
|
14
|
+
# or
|
|
15
|
+
npm install @tablecraft/engine @tablecraft/adapter-express express
|
|
16
|
+
# or
|
|
17
|
+
yarn add @tablecraft/engine @tablecraft/adapter-express express
|
|
18
|
+
# or
|
|
19
|
+
pnpm add @tablecraft/engine @tablecraft/adapter-express express
|
|
9
20
|
```
|
|
10
21
|
|
|
22
|
+
## Features
|
|
23
|
+
|
|
24
|
+
- **Express.js native** — Seamless integration with Express middleware ecosystem
|
|
25
|
+
- **Dynamic routes** — Single endpoint for multiple tables
|
|
26
|
+
- **Type-safe** — Full TypeScript support
|
|
27
|
+
- **Middleware compatible** — Works with authentication, logging, and other Express middleware
|
|
28
|
+
|
|
11
29
|
## Usage
|
|
12
30
|
|
|
13
|
-
### Dynamic route
|
|
31
|
+
### Dynamic route (multiple tables)
|
|
14
32
|
|
|
15
33
|
```ts
|
|
16
34
|
import express from 'express';
|
|
17
35
|
import { createExpressMiddleware } from '@tablecraft/adapter-express';
|
|
36
|
+
import { db } from './db';
|
|
37
|
+
import * as schema from './db/schema';
|
|
38
|
+
import { configs } from './tablecraft.config';
|
|
18
39
|
|
|
19
40
|
const app = express();
|
|
20
41
|
|
|
@@ -27,12 +48,93 @@ app.get('/api/data/:table', createExpressMiddleware({
|
|
|
27
48
|
user: req.user, // from auth middleware
|
|
28
49
|
}),
|
|
29
50
|
}));
|
|
51
|
+
|
|
52
|
+
app.listen(3000);
|
|
30
53
|
```
|
|
31
54
|
|
|
32
|
-
### Single table
|
|
55
|
+
### Single table route
|
|
33
56
|
|
|
34
57
|
```ts
|
|
58
|
+
import express from 'express';
|
|
35
59
|
import { createExpressHandler } from '@tablecraft/adapter-express';
|
|
36
60
|
|
|
37
|
-
app
|
|
61
|
+
const app = express();
|
|
62
|
+
|
|
63
|
+
app.get('/api/users', createExpressHandler({
|
|
64
|
+
db,
|
|
65
|
+
schema,
|
|
66
|
+
config: usersConfig,
|
|
67
|
+
getContext: (req) => ({
|
|
68
|
+
tenantId: req.headers['x-tenant-id'],
|
|
69
|
+
}),
|
|
70
|
+
}));
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### With authentication middleware
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import express from 'express';
|
|
77
|
+
import { createExpressMiddleware } from '@tablecraft/adapter-express';
|
|
78
|
+
import { authMiddleware } from './auth';
|
|
79
|
+
|
|
80
|
+
const app = express();
|
|
81
|
+
|
|
82
|
+
// Auth middleware populates req.user
|
|
83
|
+
app.use(authMiddleware);
|
|
84
|
+
|
|
85
|
+
app.get('/api/data/:table', createExpressMiddleware({
|
|
86
|
+
db,
|
|
87
|
+
schema,
|
|
88
|
+
configs,
|
|
89
|
+
getContext: (req) => ({
|
|
90
|
+
tenantId: req.user.tenantId,
|
|
91
|
+
user: req.user,
|
|
92
|
+
}),
|
|
93
|
+
}));
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### With error handling
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import express from 'express';
|
|
100
|
+
import { createExpressMiddleware } from '@tablecraft/adapter-express';
|
|
101
|
+
|
|
102
|
+
const app = express();
|
|
103
|
+
|
|
104
|
+
app.get('/api/data/:table', createExpressMiddleware({
|
|
105
|
+
db,
|
|
106
|
+
schema,
|
|
107
|
+
configs,
|
|
108
|
+
getContext: (req) => ({ tenantId: req.headers['x-tenant-id'] }),
|
|
109
|
+
onError: (error, req, res) => {
|
|
110
|
+
console.error(error);
|
|
111
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
112
|
+
},
|
|
113
|
+
}));
|
|
114
|
+
|
|
115
|
+
// Global error handler
|
|
116
|
+
app.use((err, req, res, next) => {
|
|
117
|
+
res.status(500).json({ error: err.message });
|
|
118
|
+
});
|
|
38
119
|
```
|
|
120
|
+
|
|
121
|
+
## Configuration Options
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
createExpressMiddleware({
|
|
125
|
+
db, // Drizzle database instance
|
|
126
|
+
schema, // Drizzle schema object
|
|
127
|
+
configs, // Table configs map
|
|
128
|
+
getContext: (req) => ({
|
|
129
|
+
tenantId: string,
|
|
130
|
+
user: { id: string, roles: string[] },
|
|
131
|
+
}),
|
|
132
|
+
onError: (error, req, res) => {
|
|
133
|
+
// Custom error handling
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
|
|
140
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tablecraft/adapter-express",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.2",
|
|
4
4
|
"description": "Express adapter for TableCraft",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.
|
|
6
|
+
"module": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
|
-
"dist"
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
16
17
|
],
|
|
17
18
|
"scripts": {
|
|
18
19
|
"build": "bun run --bun tsc",
|