Skip to content

Commit 5ee1fde

Browse files
committed
✨ lazily init db
lazily init db ✨ New feature
1 parent e42312b commit 5ee1fde

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

__tests__/GistDatabase.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ for (const compressionType of Object.values(CompressionType)) {
1010
token: process.env.GIST_TOKEN,
1111
compression: compressionType
1212
})
13-
await db.init()
1413
})
1514
afterAll(async () => {
1615
await db.destroy()
@@ -68,7 +67,7 @@ for (const compressionType of Object.values(CompressionType)) {
6867
)) as GistResponse
6968

7069
expect(found).toEqual({})
71-
}, 30000)
70+
})
7271
it('gets and deletes many', async () => {
7372
await db.set('test_many_one', {})
7473
await db.set('test_many_two', {})
@@ -83,7 +82,7 @@ for (const compressionType of Object.values(CompressionType)) {
8382
undefined,
8483
undefined
8584
])
86-
}, 30000)
85+
})
8786
})
8887
}
8988

jest.config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
"useESM": true
1414
}
1515
]
16-
}
16+
},
17+
"testTimeout": 60000
1718
}

src/GistDatabase.ts

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class GistDatabase {
5555
public static MAX_FILE_SIZE_BYTES = 999999 // 0.99mb
5656
public static MAX_FILES_PER_GIST = 10
5757
public isNewDatabase: boolean
58+
public initialized: boolean = false
5859

5960
constructor(options: GistDatabaseOptions) {
6061
this.options = {
@@ -98,10 +99,18 @@ export class GistDatabase {
9899
this.isNewDatabase = false
99100
this.options.id = gist.id
100101
}
102+
this.initialized = true
101103
return gist
102104
}
103105

106+
private async initIfNeeded() {
107+
if (!this.initialized) {
108+
await this.init()
109+
}
110+
}
111+
104112
public async getRoot(): Promise<GistResponse> {
113+
await this.initIfNeeded()
105114
return (await this.gistApi(
106115
`/gists/${this.options.id}`,
107116
'GET'

src/cli.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const main = async () => {
2121

2222
console.log({
2323
id: res.id,
24-
url: res.url,
24+
rawUrl: res.url,
25+
url: `https://gist.github.com/${res.id}`,
2526
public: isPublic,
2627
description: process.argv[3]
2728
})

0 commit comments

Comments
 (0)