@tablecraft/adapter-elysia 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 +123 -3
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,18 +1,37 @@
|
|
|
1
1
|
# @tablecraft/adapter-elysia
|
|
2
2
|
|
|
3
|
-
Elysia (Bun) adapter for
|
|
3
|
+
Elysia (Bun) adapter for TableCraft — build powerful data APIs with Elysia framework.
|
|
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-elysia
|
|
13
|
+
bun add @tablecraft/engine @tablecraft/adapter-elysia elysia
|
|
14
|
+
# or
|
|
15
|
+
npm install @tablecraft/engine @tablecraft/adapter-elysia elysia
|
|
9
16
|
```
|
|
10
17
|
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- **Bun native** — Optimized for Bun runtime with excellent performance
|
|
21
|
+
- **Type-safe** — Full TypeScript support with Elysia's Eden treaty
|
|
22
|
+
- **Plugin system** — Integrates as an Elysia plugin
|
|
23
|
+
- **Minimal overhead** — Lightweight adapter with near-zero performance cost
|
|
24
|
+
|
|
11
25
|
## Usage
|
|
12
26
|
|
|
27
|
+
### As a plugin (multiple tables)
|
|
28
|
+
|
|
13
29
|
```ts
|
|
14
30
|
import { Elysia } from 'elysia';
|
|
15
31
|
import { createElysiaPlugin } from '@tablecraft/adapter-elysia';
|
|
32
|
+
import { db } from './db';
|
|
33
|
+
import * as schema from './db/schema';
|
|
34
|
+
import { configs } from './tablecraft.config';
|
|
16
35
|
|
|
17
36
|
const app = new Elysia()
|
|
18
37
|
.use(createElysiaPlugin({
|
|
@@ -20,8 +39,109 @@ const app = new Elysia()
|
|
|
20
39
|
schema,
|
|
21
40
|
configs,
|
|
22
41
|
getContext: ({ request }) => ({
|
|
23
|
-
tenantId: request.headers.get('x-tenant-id'),
|
|
42
|
+
tenantId: request.headers.get('x-tenant-id') ?? undefined,
|
|
43
|
+
}),
|
|
44
|
+
prefix: '/api/data', // Optional: defaults to '/api/data'
|
|
45
|
+
}))
|
|
46
|
+
.listen(3000);
|
|
47
|
+
|
|
48
|
+
console.log('Server running at http://localhost:3000');
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Single route
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { Elysia } from 'elysia';
|
|
55
|
+
import { createElysiaHandler } from '@tablecraft/adapter-elysia';
|
|
56
|
+
|
|
57
|
+
const app = new Elysia()
|
|
58
|
+
.get('/api/users', createElysiaHandler({
|
|
59
|
+
db,
|
|
60
|
+
schema,
|
|
61
|
+
config: usersConfig,
|
|
62
|
+
getContext: ({ request }) => ({
|
|
63
|
+
tenantId: request.headers.get('x-tenant-id') ?? undefined,
|
|
24
64
|
}),
|
|
25
65
|
}))
|
|
26
66
|
.listen(3000);
|
|
27
67
|
```
|
|
68
|
+
|
|
69
|
+
### With authentication
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { Elysia } from 'elysia';
|
|
73
|
+
import { jwt } from '@elysiajs/jwt';
|
|
74
|
+
import { createElysiaPlugin } from '@tablecraft/adapter-elysia';
|
|
75
|
+
|
|
76
|
+
const app = new Elysia()
|
|
77
|
+
.use(jwt({
|
|
78
|
+
name: 'jwt',
|
|
79
|
+
secret: process.env.JWT_SECRET!,
|
|
80
|
+
}))
|
|
81
|
+
.use(createElysiaPlugin({
|
|
82
|
+
db,
|
|
83
|
+
schema,
|
|
84
|
+
configs,
|
|
85
|
+
getContext: async ({ jwt, request }) => {
|
|
86
|
+
const authHeader = request.headers.get('authorization');
|
|
87
|
+
const token = authHeader?.replace('Bearer ', '');
|
|
88
|
+
|
|
89
|
+
if (!token) return {};
|
|
90
|
+
|
|
91
|
+
const user = await jwt.verify(token);
|
|
92
|
+
return {
|
|
93
|
+
tenantId: user.tenantId,
|
|
94
|
+
user,
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
}))
|
|
98
|
+
.listen(3000);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### With Eden Treaty (type-safe client)
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
// server.ts
|
|
105
|
+
import { Elysia, t } from 'elysia';
|
|
106
|
+
import { createElysiaPlugin } from '@tablecraft/adapter-elysia';
|
|
107
|
+
|
|
108
|
+
const app = new Elysia()
|
|
109
|
+
.use(createElysiaPlugin({ db, schema, configs }))
|
|
110
|
+
.listen(3000);
|
|
111
|
+
|
|
112
|
+
export type App = typeof app;
|
|
113
|
+
|
|
114
|
+
// client.ts
|
|
115
|
+
import { treaty } from '@elysiajs/eden';
|
|
116
|
+
import type { App } from './server';
|
|
117
|
+
|
|
118
|
+
const client = treaty<App>('http://localhost:3000');
|
|
119
|
+
|
|
120
|
+
// Type-safe API calls
|
|
121
|
+
const users = await client.api.data.users.get({
|
|
122
|
+
query: { page: 1, pageSize: 25 }
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Configuration Options
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
createElysiaPlugin({
|
|
130
|
+
db, // Drizzle database instance
|
|
131
|
+
schema, // Drizzle schema object
|
|
132
|
+
configs, // Table configs map
|
|
133
|
+
prefix: '/api/data', // Route prefix (optional)
|
|
134
|
+
getContext: async ({ request, store, ...context }) => ({
|
|
135
|
+
tenantId: string,
|
|
136
|
+
user: { id: string, roles: string[] },
|
|
137
|
+
}),
|
|
138
|
+
onError: (error, context) => {
|
|
139
|
+
// Custom error handling
|
|
140
|
+
console.error(error);
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tablecraft/adapter-elysia",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.2",
|
|
4
4
|
"description": "Elysia (Bun) 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",
|