这是indexloc提供的服务,不要输入任何密码
Skip to content
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
5 changes: 5 additions & 0 deletions community/tools/ra-data-hasura/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.5 (May 16, 2019)

- Feature: Support specifying primary keys other than id for tables using a config object
Example: `const config = { 'primaryKey': {'author': 'name'} }`

## 0.0.4 (April 25, 2019)

- Feature: Support multiple schemas using "." separator.
Expand Down
20 changes: 17 additions & 3 deletions community/tools/ra-data-hasura/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ $ npm install --save ra-data-hasura

## Usage

The `ra-data-hasura` provider accepts two arguments:
The `ra-data-hasura` provider accepts three arguments:

- `serverEndpoint` - The URL at which Hasura GraphQL Engine is running. (for example: http://localhost:8080). This is required.
- `serverEndpoint` - The URL at which Hasura GraphQL Engine is running. (for example: http://localhost:8080). This is required. It should also expose `/v1/query` endpoint.

- `headers` - An optional argument. Pass your auth headers here.

- `config` - An optional argument. Pass your config here.

```
hasuraDataProvider(serverEndpoint, headers)
hasuraDataProvider(serverEndpoint, headers, config)
```

In the following example, we import `hasuraDataProvider` from `ra-data-hasura` and give it the hasura server endpoint (assumed to be running at http://localhost:8080) and an optional headers object.
Expand Down Expand Up @@ -71,6 +73,18 @@ For example to fetch data from schema `test` and table `author`, use the followi
<Resource name="test.author" list={list} />
```

### Different Primary Keys

Sometimes the table you are querying might have a primary key other than `id`. `react-admin` enforces `id` to be returned in the response by the DataProvider. But you can configure a different primary key column for specific tables using the config object as below:

```
const config = {
'primaryKey': {
'tableName': 'primaryKeyColumn', 'tableName2': 'primaryKeyColumn'
}
};
```

## Known Issues

Filter as you type (search) functionality inside tables is not supported right now. It is a work in progress.
Expand Down
2 changes: 1 addition & 1 deletion community/tools/ra-data-hasura/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion community/tools/ra-data-hasura/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ra-data-hasura",
"version": "0.0.4",
"version": "0.0.5",
"description": "react-admin data provider for Hasura GraphQL Engine",
"main": "lib/ra-data-hasura.min.js",
"scripts": {
Expand Down
79 changes: 56 additions & 23 deletions community/tools/ra-data-hasura/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import {
deleteQuery
} from './queries';

const DEFAULT_PRIMARY_KEY = 'id';

const cloneQuery = (query) => {
return JSON.parse(JSON.stringify(query));
};

export default (serverEndpoint, headers) => {
const convertDataRequestToHTTP = (type, resource, params) => {
const options = {};
let finalQuery = {};
export default (serverEndpoint, headers, config) => {

let schema;
const getTableSchema = (resource) => {
let tableName;
let schema;

// parse schema in resource
if (resource && resource.split('.').length === 1) {
Expand All @@ -31,6 +31,27 @@ export default (serverEndpoint, headers) => {
} else {
throw new Error(JSON.stringify({'error': 'Invalid table/schema resource'}));
}
return { schema, tableName };
};

const getPrimaryKey = (resource) => {
let primaryKey = DEFAULT_PRIMARY_KEY;

if (config && config['primaryKey'][resource]) {
primaryKey = config['primaryKey'][resource];
}
return primaryKey;
};

const convertDataRequestToHTTP = (type, resource, params) => {
const options = {};
let finalQuery = {};

const tableSchema = getTableSchema(resource);

let { schema, tableName } = tableSchema;

const primaryKey = getPrimaryKey(resource);

switch (type) {
case 'GET_LIST':
Expand All @@ -42,8 +63,10 @@ export default (serverEndpoint, headers) => {
finalSelectQuery.args.limit = params.pagination.perPage;
finalSelectQuery.args.offset = (params.pagination.page * params.pagination.perPage) - params.pagination.perPage;
finalSelectQuery.args.where = params.filter;
finalSelectQuery.args.order_by = {column: params.sort.field, type: params.sort.order.toLowerCase()};
finalSelectQuery.args.order_by = {column: primaryKey, type: params.sort.order.toLowerCase()};
finalCountQuery.args.table = {'name': tableName, 'schema': schema};;
finalCountQuery.args.where = {};
finalCountQuery.args.where[primaryKey] = { '$ne': null };
finalQuery = cloneQuery(bulkQuery);
finalQuery.args.push(finalSelectQuery);
finalQuery.args.push(finalCountQuery);
Expand All @@ -52,7 +75,8 @@ export default (serverEndpoint, headers) => {
// select one
finalQuery = cloneQuery(selectQuery);
finalQuery.args.table = {'name': tableName, 'schema': schema};
finalQuery.args.where = { id: { '$eq': params.id } };
finalQuery.args.where = {};
finalQuery.args.where[primaryKey] = { '$eq': params.id };
break;
case 'CREATE':
// create one
Expand All @@ -61,8 +85,7 @@ export default (serverEndpoint, headers) => {
finalQuery = cloneQuery(insertQuery);
finalQuery.args.table = {'name': tableName, 'schema': schema};
finalQuery.args.objects.push(params.data);
// id is mandatory
createFields.push('id');
createFields.push(primaryKey);
finalQuery.args.returning = createFields;
break;
case 'UPDATE':
Expand All @@ -72,9 +95,9 @@ export default (serverEndpoint, headers) => {
finalQuery = cloneQuery(updateQuery);
finalQuery.args.table = {'name': tableName, 'schema': schema};
finalQuery.args['$set'] = params.data;
finalQuery.args.where = { id: { '$eq': params.id }};
// id is mandatory
updateFields.push('id');
finalQuery.args.where = {};
finalQuery.args.where[primaryKey] = { '$eq': params.id };
updateFields.push(primaryKey);
finalQuery.args.returning = updateFields;
break;
case 'UPDATE_MANY':
Expand All @@ -84,9 +107,9 @@ export default (serverEndpoint, headers) => {
finalQuery = cloneQuery(updateQuery);
finalQuery.args.table = {'name': tableName, 'schema': schema};
finalQuery.args['$set'] = params.data;
finalQuery.args.where = { 'id': { '$in': params.ids } };
// id is mandatory
updateManyFields.push('id');
finalQuery.args.where = {};
finalQuery.args.where[primaryKey] = { '$in': params.ids };
updateManyFields.push(primaryKey);
finalQuery.args.returning = updateManyFields;
break;
case 'DELETE':
Expand All @@ -95,24 +118,25 @@ export default (serverEndpoint, headers) => {

finalQuery = cloneQuery(deleteQuery);
finalQuery.args.table = {'name': tableName, 'schema': schema};
finalQuery.args.where = { id: { '$eq': params.id }};
// id is mandatory
deleteFields.push('id');
finalQuery.args.where = {};
finalQuery.args.where[primaryKey] = { '$eq': params.id };
deleteFields.push(primaryKey);
finalQuery.args.returning = deleteFields;
break;
case 'DELETE_MANY':
// delete multiple
finalQuery = cloneQuery(deleteQuery);
finalQuery.args.table = {'name': tableName, 'schema': schema};
finalQuery.args.where = { 'id': { '$in': params.ids } };
// id is mandatory
finalQuery.args.returning = ['id'];
finalQuery.args.where = {};
finalQuery.args.where[primaryKey] = { '$in': params.id };
finalQuery.args.returning = [primaryKey];
break;
case 'GET_MANY':
// select multiple within where clause
finalQuery = cloneQuery(selectQuery);
finalQuery.args.table = {'name': tableName, 'schema': schema};
finalQuery.args.where = { 'id': { '$in': params.ids } };
finalQuery.args.where = {};
finalQuery.args.where[primaryKey] = { '$in': params.ids };
break;
case 'GET_MANY_REFERENCE':
// select multiple with relations
Expand All @@ -123,8 +147,10 @@ export default (serverEndpoint, headers) => {
finalManyQuery.args.limit = params.pagination.perPage;
finalManyQuery.args.offset = (params.pagination.page * params.pagination.perPage) - params.pagination.perPage;
finalManyQuery.args.where = { [params.target]: params.id };
finalManyQuery.args.order_by = {column: params.sort.field, type: params.sort.order.toLowerCase()};
finalManyQuery.args.order_by = {column: primaryKey, type: params.sort.order.toLowerCase()};
finalManyCountQuery.args.table = {'name': tableName, 'schema': schema};;
finalManyCountQuery.args.where = {};
finalManyCountQuery.args.where[primaryKey] = { '$ne': null };
finalQuery = cloneQuery(bulkQuery);
finalQuery.args.push(finalManyQuery);
finalQuery.args.push(finalManyCountQuery);
Expand All @@ -141,6 +167,13 @@ export default (serverEndpoint, headers) => {
if ('error' in response || 'code' in response) {
throw new Error(JSON.stringify(response));
}
const primaryKey = getPrimaryKey(resource);

if (primaryKey !== DEFAULT_PRIMARY_KEY) {
response[0].map((res) => {
res[DEFAULT_PRIMARY_KEY] = res[primaryKey];
});
}
switch (type) {
case 'GET_LIST':
return {
Expand Down
2 changes: 1 addition & 1 deletion community/tools/ra-data-hasura/src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const countQuery = {
type: 'count',
args: {
table: {'schema': '', 'name': ''},
where: { id: { '$ne': null }}
where: {}
}
};

Expand Down