swiftbase-admin-sdk 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 ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2026 Swiftbase
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,224 @@
1
+ # Swiftbase TypeScript Admin SDK
2
+
3
+ TypeScript Admin SDK for integrating customer backend services with the Swiftbase platform. Supports service authentication, role & service management, configurations, real-time database querying, and object storage.
4
+
5
+ ## Installation
6
+
7
+ Install the package via npm:
8
+
9
+ ```bash
10
+ npm install swiftbase-admin-sdk
11
+ ```
12
+
13
+ ---
14
+
15
+ ## Getting Started
16
+
17
+ ### Initialize the SDK
18
+
19
+ Initialize the SDK at the entry point of your application using your Project ID:
20
+
21
+ ```typescript
22
+ import { initializeSdk } from "swiftbase-admin-sdk";
23
+
24
+ initializeSdk("your-project-id");
25
+ ```
26
+
27
+ ---
28
+
29
+ ## Authentication
30
+
31
+ The Admin SDK supports authenticating backend services, managing access tokens, and verifying user or service credentials.
32
+
33
+ ### Service Login (Client Credentials)
34
+
35
+ Log in as a backend service using a Service ID and Secret Key:
36
+
37
+ ```typescript
38
+ import { login } from "swiftbase-admin-sdk";
39
+
40
+ await login("your-service-id", "your-service-secret");
41
+ ```
42
+
43
+ ### Verify Credentials
44
+
45
+ Verify a user token or service token in custom middleware:
46
+
47
+ ```typescript
48
+ import { verifyToken, verifyServiceToken } from "swiftbase-admin-sdk";
49
+
50
+ // Verify a user access token
51
+ const user = await verifyToken("user-jwt-token");
52
+
53
+ // Verify a service token with a specific scope
54
+ const service = await verifyServiceToken("service-jwt-token", "admin");
55
+ ```
56
+
57
+ ### Session Helper Methods
58
+
59
+ ```typescript
60
+ import { isLoggedIn, logout, getAccessToken } from "swiftbase-admin-sdk";
61
+
62
+ if (isLoggedIn()) {
63
+ const token = await getAccessToken();
64
+ console.log("Active Session Access Token:", token);
65
+ }
66
+
67
+ logout(); // Clears access token from the active session
68
+ ```
69
+
70
+ ---
71
+
72
+ ## Identity & Access Control
73
+
74
+ Manage roles, services, and user role assignments.
75
+
76
+ ### Role Management
77
+
78
+ ```typescript
79
+ import { getRoles, createRole, updateRole, deleteRole } from "swiftbase-admin-sdk";
80
+
81
+ // Get roles
82
+ const roles = await getRoles("your-project-id");
83
+
84
+ // Create role
85
+ const role = await createRole({
86
+ projectId: "your-project-id",
87
+ name: "Manager",
88
+ permissions: ["read:reports", "write:reports"],
89
+ });
90
+
91
+ // Update role
92
+ const updatedRole = await updateRole({
93
+ id: "role-id",
94
+ name: "Senior Manager",
95
+ });
96
+
97
+ // Delete role
98
+ await deleteRole("role-id");
99
+ ```
100
+
101
+ ### Service Management
102
+
103
+ ```typescript
104
+ import { getServices, createService, updateService, deleteService } from "swiftbase-admin-sdk";
105
+
106
+ // Get services
107
+ const services = await getServices("your-project-id");
108
+
109
+ // Create service
110
+ const newService = await createService({
111
+ projectId: "your-project-id",
112
+ name: "Reporting Engine",
113
+ secretKey: "secure-key",
114
+ scope: ["reports"],
115
+ });
116
+
117
+ // Update service
118
+ await updateService({
119
+ id: "service-id",
120
+ name: "Advanced Reporting Engine",
121
+ });
122
+
123
+ // Delete service
124
+ await deleteService("service-id");
125
+ ```
126
+
127
+ ### User Role Assignments
128
+
129
+ ```typescript
130
+ import { getUsers, assignRole, unassignRole } from "swiftbase-admin-sdk";
131
+
132
+ // List all project users
133
+ const users = await getUsers("your-project-id");
134
+
135
+ // Assign a role to a user
136
+ await assignRole("user-id", "Manager");
137
+
138
+ // Unassign a role from a user
139
+ await unassignRole("user-id", "Manager");
140
+ ```
141
+
142
+ ---
143
+
144
+ ## Database Queries
145
+
146
+ Query your Swiftbase databases using the Query Builder. Query execution automatically utilizes WebSockets if available, otherwise it transparently falls back to REST.
147
+
148
+ ```typescript
149
+ import { db } from "swiftbase-admin-sdk";
150
+
151
+ // Fetch rows
152
+ const users = await db("my_database")("users")
153
+ .select("id", "email", "firstName")
154
+ .where("status", "active")
155
+ .limit(10)
156
+ .offset(0);
157
+
158
+ // Insert row
159
+ await db("my_database")("users")
160
+ .insert({ firstName: "Alice", email: "alice@example.com" })
161
+ .execute();
162
+
163
+ // Update rows
164
+ await db("my_database")("users").where("id", "user-id").update({ firstName: "Bob" }).execute();
165
+
166
+ // Delete rows
167
+ await db("my_database")("users").where("id", "user-id").delete().execute();
168
+ ```
169
+
170
+ ### Real-time Subscriptions
171
+
172
+ Subscribe to real-time table modifications using WebSockets:
173
+
174
+ ```typescript
175
+ const unsubscribe = db("my_database")("posts")
176
+ .where("status", "published")
177
+ .listen((change) => {
178
+ console.log("Change Event:", change.event); // 'insert' | 'update' | 'delete'
179
+ console.log("Changed Data:", change.data);
180
+ });
181
+
182
+ // To stop listening later:
183
+ unsubscribe();
184
+ ```
185
+
186
+ ---
187
+
188
+ ## Object Storage (S3-Compatible)
189
+
190
+ Interface with storage buckets using the S3-compatible client.
191
+
192
+ ```typescript
193
+ import { Storage } from "swiftbase-admin-sdk";
194
+
195
+ const storage = new Storage({
196
+ bucket: "my-bucket",
197
+ // accessKeyId: "key", // Optional: Signs requests with SigV4;
198
+ // secretAccessKey: "secret" // defaults to Bearer token authentication
199
+ });
200
+
201
+ // List files
202
+ const { contents } = await storage.listObjects({ prefix: "uploads/" });
203
+
204
+ // Upload object
205
+ await storage.putObject("uploads/hello.txt", "Hello World!", {
206
+ contentType: "text/plain",
207
+ });
208
+
209
+ // Retrieve object text
210
+ const text = await storage.getObjectAsText("uploads/hello.txt");
211
+
212
+ // Delete object
213
+ await storage.deleteObject("uploads/hello.txt");
214
+ ```
215
+
216
+ ---
217
+
218
+ ## Running Tests
219
+
220
+ Run the Vitest suite locally:
221
+
222
+ ```bash
223
+ npm run test
224
+ ```