-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import createNano from "nano" | ||
import { SafeMangoQuery, DocType, ModelOf } from "@oboku/shared" | ||
import { retryFn } from "./dbHelpers" | ||
|
||
type FindOneOptionsBase = { | ||
db: createNano.DocumentScope<unknown> | ||
} | ||
|
||
type FindOneOptionsWithThrow = FindOneOptionsBase & { | ||
throwOnNotFound: true | ||
} | ||
|
||
type FindOneOptionsWithoutThrow = FindOneOptionsBase & { | ||
throwOnNotFound?: false | ||
} | ||
|
||
type FindOneOptions = FindOneOptionsWithThrow | FindOneOptionsWithoutThrow | ||
|
||
export function findOne<M extends DocType["rx_model"], D extends ModelOf<M>>( | ||
rxModel: M, | ||
query: SafeMangoQuery<D>, | ||
options: FindOneOptionsWithThrow | ||
): Promise< | ||
D & { | ||
_id: string | ||
_rev: string | ||
} | ||
> | ||
|
||
export function findOne<M extends DocType["rx_model"], D extends ModelOf<M>>( | ||
rxModel: M, | ||
query: SafeMangoQuery<D>, | ||
options: FindOneOptionsWithoutThrow | ||
): Promise< | ||
| (D & { | ||
_id: string | ||
_rev: string | ||
}) | ||
| null | ||
> | ||
|
||
export async function findOne< | ||
M extends DocType["rx_model"], | ||
D extends ModelOf<M> | ||
>(rxModel: M, query: SafeMangoQuery<D>, options: FindOneOptions) { | ||
const { fields, ...restQuery } = query | ||
const fieldsWithRequiredFields = fields | ||
if (Array.isArray(fieldsWithRequiredFields)) { | ||
fieldsWithRequiredFields.push(`rx_model`) | ||
} | ||
|
||
const response = await retryFn(() => | ||
options.db.find({ | ||
...restQuery, | ||
fields: fields as string[], | ||
selector: { rx_model: rxModel, ...(query?.selector as any) }, | ||
limit: 1 | ||
}) | ||
) | ||
|
||
if (response.docs.length === 0) { | ||
if (options.throwOnNotFound) { | ||
throw new Error("Document not found") | ||
} | ||
return null | ||
} | ||
|
||
const doc = response | ||
.docs[0] as createNano.MangoResponse<unknown>["docs"][number] & D | ||
|
||
if (rxModel !== doc.rx_model) throw new Error(`Invalid document type`) | ||
|
||
return doc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters