0.2.14 • Published 4 years ago

showsourcing-api-lib v0.2.14

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

This readme is not up to date

Showsourcing front-end api

Features

  • simple api
  • local db
  • search / filters
  • synchronisation
  • model generation
  • logging

Overview

ApiClient is the entry point that generates all other services. Most of the time you'll work with the ApiClient.db which is the driver to access the local database.

  • ApiClient: entry point, is responsible of configuration
  • ApiDb: performs Crud operation on the local DB
  • ApiSynchronizer: local synchronization with the server DB
  • ApiCache: is responsible of cache access

Installation

The package is a wrapper above app sync. It does the configuration on its own and also exports the different services furnished by appsync. Therefor you don't need to configure, import or even install the followings: amplify, app-sync & apollo-client.

You just need to npm i

Generate queries and mutations

amplify pull --appId d1c62vlrmdaq59 --envName antoine
amplify add codegen

amplify pull
amplify codegen
amplify codegen models

Usage

import { ApiClient } from '@cedric_showsourcing/showsourcing-frontend-api';

export const client = new ApiClient({
  awsExport: environment.awsExport,
  mutationMap: {
    Product: { update: gql(updateProduct), create: gql(createProduct) }
  },
  syncMap: {
    Product: {
      base: {
        query: gql(syncProducts),
        __typename: 'ModelProductConnection'
      }
    }
  }
});
client.sync();
export const db = client.db;
db.get('Product', 'some-id').subscribe(product => console.log(product));
db.find('Product').data$.subscribe(products => console.log(products));
client.Auth.signIn('augustin@showsourcing.com', 'Test1234').then(_ => {
  client.synchronizer.sync();
});

ApiDB

Here are the methods of to perform operations on the DB

  /** get one entity by id */
  get(typename: Typename, id: string): Observable<any>

  /**
   * find entities in the cache
   * @param typename: the type you want to find
   * @param filters: recursive object to filter entities e.g. {and: [{property: 'name', contains: 'hello'}, ...]}
   * @param pagination: the slice of entities e.g. {page: 0, limit: 25}
   * @param sort: how the entities are sorted e.g. {direction: 'ASC', property: 'name'}
   * @returns a single response (first()) trough an observable.
   */
  find(
    typename: Typename,
    sort?: SortType,
    filters?: FilterType,
    pagination: PaginationType = { page: 0, limit: 25 }
  ): IApiResponse<any>

  /** update many entities in backend and update the cache */
  update(typename: Typename, entities: EntityFields[]): Observable<any[]>

  /** delete many entities in backend and update the cache */
  delete(typename: Typename, entities: any[]): Observable<any[]>

  /** create many entities in backend and update the cache */
  create(typename: Typename, entities: any[]): Observable<any[]>

Logging

Set log level

You can set the log level in the options of the client

export const client = new ApiClient({
  awsExport: environment.awsExport,
  logLevel: LogLevel.DEBUG,
  // ...
});

Here are is the legend for logging:

/** colors */
const COLOR_LOCAL = 'color: gold; background: #3b2d44; padding: 4px';
const COLOR_LOCAL_RESPONSE = 'color: lime; background: #3b2d44; padding: 4px';
const COLOR_SERVER_RESPONSE = 'color: pink; background: #3b2d44; padding: 4px';
/** icons */
const iconQuery = '🍌';
const iconMutation = '🍇';

Scripts

build models from datastore models:

the src/generated/models folder can be replaced with new models generated by amplify. then the command line npm run models should use the src/generated/models to create a file in src/models/index.ts with the models usable by the client.

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.16

4 years ago

0.1.17

4 years ago

0.1.18

4 years ago

0.1.15

4 years ago

0.1.14

4 years ago

0.1.13

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.0.87

4 years ago

0.0.88

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.0.86

4 years ago

0.0.85

4 years ago

0.0.84

4 years ago

0.1.0

4 years ago

0.0.83

4 years ago

0.0.81

4 years ago

0.0.82

4 years ago

0.0.80

4 years ago

0.0.79

4 years ago

0.0.78

4 years ago

0.0.77

4 years ago

0.0.76

4 years ago

0.0.75

4 years ago

0.0.68

4 years ago

0.0.69

4 years ago

0.0.73

4 years ago

0.0.74

4 years ago

0.0.70

4 years ago

0.0.71

4 years ago

0.0.72

4 years ago

0.0.67

4 years ago

0.0.66

4 years ago

0.0.65

4 years ago

0.0.63

4 years ago

0.0.64

4 years ago

0.0.62

4 years ago

0.0.60

4 years ago

0.0.61

4 years ago

0.0.59

4 years ago

0.0.57

4 years ago

0.0.58

4 years ago

0.0.56

4 years ago

0.0.55

4 years ago

0.0.54

4 years ago

0.0.53

4 years ago

0.0.52

4 years ago

0.0.51

4 years ago

0.0.50

4 years ago

0.0.49

4 years ago

0.0.48

4 years ago

0.0.47

4 years ago

0.0.46

4 years ago

0.0.45

4 years ago

0.0.44

4 years ago

0.0.42

4 years ago

0.0.43

4 years ago

0.0.41

4 years ago

0.0.40

4 years ago

0.0.39

4 years ago

0.0.38

4 years ago

0.0.36

4 years ago