@rdyl/node-mysql 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ ## 0.0.1 (2025-01-22)
2
+
3
+
4
+
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ ## Installation
2
+
3
+ ```bash
4
+ $ npm install @rdyl/node-mysql
5
+ # or
6
+ $ yarn add @rdyl/node-mysql
7
+ ```
8
+
9
+ ## Getting started
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./selector"), exports);
18
+ __exportStar(require("./meta"), exports);
19
+ __exportStar(require("./utils"), exports);
package/dist/meta.js ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConditionKey = exports.SortKey = exports.RelationKey = exports.Options = exports.Condition = void 0;
4
+ var utils_1 = require("./utils");
5
+ var Condition = /** @class */ (function () {
6
+ function Condition(props) {
7
+ this.field = props.field;
8
+ this.value = props.value;
9
+ this.condition = props.condition || ConditionKey.eq;
10
+ this.relation = props.relation || RelationKey.and;
11
+ }
12
+ Object.defineProperty(Condition.prototype, "_valueSql", {
13
+ get: function () {
14
+ var target = '';
15
+ var _a = this, condition = _a.condition, value = _a.value;
16
+ if (condition == ConditionKey.include) {
17
+ if (Array.isArray(value) && value.length > 0) {
18
+ target = utils_1.MysqlParser.toValuesOf(value);
19
+ }
20
+ }
21
+ else if (condition == ConditionKey.area) {
22
+ if (Array.isArray(value) && value.length > 0) {
23
+ target = "".concat(value[0], " AND ").concat(value[1]);
24
+ }
25
+ }
26
+ else {
27
+ target = utils_1.MysqlParser.valueTypeOf(value);
28
+ }
29
+ return target;
30
+ },
31
+ enumerable: false,
32
+ configurable: true
33
+ });
34
+ Object.defineProperty(Condition.prototype, "result", {
35
+ get: function () {
36
+ var target = '';
37
+ var _a = this, condition = _a.condition, field = _a.field;
38
+ if (this._valueSql) {
39
+ if (condition == ConditionKey.not) {
40
+ target += 'NOT ';
41
+ }
42
+ target += "".concat(field, " ").concat(condition, " ").concat(this._valueSql);
43
+ }
44
+ return target;
45
+ },
46
+ enumerable: false,
47
+ configurable: true
48
+ });
49
+ Condition.prototype.connect = function (prev) {
50
+ var _a = this, relation = _a.relation, result = _a.result;
51
+ if (!prev) {
52
+ return result;
53
+ }
54
+ if (!result) {
55
+ return prev;
56
+ }
57
+ return "".concat(prev, " ").concat(relation, " ").concat(result);
58
+ };
59
+ return Condition;
60
+ }());
61
+ exports.Condition = Condition;
62
+ var Options = /** @class */ (function () {
63
+ function Options() {
64
+ this.limit = 0;
65
+ this.skip = 0;
66
+ // 排序
67
+ this.sort = {};
68
+ }
69
+ Object.defineProperty(Options.prototype, "result", {
70
+ get: function () {
71
+ var target = '';
72
+ var _a = this, sort = _a.sort, limit = _a.limit, skip = _a.skip;
73
+ if (Object.keys(sort).length > 0) {
74
+ var items_1 = [];
75
+ Object.keys(sort).forEach(function (name, v) {
76
+ items_1.push("".concat(name, " ").concat(v));
77
+ });
78
+ target += "ORDER BY ".concat(items_1.join('), '));
79
+ }
80
+ if (limit > 0) {
81
+ target += "\nLIMIT ".concat(limit, " OFFSET ").concat(skip);
82
+ }
83
+ return target;
84
+ },
85
+ enumerable: false,
86
+ configurable: true
87
+ });
88
+ return Options;
89
+ }());
90
+ exports.Options = Options;
91
+ var RelationKey;
92
+ (function (RelationKey) {
93
+ RelationKey["and"] = "AND";
94
+ RelationKey["or"] = "OR";
95
+ })(RelationKey || (exports.RelationKey = RelationKey = {}));
96
+ var SortKey;
97
+ (function (SortKey) {
98
+ SortKey["asc"] = "ASC";
99
+ SortKey["desc"] = "DESC";
100
+ })(SortKey || (exports.SortKey = SortKey = {}));
101
+ var ConditionKey;
102
+ (function (ConditionKey) {
103
+ ConditionKey["eq"] = "=";
104
+ ConditionKey["ne"] = "!=";
105
+ ConditionKey["gt"] = ">";
106
+ ConditionKey["gte"] = ">=";
107
+ ConditionKey["lt"] = "<";
108
+ ConditionKey["lte"] = "<=";
109
+ ConditionKey["like"] = "LIKE";
110
+ ConditionKey["include"] = "IN";
111
+ ConditionKey["area"] = "BETWEEN";
112
+ ConditionKey["not"] = "=";
113
+ })(ConditionKey || (exports.ConditionKey = ConditionKey = {}));
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.MysqlDeleter = exports.MysqlUpdater = exports.MysqlInserter = exports.MysqlSelector = void 0;
40
+ var meta_1 = require("./meta");
41
+ var utils_1 = require("./utils");
42
+ /** @description 查询数据 */
43
+ var MysqlSelector = /** @class */ (function () {
44
+ function MysqlSelector(_tableName) {
45
+ this._tableName = _tableName;
46
+ this.conditions = [];
47
+ this.options = new meta_1.Options();
48
+ }
49
+ Object.defineProperty(MysqlSelector.prototype, "sql", {
50
+ get: function () {
51
+ var _a = this, conditions = _a.conditions, options = _a.options;
52
+ if (conditions.length === 0) {
53
+ return '';
54
+ }
55
+ var target = 'SELECT * FROM $tableName\n';
56
+ conditions.forEach(function (ele) {
57
+ target = ele.connect(target);
58
+ });
59
+ return "".concat(target, "\n").concat(options.result, ";");
60
+ },
61
+ enumerable: false,
62
+ configurable: true
63
+ });
64
+ MysqlSelector.prototype.select = function (opts) {
65
+ this.conditions.push(new meta_1.Condition(opts));
66
+ return this;
67
+ };
68
+ MysqlSelector.prototype.limit = function (n) {
69
+ this.options.limit = n;
70
+ return this;
71
+ };
72
+ MysqlSelector.prototype.skip = function (n) {
73
+ this.options.skip = n;
74
+ return this;
75
+ };
76
+ MysqlSelector.prototype.sort = function (field, v) {
77
+ this.options.sort[field] = v;
78
+ return this;
79
+ };
80
+ /** @查询结果 */
81
+ MysqlSelector.prototype.result = function () {
82
+ return __awaiter(this, void 0, void 0, function () {
83
+ return __generator(this, function (_a) {
84
+ try {
85
+ }
86
+ catch (_b) { }
87
+ return [2 /*return*/];
88
+ });
89
+ });
90
+ };
91
+ return MysqlSelector;
92
+ }());
93
+ exports.MysqlSelector = MysqlSelector;
94
+ /** @description 插入数据 */
95
+ var MysqlInserter = /** @class */ (function () {
96
+ function MysqlInserter(_tableName) {
97
+ this._tableName = _tableName;
98
+ this.fields = [];
99
+ this.values = [];
100
+ }
101
+ Object.defineProperty(MysqlInserter.prototype, "_fieldSql", {
102
+ get: function () {
103
+ return "(".concat(this.fields.join(', '), ")");
104
+ },
105
+ enumerable: false,
106
+ configurable: true
107
+ });
108
+ Object.defineProperty(MysqlInserter.prototype, "_valueSql", {
109
+ get: function () {
110
+ var _this = this;
111
+ var items = [];
112
+ this.values.forEach(function (item) {
113
+ var list = _this.fields.map(function (k) { return item[k] || ''; });
114
+ items.push(utils_1.MysqlParser.toValuesOf(list));
115
+ });
116
+ return items.join(',\n');
117
+ },
118
+ enumerable: false,
119
+ configurable: true
120
+ });
121
+ Object.defineProperty(MysqlInserter.prototype, "sql", {
122
+ get: function () {
123
+ var target = 'INSERT INTO $tableName';
124
+ target += this._fieldSql;
125
+ if (!this._valueSql) {
126
+ return "".concat(target, "\nVALUES\n").concat(this._valueSql, ";");
127
+ }
128
+ return '';
129
+ },
130
+ enumerable: false,
131
+ configurable: true
132
+ });
133
+ MysqlInserter.prototype.push = function () {
134
+ var _a;
135
+ var _this = this;
136
+ var list = [];
137
+ for (var _i = 0; _i < arguments.length; _i++) {
138
+ list[_i] = arguments[_i];
139
+ }
140
+ if (list.length > 0) {
141
+ Object.keys(list[0]).forEach(function (k) {
142
+ if (!_this.fields.includes(k)) {
143
+ _this.fields.push(k);
144
+ }
145
+ });
146
+ }
147
+ (_a = this.values).push.apply(_a, list);
148
+ };
149
+ // 插入数据
150
+ MysqlInserter.prototype.result = function () {
151
+ return __awaiter(this, void 0, void 0, function () {
152
+ return __generator(this, function (_a) {
153
+ return [2 /*return*/];
154
+ });
155
+ });
156
+ };
157
+ return MysqlInserter;
158
+ }());
159
+ exports.MysqlInserter = MysqlInserter;
160
+ /** @description 更新数据 */
161
+ var MysqlUpdater = /** @class */ (function () {
162
+ function MysqlUpdater(_tableName) {
163
+ this._tableName = _tableName;
164
+ }
165
+ return MysqlUpdater;
166
+ }());
167
+ exports.MysqlUpdater = MysqlUpdater;
168
+ /** @description 删除数据 */
169
+ var MysqlDeleter = /** @class */ (function () {
170
+ function MysqlDeleter(_tableName) {
171
+ this._tableName = _tableName;
172
+ }
173
+ return MysqlDeleter;
174
+ }());
175
+ exports.MysqlDeleter = MysqlDeleter;
package/dist/utils.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MysqlParser = void 0;
4
+ var MysqlParser = /** @class */ (function () {
5
+ function MysqlParser() {
6
+ }
7
+ MysqlParser.valueTypeOf = function (v) {
8
+ if (typeof v === 'string')
9
+ return "'".concat(v, "'");
10
+ return "".concat(v);
11
+ };
12
+ MysqlParser.toValuesOf = function (values) {
13
+ var items = [];
14
+ for (var v in values) {
15
+ items.push(MysqlParser.valueTypeOf(v));
16
+ }
17
+ return "(".concat(items.join(', '), ")");
18
+ };
19
+ return MysqlParser;
20
+ }());
21
+ exports.MysqlParser = MysqlParser;
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@rdyl/node-mysql",
3
+ "version": "0.0.2",
4
+ "license": "MIT",
5
+ "main": "dist/index.js",
6
+ "types": "types/index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "types",
13
+ "CHANGELOG.md",
14
+ "README.md"
15
+ ],
16
+ "scripts": {
17
+ "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
18
+ "build": "tsc && copyfiles -u 1 src/**/*.d.ts src/*.d.ts types && npm run changelog && git add CHANGELOG.md"
19
+ },
20
+ "dependencies": {
21
+ "mysql": "^2.18.1"
22
+ },
23
+ "devDependencies": {
24
+ "@commitlint/cli": "^19.6.1",
25
+ "@commitlint/config-conventional": "^19.6.0",
26
+ "@types/node": "^22.9.0",
27
+ "conventional-changelog-cli": "^5.0.0",
28
+ "copyfiles": "^2.4.1",
29
+ "typescript": "^5.6.3"
30
+ }
31
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./selector";
2
+ export * from "./meta";
3
+ export * from "./utils";
@@ -0,0 +1,42 @@
1
+ export declare class Condition {
2
+ relation: RelationKey;
3
+ field: string;
4
+ value: unknown;
5
+ condition: ConditionKey;
6
+ constructor(props: ConditionProps);
7
+ get _valueSql(): string;
8
+ get result(): string;
9
+ connect(prev: string): string;
10
+ }
11
+ export declare class Options {
12
+ limit: number;
13
+ skip: number;
14
+ sort: Record<string, SortKey>;
15
+ get result(): string;
16
+ }
17
+ export declare enum RelationKey {
18
+ and = "AND",
19
+ or = "OR"
20
+ }
21
+ export declare enum SortKey {
22
+ asc = "ASC",
23
+ desc = "DESC"
24
+ }
25
+ export declare enum ConditionKey {
26
+ eq = "=",
27
+ ne = "!=",
28
+ gt = ">",
29
+ gte = ">=",
30
+ lt = "<",
31
+ lte = "<=",
32
+ like = "LIKE",
33
+ include = "IN",
34
+ area = "BETWEEN",// 范围
35
+ not = "="
36
+ }
37
+ export interface ConditionProps {
38
+ field: string;
39
+ value: unknown;
40
+ relation?: RelationKey;
41
+ condition?: ConditionKey;
42
+ }
@@ -0,0 +1,37 @@
1
+ import { SortKey, Condition, ConditionProps, Options } from './meta';
2
+ /** @description 查询数据 */
3
+ export declare class MysqlSelector {
4
+ _tableName: string;
5
+ constructor(_tableName: string);
6
+ conditions: Condition[];
7
+ options: Options;
8
+ get sql(): string;
9
+ select(opts: ConditionProps): this;
10
+ limit(n: number): this;
11
+ skip(n: number): this;
12
+ sort(field: string, v: SortKey): this;
13
+ /** @查询结果 */
14
+ result(): Promise<void>;
15
+ }
16
+ /** @description 插入数据 */
17
+ export declare class MysqlInserter {
18
+ _tableName: string;
19
+ constructor(_tableName: string);
20
+ fields: string[];
21
+ values: Array<Record<string, unknown>>;
22
+ get _fieldSql(): string;
23
+ get _valueSql(): string;
24
+ get sql(): string;
25
+ push(...list: Record<string, unknown>[]): void;
26
+ result(): Promise<void>;
27
+ }
28
+ /** @description 更新数据 */
29
+ export declare class MysqlUpdater {
30
+ _tableName: string;
31
+ constructor(_tableName: string);
32
+ }
33
+ /** @description 删除数据 */
34
+ export declare class MysqlDeleter {
35
+ _tableName: string;
36
+ constructor(_tableName: string);
37
+ }
@@ -0,0 +1,4 @@
1
+ export declare class MysqlParser {
2
+ static valueTypeOf(v: unknown): string;
3
+ static toValuesOf(values: unknown[]): string;
4
+ }