cherry-studio/src/main/data/db/schemas/_columnHelpers.ts
fullex 190f7ba2e1 refactor(database-patterns): enhance schema guidelines and file organization
- Added new section on schema file organization, detailing principles and decision criteria for merging or separating table files.
- Updated file naming conventions for single and multi-table files, as well as helper utilities.
- Refactored import paths in various schema files to use the new `_columnHelpers` module instead of the deprecated `columnHelpers`.
- Removed obsolete `customSql.ts`, `columnHelpers.ts`, `messageFts.ts`, `tag.ts`, `entityTag.ts`, and other related files to streamline the codebase.

This refactor improves clarity in database schema management and aligns file organization with best practices.
2026-01-07 20:46:09 +08:00

45 lines
1.2 KiB
TypeScript

/**
* Column helper utilities for Drizzle schemas
*
* USAGE RULES:
* - DO NOT manually set id, createdAt, or updatedAt - they are auto-generated
* - Use .returning() to get inserted/updated rows instead of re-querying
* - See db/README.md for detailed field generation rules
*/
import { integer, text } from 'drizzle-orm/sqlite-core'
import { v4 as uuidv4, v7 as uuidv7 } from 'uuid'
/**
* UUID v4 primary key with auto-generation
* Use for general purpose tables
*/
export const uuidPrimaryKey = () =>
text()
.primaryKey()
.$defaultFn(() => uuidv4())
/**
* UUID v7 primary key with auto-generation (time-ordered)
* Use for tables with large datasets that benefit from sequential inserts
*/
export const uuidPrimaryKeyOrdered = () =>
text()
.primaryKey()
.$defaultFn(() => uuidv7())
const createTimestamp = () => {
return Date.now()
}
export const createUpdateTimestamps = {
createdAt: integer().$defaultFn(createTimestamp),
updatedAt: integer().$defaultFn(createTimestamp).$onUpdateFn(createTimestamp)
}
export const createUpdateDeleteTimestamps = {
createdAt: integer().$defaultFn(createTimestamp),
updatedAt: integer().$defaultFn(createTimestamp).$onUpdateFn(createTimestamp),
deletedAt: integer()
}