@ 细雨适应器/ sqlite- core
Drizzle适配器生态系统共享SQLite功能.
概览
这个 @drizzle-adapter/sqlite-core 包提供了SQLite兼容适配器在Drizzle适配器生态系统中使用的共享SQLite功能. 此软件包是一个内部依赖:
目的
这个软件包将不同SQLite兼容适配器的SQLite特定功能标准化:
- 常见的 SQLite 数据类型定义
- 共享 SQLite 查询构建逻辑
- 统一 SQLite 错误处理
- 通用 SQLite 工具函数
安装
# This package is typically not installed directly but as a dependency of SQLite adapters
pnpm install @drizzle-adapter/sqlite-core
适用于适配器开发者
如果您正在开发用于 Drizzle 适应器生态系统的新的 SQLite 兼容适配器, 您应该使用此包来确保与其他 SQLite 适配器的一致性 :
import { SQLiteDrizzleDataTypes } from '@drizzle-adapter/sqlite-core';
export class YourSQLiteAdapter implements DrizzleAdapterInterface {
public getDataTypes(): SQLiteDrizzleDataTypes {
return new SQLiteDrizzleDataTypes();
}
// ... rest of your adapter implementation
}
数据类型
该包提供了标准化的SQLite数据类型定义:
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
});
处理错误
软件包为 SQLite 特定错误提供统一错误处理:
import { handleSQLiteError } from '@drizzle-adapter/sqlite-core';
try {
// Your database operation
} catch (error) {
throw handleSQLiteError(error);
}
查询大楼
该套件包括用于构建 SQLite 特定查询的公用事业:
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']
});
类型映射
软件包处理 SQLite 特定类型映射 :
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- 特定特性
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);
日期/时间
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 });
捐款
这个软件包作为Drizzle适配器生态系统的一部分得以维护. 如果你愿意贡献:
- 叉子存储器
- 创建您的特性分支( E)
git checkout -b feature/amazing-feature(中文(简体) ) - 提交更改( E)
git commit -m 'Add some amazing feature'(中文(简体) ) - 推向分行(
git push origin feature/amazing-feature(中文(简体) ) - 打开拉请求
相关软件包
- @ 细雨适应器/ 核心 - 核心接口和类型
- @ rizzle- adapter/ libsql (小雨) - 使用 libsql 的 SQLite 适配器
- @ 细雨适应器/ d1 - Cloudflare D1适配器