npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

adaptic-backend

v1.0.247

Published

Backend executable CRUD functions with dynamic variables construction, and type definitions for the Adaptic AI platform.

Downloads

6,620

Readme

adaptic-backend

Adaptic Readme Banner

Description

The adaptic-backend NPM package provides a comprehensive set of executable CRUD (Create, Read, Update, Delete) functions, type and enums definitions, tailored for the Adaptic AI platform. Designed for both client-side and server-side applications, it leverages the power of Apollo Client for GraphQL interactions, and TypeGraphQL for building type-safe APIs.

It's primary goal, is to enabled developers to quickly scaffold and interact with their data models, without the need to write elaborate gql operations, or lack certainty of what data to pass through as variables or arguments. Rather, the executable CRUD functions are generated dynamically based on the models defined in the project, and are accessible under the global adaptic namespace. Rather than requiring various inputTypes, you simply pass through the data as an object that conforms to the corresponding model's type definition, and the package will handle the rest, irrespective of mutation or query operation being performed.

This package streamlines the development process by offering a unified adaptic namespace, encompassing model-specific functions, types, and enums, ensuring consistency and type safety across your application.

Features

This package offers a robust backend solution tailored for the Adaptic AI platform, featuring:

  • Dynamic Model Functions: Automatically generated CRUD functions for each content model.
  • Unified Namespace: Access all functions, types, and enums under the global adaptic namespace.
  • Type Safety: Comprehensive type definitions ensure consistency and reduce runtime errors.
  • GraphQL Integration: Seamless interaction with GraphQL endpoints using Apollo Client.
  • Dynamically contructed variables and arguments: No need to worry about constructing gql operations, simply pass through the data as an object that conforms to the model's type definition.
  • Server-Side and Client-Side Support: Versatile usage in both environments, including AWS Lambda functions.
  • Enums Namespace: Organized enums for consistent value usage across models.
  • TypeStrings Namespace: TypeStrings const definitions that are stringified versions of the various model types (including any nested types or enums within them). This is useful when wanting to pass these on to an LLM as a reference (e.g. when asking it to return a specific type of data).
  • Automated Documentation: The build script dynamically generates a list of all models and their CRUD resolvers in the README.md.

Prerequisites

To use the adaptic-backend package, the only requirement is to ensure you have several environment variables configured (see below). This can be a local development server or a production endpoints for the graphql server, and the corresponding HTTPS and WebSocket URLs.

Environment Variables

Add the folowing to your .env file or add them as environment variables in your deployment environment:

  • BACKEND_HTTPS_URL: The HTTPS URL of your GraphQL server. E.g. https://api.adaptic.ai/graphql for production, and https://localhost:4000/graphql for local development.

Example .env file:

GRAPHQL_ENDPOINT=http://localhost:4000/graphql
BACKEND_HTTPS_URL=https://api.example.com/graphql
BACKEND_WS_URL=wss://api.example.com/subscriptions

Installation

To install the adaptic-backend package, follow these steps:

  1. Install NPM Package:

    npm adaptic-backend
  2. Set Up Environment Variables: Create a .env file in the root of the project and add:

    GRAPHQL_ENDPOINT=http://localhost:4000/graphql // Or any other port you wish to use for the server

Usage

Adaptic Namespace

All the dynamically generated functions for each content model are available under the global adaptic namespace. You can import and use them in your application as follows:

Client-Side Usage (Root Level)

// client-side/index.ts
import adaptic, { types, enums } from 'adaptic-backend';

export const main = async () => {
// Example: Create a new User
const userProps = {
  name: 'John Doe',
  email: '[email protected]',
  image: 'https://example.com/johndoe.jpg',
  role: 'ADMIN',
} as types.User;


try {
  const createdUser = await adaptic.User.create(userProps) as types.User;
  console.log('Created User:', createdUser);
} catch (error) {
  console.error('Error creating user:', error);
};

// Example: Update a User
const updateUser = async () => {
  const updateProps: types.User = {
    id: 'USER_ID',
    email: '[email protected]',
  };

  try {
    const updatedUser = await adaptic.User.update(updateProps) as types.User;
    console.log('Updated User:', updatedUser);
  } catch (error) {
    console.error('Error updating user:', error);
  }
};

// Execute functions
createUser();
updateUser();

};

Server-Side Usage (Within a Lambda Function)

The only difference between client-side and server-side usage is the import statement. On the server-side, you import the functions from adaptic-backend/server/index instead of adaptic-backend, and you need to provide an Apollo Client instance to the functions with the use of 'fetch' for the HTTP link.

// server-side/lambdaFunction.mjs
import adaptic from 'adaptic-backend/server/index';

export const handler = async (event) => {
  // Parse the incoming event data
  const data = JSON.parse(event.body);

  // Validate the input
  if (!data.name || !data.type || !data.version || !data.url || !data.website || !data.description) {
    return {
      statusCode: 400,
      body: JSON.stringify({ error: 'Missing required fields: name, type, version, url, website, description' }),
    };
  }

  const dependencyObject = {
    name: data.name,
    type: data.type,
    version: data.version,
    url: data.url,
    website: data.website,
    description: data.description,
  };

  try {
    const result = await adaptic.dependency.create(dependencyObject);

    return {
      statusCode: 200,
      body: JSON.stringify(result),
    };
  } catch (error) {
    console.error('Error in createOneDependency:', error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: 'An error occurred', details: error.message }),
    };
  }
};

Types Namespace

Types associated with your data models are available under the types namespace. This provides type safety and consistency when working with your models.

// types-example.ts
import { types } from 'adaptic-backend'; 

// Define a new user
const newUser: types.UserCreateInput = {
  username: 'johndoe',
  email: '[email protected]',
  password: 'securepassword',
  role: types.UserRole.ADMIN, // Using enums
};

// Define update input
const updateUser: types.UserUpdateInput = {
  id: 'USER_ID',
  email: '[email protected]',
};

Enums Namespace

Enums associated with your data models are available under the enums namespace. This provides type safety and consistency when working with your models.

// enums-example.ts
import { enums } from 'adaptic-backend';

// Assign a user role
const userRole: enums.UserRole = enums.UserRole.MODERATOR;

// Use enums in functions
const setUserRole = (role: enums.UserRole) => {
  // Function logic
};

TypeStrings Namespace

TypeStrings are stringified versions of the various model types (including any nested types or enums within them). These are available under the typeStrings namespace.

Their purpose is to provide a reference to the type of data being requested, which can be passed on to an LLM (Language Learning Model) as a reference within a prompt or query. This is useful when asking the LLM to return a specific type of data that should conform to the model's structure.


// typeStrings-example.ts

import { typeStrings } from 'adaptic-backend';

// Use typeStrings in a prompt being sent to an LLM
const prompt = `

... some other prompt text

\${typeStrings.User}

`;

Model CRUD Resolvers

The adaptic-backend package includes a comprehensive set of CRUD (Create, Read, Update, Delete) resolvers for each of your models. Each model has the following functions:

  • ModelName.create: Create a single record.
  • ModelName.createMany: Create multiple records.
  • ModelName.update: Update a single record.
  • ModelName.delete: Delete a single record.
  • ModelName.get: Retrieve a single record by unique identifier.
  • ModelName.getAll: Retrieve all records.
  • ModelName.findMany: Retrieve multiple records based on criteria.

Model TypeStrings, Types, and available CRUD Resolvers

The adaptic-backend package includes a comprehensive set of CRUD (Create, Read, Update, Delete) resolvers for each of your models. Each model has the following functions (available directly under the adaptic namespace) and types (under the types namespace):

| Model Name | TypeString | Type | CRUD Resolvers | |------------|------------|------------|-----------------| | Account | typeStrings.Account | types.Account | adaptic.account.create, adaptic.account.createMany, adaptic.account.update, adaptic.account.delete, adaptic.account.get, adaptic.account.getAll, adaptic.account.findMany | | Action | typeStrings.Action | types.Action | adaptic.action.create, adaptic.action.createMany, adaptic.action.update, adaptic.action.delete, adaptic.action.get, adaptic.action.getAll, adaptic.action.findMany | | Alert | typeStrings.Alert | types.Alert | adaptic.alert.create, adaptic.alert.createMany, adaptic.alert.update, adaptic.alert.delete, adaptic.alert.get, adaptic.alert.getAll, adaptic.alert.findMany | | AlpacaAccount | typeStrings.AlpacaAccount | types.AlpacaAccount | adaptic.alpacaAccount.create, adaptic.alpacaAccount.createMany, adaptic.alpacaAccount.update, adaptic.alpacaAccount.delete, adaptic.alpacaAccount.get, adaptic.alpacaAccount.getAll, adaptic.alpacaAccount.findMany | | Asset | typeStrings.Asset | types.Asset | adaptic.asset.create, adaptic.asset.createMany, adaptic.asset.update, adaptic.asset.delete, adaptic.asset.get, adaptic.asset.getAll, adaptic.asset.findMany | | Authenticator | typeStrings.Authenticator | types.Authenticator | adaptic.authenticator.create, adaptic.authenticator.createMany, adaptic.authenticator.update, adaptic.authenticator.delete, adaptic.authenticator.get, adaptic.authenticator.getAll, adaptic.authenticator.findMany | | Contract | typeStrings.Contract | types.Contract | adaptic.contract.create, adaptic.contract.createMany, adaptic.contract.update, adaptic.contract.delete, adaptic.contract.get, adaptic.contract.getAll, adaptic.contract.findMany | | Customer | typeStrings.Customer | types.Customer | adaptic.customer.create, adaptic.customer.createMany, adaptic.customer.update, adaptic.customer.delete, adaptic.customer.get, adaptic.customer.getAll, adaptic.customer.findMany | | Deliverable | typeStrings.Deliverable | types.Deliverable | adaptic.deliverable.create, adaptic.deliverable.createMany, adaptic.deliverable.update, adaptic.deliverable.delete, adaptic.deliverable.get, adaptic.deliverable.getAll, adaptic.deliverable.findMany | | EconomicEvent | typeStrings.EconomicEvent | types.EconomicEvent | adaptic.economicEvent.create, adaptic.economicEvent.createMany, adaptic.economicEvent.update, adaptic.economicEvent.delete, adaptic.economicEvent.get, adaptic.economicEvent.getAll, adaptic.economicEvent.findMany | | MarketSentiment | typeStrings.MarketSentiment | types.MarketSentiment | adaptic.marketSentiment.create, adaptic.marketSentiment.createMany, adaptic.marketSentiment.update, adaptic.marketSentiment.delete, adaptic.marketSentiment.get, adaptic.marketSentiment.getAll, adaptic.marketSentiment.findMany | | NewsArticle | typeStrings.NewsArticle | types.NewsArticle | adaptic.newsArticle.create, adaptic.newsArticle.createMany, adaptic.newsArticle.update, adaptic.newsArticle.delete, adaptic.newsArticle.get, adaptic.newsArticle.getAll, adaptic.newsArticle.findMany | | NewsArticleAssetSentiment | typeStrings.NewsArticleAssetSentiment | types.NewsArticleAssetSentiment | adaptic.newsArticleAssetSentiment.create, adaptic.newsArticleAssetSentiment.createMany, adaptic.newsArticleAssetSentiment.update, adaptic.newsArticleAssetSentiment.delete, adaptic.newsArticleAssetSentiment.get, adaptic.newsArticleAssetSentiment.getAll, adaptic.newsArticleAssetSentiment.findMany | | Order | typeStrings.Order | types.Order | adaptic.order.create, adaptic.order.createMany, adaptic.order.update, adaptic.order.delete, adaptic.order.get, adaptic.order.getAll, adaptic.order.findMany | | Position | typeStrings.Position | types.Position | adaptic.position.create, adaptic.position.createMany, adaptic.position.update, adaptic.position.delete, adaptic.position.get, adaptic.position.getAll, adaptic.position.findMany | | ScheduledOptionOrder | typeStrings.ScheduledOptionOrder | types.ScheduledOptionOrder | adaptic.scheduledOptionOrder.create, adaptic.scheduledOptionOrder.createMany, adaptic.scheduledOptionOrder.update, adaptic.scheduledOptionOrder.delete, adaptic.scheduledOptionOrder.get, adaptic.scheduledOptionOrder.getAll, adaptic.scheduledOptionOrder.findMany | | Session | typeStrings.Session | types.Session | adaptic.session.create, adaptic.session.createMany, adaptic.session.update, adaptic.session.delete, adaptic.session.get, adaptic.session.getAll, adaptic.session.findMany | | StopLoss | typeStrings.StopLoss | types.StopLoss | adaptic.stopLoss.create, adaptic.stopLoss.createMany, adaptic.stopLoss.update, adaptic.stopLoss.delete, adaptic.stopLoss.get, adaptic.stopLoss.getAll, adaptic.stopLoss.findMany | | TakeProfit | typeStrings.TakeProfit | types.TakeProfit | adaptic.takeProfit.create, adaptic.takeProfit.createMany, adaptic.takeProfit.update, adaptic.takeProfit.delete, adaptic.takeProfit.get, adaptic.takeProfit.getAll, adaptic.takeProfit.findMany | | Trade | typeStrings.Trade | types.Trade | adaptic.trade.create, adaptic.trade.createMany, adaptic.trade.update, adaptic.trade.delete, adaptic.trade.get, adaptic.trade.getAll, adaptic.trade.findMany | | User | typeStrings.User | types.User | adaptic.user.create, adaptic.user.createMany, adaptic.user.update, adaptic.user.delete, adaptic.user.get, adaptic.user.getAll, adaptic.user.findMany | | VerificationToken | typeStrings.VerificationToken | types.VerificationToken | adaptic.verificationToken.create, adaptic.verificationToken.createMany, adaptic.verificationToken.update, adaptic.verificationToken.delete, adaptic.verificationToken.get, adaptic.verificationToken.getAll, adaptic.verificationToken.findMany |

Enums

The following enums are available for use under the enums namespace :

| Enum Name | |-----------| | AccountScalarFieldEnum | | ActionScalarFieldEnum | | ActionStatus | | ActionType | | AlertScalarFieldEnum | | AlertType | | AlpacaAccountScalarFieldEnum | | AlpacaAccountType | | AssetScalarFieldEnum | | AssetType | | AuthenticatorScalarFieldEnum | | ContractScalarFieldEnum | | CustomerScalarFieldEnum | | DeliverableScalarFieldEnum | | DeliverableType | | EconomicEventScalarFieldEnum | | EventImportance | | JsonNullValueFilter | | JsonNullValueInput | | MarketSentimentLevel | | MarketSentimentScalarFieldEnum | | NewsArticleAssetSentimentScalarFieldEnum | | NewsArticleScalarFieldEnum | | NullableJsonNullValueInput | | NullsOrder | | OpenaiModel | | OptionStyle | | OptionType | | OrderClass | | OrderScalarFieldEnum | | OrderSide | | OrderStatus | | OrderType | | PositionScalarFieldEnum | | QueryMode | | ScheduledOptionOrderScalarFieldEnum | | ScheduledOptionOrderStatus | | SessionScalarFieldEnum | | SortOrder | | StopLossScalarFieldEnum | | SubscriptionPlan | | TakeProfitScalarFieldEnum | | TimeInForce | | TradeScalarFieldEnum | | TradeSignal | | TradeStatus | | TradeStrategy | | TransactionIsolationLevel | | UserRole | | UserScalarFieldEnum | | VerificationTokenScalarFieldEnum |