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

Add methods #8

Merged
merged 6 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 2
jobs:
test:
docker:
- image: circleci/node:12-stretch
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run:
name: Installing Dependencies
command: npm install
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
- node_modules
- run:
name: Running Mocha Tests
command: npm test
workflows:
version: 2
build_and_test:
jobs:
- test
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# dotenv environment variables file
.env

.idea
/dist
node_modules
*.js
*.js.map
*.map
*.d.ts
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 3.0.0 (June 10, 2021)
* Library interface has been refactored to comply well-known CRUD approach

# 2.0.0 (May 28, 2021)
* Initial version of the reworked library
213 changes: 52 additions & 161 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,213 +1,104 @@
# Maester Client

The official object-storage client for elasticio-sailor-nodejs.
The official Elastic.io object-storage client.

## Usage

### Create client
```
const Client = require('@elasticio/maester-client');

const client = new Client('http://maester.local:3002', 'my-token');
```

### Buckets API

Get bucket:

```
const bucket = await client.buckets.get(id);
```

List buckets:

```
const buckets = await client.buckets.list({
page: {
number: 1,
size: 25
}
});
```

List buckets by external ID:

```
const buckets = await client.buckets.list({
externalId: 'my-external-id'
});
```

Create bucket:

```
const bucket = await client.buckets.create({
objects: ['object-1', 'object-2', ..., 'object-N'],
extrenalId: 'my-external-id
});
```

Update bucket:

```
const bucket = await client.buckets.update(id, {
closed: true
});
```

Delete bucket:
Note: All the code snippets written in Typescript

### Create client
```
await client.buckets.delete(id);
```

### Objects API

Get object:
import { ObjectStorageWrapper } from '@elastic.io/maester-client/dist/ObjectStorageWrapper';

```
const object = await client.objects.get(id);
console.log(object.data);
const objectStorage = new ObjectStorageWrapper(this);
```

Object's property `data` has value of type `string`, `object`, `Buffer` or `Stream`.
### CRUD operations

Get object as JSON:
### Create object

The method has the following signature:
```
const object = await client.objects.getJSON(id);
console.log(object.data);
async createObject(data: object, queryKey?: string, queryValue?: string, ttl?: number)
```

Get object as buffer:
where
- data - object data to create. *Required*
- queryKey, queryValue - searchable field (see below in `Get objects by query parameter` section). *Optional*, but if queryKey is specified, queryValue must be specified as well.
- ttl - configurable object's time to live, milliseconds. *Optional*

```
const object = await client.objects.getBuffer(id);
console.log(object.data.toString())
const obj = await objectStorage.createObject(data, somequeriablefieldKey, somequeriablefieldValue, 60000);
```

Get object as stream:
### Read operations
#### Get object by ID:

The method has the following signature:
```
const object = await client.objects.getStream(id);
object.data.pipe(...)
async lookupObjectById(id: string)
```

Get object query:
where
- id - Maester internal id of the object to update. E.g. '76380cae-aee3-457a-9029-d971f61e3731'. *Required*

```
const query = {
'x-query-foo': 'fooQuery',
'x-query-bar': 'barQuery',
};

const response = await this.client.objects.getObjectQuery(query);
```

Create read stream example:

```
client.objects.createReadStream(id).pipe(fs.createWriteStream('/foo/bar.jpg'));
const obj = await objectStorage.lookupObjectById(id);
```

Create object:

As Maester is able to store any data type, the method returns **a raw string**.
You may want to parse JSON or do any other data processing according to object's expected data type:
```
const response = await client.objects.create(data);
const parsedObject = JSON.parse(obj);
```
The following errors can be thrown:
- Object Not Found
- Invalid object id

Where `data` can be `string`, `Buffer`, `Stream` or array of these values.

Create object with queryable parameters:
#### Get objects by query parameter:

The method has the following signature:
```
const params = {
objectFields: {
key1: {
Meta: 'someMeta',
Query: 'someQuery',
}
}
}

const response = await client.objects.create(data, params);
async lookupObjectByQueryParameter(key: string, value: string)
```
where
- key, value - searchable field. *Required*

Create object with metadata:

If you create an object with a queriable header, internally it looks like this:
```
const response = await client.objects.create(data, {
metadata: {
key: 'value'
}
});
x-query-somequeriablefieldKey: somequeriablefieldValue
```
where 'x-query-' is a default prefix.

Create object and override its content type:

Using Maester REST API you can find this object by:
```
const response = await client.objects.create({
data: 'hello world',
contentType: 'text/plain'
});
/objects?query[somequeriablefieldkey]=somequeriablefieldValue
```

Create multiple objects at once:

Using the library:
```
const data = [
{
data: 'hello world'
},
{
data: JSON.stringify(json),
contentType: 'application/json'
},
fs.createReadStream('/foo/bar.jpg'),
Buffer.allocUnsafe(1024)
];

const response = await client.objects.create(data, {
bucket: 'bucket-id',
metadata: {
description: 'my stuff'
}
});
const obj = await objectStorage.lookupObjectByQueryParameter('somequeriablefieldKey', 'somequeriablefieldValue');
```

Writable stream example:
### Update object

The method has the following signature:
```
fs.createReadStream('/foo/bar.jpg').pipe(client.objects.createWriteStream());
async updateObject(id: string, data: object)
```

Update object query:
where
- id - Maester internal id of the object to update. E.g. '76380cae-aee3-457a-9029-d971f61e3731'. *Required*
- data - object to update. *Required*

```
const data = 'hello world';

const objectFields = {
foo: { Query: 'fooQuery', Meta: 'fooMeta' },
bar: { Query: 'barQuery', Meta: 'barMeta' }
};
const params = { id: 'some', objectFields };

const object = await this.client.objects.updateObjectQuery(data, params);
const obj = await objectStorage.updateObject(id, data);
```

Delete object:
### Delete object

The method has the following signature:
```
await client.objects.delete(id);
async deleteObjectById(id: string)
```

Delete object query:
where
- id - Maester internal id of the object to update. E.g. '76380cae-aee3-457a-9029-d971f61e3731'. *Required*

```
const query = {
foo: 'a',
bar: 'b'
};

await client.objects.delete(query);
const obj = await objectStorage.deleteObjectById(id);
```
Loading