1.0.8 • Published 6 months ago

@drizzle-adapter/sqlite-core v1.0.8

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

@drizzle-adapter/sqlite-core

Shared SQLite functionality for the Drizzle Adapter ecosystem.

Overview

The @drizzle-adapter/sqlite-core package provides shared SQLite functionality used by SQLite-compatible adapters in the Drizzle Adapter ecosystem. This package is an internal dependency used by:

Purpose

This package standardizes SQLite-specific functionality across different SQLite-compatible adapters:

  • Common SQLite data type definitions
  • Shared SQLite query building logic
  • Unified SQLite error handling
  • Common SQLite utility functions

Installation

# This package is typically not installed directly but as a dependency of SQLite adapters
pnpm install @drizzle-adapter/sqlite-core

For Adapter Developers

If you're developing a new SQLite-compatible adapter for the Drizzle Adapter ecosystem, you should use this package to ensure consistency with other SQLite adapters:

import { SQLiteDrizzleDataTypes } from '@drizzle-adapter/sqlite-core';

export class YourSQLiteAdapter implements DrizzleAdapterInterface {
  public getDataTypes(): SQLiteDrizzleDataTypes {
    return new SQLiteDrizzleDataTypes();
  }
  
  // ... rest of your adapter implementation
}

Data Types

The package provides standardized SQLite data type definitions:

const dataTypes = new SQLiteDrizzleDataTypes();

const table = dataTypes.dbTable('example', {
  // Numeric types
  id: dataTypes.dbInteger('id').primaryKey().autoincrement(),
  count: dataTypes.dbInteger('count'),
  amount: dataTypes.dbReal('amount'),
  
  // String types
  name: dataTypes.dbText('name'),
  description: dataTypes.dbText('description'),
  
  // Date/Time types (stored as INTEGER or TEXT)
  createdAt: dataTypes.dbInteger('created_at')
    .default(sql`(strftime('%s', 'now'))`),
  updatedAt: dataTypes.dbText('updated_at')
    .default(sql`CURRENT_TIMESTAMP`),
  
  // SQLite-specific handling
  json: dataTypes.dbText('json'), // JSON stored as TEXT
  isActive: dataTypes.dbInteger('is_active'), // Boolean as INTEGER
  blob: dataTypes.dbBlob('data') // Binary data
});

Error Handling

The package provides unified error handling for SQLite-specific errors:

import { handleSQLiteError } from '@drizzle-adapter/sqlite-core';

try {
  // Your database operation
} catch (error) {
  throw handleSQLiteError(error);
}

Query Building

The package includes utilities for building SQLite-specific queries:

import { 
  buildInsertQuery,
  buildUpdateQuery,
  buildDeleteQuery,
  buildSelectQuery,
  buildFTSQuery
} from '@drizzle-adapter/sqlite-core';

// Build INSERT query
const insertQuery = buildInsertQuery(table, data);

// Build UPDATE query with JSON operations
const updateQuery = buildUpdateQuery(table, {
  json: sql`json_set(json, '$.key', 'value')`
});

// Build DELETE query with conditions
const deleteQuery = buildDeleteQuery(table, conditions);

// Build SELECT query with joins
const selectQuery = buildSelectQuery({
  table,
  joins: [...],
  conditions: [...],
  orderBy: [...]
});

// Build full-text search query
const searchQuery = buildFTSQuery({
  table: 'posts_fts',
  searchTerm: 'search term',
  columns: ['title', 'content']
});

Type Mapping

The package handles SQLite-specific type mapping:

import { mapSQLiteType } from '@drizzle-adapter/sqlite-core';

// Map JavaScript types to SQLite types
const sqliteType = mapSQLiteType(value);

// Map SQLite types to JavaScript types
const jsValue = mapToJavaScript(sqliteValue, sqliteType);

// Handle JSON serialization
const jsonText = serializeJSON(value);
const jsonValue = deserializeJSON(text);

// Handle date/time
const timestamp = dateToUnixTimestamp(date);
const date = unixTimestampToDate(timestamp);

SQLite-Specific Features

import { 
  createFTSTable,
  createFTSIndex,
  handleJSONOperations,
  handleDateTimeOperations
} from '@drizzle-adapter/sqlite-core';

// Create FTS table
const ftsSQL = createFTSTable('posts_fts', ['title', 'content']);

// Create FTS index
const indexSQL = createFTSIndex('posts_fts_idx', 'posts', ['title', 'content']);

// Handle JSON operations
const jsonCondition = handleJSONOperations(column, path, value);

// Handle date/time operations
const dateCondition = handleDateTimeOperations(column, operator, value);

Date/Time Utilities

import { 
  getCurrentTimestamp,
  formatDateTime,
  parseDateTime,
  addInterval
} from '@drizzle-adapter/sqlite-core';

// Get current timestamp
const now = getCurrentTimestamp();

// Format date/time
const formatted = formatDateTime(date, format);

// Parse date/time
const date = parseDateTime(text, format);

// Add interval
const future = addInterval(date, { days: 7, hours: 12 });

Contributing

This package is maintained as part of the Drizzle Adapter ecosystem. If you'd like to contribute:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Related Packages

1.0.8

6 months ago

1.0.7

8 months ago

1.0.6

8 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.0

11 months ago