Skip to content

Commit

Permalink
useMutate
Browse files Browse the repository at this point in the history
  • Loading branch information
daveycodez committed Jan 31, 2025
1 parent 27a3a1f commit 544bde3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/hooks/create-query-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useDelete } from "./use-delete"
import { useFindFirst } from "./use-find-first"
import { useFindMany } from "./use-find-many"
import { useInsert } from "./use-insert"
import { useMutate } from "./use-mutate"
import { useUpdate } from "./use-update"

export function createQueryHooks<
Expand Down Expand Up @@ -63,6 +64,15 @@ export function createQueryHooks<
table?: TableName | null | false | "",
config?: TConfig | null,
context?: NeonQueryContextType | null
) => useDelete(db, table, config, context)
) => useDelete(db, table, config, context),
useMutate:
<
TableName extends keyof TSchema,
TConfig extends DBQueryConfig<"many", true, TSchema, TSchema[TableName]>
>(
table?: TableName | null | false | "",
config?: TConfig | null,
context?: NeonQueryContextType | null
) => useMutate(db, table, config, context)
}
}
27 changes: 27 additions & 0 deletions src/hooks/use-mutate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { DBQueryConfig, TablesRelationalConfig } from "drizzle-orm"
import type { PgDatabase, PgQueryResultHKT } from "drizzle-orm/pg-core"

import type { NeonQueryContextType } from "../lib/neon-query-provider"

import { useDelete } from "./use-delete"
import { useInsert } from "./use-insert"
import { useUpdate } from "./use-update"

export function useMutate<
TQueryResult extends PgQueryResultHKT,
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig,
TableName extends keyof TSchema,
TConfig extends DBQueryConfig<"many", true, TSchema, TSchema[TableName]>
>(
db: PgDatabase<TQueryResult, TFullSchema, TSchema>,
table?: TableName | null | false | "",
config?: TConfig | null,
context?: NeonQueryContextType | null
) {
const { update } = useUpdate(db, table, config, context)
const { delete: deleteRecord } = useDelete(db, table, config, context)
const { insert } = useInsert(db, table, config, context)

return { insert, update, delete: deleteRecord }
}

0 comments on commit 544bde3

Please sign in to comment.