@deploybay/sdk
v1.0.1
Published
SDK for DeployBay serverless applications
Readme
DeployBay SDK
SDK for DeployBay serverless applications.
Installation
Beta Version
npm install @deploybay/sdk@betaThis package is automatically installed when you run npm install -g @deploybay/cli@beta.
Usage
The DeployBay SDK provides a simple, clean interface for common application needs:
Key-Value Storage
import { store } from '@deploybay/sdk';
// Store a value
await store.put('user:123', JSON.stringify({ name: 'John', email: '[email protected]' }));
// Retrieve a value
const userData = await store.get('user:123');
const user = userData ? JSON.parse(userData) : null;
// Delete a value
await store.delete('user:123');Database Operations
import { data } from '@deploybay/sdk';
// Run a SQL query
const users = await data.query('SELECT * FROM users WHERE active = ?', [true]);
// Prepare statements for better performance
const stmt = data.prepare('SELECT * FROM users WHERE status = ?');
const activeUsers = await stmt.first();
// Use transactions for atomic operations
const transaction = await data.beginTransaction();
try {
await transaction.query('UPDATE accounts SET balance = balance - ? WHERE id = ?', [100, 1]);
await transaction.query('UPDATE accounts SET balance = balance + ? WHERE id = ?', [100, 2]);
await transaction.commit();
} catch (error) {
await transaction.rollback();
throw error;
}File Storage
import { media } from '@deploybay/sdk';
// Upload a file
const fileData = new ArrayBuffer(1024); // Your file data
await media.put('uploads/photo.jpg', fileData, {
metadata: { contentType: 'image/jpeg', size: '1024' }
});
// Download a file
const downloadedFile = await media.get('uploads/photo.jpg');
// List files with prefix
const { objects } = await media.list({ prefix: 'uploads/', limit: 50 });
// Delete a file
await media.delete('uploads/photo.jpg');Configuration
import { config } from '@deploybay/sdk';
// Get configuration values
const apiKey = config.get('API_KEY');
const databaseUrl = config.get('DATABASE_URL');
// Environment detection
if (config.isDevelopment()) {
console.log('Running in development mode');
}
// Access secrets (automatically decrypted)
const jwtSecret = config.getSecret('JWT_SECRET');API Reference
Store
get(key: string, options?: StorageOptions): Promise<string | object | ArrayBuffer | ReadableStream | null>- Retrieve a value by keyput(key: string, value: string | ArrayBuffer | ArrayBufferView | ReadableStream, options?: StorageOptions): Promise<void>- Store a value by keydelete(key: string): Promise<void>- Delete a key-value pairgetWithMetadata(key: string, options?: StorageOptions): Promise<{value: any, metadata: any}>- Get value with metadatagetMany(keys: string[], options?: StorageOptions): Promise<Map<string, any>>- Get multiple valueslist(options?: {prefix?: string, limit?: number, cursor?: string}): Promise<{keys: string[], list_complete: boolean, cursor?: string}>- List keys
Data
query(sql: string, params?: any[]): Promise<any>- Run a SQL queryprepare<T>(sql: string): PreparedStatement<T>- Prepare a statement for reusebatch(operations: {sql: string, params?: any[]}[]): Promise<any[]>- Execute multiple operationsbeginTransaction(): Promise<Transaction>- Start a database transactionwithSession(mode?: string): DatabaseSession- Create a session with consistencyexec(sql: string): Promise<any>- Execute SQL directlydump(): Promise<ArrayBuffer>- Export database dump
Media
put(key: string, data: string | ArrayBuffer | ArrayBufferView | Blob, options?: {metadata?: Record<string, string>, httpMetadata?: Record<string, string>}): Promise<void>- Upload a fileget(key: string): Promise<ArrayBuffer | null>- Download a filedelete(key: string): Promise<void>- Delete a filelist(options?: {prefix?: string, limit?: number, cursor?: string}): Promise<{objects: any[], truncated: boolean, cursor?: string}>- List filescreateMultipartUpload(key: string, options?): Promise<{uploadId: string}>- Start multipart uploaduploadPart(key: string, uploadId: string, partNumber: number, data: ArrayBuffer): Promise<{etag: string}>- Upload partcompleteMultipartUpload(key: string, uploadId: string, parts: {partNumber: number, etag: string}[]): Promise<void>- Complete upload
Config
get(key: string): string | undefined- Get a configuration valuegetVar(key: string): string | undefined- Get an environment variablegetSecret(key: string): string | undefined- Get a secret (automatically decrypted)isDevelopment(): boolean- Check if in development environmentisProduction(): boolean- Check if in production environmentisStaging(): boolean- Check if in staging environmentgetEnvironment(): string- Get current environment name
AI
run<T>(model: string, params: Record<string, any>): Promise<T>- Run AI model inference
