npm package: @methodexists/me-core
Description: Core functionality for MethodExists apps
Latest version: 0.1.2

# me-core

Core functionality for MethodExists apps.

## Usage

Init core module

```
core.init({
  apiService: APIService, // api service module with REST methods
  getLocale: () => app._store.getState().app.locale, // getter of the current app locale (for error messages i18n)
});
```
`apiService` is required for me-core functioning. `getLocale` is optional. 'en-US' locale is used by default.

Register formulas in records model

```
import recordsModel, { init as recordsModelInit } from '~/models/records';

app.model(recordsModel);
recordsModelInit({
  formulas: {
    users: record => ({
      ...record,
      generatedByFormula: Math.random(),
    }),
  },
});
```

Provide options into notifications model

```
import notificationsModel, { init as notificationsModelInit } from '~/models/notifications';
import icon from '~/assets/images/icon.png';
import { profileUsernameSelector } from '~/selectors';

app.model(notificationsModel);
notificationsModelInit({
  icon,
  profileUsernameSelector,
});
```

## Examples

### ListProvider passing props:

```
<ListProvider namespace="users" table="users" primaryKey="username">
  {({ list, isLoading } => (
    isLoading ? <div>Loading...</div> : <div>{ list.map(renderListItem) }</div>
  )}
</ListProvider>
```


## API

### `<ListProvider /> component`

```
import { ListProvider } from 'me-core';
```

| Prop          | Type            | Default                 | Description
| --------------| ----------------|-------------------------|------------
| `*` namespace | string          |                         |
| `*` table     | string          |                         |
| `*` children  | element or func |                         | If `children` is func then ListProvider will pass `{ isLoading, list, total, schema }` as arguments
| filters       | object          | `{}`                    |
| esQuery       | object          | `undefined`             |
| sort          | object          | `{ 'name.raw': 'asc' }` |
| limit         | number          | `20`                    |
| primaryKey    | string          | `'id'`                  |
| source        | object          |                         |


### `<RecordProvider /> component`

```
import { RecordProvider } from 'me-core';
```

| Prop           | Type            | Default  | Description
| -------------- | --------------- | -------- | ------------
| `*` namespace  | string          |          |
| `*` table      | string          |          |
| `*` children   | element or func |          | If `children` is func then RecordProvider will pass `{ isLoading, record, schema }` as arguments
| primaryKey     | string          | `'id'`   |
| id             | string          |          |


### `<AuditLogProvider /> component`

```
import { AuditLogProvider } from 'me-core';
```

| Prop           | Type            | Description
| -------------- | --------------- | ------------
| `*` namespace  | string          |
| `*` table      | string          |
| `*` children   | element or func | If `children` is func then provider will pass `{ isLoading, log, schema }` as argument
| `*` id         | string          |


### Selectors

#### `recordHasUnsavedChangesSelector`

Compares metafields `lastSynced` and `lastChanged` to figure out if the record was changed and not saved.
`lastSynced` field updated on any Save/Fetch operation (`setRecord` action).
`lastChanged` field updated on any Update operation (`update` action).


### Models

#### lists

Lists model works with `/table` API by default. If the `tableName` is `users` it calls `/user` API.
To use `/dynamo` API you need to set `params.useDynamo = true`.

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md) for how to develop a component.