1.4.42 • Published 8 months ago

@less-is-more/less-js v1.4.42

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

lim-js

Introduction

A quick utility library for Node.js and JavaScript, designed to simplify tasks with minimal code. Contact: ivanlin96@gmail.com

Installation

npm install @less-is-more/less-js

Getting Started

  1. Generate database mapping objects using sequelize-automatic. Please refer to its official npm documentation for details.
  2. Create an index.js file under the src directory. You can refer to the following example:

    const { Router, Db, Redis } = require("@less-is-more/less-js");
    const ApiController = require("./api/api-controller");
    const process = require("process");
    
    exports.init = (context, callback) => {
      Db.init(
        process.env.dbHost,
        process.env.dbDatabase,
        process.env.dbUser,
        process.env.dbPassword
      );
      Redis.init(process.env.redisUrl, "default", process.env.redisPassword);
      callback(null, "");
    };
    
    exports.handler = (req, res, context) => {
      res.setHeader("content-type", "application/json");
      const targets = {
        "/api": new ApiController(),
      };
      Router.route(targets, req, res, context);
    };
  3. Automatically generate CRUD operations. Execute the following command in the root of the project and select the database table:

    less-js-gen

Features

Each function has specific documentation for its parameters, which will not be repeated here.

Route

Simple routing, usually placed in the entry function index. In the Controller, you need to execute send or directly return a value (the value will be automatically converted to text to meet the send standard).

// ProductController is a class implementation
let targets = {
  "/product": ProductController,
  "/order": OrderController,
};
Router.route(targets, req, res);

Cache

Caching returns data if available, otherwise calls a function and saves the result. Uses Redis for caching and requires initialization. No annotations are used, allowing for quick testing with Mocha.

const { Cache, Redis } = require("@less-is-more/less-js");
// Initialize once, can be placed in a common area
Redis.init("address", "username", "password");

let testFunction = (a, b) => {
  console.log(a + b);
  return a + b + "";
};
// Call
const result = await Cache.get("test", 20, testFunction, testFunction, 1, 2);

Db

Currently supports MySQL. Uses Sequelize; refer to the official documentation for objects and query methods.

const { Db } = require("@less-is-more/less-js");
// Initialize once, can be placed in a common area
Db.init("address", "database", "user", "password");
// Simple query
let data = await Db.find(Menu, { ordering: "1" });
// With pagination
let data = await Db.find(Menu, { ordering: "2" }, [["id", "desc"]], 2, 3);
// Specify return columns
let data = await Db.find(Menu, { ordering: "0" }, [["id", "desc"]], 1, 10, [
  "id",
]);
// Add, Product is a generated object, refer to sequelize-automate
Db.add(Product, {
  code: "test",
  name: "product",
});
// Update
Db.update(Menu, { name: "test name2", ordering: "2" }, { id: 7 });
// Delete, limited to one record
Db.delOne(Menu, { code: "abc" });

Redis

Simple encapsulation. Commands are the same as the official ones.

const { Redis } = require("@less-is-more/less-js");
// Initialize once, can be placed in a common area
Redis.init("address", "username", "password");
// Execute command, call official command, parameter order is the same
let result = await Redis.exec("get", "test");

Param

Parameter handling tool. Checks parameters, throws an Error on failure, returns a parameter set on success. Primarily uses methods from the validator package: https://www.npmjs.com/package/validator. The check method adds isNotEmpty and require. Supports multiple checks, separated by spaces. By default, is methods do not include empty values.

let params = await Param.checkParams([
  ["return parameter key", value, "isNotEmpty", "error message"],
  ["return parameter key2", value2, "isNotEmpty", "error message2"],
]);
// Check if empty
Param.isBlank(value);

Sms

Currently supports Alibaba Cloud's SMS service.

// Initialize once, can be placed in a common area
Sms.init("key", "secret", "token");
Sms.debug(true);
Sms.send("phone number", { info: "SMS parameters" }, "signature", "SMS code");
1.4.42-0

8 months ago

1.4.41-0

1 year ago

1.4.42

8 months ago

1.4.41

1 year ago

1.4.38

1 year ago

1.4.39-0

1 year ago

1.4.37

1 year ago

1.4.37-0

1 year ago

1.4.32-2

2 years ago

1.4.32-1

2 years ago

1.4.32-0

2 years ago

1.4.36-1

1 year ago

1.4.32-5

2 years ago

1.4.36-0

1 year ago

1.4.33

2 years ago

1.4.32-4

2 years ago

1.4.32

2 years ago

1.4.32-3

2 years ago

1.4.35

1 year ago

1.4.34

1 year ago

1.4.36

1 year ago

1.4.34-0

1 year ago

1.4.34-1

1 year ago

1.4.33-1

2 years ago

1.4.33-0

2 years ago

1.4.33-3

2 years ago

1.4.33-2

2 years ago

1.4.31

2 years ago

1.4.31-0

2 years ago

1.4.30

2 years ago

1.4.28

2 years ago

1.4.29

2 years ago

1.4.28-3

2 years ago

1.4.28-2

2 years ago

1.4.28-1

2 years ago

1.4.28-0

2 years ago

1.4.27-9

2 years ago

1.4.27-4

2 years ago

1.4.27-3

2 years ago

1.4.27-2

2 years ago

1.4.27-8

2 years ago

1.4.27-7

2 years ago

1.4.27-6

2 years ago

1.4.27-5

2 years ago

1.4.27-13

2 years ago

1.4.27-12

2 years ago

1.4.27-11

2 years ago

1.4.27-10

2 years ago

1.4.27-17

2 years ago

1.4.27-16

2 years ago

1.4.27-15

2 years ago

1.4.27-14

2 years ago

1.4.27-0

2 years ago

1.4.27-1

2 years ago

1.4.26

2 years ago

1.4.26-5

2 years ago

1.4.26-4

2 years ago

1.4.26-3

2 years ago

1.4.26-2

2 years ago

1.4.26-1

2 years ago

1.4.26-0

2 years ago

1.4.25

2 years ago

1.4.24

2 years ago

1.4.23

2 years ago

1.4.22

2 years ago

1.4.21

2 years ago

1.4.20

2 years ago

1.4.19

2 years ago

1.4.15

2 years ago

1.4.14

2 years ago

1.4.17

2 years ago

1.4.16

2 years ago

1.4.18

2 years ago

1.4.13

2 years ago

1.4.12

2 years ago

1.4.9

2 years ago

1.4.11

2 years ago

1.4.8

2 years ago

1.4.10

2 years ago

1.4.7

2 years ago

1.4.6

3 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.34

4 years ago

1.2.35

3 years ago

1.2.33

4 years ago

1.2.36

3 years ago

1.2.29

4 years ago

1.2.30

4 years ago

1.2.31

4 years ago

1.2.32

4 years ago

1.2.27

4 years ago

1.2.28

4 years ago

1.2.26

4 years ago

1.2.25

4 years ago

1.2.18

4 years ago

1.2.19

4 years ago

1.2.20

4 years ago

1.2.23

4 years ago

1.2.24

4 years ago

1.2.21

4 years ago

1.2.22

4 years ago

1.2.19-1

4 years ago

1.2.16

4 years ago

1.2.17

4 years ago

1.2.14

4 years ago

1.2.15

4 years ago

1.2.12

4 years ago

1.2.13

4 years ago

1.2.10

4 years ago

1.2.11

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.9

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.12

4 years ago

1.1.13

4 years ago

1.1.11

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.0.38

5 years ago