JSPM

service-accounts2-node

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q25542F
  • License MIT

User accounts microservice in Node.js / ES2017 V2

Package Exports

  • service-accounts2-node
  • service-accounts2-node/obj/src/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (service-accounts2-node) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

User Accounts Microservice for Node.js / ES2017 V2

This is a user account management microservice from the Pip.Services library.

  • Registers users and creates their accounts
  • Keeps key user descriptions and settings (about, location, language, theme, ...)

The microservice currently supports the following deployment options:

  • Deployment platforms: Standalone process, AWS Lambda function
  • External APIs: HTTP/REST, gRPC
  • Persistence: In-memory, flat files, MongoDB

This microservice has optional dependencies on the following microservices:

Contract

The logical contract of the microservice is presented below. For physical implementation (HTTP/REST, gRPC, Lambda), please refer to the documentation of the specific protocol.

class AccountV1 implements IStringIdentifiable {
    /* Identification */
    public id: string;
    public login: string;
    public name: string;

    /* Activity tracking */
    public create_time: Date;
    public deleted?: boolean;
    public active: boolean;

    /* User preferences */
    public about: string;
    public time_zone: string;
    public language: string;
    public theme: string;

    /* Custom fields */
    public custom_hdr: any;
    public custom_dat: any;
}

interface IAccountsService {
    getAccounts(ctx: IContext, filter: FilterParams, paging: PagingParams): Promise<DataPage<AccountV1>>;

    getAccountById(ctx: IContext, id: string): Promise<AccountV1>;

    getAccountByLogin(ctx: IContext, login: string): Promise<AccountV1>;

    getAccountByIdOrLogin(ctx: IContext, idOrLogin: string): Promise<AccountV1>;

    createAccount(ctx: IContext, account: AccountV1): Promise<AccountV1>;

    updateAccount(ctx: IContext, account: AccountV1): Promise<AccountV1>;

    deleteAccountById(ctx: IContext, id: string): Promise<AccountV1>;
    
    dropAccountById(ctx: IContext, id: string): Promise<AccountV1>;
}

Download

The main way to get the microservice is to check it out directly from the Bitbucket repository:

git clone git@bitbucket.org:entinco/eic-services-users2.git

The microservice is also available as a NPM package under the same name. You can add a dependency to the microservice into the package.json file of your project:

{
    ...
    "dependencies": {
        ...
        "service-accounts2-node": "^1.0.*",
        ...
    }
}

Develop

Environment Setup

This is a Node.js project and you have to install Node.js tools. You can download them from the official Node.js website: https://nodejs.org/en/download/

After Node.js is installed, you can verify its version:

node --version

Then you need to install some node tools (recommended to be global):

# Install typescript compiler
npm install typescript -g
 
# Install mocha test runner
npm install mocha -g

To work with the code repository, you need to install Git: https://git-scm.com/downloads

If you are planning to develop and test using persistent storages other than flat files, you may need to install database servers:

Installing

After your environment is ready, you can copy the microservice source code from the repository:

git clone git@bitbucket.org:entinco/eic-services-users2.git

Then go to the project folder and install dependent modules:

# Install dependencies
npm install

If you have worked with the microservice before, you can check out the latest changes and update the dependencies:

# Update source code
git pull

# Update dependencies
npm update

Building

This microservice is written in TypeScript language which is transcompiled into JavaScript. So, if you make changes to the source code, you need to compile it before running or committing. The process will output compiled JavaScript files into the "obj" folder:

tsc

When you do continuous edit-build-test cycles, you can run the TypeScript compiler with "--watch" to detect and compile changes you make automatically:

tsc --watch

Testing

Before you execute tests, you may need to set configuration options into a config.yml file, located by default in the "config" folder in the root of the project. For more information, see the Configure guide.

Run unit tests:

npm test

Execute benchmarks:

npm run benchmark

Contributing

Developers interested in contributing should read the following instructions:

Please do not ask general questions in an issue. Issues are only to report bugs, request enhancements, or request new features. For general questions and discussions, use the Contributors Forum.

It is important to note that for each release, the Changelog is a resource that will itemize all:

  • Bug Fixes
  • New Features
  • Breaking Changes

Configure

As a starting point, you can use this example to create your own config.yml file: ```yaml

  • descriptor: "pip-services:context-info:default:default:1.0" name: "service-accounts2" description: "Accounts microservice for pip-services V2"

  • descriptor: "pip-services:logger:console:default:1.0" level: "trace"

  • descriptor: "accounts:persistence:file:default:1.0" path: "../data/accounts.json"

  • descriptor: "accounts:service:default:default:1.0"

  • descriptor: "pip-services:endpoint:http:default:1.0" connection: protocol: "http" host: "0.0.0.0" port: 8080

  • descriptor: "accounts:controller:commandable-http:default:1.0"


Afterwards, check all configuration options. Specifically, pay attention to connection options 
for database and dependent microservices.

For more information on this section, read the 
[Pip.Services Configuration Guide](http://docs.pipservices.org/toolkit/recipes/config_file_syntax/).

### <a name="persistence"></a> Persistence

The microservice supports three types of persistence: in-memory, flat files, or MongoDB. 
In-memory and flat files are great for development and testing, 
while MongoDB is a good option with outstanding performance and scalability, suitable for demanding production installations. 
You can choose and configure the option that suits your needs.

#### <a name="memory"></a> Memory

Memory persistence has the following configuration properties:

* options: object - Misc configuration options
    - max_page_size: number - Maximum number of items per page (default: 100)

Example:
```yaml
- descriptor: "accounts:persistence:memory:default:1.0"
  options:
    max_page_size: 100

File

Flat file persistence has the following configuration properties:

  • path: string - file path where SystemEventV1 objects are stored. The object are written into the file in JSON format.
  • options: object - Misc configuration options
    • max_page_size: number - Maximum number of items per page (default: 100)

Example:

- descriptor: "accounts:persistence:file:default:1.0"
  path: "./data/accounts.json"

MongoDB

MongoDB persistence has the following configuration properties:

  • connection(s): object - MongoDB connection properties
  • options: object - (optional) MongoDB connection options. See http://mongoosejs.com/docs/connections.html for more details.
  • debug: boolean - (optional) Enables or disables connection debugging

Example:

- descriptor: "accounts:persistence:file:default:1.0"
  connection:
    uri: "mongodb://localhost/pipservicestest"
  options:
    server:
      poolSize: 4
      socketOptions:
        keepAlive: 1
        connectTimeoutMS: 5000
    auto_reconnect: true

Service

Service has the following configuration properties:

  • options
    • login_as_email: boolean - (optional) treats user login as email address

Example:

- descriptor: "accounts:service:default:default:1.0"
  options:
    login_as_email: true

Controllers

The controller components (also called "endpoints") expose external microservice API for consumers. Each microservice can expose multiple APIs (HTTP/REST, gRPC) and multiple versions of the same API type. At least one controller is required for the microservice to run successfully.

HTTP

HTTP/REST controller has the following configuration properties:

  • connection: object - HTTP transport configuration options
    • protocol: string - HTTP protocol - 'http' or 'https' (default is 'http')
    • host: string - IP address/hostname binding (default is '0.0.0.0')
    • port: number - HTTP port number
  • swagger: object - (optional) Swagger controller integration options
    • enable: boolean - (optional) whether to enable Swagger integration
    • auto: boolean - (optional) whether to automatically configure Swagger integration

Example:

# Common HTTP endpoint
- descriptor: "pip-services:endpoint:http:default:1.0"
  connection:
    protocol: "http"
    host: "0.0.0.0"
    port: "8080"

# HTTP controller
- descriptor: "accounts:controller:commandable-http:default:1.0"
  swagger:
    enable: true
    auto: true

gRPC

gRPC controller has the following configuration properties:

  • connection: object - gRPC transport configuration options
    • protocol: string - gRPC protocol - 'http' or 'https' (default is 'http')
    • host: string - IP address/hostname binding (default is '0.0.0.0')
    • port: number - gRPC port number

Example:

# Common GRPC endpoint
- descriptor: "pip-services:endpoint:grpc:default:1.0"
  connection:
    protocol: "http"
    host: "0.0.0.0"
    port: 8090

# GRPC controller
- descriptor: "accounts:controller:grpc:default:1.0"

# Commandable GRPC controller
- descriptor: "accounts:controller:commandable-grpc:default:1.0"

For more information on this section, read the Pip.Services Configuration Guide.

Run

Standalone Process

The simplest way to deploy the microservice is to run it as a standalone process. This microservice is implemented in JavaScript and requires installation of Node.js. You can get it from the official site at https://nodejs.org/en/download

Step 1. Download the microservice by following the Download instructions.

Step 2. Add a config.yml file to the "config" folder in the root of the project and set configuration parameters. See the Configure guide for details.

Step 3. Start the microservice:

node ./bin/main.js

AWS Lambda Function

This microservice can also be packaged and deployed to AWS as a Lambda function. This microservice is implemented in JavaScript and requires the installation of Node.js. You can download it from the official Node.js website: https://nodejs.org/en/download/

Step 1. Download the microservice by following the Download instructions.

Step 2. Add a config.yml file to the "config" folder in the root of the project and set configuration parameters. See the Configure guide for details.

Step 3. Go to the project folder and install the dependent modules:

npm install

Step 4. Package the project folder into a .zip file:

zip -r lambda_function.zip .

Step 5. Go to the AWS Lambda console and create a new Lambda function.

  • Name: "service-accounts2-node"
  • Runtime: Choose "Node.js 14.x" or newer
  • Function code: Upload the .zip package of the project folder

Step 6: Configure any triggers necessary to invoke the function or invoke it via the AWS Lambda console.

Use

The easiest way to work with the microservice is to use the client library. The complete list of available client libraries for different languages is listed in the Quick Links.

If you use Node.js, then you can add a dependency to the client library into the package.json file of your project:

{
    ...
    "dependencies": {
        ...
        "client-accounts2-node": "^1.0.*",
        ...
    }
}

Then install the dependency:

# Install new dependencies
npm install

# Update already installed dependencies
npm update

Inside your code, get the reference to the client library:

let node_lib = new require('client-accounts2-node');

Define client configuration parameters that match the configuration of the microservice external API:

// Client configuration
let config = {
    connection: {
        protocol: 'http',
        host: 'localhost', 
        port: 8080
    }
};

Instantiate the client and open a connection to the microservice:

// Create the client instance
let client = node_lib.AccountsHttpClientV1(config);

// Connect to the microservice
try {
    await client.open(null);
    // Work with the microservice
    ...
}
catch (err) {
    console.error('Connection to the microservice failed');
    console.error(err);
}

Now the client is ready to perform operations:

// Register a new account
let account = await client.createAccount(
    null,
    { 
        name: 'Test User',
        login: 'somebody@somewhere.com'
    },
);
// Find created account
let account = await client.getAccountByLogin(
    null,
    'somebody@somewhere.com',
);

Acknowledgements

This microservice was created by Sergey Seroukhov and is currently maintained by Michael Seroukhov.