Skip to content

Commit

Permalink
🦺 add context and implement preAct
Browse files Browse the repository at this point in the history
  • Loading branch information
hemedani committed Apr 20, 2023
1 parent f74fd02 commit a3db1b3
Show file tree
Hide file tree
Showing 24 changed files with 237 additions and 211 deletions.
3 changes: 2 additions & 1 deletion src/acts/setAct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ActInp, Services } from "./types.ts";

export const setAct = (
acts: Services,
{ type, schema, actName, validator, fn }: ActInp,
{ type, schema, actName, validator, fn, preAct }: ActInp,
) => {
if (!acts.main[type]) {
throw new Error(`Invalid type: ${type}`);
Expand All @@ -25,5 +25,6 @@ export const setAct = (
acts.main[type][schema][actName] = {
validator,
fn,
preAct,
};
};
74 changes: 40 additions & 34 deletions src/acts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ export type ActFn = (body: Body) => any;
* @interface
*/
export interface Act {
validator: Struct<any>;
fn: ActFn;
validator: Struct<any>;
fn: ActFn;
preAct?: Function[];
}
/**
* Acts include tow features : dynamic and static
Expand Down Expand Up @@ -72,16 +73,16 @@ export interface Act {
* },
*/
export interface Acts {
dynamic: {
[key: string]: {
[key: string]: Act;
};
dynamic: {
[key: string]: {
[key: string]: Act;
};
static: {
[key: string]: {
[key: string]: Act;
};
};
static: {
[key: string]: {
[key: string]: Act;
};
};
}

/**
Expand Down Expand Up @@ -131,8 +132,8 @@ export interface Acts {
* if type of string we get answer of req with http Request , but if type of it equal to Acts with anwer to req directly
*/
export interface Services {
main: Acts;
[key: string]: Acts | string | undefined;
main: Acts;
[key: string]: Acts | string | undefined;
}

/**
Expand All @@ -141,27 +142,32 @@ export interface Services {
* @interface
*/
export interface ActInp {
/**
* type of action static or dynamic
* when equal to static for get static file (isdb)
* else for dynamic request(request to db ideed)
*/
type: "static" | "dynamic";
/**
* name of schema that set action for it
*/
schema: string;
/**
* name of action
*/
actName: string;
/**
* validator function for example for validion input date
*/
validator: Struct<any>;
/**
* type of action static or dynamic
* when equal to static for get static file (isdb)
* else for dynamic request(request to db ideed)
*/
type: "static" | "dynamic";
/**
* name of schema that set action for it
*/
schema: string;
/**
* name of action
*/
actName: string;
/**
* validator function for example for validion input date
*/
validator: Struct<any>;

/**
* function
*/
fn: ActFn;
/**
* function
*/
fn: ActFn;

/**
* these functions use to implement somthing in context before run fn
*/
preAct?: Function[];
}
65 changes: 65 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/// context type ----
//

import { LesanContenxt } from "./types.ts";

/**
* variable of context
* @defaultValue
* the value of context is '{}'
*/
let context: LesanContenxt = {
Headers: new Headers(),
};

/**
* @returns The contextObj
*/
const getContextModel = () => context;

/**
* @returns The contextObj
*/
const setContext = (
obj: Record<string, any>,
) => (context = { ...getContextModel(), ...obj });
/**
* asigne all of value that we want to carry
* @param con - objects of key , value
* @returns nothing
*/
const addContexts = (con: LesanContenxt) => context = con;

/**
* add values to previous values that we want to carry
* @param con - objects of key , value
* @returns nothing
*/
const addContext = (con: LesanContenxt) => context = { ...context, con };
/**
* add Request to Context because the requeste may be required in later functions
* @param con - request of user
* @returns nothing
*/
const addReqToContext = (con: Request) => context["Request"] = con;

/**
* add Request Header to Context because the requeste may be required in later functions
* @param con - Headers of user
* @returns nothing
*/
const addHeaderToContext = (con: Headers) => context["Headers"] = con;

/**
* this function is create for define all things in local scope
* and also all functions of context define in this function
* @returns - return objects of all functions that define in this function
*/
export const contextFns = {
getContextModel,
setContext,
addContexts,
addContext,
addReqToContext,
addHeaderToContext,
};
2 changes: 2 additions & 0 deletions src/lesan.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { acts, Services } from "./acts/mod.ts";
import { contextFns } from "./context.ts";
import { Database } from "./deps.ts";
import { Model, schemas } from "./models/mod.ts";
import { odm } from "./odm/mod.ts";
Expand All @@ -20,6 +21,7 @@ export const lesan = () => {
schemas: { ...schemas(schemasObj) },
odm: { ...odm(schemasObj) },
runServer: lesanServer(schemasObj, actsObj),
contextFns,
generateSchemTypes: generateSchemTypes(schemasObj),
};
};
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from "./acts/types.ts";
export * from "./deps.ts";
export * from "./lesan.ts";
export * from "./models/types.ts";
export * from "./types.ts";
62 changes: 0 additions & 62 deletions src/models/context.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/models/inrelation/addInrelations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ISchema } from "../mod.ts";
import { schemaFns } from "../schema.ts";
import { ISchema, schemaFns } from "../mod.ts";
import { InRelation } from "./../types.ts";
/**
* asign inrelation of schema that schema has relation with it example of relation of SQL that we keep foriegn key.
Expand Down
3 changes: 1 addition & 2 deletions src/models/inrelation/addOneInRelation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ISchema } from "../mod.ts";
import { schemaFns } from "../schema.ts";
import { ISchema, schemaFns } from "../mod.ts";

/**
* add one innerRelation to previous inrelation of schema that schema has relation with it example of relation of SQL that we keep foriegn key.
Expand Down
3 changes: 1 addition & 2 deletions src/models/inrelation/getInrelations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ISchema } from "../mod.ts";
import { schemaFns } from "../schema.ts";
import { ISchema, schemaFns } from "../mod.ts";

/**
* get all of inerRealation of one schema
Expand Down
2 changes: 0 additions & 2 deletions src/models/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { contextFns } from "./context.ts";
import { inrelationFns } from "./inrelation/mod.ts";
import { outrelationFns } from "./outrelation/mod.ts";
import { pureFns } from "./pure/mod.ts";
Expand All @@ -17,7 +16,6 @@ export const schemas = (schemas: ISchema) => {
return {
...schemaFns(schemas),
...inrelationFns(schemas),
...contextFns(),
...outrelationFns(schemas),
...pureFns(schemas),
...relationFns(schemas),
Expand Down
3 changes: 1 addition & 2 deletions src/models/outrelation/addOneOutRelation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ISchema } from "../mod.ts";
import { schemaFns } from "../schema.ts";
import { ISchema, schemaFns } from "../mod.ts";

/**
* add addOneOutRelation to previous outrelation of schema.
Expand Down
3 changes: 1 addition & 2 deletions src/models/outrelation/addOutRelations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ISchema } from "../mod.ts";
import { schemaFns } from "../schema.ts";
import { ISchema, schemaFns } from "../mod.ts";
import { OutRelation } from "../types.ts";

/**
Expand Down
3 changes: 1 addition & 2 deletions src/models/outrelation/getOutRelations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ISchema } from "../mod.ts";
import { schemaFns } from "../schema.ts";
import { ISchema, schemaFns } from "../mod.ts";

/**
* getOutRelations of one schema
Expand Down
3 changes: 1 addition & 2 deletions src/models/pure/addPureModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ISchema, PureModel } from "../mod.ts";
import { schemaFns } from "../schema.ts";
import { ISchema, PureModel, schemaFns } from "../mod.ts";

/**
* add pure feature of model to schema
Expand Down
3 changes: 1 addition & 2 deletions src/models/pure/getPureModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ISchema } from "../mod.ts";
import { schemaFns } from "../schema.ts";
import { ISchema, schemaFns } from "../mod.ts";

/**
* get pure features of one schema
Expand Down
3 changes: 1 addition & 2 deletions src/models/pure/getPureModelByNameAndKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ISchema } from "../mod.ts";
import { schemaFns } from "../schema.ts";
import { ISchema, schemaFns } from "../mod.ts";

/**
* get pure one feature of one schema by name of schema and key of feature
Expand Down
3 changes: 1 addition & 2 deletions src/models/relation/getRelation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ISchema } from "../mod.ts";
import { schemaFns } from "../schema.ts";
import { ISchema, schemaFns } from "../mod.ts";

/**
* get inerRelatrion or outerRealtion of one schema
Expand Down
2 changes: 1 addition & 1 deletion src/odm/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
UpdateOptions,
} from "../deps.ts";
import { InRelation, ISchema, OutRelation, PureModel } from "../models/mod.ts";
import { schemaFns } from "../models/schema.ts";
import { schemaFns } from "../models/mod.ts";
import { throwError } from "../utils/throwError.ts";
import {
addOutrelation,
Expand Down
1 change: 0 additions & 1 deletion src/odm/utils/addOutrelation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Bson } from "../../deps.ts";
import { schemaFns } from "../../models/schema.ts";

export const addOutrelation = (
schemaName: string,
Expand Down
2 changes: 1 addition & 1 deletion src/odm/utils/checkInsertRelation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bson, Database } from "../../deps.ts";
import { schemaFns } from "../../models/schema.ts";
import { schemaFns } from "../../models/mod.ts";
import { getPureFromDoc } from "./mod.ts";

export const checkRelation = (
Expand Down
2 changes: 1 addition & 1 deletion src/odm/utils/getPureFromDoc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { schemaFns } from "../../models/schema.ts";
import { schemaFns } from "../../models/mod.ts";

export const getPureFromDoc = (
collectionName: string,
Expand Down
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Context Holds values and carries them in functions.
* @example
* {
* Request: "values of Req",
* user: {
* "_id":1,
* "name":"ali",
* "lastName":"Alavi"
* "role":"Admin"
* } *
*/
export interface LesanContenxt {
[key: string]: any;
Headers: Headers;
}
Loading

0 comments on commit a3db1b3

Please sign in to comment.