Skip to content

Commit

Permalink
update: improve IO package
Browse files Browse the repository at this point in the history
- improve types
- update fragments
- update query builder utilities
  • Loading branch information
calebpitan committed Nov 6, 2024
1 parent ca078df commit 261e62a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
10 changes: 7 additions & 3 deletions packages/io/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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<typeof schema>
mapper: SQLJsDatabase<schema.Schema>
/**
* A method to use to export the database file from memory as a
* `Uint8Array` representation in memory.
Expand Down Expand Up @@ -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<schema.Schema>(sqlite, {
casing: 'snake_case',
schema: schema,
logger: config.log
})

const repo: StitchesIORepos = {
tasks: new TasksRepository(mapper),
Expand Down
19 changes: 6 additions & 13 deletions packages/io/src/repositories/utils.ts
Original file line number Diff line number Diff line change
@@ -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<TableConfig, { columns: { deletedAt: SQLiteColumn } }>
>
type Query = AnySQLiteSelect | Omit<AnySQLiteSelect, 'where'>
type DynamicQuery<Q extends Query> = ReturnType<Q['$dynamic']>

export function withUnredacted<Q extends Query, T extends Table>(q: Q, t: T): DynamicQuery<Q> {
return q.$dynamic().where(isNull(t.deletedAt)) as ReturnType<Q['$dynamic']>
export function withUnredacted<T extends Table>(t: T, filters: SQLWrapper[]) {
return [...filters, isNull(t.deletedAt)]
}

export function withRedacted<Q extends Query, T extends Table>(q: Q, t: T): DynamicQuery<Q> {
return q.$dynamic().where(isNotNull(t.deletedAt)) as DynamicQuery<Q>
export function withRedacted<T extends Table>(t: T, filters: SQLWrapper[]) {
return [...filters, isNotNull(t.deletedAt)]
}
1 change: 1 addition & 0 deletions packages/io/src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './tags-to-tasks.schema'
export * from './tags.schema'
export * from './tasks.schema'
export * from './type'
7 changes: 7 additions & 0 deletions packages/io/src/schema/type.ts
Original file line number Diff line number Diff line change
@@ -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 {}
2 changes: 1 addition & 1 deletion packages/io/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))`
}

0 comments on commit 261e62a

Please sign in to comment.