@alphanova/builder
Version:
A fully fledged facade that facilitates object manipulation
120 lines (119 loc) • 4.71 kB
TypeScript
import { QueryClient } from "@tanstack/react-query";
import { Builder } from "./Builder";
import { Structure } from "./Document";
export interface Rule<Action extends string, Model extends Structure, Container extends Builder<Model, Container>> {
/**
* This is the initial state of the data. Redux will be initially populated with this data.
* @example { '1': { id: 1, name: 'John' }, '2': { id: 2, name: 'Jane' } }
*/
readonly initial: Record<string, Structure>;
/**
* This will represent the action that will be dispatched to the Redux store.
* It will also represent the name of the data in the Redux store (Lowercase).
* Make sure to be faithful to the Redux naming conventions.
* Your implementation of Redux should match the action name you provide here.
* This should preferably match the name of the model in the database. If it does not, make sure to provide the api_route.
* Use this value when invoking the useStorage hook.
* The syntax is: [ADD/REMOVE...]_[ACTION]. Only include the name of the model: USERS NOT ADD_USERS
* @example 'USERS'
*
*/
readonly action: Action;
/**
* This is the builder that will be used to build, hold, debug, test and validate the data.
* Make sure to provide a class that extends the Builder class. The class has to override the instance method.
* @example UserBuilder
*/
readonly builder: Container;
/**
* This is the route that will be used to fetch the data from the server.
* If not provided the system will use the action name as the route. (Lowercase with an 's' at the end)
* @example 'users'
*/
readonly api_route?: string;
/**
* This is the list of the actions that will be provided by using the useStorage hook.
*/
readonly providers?: {
/**
* Retrieves the entire associated data from Redux.
* @default true
*/
GET?: boolean;
/**
* Adds an object to the Redux store and pushes it to the server.
* @default false
*/
ADD?: boolean;
/**
* Adds an object to the Redux store, then, and only then, pushes it to the server. The id of the object will then be rectified from the server.
* @default false
*/
ASYNC_ADD?: boolean;
/**
* Pushes an object to the server without adding it to the Redux Store.
* @default false
*/
SERVER_ADD?: boolean;
/**
* Updates an object on the server without updating it to the Redux Store.
* @default false
*/
SERVER_UPDATE?: boolean;
/**
* Removes an object from the server without removing it from the Redux Store.
* @default false
*/
SERVER_REMOVE?: boolean;
/**
* Adds an object to the Redux store without pushing it to the server.
* @default false
*/
LOCAL_ADD?: boolean;
/**
* Removes an object from the Redux store without deleting it from the server.
* @default false
*/
LOCAL_DELETE?: boolean;
/**
* Removes an object from the Redux store and deletes it from the server (WIP).
* @default false
*/
DELETE?: boolean;
/**
* Updates an object in the Redux store without updating it on the server.
* @default false
*/
LOCAL_UPDATE?: boolean;
/**
* Updates an object in the Redux store and updates it on the server.
* @default false
*/
UPDATE?: boolean;
/**
* Retrieves a single object from the server and adds it to the Redux store.
* The data to be retrieved will be fetched from master_url/api_route/{id}
* @default false
*/
FETCH_ONE?: boolean;
/**
* Retrieves a list of objects from the server and adds them to the Redux store.
* The data to be retrieved will be fetched from master_url/api_route/
* @default false
*/
FETCH_MANY?: boolean;
/**
* Retrieves a list of objects from the server and adds them to the Redux store.
* Takes a route array that will be used to build the route.
* The data to be retrieved will be fetched from master_url/api_route/{route}
* @default false
*/
FETCH_ANY?: boolean;
};
}
export interface Config<Action extends string, Model extends Structure, Container extends Builder<Model, Container>> {
queryClient: QueryClient;
server: string;
token: string;
rules: Rule<Action, Model, Container>[];
}