@less-is-more/less-js
v1.4.41
Published
Fast develop kit for nodejs
Downloads
461
Readme
lim-js
Introduction
A quick utility library for Node.js and JavaScript, designed to simplify tasks with minimal code. Contact: [email protected]
Installation
npm install @less-is-more/less-js
Getting Started
- Generate database mapping objects using
sequelize-automatic
. Please refer to its official npm documentation for details. - Create an
index.js
file under thesrc
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); };
- 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");