1.2.2 • Published 5 days ago

@memberjunction/server v1.2.2

Weekly downloads
-
License
ISC
Repository
-
Last release
5 days ago

@memberjunction/server

The @memberjunction/server library provides a simple way to run the MemberJunction API server. It includes all the functions required to start up the GraphQL server, manage authentication, and connect to the common data store.

comprehensive interface for accessing and managing metadata within MemberJunction, along with facilities for working with entities, applications, and various other aspects central to the MemberJunction ecosystem. This library primarily exports a Metadata class which acts as the gateway to many functionalities.

Installation

npm install @memberjunction/server

Configuration

The server uses configuration from its environment

Env variableDescription
DB_HOSTThe hostname for the common data store database
DB_PORTThe port for the common data store database (default 1433)
DB_USERNAMEThe username used to authenticate with the common data store
DB_PASSWORDThe password used to authenticate with the common data store
DB_DATABASEThe common data store database name
PORTThe port used by the server (default 4000)
ROOT_PATHThe GraphQL root path (default /)
WEB_CLIENT_IDThe client ID used for MSAL authentication
TENANT_IDThe tenant ID used for MSAL authentication
ENABLE_INTROSPECTIONA flag to allow GraphQL introspection (default false)
WEBSITE_RUN_FROM_PACKAGEAn Azure flag to indicate a read-only file system
AUTH0_DOMAINThe Auth0 domain
AUTH0_CLIENT_IDThe Auth0 Client ID
AUTH0_CLIENT_SECRETThe Auth0 Client secret
MJ_CORE_SCHEMAThe core schema to use for the data provider
CONFIG_FILEAn absolute path to the config file json

Usage

Import the serve function from the package and run it as part of the server's main function. The function accepts an array of absolute paths to the resolver code.

import { serve } from '@memberjunction/server';
import { resolve } from 'node:path';

const localPath = (p: string) => resolve(__dirname, p);

const resolverPaths = [
  'resolvers/**/*Resolver.{js,ts}',
  'generic/*Resolver.{js,ts}',
  'generated/generated.ts',
]

serve(resolverPaths.map(localPath));

Custom New User Behavior

The behavior to handle new users can be customized by subclassing the NewUserBase class. The subclass can pre-process, post-process or entirely override the base class behavior as needed. Import the class before calling serve to ensure the class is registered.

index.ts

import { serve } from '@memberjunction/server';
import { resolve } from 'node:path';

import './auth/exampleNewUserSubClass'; // make sure this new class gets registered

// ...

auth/exampleNewUserSubClass.ts

import { LogError, Metadata, RunView } from "@memberjunction/core";
import { RegisterClass } from "@memberjunction/global";
import { NewUserBase, configInfo } from '@memberjunction/server';
import { UserCache } from "@memberjunction/sqlserver-dataprovider";

/**
 * This example class subclasses the @NewUserBase class and overrides the createNewUser method to create a new person record and then call the base class to create the user record. In this example there is an entity
 * called "Persons" that is mapped to the User table in the core MemberJunction schema. You can sub-class the NewUserBase to do whatever behavior you want and pre-process, post-process or entirely override the base
 * class behavior.
 */
@RegisterClass(NewUserBase, undefined, 1) /*by putting 1 into the priority setting, MJGlobal ClassFactory will use this instead of the base class as that registration had no priority*/
export class ExampleNewUserSubClass extends NewUserBase {
    public override async createNewUser(firstName: string, lastName: string, email: string) {
        try {
            const md = new Metadata();
            const contextUser = UserCache.Instance.Users.find(u => u.Email.trim().toLowerCase() === configInfo?.userHandling?.contextUserForNewUserCreation?.trim().toLowerCase())
            if(!contextUser) {
                LogError(`Failed to load context user ${configInfo?.userHandling?.contextUserForNewUserCreation}, if you've not specified this on your config.json you must do so. This is the user that is contextually used for creating a new user record dynamically.`);
                return undefined;
            }

            const pEntity = md.Entities.find(e => e.Name === 'Persons'); // look up the entity info for the Persons entity
            if (!pEntity) {
                LogError('Failed to find Persons entity');
                return undefined;
            }

            let personId;
            // this block of code only executes if we have an entity called Persons
            const rv = new RunView();
            const viewResults = await rv.RunView({
                EntityName: 'Persons',
                ExtraFilter: `Email = '${email}'`
            }, contextUser)
    
            if (viewResults && viewResults.Success && Array.isArray(viewResults.Results) && viewResults.Results.length > 0) {
                // we have a match so use it
                const row = (viewResults.Results as { ID: number }[])[0]; // we know the rows will have an ID number
                personId = row['ID'];
            }
    
            if (!personId) {
                // we don't have a match so create a new person record
                const p = await md.GetEntityObject('Persons', contextUser);
                p.NewRecord(); // assumes we have an entity called Persons that has FirstName/LastName/Email fields
                p.FirstName = firstName;
                p.LastName = lastName;
                p.Email = email;
                p.Status = 'active';
                if (await p.Save()) {
                    personId = p.ID;
                }   
                else {
                    LogError(`Failed to create new person ${firstName} ${lastName} ${email}`)
                }
            }
    
            // now call the base class to create the user, and pass in our LinkedRecordType and ID
            return super.createNewUser(firstName, lastName, email, 'Other', pEntity?.ID, personId);        
        }
        catch (e) {
            LogError(`Error creating new user ${email} ${e}`);
            return undefined;
        }
    }
}
1.2.2

5 days ago

1.2.1

5 days ago

1.2.0

8 days ago

1.1.1

11 days ago

1.1.0

11 days ago

1.1.3

11 days ago

1.1.2

11 days ago

1.0.11

13 days ago

1.0.9

23 days ago

1.0.7-next.0

26 days ago

1.0.8

24 days ago

1.0.7

26 days ago

1.0.8-next.6

25 days ago

1.0.8-next.5

25 days ago

1.0.8-next.4

25 days ago

1.0.8-next.3

25 days ago

1.0.8-next.2

25 days ago

1.0.8-next.1

26 days ago

1.0.8-next.0

26 days ago

1.0.8-beta.0

24 days ago

1.0.6

28 days ago

1.0.4

29 days ago

1.0.3

29 days ago

1.0.1

30 days ago

1.0.0

30 days ago

0.9.264

1 month ago

0.9.263

1 month ago

0.9.265

1 month ago

0.9.261

1 month ago

0.9.260

1 month ago

0.9.259

1 month ago

0.9.258

1 month ago

0.9.255

1 month ago

0.9.254

1 month ago

0.9.257

1 month ago

0.9.256

1 month ago

0.9.253

1 month ago

0.9.252

1 month ago

0.9.251

1 month ago

0.9.250

1 month ago

0.9.248

1 month ago

0.9.246

1 month ago

0.9.247

1 month ago

0.9.244

1 month ago

0.9.243

2 months ago

0.9.245

1 month ago

0.9.241

2 months ago

0.9.240

2 months ago

0.9.242

2 months ago

0.9.238

2 months ago

0.9.237

2 months ago

0.9.236

2 months ago

0.9.231

2 months ago

0.9.230

2 months ago

0.9.233

2 months ago

0.9.232

2 months ago

0.9.229

2 months ago

0.9.235

2 months ago

0.9.234

2 months ago

0.9.228

2 months ago

0.9.227

2 months ago

0.9.226

2 months ago

0.9.225

2 months ago

0.9.222

2 months ago

0.9.221

2 months ago

0.9.224

2 months ago

0.9.223

2 months ago

0.9.220

2 months ago

0.9.219

2 months ago

0.9.217

2 months ago

0.9.216

2 months ago

0.9.218

2 months ago

0.9.211

2 months ago

0.9.210

2 months ago

0.9.213

2 months ago

0.9.212

2 months ago

0.9.215

2 months ago

0.9.214

2 months ago

0.9.209

2 months ago

0.9.206

2 months ago

0.9.205

2 months ago

0.9.208

2 months ago

0.9.207

2 months ago

0.9.204

2 months ago

0.9.200

2 months ago

0.9.202

2 months ago

0.9.198

2 months ago

0.9.199

2 months ago

0.9.197

2 months ago

0.9.196

2 months ago

0.9.187

2 months ago

0.9.186

2 months ago

0.9.189

2 months ago

0.9.188

2 months ago

0.9.183

3 months ago

0.9.185

2 months ago

0.9.194

2 months ago

0.9.193

2 months ago

0.9.190

2 months ago

0.9.192

2 months ago

0.9.191

2 months ago

0.9.182

3 months ago

0.9.181

3 months ago

0.9.180

3 months ago

0.9.179

3 months ago

0.9.176

3 months ago

0.9.175

3 months ago

0.9.178

3 months ago

0.9.172

3 months ago

0.9.174

3 months ago

0.9.173

3 months ago

0.9.170

3 months ago

0.9.169

3 months ago

0.9.166

3 months ago

0.9.168

3 months ago

0.9.165

3 months ago

0.9.164

3 months ago

0.9.163

3 months ago

0.9.162

3 months ago

0.9.161

3 months ago

0.9.160

3 months ago

0.9.154

3 months ago

0.9.153

3 months ago

0.9.155

3 months ago

0.9.158

3 months ago

0.9.157

3 months ago

0.9.159

3 months ago

0.9.151

3 months ago

0.9.142

3 months ago

0.9.141

3 months ago

0.9.129

3 months ago

0.9.128

3 months ago

0.9.125

3 months ago

0.9.124

3 months ago

0.9.110

4 months ago

0.9.112

4 months ago

0.9.111

4 months ago

0.9.114

4 months ago

0.9.113

4 months ago

0.9.115

4 months ago

0.9.101

4 months ago

0.9.100

4 months ago

0.9.96

4 months ago

0.9.97

4 months ago

0.9.98

4 months ago

0.9.99

4 months ago

0.9.107

4 months ago

0.9.106

4 months ago

0.9.93

4 months ago

0.9.109

4 months ago

0.9.94

4 months ago

0.9.108

4 months ago

0.9.95

4 months ago

0.9.103

4 months ago

0.9.102

4 months ago

0.9.105

4 months ago

0.9.104

4 months ago

0.9.90

4 months ago

0.9.89

4 months ago

0.9.85

4 months ago

0.9.87

4 months ago

0.9.88

4 months ago

0.9.84

4 months ago

0.9.81

4 months ago

0.9.82

4 months ago

0.9.83

4 months ago

0.9.80

4 months ago

0.9.78

4 months ago

0.9.79

4 months ago

0.9.77

4 months ago

0.9.76

4 months ago

0.9.75

4 months ago

0.9.74

4 months ago

0.9.72

4 months ago

0.9.73

4 months ago

0.9.70

5 months ago

0.9.71

5 months ago

0.9.67

5 months ago

0.9.68

5 months ago

0.9.69

5 months ago

0.9.65

5 months ago

0.9.66

5 months ago

0.9.64

5 months ago

0.9.63

5 months ago

0.9.61

5 months ago

0.9.62

5 months ago

0.9.58

5 months ago

0.9.59

5 months ago

0.9.60

5 months ago

0.9.57

5 months ago

0.9.56

5 months ago

0.9.55

5 months ago

0.9.54

5 months ago

0.9.53

5 months ago

0.9.52

6 months ago

0.9.51

6 months ago

0.9.50

6 months ago

0.9.49

6 months ago

0.9.48

6 months ago

0.9.47

6 months ago

0.9.46

6 months ago

0.9.45

6 months ago

0.9.44

6 months ago

0.9.43

6 months ago

0.9.42

6 months ago

0.9.41

6 months ago

0.9.40

6 months ago

0.9.39

6 months ago

0.9.38

6 months ago

0.9.37

6 months ago

0.9.36

6 months ago

0.9.35

6 months ago

0.9.34

6 months ago

0.9.32

7 months ago

0.9.31

7 months ago

0.9.30

7 months ago

0.9.28

7 months ago

0.9.27

7 months ago

0.9.26

7 months ago

0.9.25

7 months ago

0.9.24

7 months ago

0.9.23

7 months ago

0.9.22

7 months ago

0.9.21

7 months ago

0.9.20

7 months ago

0.9.19

7 months ago

0.9.16

7 months ago

0.9.15

7 months ago

0.9.14

7 months ago

0.9.13

7 months ago

0.9.12

7 months ago

0.9.11

7 months ago

0.9.10

7 months ago

0.9.9

7 months ago

0.9.8

7 months ago

0.9.7

7 months ago

0.9.6

7 months ago

0.9.5

7 months ago

0.9.4

7 months ago

0.9.3

7 months ago

0.9.2

7 months ago

0.9.1

7 months ago

0.9.0

7 months ago