Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add basic docs for KeyValueRepository #1654

Merged
merged 1 commit into from
Aug 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 103 additions & 2 deletions docs/site/Repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface CustomerRepository extends Repository<Customer> {
See more examples at:

- [Repository/CrudRepository/EntityRepository](https://github.com/strongloop/loopback-next/blob/master/packages/repository/src/repositories/repository.ts)
- [KVRepository](https://github.com/strongloop/loopback-next/blob/master/packages/repository/src/repositories/kv.repository.ts)
- [KeyValueRepository](https://github.com/strongloop/loopback-next/blob/master/packages/repository/src/repositories/kv.repository.ts)

## Installation

Expand Down Expand Up @@ -291,7 +291,108 @@ Please See [Testing Your Application](Testing-Your-Application.md) section in
order to set up and write unit, acceptance, and integration tests for your
application.

## Persisting Data without Juggler [Using MySQL database]
## Access KeyValue Stores

We can now access key-value stores such as [Redis](https://redis.io/) using the
[KeyValueRepository]((https://github.com/strongloop/loopback-next/blob/master/packages/repository/src/repositories/kv.repository.ts).

### Define a KeyValue Datasource

We first need to define a datasource to configure the key-value store. For
better flexibility, we spilt the datasource definition into two files. The json
file captures the configuration properties and it can be possibly overridden by
dependency injection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry to chime in, but wouldn't be good to add the following NOTE?. Especially for new LB4 adopters?

NOTE:
The prefix redis is related to the name of the data source, so you can use any name you want here. Usually you use lb4 datasource name to generate it, where name is the prefix mentioned above.

Copy link
Contributor Author

@raymondfeng raymondfeng Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I didn't notice the context of the example-shopping.

1. redis.datasource.json

```json
{
"name": "redis",
"connector": "kv-redis",
"host": "127.0.0.1",
"port": 6379,
"password": "",
"db": 0
}
```

2. redis.datasource.ts

The class uses a configuration object to set up a datasource for the Redis
instance. By default, the configuration is loaded from `redis.datasource.json`.
We can override it by binding a new object to `datasources.config.redis` for a
context.

```ts
import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
const config = require('./redis.datasource.json');

export class RedisDataSource extends juggler.DataSource {
static dataSourceName = 'redis';

constructor(
@inject('datasources.config.redis', {optional: true})
dsConfig: AnyObject = config,
) {
super(dsConfig);
}
}
```

To generate the datasource automatically, use `lb4 datasource` command and
select `Redis key-value connector (supported by StrongLoop)`.

### Define a KeyValueRepository

The KeyValueRepository binds a model such as `ShoppingCart` to the
`RedisDataSource`. The base `DefaultKeyValueRepository` class provides an
implementation based on `loopback-datasource-juggler`.

```ts
import {DefaultKeyValueRepository} from '@loopback/repository';
import {ShoppingCart} from '../models/shopping-cart.model';
import {RedisDataSource} from '../datasources/redis.datasource';
import {inject} from '@loopback/context';

export class ShoppingCartRepository extends DefaultKeyValueRepository<
ShoppingCart
> {
constructor(@inject('datasources.redis') ds: RedisDataSource) {
super(ShoppingCart, ds);
}
}
```

### Perform Key Value Operations

The KeyValueRepository provides a set of key based operations, such as `set`,
`get`, `delete`, `expire`, `ttl`, and `keys`. See
[KeyValueRepository](https://github.com/strongloop/loopback-next/blob/master/packages/repository/src/repositories/kv.repository.ts)
for a complete list.

```ts
// Please note the ShoppingCartRepository can be instantiated using Dependency
// Injection
const repo: ShoppingCartRepository =
new ShoppingCartRepository(new RedisDataSource());
const cart1: ShoppingCart = givenShoppingCart1();
const cart2: ShoppingCart = givenShoppingCart2();

async function testKV() {
// Store carts using userId as the key
await repo.set(cart1.userId, cart1);
await repo.set(cart2.userId, cart2);

// Retrieve a cart by its key
const result = await repo.get(cart1.userId);
console.log(result);
});

testKV();
```

## Persist Data without Juggler [Using MySQL database]

{% include important.html content="This section has not been updated and code
examples may not work out of the box.
Expand Down