1.0.12 • Published 6 months ago

kodiebase v1.0.12

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

Kodiebase

A JavaScript client for interacting with the Kodiebase API. This client provides a simple interface for database operations, supporting both frontend applications (using auth tokens) and backend services (using API keys).

Getting Started

  1. Create a Kodiebase account at https://base.kodie.co.uk
  2. Create a new project in your dashboard
  3. Get your Project ID and API keys from the project settings

Installation

npm install kodiebase

Usage

Initialization

import { KodieClient } from 'kodiebase';
// OR
const { KodieClient } = require('kodiebase');

Authentication

The client supports two authentication methods: API keys (for backend services) and JWT tokens (for frontend applications).

Using API Keys

// Initialize with API key from your project settings (https://base.kodie.co.uk/dashboard/settings)
const client = new KodieClient({
  projectId: 'your-project-id',
  apiKey: 'your-api-key'
});

// The client will automatically use the API key for all requests
const users = await client.findAll('users');

Using JWT Authentication

// Initialize without authentication
const client = new KodieClient({
  projectId: 'your-project-id'
});

// Method 1: Login to get a token
const loginResult = await client.login({
  email: 'user@example.com',
  password: 'securepassword'
});
// Token is automatically set after successful login

// Method 2: Initialize with an existing token
const client = new KodieClient({
  projectId: 'your-project-id',
  authToken: 'your-jwt-token'  // Pass an existing JWT token
});

// Method 3: Set token manually
client.setAuthToken('your-jwt-token');

// Check authentication status
console.log(client.isAuthenticated()); // true

// Get the current auth token
const token = client.getAuthToken();

// Make authenticated requests
const users = await client.findAll('users');

// Logout (clears the token)
client.logout();

User Registration

// Register a new user
const registerResult = await client.register({
  email: 'user@example.com',
  password: 'securepassword',
  firstName: 'John',
  lastName: 'Doe'
});

// The client will automatically use the token returned from registration
console.log(client.isAuthenticated()); // true

Database Operations

Find Records

// Find all records
const users = await client.findAll('users', {
  status: 'active'
});

// Find one record
const user = await client.findOne('users', {
  id: 123
});

Create Records

const newUser = await client.create('users', {
  email: 'user@example.com',
  name: 'John Doe',
  status: 'active'
});

Update Records

const updateResult = await client.update('users', 
  { status: 'inactive' },  // Data to update
  { id: 123 }             // Conditions
);

Delete Records

const deleteResult = await client.delete('users', {
  id: 123
});

API Reference

Constructor

new KodieClient(config)

Configuration options:

  • projectId (required): Your project ID
  • apiKey (optional): API key for backend service authentication
  • authToken (optional): JWT token for frontend authentication
  • baseURL (optional): API endpoint URL (defaults to your project's API endpoint)

Authentication Methods

register(data)

Registers a new user.

  • data.email: User's email
  • data.password: User's password
  • data.firstName: First name
  • data.lastName: Last name
  • Returns: Promise<{ success: boolean, data: { token: string } }>

login(credentials)

Authenticates a user.

  • credentials.email: User's email
  • credentials.password: User's password
  • Returns: Promise<{ success: boolean, data: { token: string } }>

logout()

Clears the authentication token.

isAuthenticated()

Checks if the client has a valid auth token.

  • Returns: boolean

getAuthToken()

Gets the current auth token.

  • Returns: string | null

Database Methods

findAll(table, conditions = {})

Finds all records matching the conditions.

  • table: Table/collection name
  • conditions: Object containing field-value pairs
  • Returns: Promise<{ success: boolean, data: any[] }>

findOne(table, conditions = {})

Finds a single record matching the conditions.

  • table: Table/collection name
  • conditions: Object containing field-value pairs
  • Returns: Promise<{ success: boolean, data: any }>

create(table, data)

Creates a new record.

  • table: Table/collection name
  • data: Object containing the record data
  • Returns: Promise<{ success: boolean, data: any }>

update(table, data, conditions)

Updates records matching the conditions.

  • table: Table/collection name
  • data: Object containing fields to update
  • conditions: Object containing field-value pairs
  • Returns: Promise<{ success: boolean }>

delete(table, conditions)

Deletes records matching the conditions.

  • table: Table/collection name
  • conditions: Object containing field-value pairs
  • Returns: Promise<{ success: boolean }>

Response Format

All methods return standardized responses:

{
  success: boolean,
  data?: any,
  error?: string
}

Error Handling

try {
  const result = await client.findOne('users', { id: 123 });
  if (!result.success) {
    console.error('Operation failed:', result.error);
    return;
  }
  console.log('User found:', result.data);
} catch (error) {
  console.error('API Error:', error.message);
}

License

MIT

1.0.12

6 months ago

1.0.11

6 months ago

1.0.10

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago