Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shahradelahi committed Feb 6, 2024
1 parent 113b5b4 commit 345efa7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
5 changes: 3 additions & 2 deletions docs/examples/fs.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# File-based Storage (fs)

On this pages you can find examples of using the file-based storage. This feature is available only in Node.js and Bun environments.
On this page, you can find examples of using the file-based storage. This feature is available only in Node.js and Bun
environments.

## 1. Store a value

```typescript
import { Client } from '@litehex/storage-box';
import { FsDriver } from '@litehex/storage-box/driver/fs';
import { FsDriver } from '@litehex/storage-box/driver';
import { resovle } from 'path';

const filePath = resovle(process.cwd(), 'data.json');
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/memory.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Memory-based Storage

On this pages you can find examples of using the memory-based storage.
By default, the memory-based storage is used. On this page, you can find examples of using the memory-based storage.

## 1. Create a list of even number and expire each number after 5 seconds
## 1. Create a list of even numbers and expire each number after 5 seconds

```typescript
import { Client } from '@litehex/storage-box';
Expand Down
4 changes: 2 additions & 2 deletions src/driver/fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MemoryDrive from '@/driver/memory.ts';
import MemoryDriver from '@/driver/memory.ts';
import { JsonMap } from '@/parser';
import type { IStorageParser } from '@/typings.ts';
import debounce from 'debounce';
Expand All @@ -11,7 +11,7 @@ export interface FsOptions {
debounceTime?: number;
}

export default class FsDrive extends MemoryDrive {
export default class FsDriver extends MemoryDriver {
private readonly _path: string;
private readonly _parser: IStorageParser;
private readonly _debounceTime: number;
Expand Down
8 changes: 6 additions & 2 deletions src/driver/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export * from './memory.ts';
export * from './fs.ts';
export { default as MemoryDriver } from './memory.ts';

// -----------

export { default as FsDriver } from './fs.ts';
export type { FsOptions } from './fs.ts';
2 changes: 1 addition & 1 deletion src/driver/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { JsonMap } from '@/parser';
import type { IStorageDrive } from '@/typings.ts';
import type { JsonValue } from 'type-fest';

export default class MemoryDrive implements IStorageDrive {
export default class MemoryDriver implements IStorageDrive {
protected _storage: Map<string, JsonValue>;

constructor(storage?: Map<string, JsonValue>) {
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MemoryDrive from '@/driver/memory.ts';
import MemoryDriver from '@/driver/memory.ts';
import type { IStorageBox, IStorageDrive } from '@/typings.ts';
import type { JsonArray, JsonValue } from 'type-fest';

Expand All @@ -24,7 +24,7 @@ export class Client implements IStorageBox {
private _ttl: Map<string, TTL> = new Map();

constructor(storage?: IStorageDrive) {
this._drive = storage || new MemoryDrive();
this._drive = storage || new MemoryDriver();
this._load_ttl();
}

Expand Down Expand Up @@ -281,4 +281,10 @@ export class Client implements IStorageBox {
}
}

// -----------

export type * from '@/typings.ts';

// -----------

export { JsonMap } from '@/parser';
32 changes: 30 additions & 2 deletions tests/fs.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
import FsDrive from '@/driver/fs.ts';
import { FsDriver } from '@/driver';
import { Client } from '@/index.ts';
import { MSGPack } from '@/parser';
import { expect } from 'chai';
import fs from 'fs';
import { resolve } from 'path';

describe('fs-based storage', () => {
const filePath = resolve(process.cwd(), 'tests', 'test.json');

using drive = new FsDrive(filePath);
using drive = new FsDriver(filePath);
const client = new Client(drive);

beforeEach(() => {
client.clear();
});

after(() => {
fs.unlinkSync(filePath);
});

it('Set and get', () => {
client.set('foo', 'bar');
client.set('bar', 'baz');
expect(client.get('foo')).to.equal('bar');
});

it('Delete', () => {
client.set('foo', 'bar');
client.del('foo');
expect(client.get('foo')).to.be.null;
});
});

describe('fs-based storage - MSGPack', () => {
const filePath = resolve(process.cwd(), 'tests', 'test.pack');

using drive = new FsDriver(filePath, { parser: MSGPack });
const client = new Client(drive);

beforeEach(() => {
Expand Down

0 comments on commit 345efa7

Please sign in to comment.