zeroworx

zeroworx

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
zeroworx
1.4.12 months ago5 months agoMinified + gzip package size for zeroworx in KB

Readme

ZEROWORX ##

Simple Application Pattern ##

src/main.ts
import base from 'zeroworx';

base.app.utilize.module('zeroworx');
base.app.utilize.module('typescript');
base.app.utilize.module('express');
base.app.utilize.module('axios');
base.app.utilize.module('prisma');

base.app.register.adapter('service/sample');
base.app.register.adapter('integrator/sample');

base.app.register.model('data/sample');
base.app.register.model('constant/sample');

base.app.register.router('service/sample');
base.app.register.router('integrator/sample');
base.app.register.router('data/sample');
base.app.register.router('constant/sample');

base.app.register.schema('service/sample');
base.app.register.schema('integrator/sample');

base.app.mount();
base.app.start();

src/adapter/service/sample.ts
import base from 'zeroworx';

const sampleAdapter = {
  execute: async (ctx: any = {}) => {
    try {
      ctx.spec = {
        protocol: 'web',
        method: 'get',
        root: 'https://api.sample.com',
        url: '/sample',
        query: {
          sample: ctx.sample || 'random',
        },
      };
      ctx.yield = await base.system.adapter.exec(ctx);
      ctx.result = base.kit.construct(ctx, 'adapter:success:result');
      ctx.extent = base.kit.construct(ctx, 'adapter:success:extent');
      base.logger.info('Adapter:Sample:Execute', ctx.extent);
      return ctx.result;
    } catch (error: any) {
      ctx.error = error;
      ctx.result = base.kit.construct(ctx, 'adapter:error:result');
      ctx.extent = base.kit.construct(ctx, 'adapter:error:extent');
      base.logger.error('Adapter:Sample:Execute', ctx.extent);
      throw ctx.error;
    };
  },
};

export default sampleAdapter;

src/model/data/sample.ts
import base from 'zeroworx';

const sampleModel = {
  search: async (ctx: any) => {
    try {
      ctx.spec = {
        database: 'sample',
        mapper: 'sample',
        command: 'search',
        ...base.kit.parse(ctx.criteria),
      };
      ctx.yield = await base.system.model.exec(ctx);
      ctx.result = base.kit.construct(ctx, 'model:success:result');
      ctx.extent = base.kit.construct(ctx, 'model:success:extent');
      base.logger.info('Model:Sample:Search', ctx.extent);
      return ctx.result;
    } catch (error: any) {
      ctx.error = error;
      ctx.result = base.kit.construct(ctx, 'model:error:result');
      ctx.extent = base.kit.construct(ctx, 'model:error:extent');
      base.logger.error('Model:Sample:Search', ctx.extent);
      throw ctx.error;
    };
  },
  find: async (ctx: any) => {
    try {
      ctx.spec = {
        database: 'sample',
        mapper: 'sample',
        command: 'find',
        where: { id: Number(ctx.id) },
      };
      ctx.yield = await base.system.model.exec(ctx);
      ctx.result = base.kit.construct(ctx, 'model:success:result');
      ctx.extent = base.kit.construct(ctx, 'model:success:extent');
      base.logger.info('Model:Sample:Find', ctx.extent);
      return ctx.result;
    } catch (error: any) {
      ctx.error = error;
      ctx.result = base.kit.construct(ctx, 'model:error:result');
      ctx.extent = base.kit.construct(ctx, 'model:error:extent');
      base.logger.error('Model:Sample:Find', ctx.extent);
      throw ctx.error;
    };
  },
  create: async (ctx: any) => {
    try {
      ctx.spec = {
        database: 'sample',
        mapper: 'sample',
        command: 'create',
        data: ctx.data,
      };
      ctx.yield = await base.system.model.exec(ctx);
      ctx.result = base.kit.construct(ctx, 'model:success:result');
      ctx.extent = base.kit.construct(ctx, 'model:success:extent');
      base.logger.info('Model:Sample:Create', ctx.extent);
      return ctx.result;
    } catch (error: any) {
      ctx.error = error;
      ctx.result = base.kit.construct(ctx, 'model:error:result');
      ctx.extent = base.kit.construct(ctx, 'model:error:extent');
      base.logger.error('Model:Sample:Create', ctx.extent);
      throw ctx.error;
    };
  },
  update: async (ctx: any) => {
    try {
      ctx.spec = {
        database: 'sample',
        mapper: 'sample',
        command: 'update',
        where: { id: Number(ctx.id) },
        data: ctx.data,
      };
      ctx.yield = await base.system.model.exec(ctx);
      ctx.result = base.kit.construct(ctx, 'model:success:result');
      ctx.extent = base.kit.construct(ctx, 'model:success:extent');
      base.logger.info('Model:Sample:Update', ctx.extent);
      return ctx.result;
    } catch (error: any) {
      ctx.error = error;
      ctx.result = base.kit.construct(ctx, 'model:error:result');
      ctx.extent = base.kit.construct(ctx, 'model:error:extent');
      base.logger.error('Model:Sample:Update', ctx.extent);
      throw ctx.error;
    };
  },
  delete: async (ctx: any) => {
    try {
      ctx.spec = {
        database: 'sample',
        mapper: 'sample',
        command: 'delete',
        where: { id: Number(ctx.id) },
      };
      ctx.yield = await base.system.model.exec(ctx);
      ctx.result = base.kit.construct(ctx, 'model:success:result');
      ctx.extent = base.kit.construct(ctx, 'model:success:extent');
      base.logger.info('Model:Sample:Delete', ctx.extent);
      return ctx.result;
    } catch (error: any) {
      ctx.error = error;
      ctx.result = base.kit.construct(ctx, 'model:error:result');
      ctx.extent = base.kit.construct(ctx, 'model:error:extent');
      base.logger.error('Model:Sample:Delete', ctx.extent);
      throw ctx.error;
    };
  },
};

export default sampleModel;

src/router/service/sample.ts
import base from 'zeroworx';

const sampleRouter = {
  execute: base.app.web.router.get('/service/sample',
    async (req: any, res: any) => {
      try {
        req.spec = {
          adapter: 'sample',
          command: 'execute',
          thread: req.thread,
          sample: req.query.sample,
        };
        req.yield = await base.system.router.exec(req);
        req.result = base.kit.construct(req, 'router:success:result');
        req.extent = base.kit.construct(req, 'router:success:extent');
        base.logger.info('Router:Sample:Execute', req.extent);
        res.status(200).json(req.result);
      } catch (error: any) {
        req.error = error;
        req.result = base.kit.construct(req, 'router:error:result');
        req.extent = base.kit.construct(req, 'router:error:extent');
        base.logger.error('Router:Sample:Execute', req.extent);
        res.status(400).json(req.result);
      };
    },
  ),
};

export default sampleRouter;

src/router/data/sample.ts
import base from 'zeroworx';

const sampleRouter = {
  search: base.app.web.router.get('/data/sample',
    async (req: any, res: any) => {
      try {
        req.spec = {
          model: 'sample',
          command: 'search',
          thread: req.thread,
          criteria: req.query.criteria,
        };
        req.yield = await base.system.router.exec(req);
        req.result = base.kit.construct(req, 'router:success:result');
        req.extent = base.kit.construct(req, 'router:success:extent');
        base.logger.info('Router:Sample:Search', req.extent);
        res.status(200).json(req.result);
      } catch (error: any) {
        req.error = error;
        req.result = base.kit.construct(req, 'router:error:result');
        req.extent = base.kit.construct(req, 'router:error:extent');
        base.logger.error('Router:Sample:Search', req.extent);
        res.status(400).json(req.result);
      };
    },
  ),
  find: base.app.web.router.get('/data/sample/:id',
    async (req: any, res: any) => {
      try {
        req.spec = {
          model: 'sample',
          command: 'find',
          thread: req.thread,
          id: req.params.id,
        };
        req.yield = await base.system.router.exec(req);
        req.result = base.kit.construct(req, 'router:success:result');
        req.extent = base.kit.construct(req, 'router:success:extent');
        base.logger.info('Router:Sample:Find', req.extent);
        res.status(200).json(req.result);
      } catch (error: any) {
        req.error = error;
        req.result = base.kit.construct(req, 'router:error:result');
        req.extent = base.kit.construct(req, 'router:error:extent');
        base.logger.error('Router:Sample:Find', req.extent);
        res.status(400).json(req.result);
      };
    },
  ),
  create: base.app.web.router.post('/data/sample',
    async (req: any, res: any) => {
      try {
        req.spec = {
          model: 'sample',
          command: 'create',
          thread: req.thread,
          data: req.body,
        };
        req.yield = await base.system.router.exec(req);
        req.result = base.kit.construct(req, 'router:success:result');
        req.extent = base.kit.construct(req, 'router:success:extent');
        base.logger.info('Router:Sample:Create', req.extent);
        res.status(200).json(req.result);
      } catch (error: any) {
        req.error = error;
        req.result = base.kit.construct(req, 'router:error:result');
        req.extent = base.kit.construct(req, 'router:error:extent');
        base.logger.error('Router:Sample:Create', req.extent);
        res.status(400).json(req.result);
      };
    },
  ),
  update: base.app.web.router.put('/data/sample/:id',
    async (req: any, res: any) => {
      try {
        req.spec = {
          model: 'sample',
          command: 'update',
          thread: req.thread,
          id: req.params.id,
          data: req.body,
        };
        req.yield = await base.system.router.exec(req);
        req.result = base.kit.construct(req, 'router:success:result');
        req.extent = base.kit.construct(req, 'router:success:extent');
        base.logger.info('Router:Sample:Update', req.extent);
        res.status(200).json(req.result);
      } catch (error: any) {
        req.error = error;
        req.result = base.kit.construct(req, 'router:error:result');
        req.extent = base.kit.construct(req, 'router:error:extent');
        base.logger.error('Router:Sample:Update', req.extent);
        res.status(400).json(req.result);
      };
    },
  ),
  delete: base.app.web.router.delete('/data/sample/:id',
    async (req: any, res: any) => {
      try {
        req.spec = {
          model: 'sample',
          command: 'delete',
          thread: req.thread,
          id: req.params.id,
        };
        req.yield = await base.system.router.exec(req);
        req.result = base.kit.construct(req, 'router:success:result');
        req.extent = base.kit.construct(req, 'router:success:extent');
        base.logger.info('Router:Sample:Delete', req.extent);
        res.status(200).json(req.result);
      } catch (error: any) {
        req.error = error;
        req.result = base.kit.construct(req, 'router:error:result');
        req.extent = base.kit.construct(req, 'router:error:extent');
        base.logger.error('Router:Sample:Delete', req.extent);
        res.status(400).json(req.result);
      };
    },
  ),
};

export default sampleRouter;

src/schema/service/sample.ts
import base from 'zeroworx';

const sampleSchema = {
  sampleAction: {
    req: {
      headers: {
        'sampleAttribute': base.app.lib.lc.joi.string(),
      },
      params: {
        'sampleAttribute': base.app.lib.lc.joi.string(),
      },
      query: {
        'sampleAttribute': base.app.lib.lc.joi.string(),
      },
      body: {
        'sampleAttribute': base.app.lib.lc.joi.string(),
      },
    },
    res: {
      headers: {
        'sampleAttribute': base.app.lib.lc.joi.string(),
      },
      params: {
        'sampleAttribute': base.app.lib.lc.joi.string(),
      },
      query: {
        'sampleAttribute': base.app.lib.lc.joi.string(),
      },
      body: {
        'sampleAttribute': base.app.lib.lc.joi.string(),
      },
    },
  },
};

export default sampleSchema;

database/managed/schema.prisma
datasource db {
  provider = "sqlite"
  url      = env("SAMPLE_SQLITE_DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
  output   = "connectors"
}

model sampleManagedModel {
  id              Int       @id @default(autoincrement())
  createdTime     DateTime  @default(now())
  updatedTime     DateTime  @updatedAt
  deletedTime     DateTime?
  sampleAttribute String
}

database/managed/seed.json
{
  "sampleManagedModel": {
    "1": {
      "sampleAttribute": "sampleValue"
    },
    "2": {
      "sampleAttribute": "sampleValue"
    },
    "3": {
      "sampleAttribute": "sampleValue"
    }
  }
}

doc/service/sample.json
{
  "/service/sample": {
    "get": {
      "summary": "Execute Sample",
      "tags": [
        "Service - Sample"
      ],
      "parameters": [
        {
          "in": "query",
          "required": false,
          "name": "sample",
          "schema": {
            "type": "string"
          }
        }
      ],
      "responses": {
        "200": {
          "description": "Success"
        },
        "400": {
          "description": "Error"
        }
      }
    }
  }
}

doc/data/sample.json
{
  "/data/sample": {
    "get": {
      "summary": "Search Sample",
      "tags": [
        "Data - Sample"
      ],
      "parameters": [
        {
          "in": "query",
          "required": false,
          "name": "criteria",
          "schema": {
            "type": "string"
          }
        }
      ],
      "responses": {
        "200": {
          "description": "Success"
        },
        "400": {
          "description": "Error"
        }
      }
    },
    "post": {
      "summary": "Create Sample",
      "tags": [
        "Data - Sample"
      ],
      "parameters": [
        {
          "in": "body",
          "required": false,
          "name": "data",
          "schema": {
            "type": "object",
            "properties": {
              "sample": {
                "type": "string"
              }
            }
          }
        }
      ],
      "responses": {
        "200": {
          "description": "Success"
        },
        "400": {
          "description": "Error"
        }
      }
    }
  },
  "/data/sample/{id}": {
    "get": {
      "summary": "Find Sample",
      "tags": [
        "Data - Sample"
      ],
      "parameters": [
        {
          "in": "path",
          "required": false,
          "name": "id",
          "schema": {
            "type": "string"
          }
        }
      ],
      "responses": {
        "200": {
          "description": "Success"
        },
        "400": {
          "description": "Error"
        }
      }
    },
    "put": {
      "summary": "Update Sample",
      "tags": [
        "Data - Sample"
      ],
      "parameters": [
        {
          "in": "path",
          "required": false,
          "name": "id",
          "schema": {
            "type": "string"
          }
        },
        {
          "in": "body",
          "required": false,
          "name": "data",
          "schema": {
            "type": "object",
            "properties": {
              "sample": {
                "type": "string"
              }
            }
          }
        }
      ],
      "responses": {
        "200": {
          "description": "Success"
        },
        "400": {
          "description": "Error"
        }
      }
    },
    "delete": {
      "summary": "Delete Sample",
      "tags": [
        "Data - Sample"
      ],
      "parameters": [
        {
          "in": "path",
          "required": false,
          "name": "id",
          "schema": {
            "type": "string"
          }
        }
      ],
      "responses": {
        "200": {
          "description": "Success"
        },
        "400": {
          "description": "Error"
        }
      }
    }
  }
}