-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
4dc613c
commit 5f27f6b
Showing
15 changed files
with
712 additions
and
134 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
.../migrations/0000_tense_young_avengers.sql → ...db/migrations/0000_amusing_gabe_jones.sql
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,64 @@ | ||
import { eq } from 'drizzle-orm'; | ||
import { createDoc, getDoc, updateDoc } from './doc'; | ||
import { docsTable } from '~/db/schema'; | ||
import { db } from '~/db'; | ||
import { v4 as uuid } from 'uuid'; | ||
import { ContentBlock } from '~/types/content'; | ||
|
||
describe('createDoc', () => { | ||
it('creates a doc', async () => { | ||
const doc = await createDoc(); | ||
const response = db.query.docsTable.findFirst({ | ||
where: eq(docsTable.id, doc.id), | ||
}); | ||
|
||
expect(response).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('getDoc', () => { | ||
it('returns doc if exists', async () => { | ||
const expectedDoc = await db.insert(docsTable).values({}).returning(); | ||
const doc = await getDoc(expectedDoc[0].id); | ||
|
||
expect(doc).toBeDefined(); | ||
}); | ||
|
||
it('returns null if uuid invalid', async () => { | ||
const doc = await getDoc('abc'); | ||
|
||
expect(doc).toBe(null); | ||
}); | ||
|
||
it('returns null if not exists', async () => { | ||
const doc = await getDoc(uuid()); | ||
|
||
expect(doc).toBe(null); | ||
}); | ||
}); | ||
|
||
describe('updateDoc', () => { | ||
it('updates the doc', async () => { | ||
const content: ContentBlock[] = [ | ||
{ | ||
type: 'paragraph', | ||
nodes: [ | ||
{ | ||
type: 'text', | ||
value: 'Hello, world!', | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const doc = await db.insert(docsTable).values({}).returning(); | ||
|
||
await updateDoc(doc[0].id, content); | ||
|
||
const updatedDoc = await db.query.docsTable.findFirst({ | ||
where: eq(docsTable.id, doc[0].id), | ||
}); | ||
|
||
expect(updatedDoc?.content).toStrictEqual(content); | ||
}); | ||
}); |
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,52 @@ | ||
import { eq } from 'drizzle-orm'; | ||
import { validate as isValidUuid } from 'uuid'; | ||
import { db } from '~/db'; | ||
import { docsTable } from '~/db/schema'; | ||
import { ContentBlock } from '~/types/content'; | ||
|
||
export interface Doc { | ||
id: string; | ||
content: ContentBlock[]; | ||
createdAt: Date; | ||
updatedAt: Date; | ||
} | ||
|
||
export async function createDoc(): Promise<Doc> { | ||
const response = await db.insert(docsTable).values({}).returning(); | ||
return formatDoc(response[0]); | ||
} | ||
|
||
export async function getDoc(id: string): Promise<Doc | null> { | ||
if (!isValidUuid(id)) { | ||
return null; | ||
} | ||
|
||
const doc = await db.query.docsTable.findFirst({ | ||
where: eq(docsTable.id, id), | ||
}); | ||
|
||
return doc ? formatDoc(doc) : null; | ||
} | ||
|
||
export async function updateDoc( | ||
id: string, | ||
content: ContentBlock[] | ||
): Promise<Doc> { | ||
const response = await db | ||
.update(docsTable) | ||
.set({ | ||
content, | ||
updatedAt: new Date(), | ||
}) | ||
.where(eq(docsTable.id, id)) | ||
.returning(); | ||
|
||
return formatDoc(response[0]); | ||
} | ||
|
||
function formatDoc(doc: typeof docsTable.$inferSelect): Doc { | ||
return { | ||
...doc, | ||
content: doc.content as ContentBlock[], | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { redirect } from '@remix-run/node'; | ||
import { createDocument } from '~/repos/document'; | ||
import { createDoc } from '~/repos/doc'; | ||
|
||
export async function loader() { | ||
const document = await createDocument(); | ||
return redirect(`/${document.id}`); | ||
const doc = await createDoc(); | ||
return redirect(`/${doc.id}`); | ||
} |
Oops, something went wrong.