Skip to content

Commit

Permalink
🎨 ✨ change signature of set
Browse files Browse the repository at this point in the history
change signature of set

🎨 Improve format/structure, ✨ New feature
  • Loading branch information
TimMikeladze committed Jan 1, 2023
1 parent 548a5de commit 1653f48
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Turn [gist](https://gist.github.com/) into your personal key/value data-store.

- 🚀 [Example repo]() of a todo list build with Next.js.
- 🚀 [Example repo](https://github.com/TimMikeladze/gist-database-nextjs) of a todo list build with Next.js.
- 🔗 [Demo app]() deployed with Vercel.

```console
Expand Down Expand Up @@ -67,7 +67,9 @@ const db = new GistDatabase({
})

const res = await db.set('key', {
hello: 'world'
value: {
hello: 'world'
}
})

const found = await db.get('key')
Expand All @@ -83,8 +85,10 @@ const found = await db.get('key')
**/

const updated = await db.set('key', {
hello: 'world',
foo: 'bar'
value: {
hello: 'world',
foo: 'bar'
}
})

/**
Expand All @@ -102,13 +106,10 @@ await db.has('key') // true

await db.delete('key') // void

await db.set(
'key_with_ttl',
{
description: "I'll expire soon and be deleted upon retrieval"
},
1000
)
await db.set('key_with_ttl', {
ttl: 1000, // 1 second
description: "I'll expire soon and be deleted upon retrieval"
})

// Get or delete many keys at once. `undefined` will be returned for keys that don't exist.

Expand Down
19 changes: 10 additions & 9 deletions __tests__/GistDatabase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ describe('GistDatabase', () => {
})
it('sets and gets', async () => {
const res = await db.set('test_one', {
name: 'test_one'
value: {
name: 'test_one'
}
})
expect(res).toMatchObject({
value: {
Expand All @@ -34,7 +36,9 @@ describe('GistDatabase', () => {
})
it('deletes', async () => {
await db.set('test_two', {
name: 'test_two'
value: {
name: 'test_two'
}
})
expect(await db.get('test_two')).toEqual({
value: {
Expand All @@ -47,13 +51,10 @@ describe('GistDatabase', () => {
expect(await db.get('test_two')).toBeUndefined()
})
it('key with a ttl gets deleted', async () => {
const res = await db.set(
'test_ttl',
{
name: 'test_ttl'
},
250
)
const res = await db.set('test_ttl', {
value: {},
ttl: 250
})

await sleep(500)

Expand Down
11 changes: 7 additions & 4 deletions src/GistDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,13 @@ export class GistDatabase {

public async set<T = any>(
key: string | string[],
value: T,
ttl?: number,
description?: string
args: {
description?: string
ttl?: number
value?: T
}
): Promise<Doc<T>> {
const { description, ttl, value = {} } = args
if (!isPlainObject(value)) {
throw new Error('value must be a plain javascript object')
}
Expand Down Expand Up @@ -244,7 +247,7 @@ export class GistDatabase {
}

return {
value,
value: value as T,
gist,
id: gist.id
}
Expand Down

0 comments on commit 1653f48

Please sign in to comment.