Skip to content

Commit 3a029fc

Browse files
authored
Document KV stores (#559)
1 parent a1c4c1f commit 3a029fc

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

packages/file-store/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,17 @@ The directory to store the files on disk (`string`).
5353

5454
#### `options.configstore`
5555

56-
Provide your own storage solution for the metadata of uploads (`Class`).
56+
Provide your own storage solution for the metadata of uploads ([`KvStore`][]).
5757

58-
Default uses [`configstore`](https://www.npmjs.com/package/configstore).
58+
Default uses `FileKvStore` which puts the metadata file next to the uploaded file.
59+
See the exported [KV stores][kvstores] from `@tus/server` for more information.
5960

6061
#### `options.expirationPeriodInMilliseconds`
6162

6263
The time before an _ongoing_ upload is considered expired (`number`).
6364

6465
This is since the time of creation, not modification. Once an upload is considered expired,
65-
uploads can be removed with [`cleanUpExpiredUploads`](https://github.com/tus/tus-node-server/tree/main/packages/server#)#servercleanupexpireduploads
66+
uploads can be removed with [`cleanUpExpiredUploads`][].
6667

6768
## Extensions
6869

@@ -102,7 +103,7 @@ export class MemoryConfigstore {
102103
return this.data.delete(key)
103104
}
104105

105-
get all(): Record<string, Upload> {
106+
get list(): Record<string, Upload> {
106107
return Object.fromEntries(this.data.entries())
107108
}
108109
}
@@ -139,3 +140,6 @@ See [`contributing.md`](https://github.com/tus/tus-node-server/blob/main/.github
139140
[checksum]: https://tus.io/protocols/resumable-upload.html#checksum
140141
[termination]: https://tus.io/protocols/resumable-upload.html#termination
141142
[concatenation]: https://tus.io/protocols/resumable-upload.html#concatenation
143+
[`cleanUpExpiredUploads`]: https://github.com/tus/tus-node-server/tree/main/packages/server#cleanupexpireduploads
144+
[kvstores]: https://github.com/tus/tus-node-server/tree/main/packages/server#kvstores
145+
[`KvStore`]: https://github.com/tus/tus-node-server/blob/main/packages/server/src/kvstores/Types.ts

packages/s3-store/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ Options to pass to the AWS S3 SDK.
7171
Checkout the [`S3ClientConfig`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/s3clientconfig.html)
7272
docs for the supported options. You need to at least set the `region`, `bucket` name, and your preferred method of authentication.
7373

74-
7574
#### `options.expirationPeriodInMilliseconds`
7675

7776
Enables the expiration extension and sets the expiration period of an upload url in milliseconds.
@@ -84,8 +83,13 @@ If you are using certain features like the expiration extension and your provide
8483

8584
#### `options.cache`
8685

87-
An optional cache implementation. If not provided, the store will use an in-memory cache (`MemoryConfigStore`).
88-
When running multiple instances of the server, you need to provide a cache implementation that is shared between all instances like the `RedisConfigStore`.
86+
An optional cache implementation ([`KvStore`][]).
87+
88+
Default uses an in-memory cache (`MemoryKvStore`).
89+
When running multiple instances of the server,
90+
you need to provide a cache implementation that is shared between all instances like the `RedisKvStore`.
91+
92+
See the exported [KV stores][kvstores] from `@tus/server` for more information.
8993

9094
## Extensions
9195

@@ -181,3 +185,5 @@ See [`contributing.md`](https://github.com/tus/tus-node-server/blob/main/.github
181185
[concatenation]: https://tus.io/protocols/resumable-upload.html#concatenation
182186
[cleanExpiredUploads]: https://github.com/tus/tus-node-server/tree/main/packages/server#servercleanupexpireduploads
183187
[lifecyle]: https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lifecycle-mgmt.html
188+
[kvstores]: https://github.com/tus/tus-node-server/tree/main/packages/server#kvstores
189+
[`KvStore`]: https://github.com/tus/tus-node-server/blob/main/packages/server/src/kvstores/Types.ts

packages/s3-store/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Options = {
3131
s3ClientConfig: S3ClientConfig & {bucket: string}
3232
}
3333

34-
type MetadataValue = {
34+
export type MetadataValue = {
3535
file: Upload
3636
'upload-id': string
3737
'tus-version': string

packages/server/README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ server.listen({host, port})
5050

5151
## API
5252

53-
This package exports `Server` and all [`constants`][], [`types`][], and [`models`][]. There is no default export.
54-
You should only need the `Server` and `EVENTS` exports.
53+
This package exports `Server` and all [`constants`][], [`types`][], [`models`][], and [`kvstores`][]. There is no default export.
54+
You should only need the `Server`, `EVENTS`, and KV store exports.
5555

5656
### `new Server(options)`
5757

@@ -229,6 +229,57 @@ const {EVENTS} = require('@tus/server')
229229
server.on(EVENTS.POST_TERMINATE, (req, res, id => {})
230230
```
231231
232+
### Key-Value Stores
233+
234+
All stores (as in the `datastore` option) save two files,
235+
the uploaded file and an info file with metadata, usually adjacent to each other.
236+
237+
In `@tus/file-store` the `FileKvStore` is used to persist upload info but the KV stores
238+
can also be used as a cache in other stores, such as `@tus/s3-store`.
239+
240+
#### `MemoryKvStore`
241+
242+
```ts
243+
import {MemoryKvStore} from '@tus/server'
244+
import S3Store, {type MetadataValue} from '@tus/s3-store'
245+
246+
new S3Store({
247+
// ...
248+
cache: new MemoryKvStore<MetadataValue>(),
249+
})
250+
```
251+
252+
#### `FileKvStore`
253+
254+
```ts
255+
import {FileKvStore} from '@tus/server'
256+
import S3Store, {type MetadataValue} from '@tus/s3-store'
257+
258+
const path = './uploads'
259+
260+
new S3Store({
261+
// ...
262+
cache: new FileKvStore<MetadataValue>(path),
263+
})
264+
```
265+
266+
#### `RedisKvStore`
267+
268+
```ts
269+
import {RedisKvStore} from '@tus/server'
270+
import S3Store, {type MetadataValue} from '@tus/s3-store'
271+
import {createClient} from '@redis/client'
272+
273+
const client = await createClient().connect()
274+
const path = './uploads'
275+
const prefix = 'foo' // prefix for the key (foo${id})
276+
277+
new S3Store({
278+
// ...
279+
cache: new RedisKvStore<MetadataValue>(client, prefix),
280+
})
281+
```
282+
232283
## Examples
233284
234285
### Example: integrate tus into Express
@@ -469,4 +520,5 @@ See [`contributing.md`](https://github.com/tus/tus-node-server/blob/main/.github
469520
[`constants`]: https://github.com/tus/tus-node-server/blob/main/packages/server/src/constants.ts
470521
[`types`]: https://github.com/tus/tus-node-server/blob/main/packages/server/src/types.ts
471522
[`models`]: https://github.com/tus/tus-node-server/blob/main/packages/server/src/models/index.ts
523+
[`kvstores`]: https://github.com/tus/tus-node-server/blob/main/packages/server/src/kvstores/index.ts
472524
[expiration]: https://tus.io/protocols/resumable-upload.html#expiration

0 commit comments

Comments
 (0)