Skip to content

Commit

Permalink
fix: sync methods (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahradelahi authored May 31, 2024
1 parent 4b02546 commit ce4fd52
Show file tree
Hide file tree
Showing 19 changed files with 430 additions and 485 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-donuts-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'storage-box': minor
---

fix: methods are now sync. keyword `await` is no longer needed.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ import { Client } from 'storage-box';

const client = new Client();

await client.setex('key', 'value', 2);
client.setex('key', 'value', 2);

console.log(await client.get('key')); // value
console.log(client.get('key')); // value

// time to live in milliseconds
console.log(await client.ttl('key', true)); // 2000
console.log(client.ttl('key', true)); // 2000

// after 3 seconds
setTimeout(async () => {
console.log(await client.get('key')); // undefined
setTimeout(() => {
console.log(client.get('key')); // undefined
}, 3e3);
```

Expand Down
8 changes: 4 additions & 4 deletions docs/driver-browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { BrowserDriver } from 'storage-box/browser';
const driver = new BrowserDriver('local');
const client = new Client(driver);

await client.setex('key', 'value', 5);
client.setex('key', 'value', 5);

const value = await client.get('key');
const value = client.get('key');

console.log(value);
setTimeout(async () => {
console.log(await client.get('key')); // undefined
setTimeout(() => {
console.log(client.get('key')); // undefined
}, 5000);
```
48 changes: 24 additions & 24 deletions docs/driver-memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,75 +60,75 @@ const c = new Client();
##### set

```typescript
await client.set('key', 'value');
client.set('key', 'value');
```

##### get

```typescript
const value = await client.get('key');
const value = client.get('key');
```

##### getall

```typescript
const objs = await client.getall();
const objs = client.getall();
```

##### delete

```typescript
await client.del('key');
client.del('key');
```

##### clear

```typescript
await client.clear();
client.clear();
```

##### exists

```typescript
const exists = await client.exists('key');
const has = await client.has('key'); // has is an alias for exists
const exists = client.exists('key');
const has = client.has('key'); // has is an alias for exists
```

##### size

Length of the storage

```typescript
const size = await client.size();
const size = client.size();
```

##### keys

```typescript
const keys = await client.keys();
const keys = client.keys();
```

##### values

```typescript
const values = await client.values();
const values = client.values();
```

## Time-based Key Expiration

##### Setex

```typescript
await client.setex('key', 'value', 10); // 10 seconds
client.setex('key', 'value', 10); // 10 seconds
```

##### TTL

Returns the remaining time in seconds

```typescript
const ttl = await client.ttl('key');
const ttlMs = await client.ttl('key', true); // Returns the remaining time in milliseconds
const ttl = client.ttl('key');
const ttlMs = client.ttl('key', true); // Returns the remaining time in milliseconds
```

## Hash Operations
Expand Down Expand Up @@ -161,7 +161,7 @@ class CuteMap extends HashMap<string, CuteUser> {
await this.set(randId, user);
}

async initials(): Promise<string[]> {
async initials(): string[] {
const all = await this.getall();
return Object.values(all).map((u) => `${u.first[0]}${u.last[0]}`);
}
Expand All @@ -179,59 +179,59 @@ expect(initials).to.have.members(['MJ', 'PP']);
### hset

```typescript
await client.hset('key', 'field', 'value');
client.hset('key', 'field', 'value');
```

### hget

```typescript
const value = await client.hget('key', 'field');
const value = client.hget('key', 'field');
```

### hgetall

```typescript
const map = await client.hgetall('key');
const map = client.hgetall('key');
```

### hsetex

```typescript
await client.hsetex('key', 'field', 'value', 10); // 10 seconds
client.hsetex('key', 'field', 'value', 10); // 10 seconds
```

### hkeys

```typescript
const keys = await client.hkeys('key');
const keys = client.hkeys('key');
```

### hvalues

```typescript
const values = await client.hvalues('key');
const values = client.hvalues('key');
```

### hdel

```typescript
await client.hdel('key', 'field');
client.hdel('key', 'field');
```

### hexists

```typescript
const exists = await client.hexists('key', 'field');
const exists = client.hexists('key', 'field');
```

### hsize

```typescript
const size = await client.hsize('key');
const size = client.hsize('key');
```

### hclear

```typescript
await client.hclear('key');
client.hclear('key');
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"lint:fix": "eslint --fix .",
"format:check": "prettier --check .",
"format": "prettier --write .",
"ci:publish": "pnpm build && changeset publish",
"ci:publish": "changeset publish",
"prepublishOnly": "pnpm test && pnpm lint && pnpm format:check && pnpm build"
},
"packageManager": "pnpm@8.15.8",
Expand Down
90 changes: 45 additions & 45 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,66 @@ import { expect } from 'chai';
import { Client } from '@/client';

describe('List operations', () => {
const c = new Client();
beforeEach(async () => {
await c.clear();
const client = new Client();
beforeEach(() => {
client.clear();
});

it('List - Get entry values in list', async () => {
await c.lpush('foo', 'bar');
await c.lpush('foo', 'foo');
await c.lpush('foo', 'baz');
expect(await c.lgetall('foo')).to.have.members(['baz', 'foo', 'bar']);
it('List - Get entry values in list', () => {
client.lpush('foo', 'bar');
client.lpush('foo', 'foo');
client.lpush('foo', 'baz');
expect(client.lgetall('foo')).to.have.members(['baz', 'foo', 'bar']);
});

it('Clear - Reset list to empty state', async () => {
await c.set('foo', 'bar');
await c.set('bar', 'foo');
await c.clear();
expect(await c.keys()).to.be.empty;
it('Clear - Reset list to empty state', () => {
client.set('foo', 'bar');
client.set('bar', 'foo');
client.clear();
expect(client.keys()).to.be.empty;
});

it('List set', async () => {
await c.lpush('foo', 'bar');
await c.lpush('foo', 'foo');
await c.lpush('foo', 'baz');
await c.lset('foo', 1, 'bar');
expect(await c.lgetall('foo')).to.have.members(['baz', 'bar', 'bar']);
it('List set', () => {
client.lpush('foo', 'bar');
client.lpush('foo', 'foo');
client.lpush('foo', 'baz');
client.lset('foo', 1, 'bar');
expect(client.lgetall('foo')).to.have.members(['baz', 'bar', 'bar']);
});

it('List get', async () => {
await c.lpush('foo', 'bar');
await c.lpush('foo', 'foo');
await c.lpush('foo', 'baz');
expect(await c.lget('foo', 1)).to.equal('foo');
it('List get', () => {
client.lpush('foo', 'bar');
client.lpush('foo', 'foo');
client.lpush('foo', 'baz');
expect(client.lget('foo', 1)).to.equal('foo');
});

it('List range', async () => {
await c.lpush('foo', 'bar');
await c.lpush('foo', 'foo');
await c.lpush('foo', 'baz');
expect(await c.lgetall('foo')).to.have.members(['baz', 'foo', 'bar']);
expect(await c.lrange('foo', 0, 2)).to.have.members(['foo', 'bar']);
it('List range', () => {
client.lpush('foo', 'bar');
client.lpush('foo', 'foo');
client.lpush('foo', 'baz');
expect(client.lgetall('foo')).to.have.members(['baz', 'foo', 'bar']);
expect(client.lrange('foo', 0, 2)).to.have.members(['foo', 'bar']);
});
});

describe('Hash operations', () => {
const c = new Client();
beforeEach(async () => {
await c.clear();
const client = new Client();
beforeEach(() => {
client.clear();
});

it('Hash set and get', async () => {
await c.hset('foo', 'bar', 'baz');
expect(await c.hget('foo', 'bar')).to.equal('baz');
it('Hash set and get', () => {
client.hset('foo', 'bar', 'baz');
expect(client.hget('foo', 'bar')).to.equal('baz');
});

it('Hash get all', async () => {
await c.hset('foo', 'bar', 'baz');
await c.hset('foo', 'foo', 'bar');
await c.hset('foo', 'bar', 'baz');
it('Hash get all', () => {
client.hset('foo', 'bar', 'baz');
client.hset('foo', 'foo', 'bar');
client.hset('foo', 'bar', 'baz');

const values = await c.hgetall('foo');
const values = client.hgetall('foo');

const expected = {
bar: 'baz',
Expand All @@ -76,10 +76,10 @@ describe('Hash operations', () => {
}
});

it('Hash delete', async () => {
await c.hset('foo', 'bar', 'baz');
await c.hdel('foo', 'bar');
it('Hash delete', () => {
client.hset('foo', 'bar', 'baz');
client.hdel('foo', 'bar');

expect(await c.hget('foo', 'bar')).to.be.null;
expect(client.hget('foo', 'bar')).to.be.null;
});
});
Loading

0 comments on commit ce4fd52

Please sign in to comment.