Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate actual name for database on create and store on meta doc #42 #43

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 41 additions & 38 deletions adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,32 @@ export function adapter({
// DB DNE, so create it
() =>
/**
* Add a document, then immediately remove it
* so that the empty database and collection is implcitly created
* Create metadata doc for db
*/
Async.of($database(name)).chain(($db) =>
$db
.insertOne({
_id: 'info',
type: '_ADMIN',
created: new Date().toISOString(),
})
.chain(() => $db.removeOne({ _id: 'info' }))
/**
* Make sure to persist metadata on the db
*/
.chain(() => meta.create(name))
.bimap(
mongoErrToHyperErr({
subject: `database ${name}`,
db: `database ${name}`,
}),
always({ ok: true }),
)
),
meta.create(name).chain((metaDoc) => {
/**
* Add a document, then immediately remove it
* so that the empty database and collection are implcitly created
*/
return Async.of(
$database((metaDoc as { name: string }).name),
).chain(($db) =>
$db
.insertOne({
_id: 'info',
type: '_ADMIN',
created: new Date().toISOString(),
})
.chain(() => $db.removeOne({ _id: 'info' }))
.bimap(
mongoErrToHyperErr({
subject: `database ${name}`,
db: `database ${name}`,
}),
always({ ok: true }),
)
)
}),
// Already exists, so return a HyperErr(409)
() =>
Async.Rejected(
Expand All @@ -124,7 +127,7 @@ export function adapter({
function removeDatabase(name: string) {
return meta
.get(name)
.chain(() => $database(name).dropDatabase())
.chain(({ name: actualName }) => $database(actualName).dropDatabase())
.chain(() => meta.remove(name))
.bimap(
mongoErrToHyperErr({
Expand All @@ -151,11 +154,11 @@ export function adapter({
}) {
return meta
.get(db)
.chain(() =>
.chain(({ name: actualName }) =>
isEmpty(doc)
? Async.Rejected(HyperErr({ status: 400, msg: 'document empty' }))
: Async.of({ ...doc, _id: id }).chain((docWithId) =>
$database(db)
$database(actualName)
.insertOne(docWithId)
.bimap(
mongoErrToHyperErr({
Expand All @@ -176,7 +179,7 @@ export function adapter({
function retrieveDocument({ db, id }: { db: string; id: string }) {
return meta
.get(db)
.chain(() => $database(db).findOne({ _id: id }))
.chain(({ name: actualName }) => $database(actualName).findOne({ _id: id }))
.chain((doc) =>
doc ? Async.Resolved(doc) : Async.Rejected(
HyperErr({
Expand Down Expand Up @@ -211,8 +214,8 @@ export function adapter({
}) {
return meta
.get(db)
.chain(() =>
$database(db)
.chain(({ name: actualName }) =>
$database(actualName)
.replaceOne({ _id: id }, doc, { upsert: true })
.chain(({ matchedCount, modifiedCount }) =>
matchedCount + modifiedCount === 2 ? Async.Resolved({ ok: true, id }) : Async.Rejected(
Expand Down Expand Up @@ -240,8 +243,8 @@ export function adapter({
function removeDocument({ db, id }: { db: string; id: string }) {
return meta
.get(db)
.chain(() =>
$database(db)
.chain(({ name: actualName }) =>
$database(actualName)
.removeOne({ _id: id })
.chain(({ deletedCount }) =>
deletedCount === 1 ? Async.Resolved({ ok: true, id }) : Async.Rejected(
Expand Down Expand Up @@ -270,8 +273,8 @@ export function adapter({
function queryDocuments({ db, query }: { db: string; query: any }) {
return meta
.get(db)
.chain(() =>
$database(db)
.chain(({ name: actualName }) =>
$database(actualName)
.find(query.selector, {
...queryOptions(query),
})
Expand All @@ -295,8 +298,8 @@ export function adapter({
}) {
return meta
.get(db)
.chain(() =>
$database(db)
.chain(({ name: actualName }) =>
$database(actualName)
.createIndex({ name, spec: mapSort(fields) })
.bimap(
mongoErrToHyperErr({
Expand Down Expand Up @@ -346,8 +349,8 @@ export function adapter({

return meta
.get(db)
.chain(() =>
$database(db)
.chain(({ name: actualName }) =>
$database(actualName)
.find(q, { ...options })
.bimap(
mongoErrToHyperErr({
Expand All @@ -373,8 +376,8 @@ export function adapter({
}) {
return meta
.get(db)
.chain(() =>
$database(db)
.chain(({ name: actualName }) =>
$database(actualName)
.bulk(toBulkOperations(docs))
.bimap(
mongoErrToHyperErr({
Expand Down
15 changes: 10 additions & 5 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
export { default as crocks } from 'https://cdn.skypack.dev/[email protected]'
export * as R from 'https://cdn.skypack.dev/ramda@^0.29.0?dts'

export { EJSON } from 'npm:bson'
export { type Collection, MongoClient } from 'npm:mongodb'
export { EJSON } from 'npm:[email protected]'
export { type Collection, MongoClient } from 'npm:[email protected]'
export { default as cuid } from 'npm:[email protected]'

export {
HyperErr,
Expand Down
11 changes: 7 additions & 4 deletions meta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { crocks, HyperErr } from './deps.ts'
import { crocks, cuid, HyperErr } from './deps.ts'

import type { MongoInstanceClient } from './clients/types.ts'

Expand Down Expand Up @@ -58,7 +58,9 @@ export class MetaDb {
.chain(
Async.fromPromise((collection) => collection.findOne({ _id: name }, {})),
)
.chain((doc) =>
.chain<ReturnType<typeof HyperErr>, DatabaseMeta>((doc) =>
// deno-lint-ignore ban-ts-comment
// @ts-ignore
doc ? Async.Resolved(doc) : Async.Rejected(
HyperErr({ status: 404, msg: `database does not exist` }),
)
Expand All @@ -84,12 +86,13 @@ export class MetaDb {
Async.fromPromise((collection) => {
const doc = {
_id: name,
name,
// Use a cuid for the actual name of the database
name: cuid(),
type: 'database' as const,
createdAt: new Date().toISOString(),
}

return collection.insertOne(doc)
return collection.insertOne(doc).then(() => doc)
}),
),
// DB was found, so produce a conflict
Expand Down