flaks-sql-mock 0.0.0-stage → 1.0.0
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/LICENSE +201 -0
- package/README.MD +55 -0
- package/index.js +3 -0
- package/package.json +42 -5
- package/src/custom/column-preset.js +68 -0
- package/src/index.js +31 -0
- package/src/lib/config.js +33 -0
- package/src/lib/db-connector.js +20 -0
- package/src/lib/gen-mock-settings.js +48 -0
- package/src/lib/gen-table-struct.js +183 -0
- package/src/lib/gen-table-types.js +67 -0
- package/src/lib/helpers.js +12 -0
- package/src/lib/working.dir.js +11 -0
- package/src/methods/get-value-for-column.js +131 -0
- package/src/methods/get-values-for-table.js +105 -0
- package/src/methods/json-generate.js +6 -0
- package/src/methods/sql-generate.js +28 -0
- package/src/promises/load-column-preset.js +8 -0
- package/src/promises/mock-settings-promise.js +3 -0
- package/src/promises/struct-promise.js +3 -0
- package/src/storages/unique-storage.js +8 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.MD
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# SQL Mock data generator
|
|
2
|
+
|
|
3
|
+
Simple project for generation of mock data on any database structure
|
|
4
|
+
You don't need any clarification for your database - simply input your db's credentials and run this CLI utility
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
## Fast start
|
|
8
|
+
|
|
9
|
+
0. Copy-paste and rename `config_example.json` to `config.json`. And place your db's credentials
|
|
10
|
+
1. Run `npm install` (in root of project)
|
|
11
|
+
2. Run `node ./ 10` - to generate 10 rows for every table (in root of project)
|
|
12
|
+
3. Import `result_<DATABASE>.sql` (in results folder) to your db.
|
|
13
|
+
4. Done.
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
- NodeJS version 14+
|
|
18
|
+
- Installed dependencies (clarification for python users)
|
|
19
|
+
- Knowledge Working terminal (it's CLI utility)
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
- Install dependencies via `npm install` command in root of project
|
|
24
|
+
|
|
25
|
+
## Configuration
|
|
26
|
+
|
|
27
|
+
Copy-paste `config_example.json`, name it `config.json`.
|
|
28
|
+
Provide credentials to desired database
|
|
29
|
+
_Optional_. Edit chances of generation
|
|
30
|
+
|
|
31
|
+
## Run / CLI
|
|
32
|
+
|
|
33
|
+
Simple run `node ./` in root of project
|
|
34
|
+
|
|
35
|
+
> This procedure will generate mock-settings.json at the root of the project
|
|
36
|
+
> where you can adjust the number of rows you want to create with simple data
|
|
37
|
+
|
|
38
|
+
Edit `mock-settings.json` and run again. Or simple provide number of rows for all tables via 3rd argument.
|
|
39
|
+
|
|
40
|
+
> i.g. `node ./ 10` - will generate 10 rows for every table
|
|
41
|
+
|
|
42
|
+
## Output
|
|
43
|
+
|
|
44
|
+
Check root folder after running CLI
|
|
45
|
+
There are 2 files created inside of *results* folder. `result_<DATABASE>.json` with JSON presentation
|
|
46
|
+
And `result_<DATABASE>.sql` with SQL presentation that you can use for your database
|
|
47
|
+
|
|
48
|
+
## Deep config
|
|
49
|
+
|
|
50
|
+
You can edit `column-preset.js` in custom folder and name it `column-preset-custom.js` for your desired values for any columns.
|
|
51
|
+
This project uses `faker.js` for generating mock data
|
|
52
|
+
|
|
53
|
+
## CONTRIBUTION
|
|
54
|
+
|
|
55
|
+
Feel free to make pull requests
|
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "flaks-sql-mock",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"src",
|
|
8
|
+
"index.js"
|
|
9
|
+
],
|
|
10
|
+
"bin": {
|
|
11
|
+
"flaks-sql-mock": "./index.js"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"run": "node ./",
|
|
15
|
+
"typecheck": "tsc --project jsconfig.json"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/flaxes/sql-mock-generator.git"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"sql",
|
|
23
|
+
"mock",
|
|
24
|
+
"generator",
|
|
25
|
+
"nodejs",
|
|
26
|
+
"mysql"
|
|
27
|
+
],
|
|
28
|
+
"author": "Flaks",
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/flaxes/sql-mock-generator/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/flaxes/sql-mock-generator#readme",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"faker": "^5.5.3",
|
|
36
|
+
"mysql2": "^3.26.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/faker": "^5.5.3",
|
|
40
|
+
"@types/node": "^26.6.11",
|
|
41
|
+
"typescript": "^7.0.2"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check this documentation for more useful settings
|
|
3
|
+
*
|
|
4
|
+
* @link [https://fakerjs.dev/api/](API)
|
|
5
|
+
*/
|
|
6
|
+
const faker = require("faker");
|
|
7
|
+
const { getRandomInt } = require("../lib/helpers");
|
|
8
|
+
|
|
9
|
+
const username = faker.internet.userName;
|
|
10
|
+
const zeroOne = () => ~~(Math.random() > 0.5)
|
|
11
|
+
|
|
12
|
+
module.exports.familiarString = {
|
|
13
|
+
example: () => {
|
|
14
|
+
/**
|
|
15
|
+
* @description
|
|
16
|
+
* Create arrow function with your own logic for any property that start with -
|
|
17
|
+
* a key provided in this familiar list
|
|
18
|
+
* for example column "username" - starts with "user"
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const currentTimestamp = Date.now(); // 1671655177366
|
|
22
|
+
|
|
23
|
+
return `example_with_timestamp_${currentTimestamp}`;
|
|
24
|
+
},
|
|
25
|
+
/**
|
|
26
|
+
* For your project - `address` field perhaps *faker.address.streetAddress* value is more suitable
|
|
27
|
+
*/
|
|
28
|
+
// address: faker.address.streetAddress,
|
|
29
|
+
address: faker.internet.ip,
|
|
30
|
+
firstname: faker.name.firstName,
|
|
31
|
+
lastname: faker.name.lastName,
|
|
32
|
+
user: username,
|
|
33
|
+
login: username,
|
|
34
|
+
mail: faker.internet.email,
|
|
35
|
+
email: faker.internet.email,
|
|
36
|
+
pass: faker.internet.password,
|
|
37
|
+
name: faker.name.firstName,
|
|
38
|
+
ips: faker.internet.ip,
|
|
39
|
+
ip: faker.internet.ip,
|
|
40
|
+
serverip: faker.internet.ip,
|
|
41
|
+
secret: faker.internet.password,
|
|
42
|
+
modem: () => faker.internet.ip().split(".").slice(-2).join("_"),
|
|
43
|
+
city: faker.address.city,
|
|
44
|
+
country: faker.address.countryCode,
|
|
45
|
+
geo: faker.address.countryCode,
|
|
46
|
+
domain: faker.internet.domainName,
|
|
47
|
+
alias: faker.name.firstName,
|
|
48
|
+
key: () => `${faker.hacker.ingverb()}_${faker.hacker.ingverb()}`,
|
|
49
|
+
value: () => `random_val_${faker.random.word()}`,
|
|
50
|
+
params: faker.datatype.json,
|
|
51
|
+
deleted: zeroOne,
|
|
52
|
+
sql: () => `SELECT ${faker.database.column()} FROM table_${faker.database.column()}`,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
module.exports.familiarNumber = {
|
|
56
|
+
is_: zeroOne,
|
|
57
|
+
timestamp: () => faker.date.past().getTime(),
|
|
58
|
+
port: faker.internet.port,
|
|
59
|
+
days: () => getRandomInt(7, 60),
|
|
60
|
+
amount: () => Number((Math.random() * 200).toFixed(2)),
|
|
61
|
+
price: () => Number((Math.random() * 200).toFixed(2)),
|
|
62
|
+
httpPort: faker.internet.port,
|
|
63
|
+
socksPort: faker.internet.port,
|
|
64
|
+
has_: zeroOne,
|
|
65
|
+
percent: () => ~~(Math.random() * 101),
|
|
66
|
+
active: zeroOne,
|
|
67
|
+
enabled: zeroOne,
|
|
68
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const { db, dbConfig } = require("./lib/db-connector");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const getValuesForTable = require("./methods/get-values-for-table");
|
|
4
|
+
const jsonGenerate = require("./methods/json-generate");
|
|
5
|
+
const sqlGenerate = require("./methods/sql-generate");
|
|
6
|
+
const structPromise = require("./promises/struct-promise");
|
|
7
|
+
const { workingDirPath } = require("./lib/working.dir");
|
|
8
|
+
|
|
9
|
+
async function main() {
|
|
10
|
+
const { tables } = await structPromise;
|
|
11
|
+
|
|
12
|
+
const tablesValues = {};
|
|
13
|
+
|
|
14
|
+
for (const table in tables) {
|
|
15
|
+
const values = await getValuesForTable(table);
|
|
16
|
+
|
|
17
|
+
if (values && values.length) tablesValues[table] = values;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
db.end();
|
|
21
|
+
|
|
22
|
+
const pathToFile = path.join(workingDirPath, `mock-sql-${dbConfig.database}`);
|
|
23
|
+
|
|
24
|
+
jsonGenerate(tablesValues, `${pathToFile}.json`);
|
|
25
|
+
console.log("JSON created");
|
|
26
|
+
|
|
27
|
+
sqlGenerate(tablesValues, `${pathToFile}.sql`);
|
|
28
|
+
console.log("SQL created");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
main();
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** @type {typeof import('../../mock-sql-config-example')} */
|
|
2
|
+
let config;
|
|
3
|
+
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const { workingDirPath } = require("./working.dir");
|
|
7
|
+
|
|
8
|
+
const CONFIG_FILENAME = "mock-sql-config.js";
|
|
9
|
+
const CONFIG_EXAMPL_FILENAME = "mock-sql-config-example.js";
|
|
10
|
+
|
|
11
|
+
const pathToConfig = path.join(workingDirPath, CONFIG_FILENAME);
|
|
12
|
+
const pathToConfigExample = path.join(__dirname, "../..", CONFIG_EXAMPL_FILENAME);
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
if (!fs.existsSync(pathToConfig)) {
|
|
16
|
+
fs.copyFileSync(pathToConfigExample, pathToConfig);
|
|
17
|
+
|
|
18
|
+
console.info(
|
|
19
|
+
"Mock config not found. Created a new config file in the project root. Fill in the missing values and restart the app.",
|
|
20
|
+
pathToConfig,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
config = require(pathToConfig);
|
|
27
|
+
} catch (/** @type {any} */ err) {
|
|
28
|
+
console.error("Failed to load config", "\n", err.message);
|
|
29
|
+
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = config;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const mysql2 = require("mysql2");
|
|
2
|
+
const { database } = require("./config");
|
|
3
|
+
|
|
4
|
+
const db = mysql2.createPool(database);
|
|
5
|
+
|
|
6
|
+
module.exports.db = db;
|
|
7
|
+
module.exports.dbConfig = database;
|
|
8
|
+
module.exports.dbStream = (sql, values, cb) => {
|
|
9
|
+
const query = db.query(sql, values);
|
|
10
|
+
const stream = query.stream({});
|
|
11
|
+
|
|
12
|
+
const promise = new Promise((resolve, reject) => {
|
|
13
|
+
stream.on("error", (err) => reject(err));
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
stream.on("data", (row) => cb(row));
|
|
16
|
+
stream.on("end", (end) => resolve(end));
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
return promise;
|
|
20
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const structPromise = require("../promises/struct-promise");
|
|
4
|
+
const { workingDirPath } = require("./working.dir");
|
|
5
|
+
const FILENAME = "mock-sql-settings.json";
|
|
6
|
+
const SIZE = process.argv[2];
|
|
7
|
+
|
|
8
|
+
const pathToMockSettings = path.resolve(workingDirPath, FILENAME);
|
|
9
|
+
|
|
10
|
+
const genTableMockSettings = async () => {
|
|
11
|
+
console.log("Gen mock settings started");
|
|
12
|
+
|
|
13
|
+
let settings;
|
|
14
|
+
let size = Number(SIZE);
|
|
15
|
+
const pathToSettings = pathToMockSettings;
|
|
16
|
+
console.log("Path to settings", pathToSettings);
|
|
17
|
+
|
|
18
|
+
const createSettings = async () => {
|
|
19
|
+
const { tables } = await structPromise;
|
|
20
|
+
|
|
21
|
+
settings = {};
|
|
22
|
+
for (const key in tables) {
|
|
23
|
+
settings[key] = size;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
console.log("Gen mock settings finished");
|
|
27
|
+
fs.writeFileSync(pathToMockSettings, JSON.stringify(settings, null, 4));
|
|
28
|
+
|
|
29
|
+
console.log("Mock settings created.");
|
|
30
|
+
return settings;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
if (size) throw new Error("Go to catch block");
|
|
35
|
+
|
|
36
|
+
settings = require(pathToSettings);
|
|
37
|
+
console.log("Mock settings loaded...");
|
|
38
|
+
} catch (err) {
|
|
39
|
+
if (!size) size = 1;
|
|
40
|
+
settings = await createSettings();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
console.log("Settings\n", JSON.stringify(settings, null, 4));
|
|
44
|
+
|
|
45
|
+
return settings;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
module.exports = genTableMockSettings;
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
const { db, dbConfig, dbStream } = require("./db-connector");
|
|
2
|
+
const { COLUMN_TYPES } = require("./gen-table-types");
|
|
3
|
+
const DISPLAY_NAME_POSITION = 2;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {{
|
|
7
|
+
* name: string;
|
|
8
|
+
* nullable: boolean;
|
|
9
|
+
* uniqueGroups?: Record<string, string[]>;
|
|
10
|
+
* foreign?: {
|
|
11
|
+
* table: string;
|
|
12
|
+
* column: string;
|
|
13
|
+
* key: string;
|
|
14
|
+
* };
|
|
15
|
+
* type: string;
|
|
16
|
+
* jsType: string;
|
|
17
|
+
* defaultValue?: string;
|
|
18
|
+
* columnType: string;
|
|
19
|
+
* maxLength?: number;
|
|
20
|
+
* possibles?: string[]
|
|
21
|
+
* extra?: string;
|
|
22
|
+
* index: number;
|
|
23
|
+
* }} InterfaceRowData
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const genTableStruct = async (dontStopDb) => {
|
|
27
|
+
console.log("Collecting database structure...");
|
|
28
|
+
|
|
29
|
+
/** @type {Record<string, InterfaceRowData[]>} */
|
|
30
|
+
const tables = {};
|
|
31
|
+
/** @type {Record<string, Record<string, string[]>>} */
|
|
32
|
+
const tableIndexes = {};
|
|
33
|
+
/** @type {Record<string, Record<string, string[]>>} */
|
|
34
|
+
const tableUniqueIndexes = {};
|
|
35
|
+
/** @type {Record<string, Record<string, Record<string, string[]>>>} */
|
|
36
|
+
const tableUniqueColumns = {};
|
|
37
|
+
|
|
38
|
+
/** @type {Record<string, Record<string, { column: string; foreignColumn: string; foreignTable: string; key: string; }>>} */
|
|
39
|
+
const tableForeigns = {};
|
|
40
|
+
|
|
41
|
+
/** @type {Record<string, string>} */
|
|
42
|
+
const tableDisplayNames = {};
|
|
43
|
+
|
|
44
|
+
console.log("Collecting database tables...");
|
|
45
|
+
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
await dbStream(
|
|
48
|
+
`SELECT *
|
|
49
|
+
FROM information_schema.columns
|
|
50
|
+
WHERE table_schema = ?
|
|
51
|
+
ORDER BY table_name,ordinal_position`,
|
|
52
|
+
[dbConfig.database],
|
|
53
|
+
(row) => {
|
|
54
|
+
const {
|
|
55
|
+
TABLE_NAME,
|
|
56
|
+
COLUMN_NAME,
|
|
57
|
+
IS_NULLABLE,
|
|
58
|
+
DATA_TYPE,
|
|
59
|
+
COLUMN_TYPE,
|
|
60
|
+
COLUMN_DEFAULT,
|
|
61
|
+
CHARACTER_MAXIMUM_LENGTH,
|
|
62
|
+
EXTRA,
|
|
63
|
+
ORDINAL_POSITION,
|
|
64
|
+
} = row;
|
|
65
|
+
|
|
66
|
+
const table = tables[TABLE_NAME] || (tables[TABLE_NAME] = []);
|
|
67
|
+
|
|
68
|
+
/** @type {InterfaceRowData} */
|
|
69
|
+
const val = {
|
|
70
|
+
name: COLUMN_NAME,
|
|
71
|
+
nullable: IS_NULLABLE === "YES",
|
|
72
|
+
defaultValue: COLUMN_DEFAULT, // || EXTRA === "auto_increment" ? EXTRA : void 0,
|
|
73
|
+
type: DATA_TYPE,
|
|
74
|
+
jsType: COLUMN_TYPES[DATA_TYPE] || "unknown",
|
|
75
|
+
columnType: COLUMN_TYPE,
|
|
76
|
+
maxLength: CHARACTER_MAXIMUM_LENGTH,
|
|
77
|
+
extra: EXTRA,
|
|
78
|
+
index: ORDINAL_POSITION,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
if (ORDINAL_POSITION === DISPLAY_NAME_POSITION) {
|
|
82
|
+
tableDisplayNames[TABLE_NAME] = COLUMN_NAME;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (DATA_TYPE === "enum") {
|
|
86
|
+
val.possibles = COLUMN_TYPE.slice("enum(".length, -1)
|
|
87
|
+
.split(",")
|
|
88
|
+
.map((item) => item.slice(1, -1));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
table.push(val);
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
console.log("Collecting database indexes...");
|
|
96
|
+
|
|
97
|
+
await dbStream(
|
|
98
|
+
`SELECT
|
|
99
|
+
TABLE_NAME, INDEX_NAME, COLUMN_NAME, NON_UNIQUE
|
|
100
|
+
FROM INFORMATION_SCHEMA.STATISTICS
|
|
101
|
+
WHERE TABLE_SCHEMA = ?`,
|
|
102
|
+
[dbConfig.database],
|
|
103
|
+
(row) => {
|
|
104
|
+
const { TABLE_NAME, INDEX_NAME, COLUMN_NAME, NON_UNIQUE } = row;
|
|
105
|
+
|
|
106
|
+
const indexTable = tableIndexes[TABLE_NAME] || (tableIndexes[TABLE_NAME] = {});
|
|
107
|
+
if (!indexTable[INDEX_NAME]) indexTable[INDEX_NAME] = [];
|
|
108
|
+
|
|
109
|
+
if (NON_UNIQUE === 0) {
|
|
110
|
+
const uniqueTableIndex = tableUniqueIndexes[TABLE_NAME] || (tableUniqueIndexes[TABLE_NAME] = {});
|
|
111
|
+
const uniqueTableColumn = tableUniqueColumns[TABLE_NAME] || (tableUniqueColumns[TABLE_NAME] = {});
|
|
112
|
+
|
|
113
|
+
if (!uniqueTableColumn[COLUMN_NAME]) uniqueTableColumn[COLUMN_NAME] = {};
|
|
114
|
+
if (!uniqueTableIndex[INDEX_NAME]) {
|
|
115
|
+
uniqueTableIndex[INDEX_NAME] = [];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!uniqueTableColumn[COLUMN_NAME][INDEX_NAME]) {
|
|
119
|
+
uniqueTableColumn[COLUMN_NAME][INDEX_NAME] = uniqueTableIndex[INDEX_NAME];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
uniqueTableIndex[INDEX_NAME].push(COLUMN_NAME);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
indexTable[INDEX_NAME].push(COLUMN_NAME);
|
|
126
|
+
}
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
console.log("Collecting database foreign keys...");
|
|
130
|
+
|
|
131
|
+
await dbStream(
|
|
132
|
+
`SELECT
|
|
133
|
+
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
|
|
134
|
+
FROM
|
|
135
|
+
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
|
136
|
+
WHERE
|
|
137
|
+
REFERENCED_TABLE_SCHEMA = ?`,
|
|
138
|
+
[dbConfig.database],
|
|
139
|
+
(row) => {
|
|
140
|
+
const { TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME } = row;
|
|
141
|
+
|
|
142
|
+
const table = tableForeigns[TABLE_NAME] || (tableForeigns[TABLE_NAME] = {});
|
|
143
|
+
const tableColumns = tables[TABLE_NAME];
|
|
144
|
+
|
|
145
|
+
const column = tableColumns.find((item) => item.name === COLUMN_NAME);
|
|
146
|
+
if (column) {
|
|
147
|
+
column.foreign = {
|
|
148
|
+
key: COLUMN_NAME,
|
|
149
|
+
column: REFERENCED_COLUMN_NAME,
|
|
150
|
+
table: REFERENCED_TABLE_NAME,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
table[COLUMN_NAME] = {
|
|
155
|
+
column: COLUMN_NAME,
|
|
156
|
+
foreignColumn: REFERENCED_COLUMN_NAME,
|
|
157
|
+
foreignTable: REFERENCED_TABLE_NAME,
|
|
158
|
+
key: CONSTRAINT_NAME,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
if (!dontStopDb) db.end();
|
|
164
|
+
|
|
165
|
+
console.log("Collecting structure finished.");
|
|
166
|
+
|
|
167
|
+
for (const table in tableUniqueColumns) {
|
|
168
|
+
const tableColumns = tables[table];
|
|
169
|
+
const tableUnique = tableUniqueColumns[table];
|
|
170
|
+
|
|
171
|
+
for (const column in tableUnique) {
|
|
172
|
+
const columnUnique = tableUnique[column];
|
|
173
|
+
const columnTable = tableColumns.find((item) => item.name === column);
|
|
174
|
+
if (columnTable) {
|
|
175
|
+
columnTable.uniqueGroups = columnUnique;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return { tables, tableIndexes, tableUniqueIndexes, tableUniqueColumns, tableForeigns, tableDisplayNames };
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
module.exports = genTableStruct;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
|
|
2
|
+
const TAB_SIZE = 4;
|
|
3
|
+
|
|
4
|
+
module.exports.TAB_SIZE = TAB_SIZE;
|
|
5
|
+
module.exports.TAB = " ".repeat(TAB_SIZE);
|
|
6
|
+
|
|
7
|
+
module.exports.COLUMN_TYPES = {
|
|
8
|
+
tinyint: "number",
|
|
9
|
+
smallint: "number",
|
|
10
|
+
int: "number",
|
|
11
|
+
decimal: "number",
|
|
12
|
+
float: "number",
|
|
13
|
+
double: "number",
|
|
14
|
+
bigint: "string",
|
|
15
|
+
|
|
16
|
+
varchar: "string",
|
|
17
|
+
mediumtext: "string",
|
|
18
|
+
longtext: "string",
|
|
19
|
+
text: "string",
|
|
20
|
+
char: "string",
|
|
21
|
+
|
|
22
|
+
timestamp: "Date",
|
|
23
|
+
// datetime: 'Date',
|
|
24
|
+
// date: 'Date',
|
|
25
|
+
datetime: "string | Date",
|
|
26
|
+
date: "string | Date",
|
|
27
|
+
time: "string",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
module.exports.TYPE_SIZE = {
|
|
31
|
+
TINYINT: {
|
|
32
|
+
bytes: 1,
|
|
33
|
+
min: -128,
|
|
34
|
+
max: 127,
|
|
35
|
+
minUnsigned: 0,
|
|
36
|
+
maxUnsigned: 255,
|
|
37
|
+
},
|
|
38
|
+
SMALLINT: {
|
|
39
|
+
bytes: 2,
|
|
40
|
+
min: -32768,
|
|
41
|
+
max: 32767,
|
|
42
|
+
minUnsigned: 0,
|
|
43
|
+
maxUnsigned: 65535,
|
|
44
|
+
},
|
|
45
|
+
MEDIUMINT: {
|
|
46
|
+
bytes: 3,
|
|
47
|
+
min: -8388608,
|
|
48
|
+
max: 8388607,
|
|
49
|
+
minUnsigned: 0,
|
|
50
|
+
maxUnsigned: 16777215,
|
|
51
|
+
},
|
|
52
|
+
INT: {
|
|
53
|
+
bytes: 4,
|
|
54
|
+
min: -2147483648,
|
|
55
|
+
max: 2147483647,
|
|
56
|
+
minUnsigned: 0,
|
|
57
|
+
maxUnsigned: 4294967295,
|
|
58
|
+
},
|
|
59
|
+
/** do not use mysql's bigint with js number. use js BigInt instead */
|
|
60
|
+
BIGINT: {
|
|
61
|
+
bytes: 8,
|
|
62
|
+
min: -2147483648,
|
|
63
|
+
max: 2147483647,
|
|
64
|
+
minUnsigned: 0,
|
|
65
|
+
maxUnsigned: 4294967295,
|
|
66
|
+
},
|
|
67
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module.exports.getRandomInt = (min, max) => {
|
|
2
|
+
return Math.round(Math.random() * (max - min) + min);
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param {string} str
|
|
8
|
+
* @returns {string}
|
|
9
|
+
*/
|
|
10
|
+
const camelToSnakeCase = (str) => str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
11
|
+
|
|
12
|
+
module.exports.camelToSnakeCase = camelToSnakeCase;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
|
|
4
|
+
const workingDir = "mock";
|
|
5
|
+
const workingDirPath = path.join(process.cwd(), workingDir);
|
|
6
|
+
|
|
7
|
+
if (!fs.existsSync(workingDirPath)) {
|
|
8
|
+
fs.mkdirSync(workingDirPath);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports = { workingDir, workingDirPath };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
const faker = require("faker");
|
|
2
|
+
const { familiarNumber, familiarString } = require("../promises/load-column-preset");
|
|
3
|
+
const { chance_of_null_value, chance_of_default_value, array_with_brackets } = require("../lib/config").generation;
|
|
4
|
+
const { getRandomInt, camelToSnakeCase } = require("../lib/helpers");
|
|
5
|
+
|
|
6
|
+
const IGNORED_DEFAULT_VALUES = ["CURRENT_TIMESTAMP", "auto_increment"];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {string} _table - for future purposes
|
|
10
|
+
* @param {Awaited<ReturnType<typeof import('../lib/gen-table-struct')>>['tables'][0][0]} column
|
|
11
|
+
* @returns {string | number | null | Date | undefined}
|
|
12
|
+
*/
|
|
13
|
+
const getValueForColumn = (_table, column) => {
|
|
14
|
+
if (column.defaultValue && Math.random() < chance_of_default_value) {
|
|
15
|
+
const isIgnored =
|
|
16
|
+
IGNORED_DEFAULT_VALUES.includes(column.defaultValue) ||
|
|
17
|
+
(column.extra && IGNORED_DEFAULT_VALUES.includes(column.extra));
|
|
18
|
+
|
|
19
|
+
if (isIgnored) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return column.defaultValue;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (column.nullable && Math.random() < chance_of_null_value) {
|
|
27
|
+
if (column.defaultValue || column.extra) return;
|
|
28
|
+
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Useful for enum
|
|
33
|
+
if (column.possibles) {
|
|
34
|
+
return column.possibles[~~(Math.random() * column.possibles.length)];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const name = camelToSnakeCase(column.name);
|
|
38
|
+
const isArray = name.endsWith("_array");
|
|
39
|
+
|
|
40
|
+
const escapeArray = (array) => {
|
|
41
|
+
let val = JSON.stringify(array);
|
|
42
|
+
|
|
43
|
+
if (column.maxLength) {
|
|
44
|
+
val = val.slice(0, column.maxLength);
|
|
45
|
+
|
|
46
|
+
if (array_with_brackets) {
|
|
47
|
+
if (!val.endsWith('"]')) {
|
|
48
|
+
val += val.endsWith('"') ? "]" : '"]';
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
// Remove first bracket
|
|
52
|
+
val = val.slice(1);
|
|
53
|
+
|
|
54
|
+
if (val.endsWith("]")) val = val.slice(0, -1);
|
|
55
|
+
if (!val.endsWith('"')) val += '"';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return val;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const checkFamliliar = (list) => {
|
|
63
|
+
const genName = (key) => {
|
|
64
|
+
let res = list[key]();
|
|
65
|
+
|
|
66
|
+
if (typeof res === "string" && column.maxLength) res = res.slice(0, column.maxLength);
|
|
67
|
+
|
|
68
|
+
return res;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const genMany = (key) => {
|
|
72
|
+
const L = ~~(Math.random() * 6) + 1;
|
|
73
|
+
const array = [];
|
|
74
|
+
|
|
75
|
+
for (let i = 0; i < L; i++) {
|
|
76
|
+
array.push(genName(key));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return array;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
for (const key in list) {
|
|
83
|
+
if (name.startsWith(key)) {
|
|
84
|
+
if (isArray) {
|
|
85
|
+
return genMany(key);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return genName(key);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const randomDate = () => {
|
|
94
|
+
return new Date(16e11 - 1e11 + Math.random() * 17e10);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const getResult = () => {
|
|
98
|
+
let val;
|
|
99
|
+
|
|
100
|
+
switch (column.jsType) {
|
|
101
|
+
case "number":
|
|
102
|
+
val = checkFamliliar(familiarNumber);
|
|
103
|
+
return typeof val === "number" ? val : getRandomInt(0, column.maxLength || 50);
|
|
104
|
+
case "string":
|
|
105
|
+
val = checkFamliliar(familiarString);
|
|
106
|
+
|
|
107
|
+
if (!val) {
|
|
108
|
+
val = faker.random.word();
|
|
109
|
+
|
|
110
|
+
if (isArray) val = [val];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (Array.isArray(val)) val = escapeArray(val);
|
|
114
|
+
|
|
115
|
+
return val;
|
|
116
|
+
case "string | Date":
|
|
117
|
+
return randomDate();
|
|
118
|
+
case "Date":
|
|
119
|
+
return randomDate();
|
|
120
|
+
|
|
121
|
+
default:
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const result = getResult();
|
|
127
|
+
|
|
128
|
+
return result;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
module.exports = getValueForColumn;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const { getRandomInt } = require("../lib/helpers");
|
|
2
|
+
const mockSettingsPromise = require("../promises/mock-settings-promise");
|
|
3
|
+
const structPromise = require("../promises/struct-promise");
|
|
4
|
+
const uniqueStorage = require("../storages/unique-storage");
|
|
5
|
+
const getValueForColumn = require("./get-value-for-column");
|
|
6
|
+
|
|
7
|
+
const getValuesForTable = async (tableName) => {
|
|
8
|
+
const mockSettings = await mockSettingsPromise;
|
|
9
|
+
const maxId = mockSettings[tableName];
|
|
10
|
+
if (!maxId) return;
|
|
11
|
+
|
|
12
|
+
const { tableUniqueColumns, tables, tableForeigns } = await structPromise;
|
|
13
|
+
|
|
14
|
+
const table = tables[tableName];
|
|
15
|
+
const foreigns = tableForeigns[tableName] || {};
|
|
16
|
+
|
|
17
|
+
uniqueStorage[tableName] = {};
|
|
18
|
+
const uniqueStore = uniqueStorage[tableName];
|
|
19
|
+
|
|
20
|
+
const values = [];
|
|
21
|
+
|
|
22
|
+
const getForeignValue = (_columnName, foreignRef) => {
|
|
23
|
+
const foreignMax = mockSettings[foreignRef.foreignTable] || 1;
|
|
24
|
+
const foreignId = getRandomInt(1, foreignMax);
|
|
25
|
+
|
|
26
|
+
return foreignId;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
* @param {import('../lib/gen-table-struct').InterfaceRowData} column
|
|
32
|
+
* @param {(typeof foreigns)[0]} [foreignRef]
|
|
33
|
+
*/
|
|
34
|
+
const tryUniqueValue = (column, foreignRef) => {
|
|
35
|
+
let tries = 5;
|
|
36
|
+
const columnName = column.name;
|
|
37
|
+
const previous = uniqueStore[columnName] || (uniqueStore[columnName] = new Set());
|
|
38
|
+
|
|
39
|
+
while (tries--) {
|
|
40
|
+
let val;
|
|
41
|
+
if (foreignRef) {
|
|
42
|
+
val = getForeignValue(columnName, foreignRef);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
val = getValueForColumn(tableName, column);
|
|
46
|
+
|
|
47
|
+
if (previous.has(val)) {
|
|
48
|
+
// additional check for number
|
|
49
|
+
if (typeof val === "number" && previous.has(++val)) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// additional check for string
|
|
54
|
+
if (typeof val === "string") {
|
|
55
|
+
val = `${getRandomInt(0, 9)}${val}`;
|
|
56
|
+
if (previous.has(val)) continue;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
previous.add(val);
|
|
61
|
+
|
|
62
|
+
return val;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const getValueForTable = (id) => {
|
|
67
|
+
const obj = {};
|
|
68
|
+
|
|
69
|
+
for (const column of table) {
|
|
70
|
+
const columnName = column.name;
|
|
71
|
+
if (columnName === "id") {
|
|
72
|
+
obj.id = id;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const isUnique = !!tableUniqueColumns[column.name];
|
|
77
|
+
const foreignRef = foreigns[column.name];
|
|
78
|
+
|
|
79
|
+
let val;
|
|
80
|
+
|
|
81
|
+
if (isUnique) {
|
|
82
|
+
val = tryUniqueValue(column, foreignRef);
|
|
83
|
+
} else if (foreignRef) {
|
|
84
|
+
val = getForeignValue(columnName, foreignRef);
|
|
85
|
+
} else {
|
|
86
|
+
val = getValueForColumn(tableName, column);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (val || val !== undefined) {
|
|
90
|
+
obj[columnName] = val;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return obj;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
for (let id = 1; id < maxId + 1; id++) {
|
|
98
|
+
const val = getValueForTable(id);
|
|
99
|
+
if (val) values.push(val);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return values;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
module.exports = getValuesForTable;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const { escapeId, escape } = require("mysql2");
|
|
3
|
+
|
|
4
|
+
const CHECK_FOREIGN = "SET FOREIGN_KEY_CHECKS = ?;\n";
|
|
5
|
+
|
|
6
|
+
const sqlGenerate = (values, filePath) => {
|
|
7
|
+
const results = [CHECK_FOREIGN.replace("?", "0")];
|
|
8
|
+
|
|
9
|
+
for (const [table, rows] of Object.entries(values)) {
|
|
10
|
+
const result = [`# <${table}> mock data`];
|
|
11
|
+
|
|
12
|
+
for (const row of rows) {
|
|
13
|
+
console.log(row);
|
|
14
|
+
result.push(`INSERT IGNORE INTO ${escapeId(table)} SET ${escape(row)};`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
results.push(result.join("\n"));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
results.push("\n", CHECK_FOREIGN.replace("?", "1"));
|
|
21
|
+
|
|
22
|
+
const text = results.join("\n\n");
|
|
23
|
+
fs.writeFileSync(filePath, text);
|
|
24
|
+
|
|
25
|
+
return results;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
module.exports = sqlGenerate;
|
package/README.md
DELETED