Socket
Book a DemoInstallSign in
Socket

koma

Package Overview
Dependencies
Maintainers
2
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koma

Combind `model` `route` `controller` `services` `validators`... everything you want in just one module

0.2.3-rc1
latest
npmnpm
Version published
Weekly downloads
37
Maintainers
2
Weekly downloads
 
Created
Source

koma

Combind model route controller services validators... everything you want in just one module

install

> yarn add koma

start

const { koma } = require("koma");

koma.$use({
  routes: {
    "get /": async ctx => (ctx.body = "Hello World")
  },
  start: {
    config: {
      PORT: 8001,
      RUN: true
    }
  }
});

Graphql-compose

const _ = require("lodash");
const { MongooseUtils } = require("koma/plugins/mongoose");
const { models } = MongooseUtils;
const { signJWT } = require("koma/plugins/jwt");

module.exports = {
  name: "User",
  gql: {
    resolvers: {
      Mutation: {
        Login: {
          type: `type Login {account: Account!, jwt: String!}`,
          args: { identifier: "String!", password: "String!" },
          hide: true,
          resolve: async ({ args }) => {
            const { identifier, password } = args;
            const account = await models("Account")
              .findOne({ $or: [{ username: identifier }] })
              .select("+password");
            if (!account) throw new Error("No user founded");
            const validPassword = await account.verifyPassword(password);
            if (validPassword) {
              delete account.password;
              return { account, jwt: signJWT({ _id: account._id, role: account.role }) };
            } else {
              throw new Error("Invalid password or username");
            }
          }
        },
        SignUp: {
          type: `type User {username: String!}`,
          args: {
            username: "String!",
            password: "String!"
          },
          resolve: async ({ args }) => {
            const user = await models("Account").create(args);
            return user;
          }
        }
      }
    }
  },
  models: {
    Account: {
      schema: {
        username: { type: "string", required: true, unique: true },
        password: { type: "string", select: false, required: true, bcrypt: true, hidden: true }
      },
      options: {
        timestamp: true
      }
    }
  }
};

FAQs

Package last updated on 18 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.