diff --git a/packages/io/src/lib.ts b/packages/io/src/lib.ts index 2c775b7..479a20d 100644 --- a/packages/io/src/lib.ts +++ b/packages/io/src/lib.ts @@ -23,7 +23,7 @@ export interface StitchesIOPort { /** * The database schema objects */ - schema: typeof schema + schema: schema.Schema /** * A repository of services for working with the database objects */ @@ -32,7 +32,7 @@ export interface StitchesIOPort { * A direct access to the Object Relational Mapper (ORM) for more * advanced control when the repository doesn't suffice */ - mapper: SQLJsDatabase + mapper: SQLJsDatabase /** * A method to use to export the database file from memory as a * `Uint8Array` representation in memory. @@ -87,7 +87,11 @@ export async function open( const SQLite = await initSqlJs(cfg) const sqlite = new SQLite.Database(database) - const mapper = drizzle(sqlite, { casing: 'snake_case', schema, logger: config.log }) + const mapper = drizzle(sqlite, { + casing: 'snake_case', + schema: schema, + logger: config.log + }) const repo: StitchesIORepos = { tasks: new TasksRepository(mapper), diff --git a/packages/io/src/repositories/utils.ts b/packages/io/src/repositories/utils.ts index 8414697..ee662d6 100644 --- a/packages/io/src/repositories/utils.ts +++ b/packages/io/src/repositories/utils.ts @@ -1,21 +1,14 @@ -import { type UpdateTableConfig, isNotNull, isNull } from 'drizzle-orm' -import type { - AnySQLiteSelect, - SQLiteColumn, - SQLiteTableWithColumns, - TableConfig -} from 'drizzle-orm/sqlite-core' +import { SQLWrapper, type UpdateTableConfig, isNotNull, isNull } from 'drizzle-orm' +import type { SQLiteColumn, SQLiteTableWithColumns, TableConfig } from 'drizzle-orm/sqlite-core' type Table = SQLiteTableWithColumns< UpdateTableConfig > -type Query = AnySQLiteSelect | Omit -type DynamicQuery = ReturnType -export function withUnredacted(q: Q, t: T): DynamicQuery { - return q.$dynamic().where(isNull(t.deletedAt)) as ReturnType +export function withUnredacted(t: T, filters: SQLWrapper[]) { + return [...filters, isNull(t.deletedAt)] } -export function withRedacted(q: Q, t: T): DynamicQuery { - return q.$dynamic().where(isNotNull(t.deletedAt)) as DynamicQuery +export function withRedacted(t: T, filters: SQLWrapper[]) { + return [...filters, isNotNull(t.deletedAt)] } diff --git a/packages/io/src/schema/index.ts b/packages/io/src/schema/index.ts index f731b3d..8d45131 100644 --- a/packages/io/src/schema/index.ts +++ b/packages/io/src/schema/index.ts @@ -1,3 +1,4 @@ export * from './tags-to-tasks.schema' export * from './tags.schema' export * from './tasks.schema' +export * from './type' diff --git a/packages/io/src/schema/type.ts b/packages/io/src/schema/type.ts new file mode 100644 index 0000000..7e3c728 --- /dev/null +++ b/packages/io/src/schema/type.ts @@ -0,0 +1,7 @@ +import * as schema from './index' + +type TSchema = typeof schema + +// type StitchesSchema = { [P in keyof Schema]: Schema[P] } + +export interface Schema extends TSchema {} diff --git a/packages/io/src/utils.ts b/packages/io/src/utils.ts index b86f6c4..c3a5203 100644 --- a/packages/io/src/utils.ts +++ b/packages/io/src/utils.ts @@ -4,5 +4,5 @@ import { monotonicFactory } from 'ulidx' export const ulid = monotonicFactory() export const fragments = { - now: sql`((strftime('%s', 'now') * 1000) + (strftime('%f', 'now') % 1 * 1000))` + now: sql`(strftime('%s', 'now') || substr(strftime('%f', 'now'), -3))` }