dbtasker 3.0.3 → 3.0.5
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/README.md +102 -462
- package/addcolumn.js +1 -1
- package/altercolumn.js +1 -1
- package/dbop.js +2 -2
- package/index.js +1 -1
- package/package.json +17 -3
- package/tableop.js +4 -3
- package/validation.js +13 -6
package/README.md
CHANGED
|
@@ -1,225 +1,46 @@
|
|
|
1
|
-
# DBTASKER
|
|
1
|
+
# 🗄️ DBTASKER
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
The Intelligent MySQL Schema Automation Engine
|
|
4
|
+
`dbtasker` is a powerful, engine-aware MySQL schema intelligence and query generation module. It allows developers to define database structures declaratively using JSON, providing automated validation, normalization, and SQL generation for tables, columns, indexes, and foreign keys.
|
|
5
5
|
|
|
6
|
-
#### Engine Awareness
|
|
7
|
-
DBTASKER supports MySQL engines and handles constraints accordingly:
|
|
8
6
|
|
|
9
|
-
- InnoDB
|
|
10
|
-
- MyISAM
|
|
11
|
-
- MEMORY
|
|
12
|
-
- CSV
|
|
13
|
-
- ARCHIVE
|
|
14
|
-
- BLACKHOLE
|
|
15
|
-
- FEDERATED
|
|
16
|
-
- NDB / NDBCLUSTER
|
|
17
|
-
- MRG_MYISAM
|
|
18
|
-
- Spider
|
|
19
|
-
- RocksDB
|
|
20
|
-
- TokuDB
|
|
21
7
|
|
|
8
|
+
## 🚀 Key Features
|
|
9
|
+
- **Declarative Schema:** Define your entire database structure in a single JSON object.
|
|
10
|
+
- **Engine Awareness:** Optimized for MySQL engines including InnoDB, MyISAM, RocksDB, and more.
|
|
11
|
+
- **Validation First:** Validates and normalizes your schema before any SQL is executed.
|
|
12
|
+
- **Intelligent Migration:** Safe handling of ALTER, DROP, and CREATE operations.
|
|
13
|
+
- **Alias Flexibility:** Case-insensitive keys and multiple naming aliases (camelCase, snake_case, etc.).
|
|
22
14
|
|
|
23
|
-
#### Design Philosophy
|
|
24
15
|
|
|
25
|
-
- Schema-first, declarative JSON
|
|
26
|
-
- Validation before SQL generation
|
|
27
|
-
- Engine-aware and safe migrations
|
|
28
|
-
- Flexible key aliases and metadata support
|
|
29
|
-
- Supports both SQL-level and ORM-level annotations
|
|
30
16
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
- Node.js 16+
|
|
34
|
-
- MySQL 5.7+ / 8+
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
## Core Concept
|
|
38
|
-
DBTASKER uses a JSON-driven design:
|
|
39
|
-
```js
|
|
40
|
-
Database → Tables → Columns → Column Properties
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
- Database: Name of the MySQL database
|
|
44
|
-
- Table: Table within the database
|
|
45
|
-
- Column: Column within the table
|
|
46
|
-
- Properties: Rules, types, defaults, and constraints
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
DBTASKER normalizes keys case-insensitively, so multiple naming styles are supported (camelCase, snake_case, uppercase, lowercase).
|
|
50
|
-
|
|
51
|
-
#### JSON Schema Structure
|
|
52
|
-
```js
|
|
53
|
-
{
|
|
54
|
-
DatabaseName: {
|
|
55
|
-
TableName: {
|
|
56
|
-
ColumnName: {
|
|
57
|
-
// Column properties
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
Demo Schema Example
|
|
64
|
-
```js
|
|
65
|
-
{
|
|
66
|
-
DatabaseName: {
|
|
67
|
-
TableName: {
|
|
68
|
-
ColumnName: {
|
|
69
|
-
ColumnType: "varchar",
|
|
70
|
-
lengthvalue: 255,
|
|
71
|
-
Null: true,
|
|
72
|
-
defaults: "guest",
|
|
73
|
-
comment: "User email address"
|
|
74
|
-
},
|
|
75
|
-
ColumnTwo: {
|
|
76
|
-
type: "int",
|
|
77
|
-
zerofill: true,
|
|
78
|
-
index: "key",
|
|
79
|
-
Null: false,
|
|
80
|
-
foreign_key: {
|
|
81
|
-
table: "user",
|
|
82
|
-
column: "id",
|
|
83
|
-
delete: true,
|
|
84
|
-
update: true
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
## Configuration File
|
|
93
|
-
|
|
94
|
-
DBTASKER requires a configuration JSON file to connect to your MySQL database and define runtime behavior. This config file allows you to:
|
|
95
|
-
|
|
96
|
-
- Set database connection credentials
|
|
97
|
-
- Control whether databases, tables, or columns are dropped
|
|
98
|
-
- Protect specific databases from accidental deletion
|
|
99
|
-
- Customize separators or other runtime options
|
|
100
|
-
|
|
101
|
-
**Basic Database Configuration**
|
|
17
|
+
## 📦 Installation
|
|
18
|
+
`Bash`
|
|
102
19
|
```js
|
|
103
|
-
|
|
104
|
-
user: "root",
|
|
105
|
-
password: "password123",
|
|
106
|
-
host: "localhost",
|
|
107
|
-
port: 3306
|
|
108
|
-
}
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
1. user: MySQL username
|
|
112
|
-
2. password: MySQL password
|
|
113
|
-
3. host: MySQL host
|
|
114
|
-
4. port: MySQL port
|
|
115
|
-
5. Drop database
|
|
116
|
-
6. Do not delete database Array = []
|
|
117
|
-
7. Drop Table
|
|
118
|
-
8. Drop Column
|
|
119
|
-
9. Database structure as JSON Object
|
|
120
|
-
|
|
121
|
-
You can control dropping databases, tables, or columns using boolean flags. DBTASKER supports multiple aliases for each option.
|
|
122
|
-
|
|
123
|
-
#### Drop Database
|
|
124
|
-
|
|
125
|
-
### Aliases:
|
|
126
|
-
You can use these as key when declearing on config JSON object without caring for case sensitivity.
|
|
127
|
-
```js
|
|
128
|
-
dropdb, dropdatabase, deletedb, deletedatabase,
|
|
129
|
-
drop_db, drop_database, delete_db, delete_database,
|
|
130
|
-
removedb, removedatabase, remove_db, remove_database
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
**Example:**
|
|
135
|
-
```js
|
|
136
|
-
dropdb: true
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
### Drop Table
|
|
140
|
-
**Aliases:**
|
|
141
|
-
You can use these as key when declearing on config JSON object without caring for case sensitivity.
|
|
142
|
-
```js
|
|
143
|
-
droptable, deletetable, drop_table, delete_table,
|
|
144
|
-
removetable, remove_table,
|
|
145
|
-
dropdbtable, deletedbtable, removedbtable,
|
|
146
|
-
dropdatabasetable, deletedatabasetable, removedatabasetable,
|
|
147
|
-
drop_db_table, delete_db_table, remove_db_table,
|
|
148
|
-
drop_database_table, delete_database_table, remove_database_table
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
**Example:**
|
|
153
|
-
```js
|
|
154
|
-
droptable: true
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
### Drop Column
|
|
159
|
-
**Aliases:**
|
|
160
|
-
You can use these as key when declearing on config JSON object without caring for case sensitivity.
|
|
161
|
-
```js
|
|
162
|
-
dropcol, dropcolumn, deletecol, deletecolumn,
|
|
163
|
-
removecol, removecolumn,
|
|
164
|
-
drop_col, drop_column, delete_col, delete_column,
|
|
165
|
-
remove_col, remove_column
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
**Example:**
|
|
170
|
-
```js
|
|
171
|
-
dropcol: false
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
**Protect Databases from Deletion**
|
|
175
|
-
If you want to prevent certain databases from being dropped when dropdb is enabled, you can specify an array of database names under any of the aliases below:
|
|
176
|
-
You can use these as key when declearing on config JSON object without caring for case sensitivity.
|
|
177
|
-
```js
|
|
178
|
-
donttouch, donottouch, donttouchdb, donottouchdb,
|
|
179
|
-
donttouchdatabase, donottouchdatabase,
|
|
180
|
-
dontdelete, donotdelete, dontdeletedb, donotdeletedb,
|
|
181
|
-
dontdeletedatabase, donotdeletedatabase,
|
|
182
|
-
dont_touch, do_not_touch, dont_touch_db, do_not_touch_db,
|
|
183
|
-
dont_touch_database, do_not_touch_database,
|
|
184
|
-
dont_delete, do_not_delete, dont_delete_db, do_not_delete_db,
|
|
185
|
-
dont_delete_database, do_not_delete_database,
|
|
186
|
-
reserveddb, reserved_db
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
**Example:**
|
|
190
|
-
```js
|
|
191
|
-
donttouch: ["production_db", "legacy_db"]
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
**Force Update Column**
|
|
196
|
-
If you want to update column if it is referanced by another column as foreign key you can update it by adding another key to the config which is force update column which is turned on by default. You can turn it of if you think necessary.
|
|
197
|
-
|
|
198
|
-
```js
|
|
199
|
-
'forceupdatecol', 'forcemodifycol', 'forceupdatecolumn', 'forcemodifycolumn', 'force_update_col', 'force_modify_col', 'force_update_column', 'force_modify_column', 'forcealtercol', 'forcealtercolumn', 'force_alter_col', 'force_alter_column'
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
**Example:**
|
|
203
|
-
```js
|
|
204
|
-
forceupdatecol: true/1/\"1\"
|
|
20
|
+
npm install dbtasker
|
|
205
21
|
```
|
|
206
22
|
|
|
207
|
-
### Separator Option
|
|
208
|
-
|
|
209
|
-
You can define a custom separator for internal operations:
|
|
210
23
|
|
|
211
|
-
**Aliases:**
|
|
212
|
-
You can use these as key when declearing on config JSON object without caring for case sensitivity.
|
|
213
|
-
sep, seperator
|
|
214
24
|
|
|
25
|
+
## 🛠️ Configurationd
|
|
26
|
+
btasker requires a configuration object to manage connection credentials and safety behaviors.
|
|
27
|
+
Property | Description
|
|
28
|
+
| :--- | :--- |
|
|
29
|
+
| **host** | MySQL Host (e.g., `localhost`) |
|
|
30
|
+
| **user** | Database Username |
|
|
31
|
+
| **password** | Database Password |
|
|
32
|
+
| **port** | Connection Port (Default: `3306`) |
|
|
33
|
+
| **dropdb** | `Boolean`: If true, allows dropping databases. |
|
|
34
|
+
| **droptable** | `Boolean`: If true, allows dropping tables. |
|
|
35
|
+
| **dropcol** | `Boolean`: If true, allows dropping columns. |
|
|
36
|
+
| **donttouch** | `Array`: List of database names protected from deletion. |
|
|
37
|
+
| **forcedeletecolumn** | `Boolean`: If true, allow dropping column even if referanced by any other column. |
|
|
38
|
+
| **forceupdatecolumn** | `Boolean`: If true, allow updating column even if referanced by any other column. `default: true` |
|
|
215
39
|
|
|
216
|
-
|
|
40
|
+
### Configuration Example
|
|
41
|
+
`JavaScript`
|
|
217
42
|
```js
|
|
218
|
-
|
|
219
|
-
```
|
|
220
|
-
Full Example Config File
|
|
221
|
-
```js
|
|
222
|
-
{
|
|
43
|
+
const config = {
|
|
223
44
|
user: "root",
|
|
224
45
|
password: "password123",
|
|
225
46
|
host: "localhost",
|
|
@@ -228,88 +49,33 @@ Full Example Config File
|
|
|
228
49
|
droptable: true,
|
|
229
50
|
dropcol: false,
|
|
230
51
|
donttouch: ["production_db", "analytics_db"],
|
|
231
|
-
sep: "_"
|
|
232
|
-
}
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
This configuration will:
|
|
236
|
-
|
|
237
|
-
- Connect to MySQL using the given credentials
|
|
238
|
-
- Drop databases and tables if they exist
|
|
239
|
-
- Preserve "production_db" and "analytics_db"
|
|
240
|
-
- Avoid dropping columns
|
|
241
|
-
- Use _ as a separator internally
|
|
242
|
-
|
|
243
|
-
## Installation
|
|
244
|
-
|
|
245
|
-
DBTASKER is available via npm.
|
|
246
|
-
|
|
247
|
-
Install it using either of the following commands:
|
|
248
|
-
```js
|
|
249
|
-
npm install dbtasker
|
|
250
|
-
```
|
|
251
|
-
|
|
252
|
-
or
|
|
253
|
-
```js
|
|
254
|
-
npm i dbtasker
|
|
255
|
-
```
|
|
256
|
-
Usage
|
|
257
|
-
|
|
258
|
-
DBTASKER is designed to be simple and declarative. You provide:
|
|
259
|
-
|
|
260
|
-
1. A configuration object (database credentials + behavior options)
|
|
261
|
-
|
|
262
|
-
2. A JSON schema object (database, tables, columns)
|
|
263
|
-
|
|
264
|
-
DBTASKER handles the rest.
|
|
265
|
-
|
|
266
|
-
#### Step 1: Import DBTASKER
|
|
267
|
-
|
|
268
|
-
Create a JavaScript file (for example: index.js) and import DBTASKER.
|
|
269
|
-
```js
|
|
270
|
-
Using require (CommonJS)
|
|
271
|
-
const dbtasker = require("dbtasker");
|
|
272
|
-
|
|
273
|
-
Using import (ES Module)
|
|
274
|
-
import dbtasker from "dbtasker";
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
#### Step 2: Create a Configuration Object
|
|
278
|
-
|
|
279
|
-
This object defines how DBTASKER connects to the database and how it behaves.
|
|
280
|
-
```js
|
|
281
|
-
const config = {
|
|
282
|
-
host: "localhost",
|
|
283
|
-
user: "root",
|
|
284
|
-
password: "password",
|
|
285
|
-
port: 3306
|
|
52
|
+
sep: "_"
|
|
286
53
|
};
|
|
287
54
|
```
|
|
288
55
|
|
|
289
|
-
You can later extend this config with options like:
|
|
290
56
|
|
|
291
|
-
- Drop database
|
|
292
|
-
- Drop tables
|
|
293
|
-
- Drop columns
|
|
294
|
-
- Reserved / protected databases
|
|
295
57
|
|
|
296
|
-
|
|
58
|
+
## 📐 Schema Structure
|
|
59
|
+
The schema follows a strict nested hierarchy:
|
|
60
|
+
|
|
61
|
+
```Database ➔ Table ➔ Column ➔ Properties```
|
|
297
62
|
|
|
298
|
-
|
|
63
|
+
`JavaScript`
|
|
299
64
|
```js
|
|
300
65
|
const schema = {
|
|
301
|
-
|
|
66
|
+
app_db: {
|
|
302
67
|
users: {
|
|
303
68
|
id: {
|
|
304
69
|
type: "int",
|
|
305
|
-
|
|
306
|
-
|
|
70
|
+
primarykey: true,
|
|
71
|
+
autoincrement: true
|
|
307
72
|
},
|
|
308
73
|
email: {
|
|
309
74
|
type: "varchar",
|
|
310
75
|
length: 255,
|
|
311
76
|
required: true,
|
|
312
|
-
unique: true
|
|
77
|
+
unique: true,
|
|
78
|
+
comment: "User email address"
|
|
313
79
|
},
|
|
314
80
|
created_at: {
|
|
315
81
|
type: "timestamp",
|
|
@@ -320,215 +86,89 @@ const schema = {
|
|
|
320
86
|
};
|
|
321
87
|
```
|
|
322
88
|
|
|
323
|
-
DBTASKER supports multiple aliases for column keys and is case-insensitive.
|
|
324
89
|
|
|
325
|
-
#### Step 4: Run DBTASKER
|
|
326
90
|
|
|
327
|
-
|
|
91
|
+
## 🔗 Foreign Keys
|
|
92
|
+
Foreign keys are defined inline within the column property to reference another table's column.
|
|
93
|
+
`JavaScript`
|
|
328
94
|
```js
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
- Create or alter databases, tables, and columns
|
|
338
|
-
- Apply indexes, foreign keys, defaults, and constraints
|
|
339
|
-
|
|
340
|
-
### Full Minimal Example
|
|
341
|
-
```js
|
|
342
|
-
const DBTASKER = require("dbtasker");
|
|
343
|
-
|
|
344
|
-
const config = {
|
|
345
|
-
host: "localhost",
|
|
346
|
-
user: "root",
|
|
347
|
-
password: "password",
|
|
348
|
-
port: 3306,
|
|
349
|
-
droptable: true,
|
|
350
|
-
droptable: true,
|
|
351
|
-
dropcolumn: true,
|
|
352
|
-
donotdelete: ['test', 'example']
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
const schema = {
|
|
356
|
-
app_db: {
|
|
357
|
-
users: {
|
|
358
|
-
id: {
|
|
359
|
-
type: "int",
|
|
360
|
-
primarykey: true,
|
|
361
|
-
autoincrement: true
|
|
362
|
-
},
|
|
363
|
-
name: {
|
|
364
|
-
type: "varchar",
|
|
365
|
-
length: 100,
|
|
366
|
-
required: true
|
|
367
|
-
}
|
|
368
|
-
}
|
|
95
|
+
ColumnTwo: {
|
|
96
|
+
type: "int",
|
|
97
|
+
index: "key",
|
|
98
|
+
foreign_key: {
|
|
99
|
+
table: "user",
|
|
100
|
+
column: "id",
|
|
101
|
+
delete: "CASCADE", // Options: true, "SET NULL", "RESTRICT", etc.
|
|
102
|
+
update: true
|
|
369
103
|
}
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
DBTASKER(config, schema);
|
|
373
|
-
```
|
|
374
|
-
|
|
375
|
-
**Notes**
|
|
376
|
-
|
|
377
|
-
### Important:
|
|
378
|
-
- The config object must always come first
|
|
379
|
-
- The schema JSON object must come second
|
|
380
|
-
- All keys are case-insensitive
|
|
381
|
-
- Multiple aliases are supported for maximum flexibility
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
## Column Key Aliases (Case-Insensitive)
|
|
385
|
-
DBTASKER allows multiple aliases for each column property. Keys are normalized internally.
|
|
386
|
-
|
|
387
|
-
**Length / Size / Precision**
|
|
388
|
-
You can use these as key when declearing column property inside table object inside JSON object without caring for case sensitivity.
|
|
389
|
-
```js
|
|
390
|
-
lengthvalue, length_value, size, scale, lengths,
|
|
391
|
-
length, value, values, range, maxlength, max_length, precision
|
|
392
|
-
```
|
|
393
|
-
|
|
394
|
-
**Column Type / Data Type**
|
|
395
|
-
You can use these as key when declearing column property inside table object inside JSON object without caring for case sensitivity.
|
|
396
|
-
```js
|
|
397
|
-
type, columntype, column_type,
|
|
398
|
-
datatype, data_type, typename, type_name
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
**Zerofill**
|
|
402
|
-
You can use these as key when declearing column property inside table object inside JSON object without caring for case sensitivity.
|
|
403
|
-
```js
|
|
404
|
-
zerofill, zero_fill, iszerofill, zerofillup
|
|
104
|
+
}
|
|
405
105
|
```
|
|
106
|
+
### Foreign Key Properties
|
|
107
|
+
| Property | Description |
|
|
108
|
+
| :---: | :--- |
|
|
109
|
+
| `table` | Referenced table name |
|
|
110
|
+
| `column` | Referenced column name |
|
|
111
|
+
| `delete` | `ON DELETE` behavior (CASCADE, SET NULL, RESTRICT, NO ACTION) |
|
|
112
|
+
| `update` | `ON UPDATE` behavior (CASCADE, SET NULL, RESTRICT, NO ACTION) |
|
|
406
113
|
|
|
407
|
-
**Auto Increment / Identity**
|
|
408
|
-
You can use these as key when declearing column property inside table object inside JSON object without caring for case sensitivity.
|
|
409
|
-
```js
|
|
410
|
-
autoincrement, auto_increment, increment,
|
|
411
|
-
serial, generated, isidentity, identity
|
|
412
|
-
```
|
|
413
114
|
|
|
414
|
-
**Signed / Unsigned**
|
|
415
|
-
You can use these as key when declearing column property inside table object inside JSON object without caring for case sensitivity.
|
|
416
|
-
```js
|
|
417
|
-
signed, issigned, numericunsigned, numeric_unsigned,
|
|
418
|
-
unsigned, isunsigned
|
|
419
|
-
```
|
|
420
115
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
```js
|
|
424
|
-
default, defaults, defaultvalue, default_value,
|
|
425
|
-
example, sample, columndefault, column_default
|
|
426
|
-
```
|
|
116
|
+
## 📑 Property Aliases
|
|
117
|
+
`dbtasker` is highly flexible and accepts multiple aliases for each column property. All keys are **case-insensitive.**
|
|
427
118
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
119
|
+
| Feature | Supported Aliases |
|
|
120
|
+
| :--- | :--- |
|
|
121
|
+
| **Type** | `type, columntype, column_type, datatype, data_type, typename, type_name` |
|
|
122
|
+
| **Length** | `length, size, scale, value, range, precision, maxlength, lengthvalue` |
|
|
123
|
+
| **Zerofill** | `zerofill, zero_fill, iszerofill, zerofillup, zero, fillzero, fill_zero, iszero` |
|
|
124
|
+
| **Unsigned** | `unsigned, signed, issigned, isunsigned, numericsigned, numericunsigned` |
|
|
125
|
+
| **Identity** | `autoincrement, auto_increment, increment, generated, isidentity, serial, identity` |
|
|
126
|
+
| **Default** | `default, defaults, defaultvalue, default_value, example, sample, columndefault, column_default` |
|
|
127
|
+
| **Index** | `index, indexkey, index_key, indexing` |
|
|
128
|
+
| `PrimaryKey` | `primarykey, primary_key, primary, isprimary, isprimarykey` |
|
|
129
|
+
| `UniqueKey` | `unique, isunique, isuniquekey, uniqueindex, uniquekey, unique_index, unique_key` |
|
|
130
|
+
| `FulltextKey` | `fulltext, fulltextindex, isfulltextkey, fulltextkey, fulltext_key, isfulltext` |
|
|
131
|
+
| `SpatialKey` | `spatial, spatialindex, isspatialkey, spatialkey, spatial_key, isspatial` |
|
|
132
|
+
| **Nullability** | `null, nulls, nullable, optional, isnulable, allownull, canbenull, notnull, not_null, nonnullable, notnullable, required, disallownull, non_nullable, not_nullable, disallow_null` |
|
|
133
|
+
| **Comments** | `comment, comments, columncomment, column_comment, description, label, helptext, hint, note` |
|
|
134
|
+
| **ForeignKey** | `fk, fuck, foreign_key, foreignkey` |
|
|
435
135
|
|
|
436
|
-
**Allow NULL / Optional**
|
|
437
|
-
```js
|
|
438
|
-
null, nulls, nullable, optional, isnulable, allownull, canbenull
|
|
439
|
-
```
|
|
440
136
|
|
|
441
|
-
**Index / Key**
|
|
442
|
-
You can use these as key when declearing column property inside table object inside JSON object without caring for case sensitivity.
|
|
443
|
-
```js
|
|
444
|
-
index, spatial, isspatial, fulltext, isfulltext,
|
|
445
|
-
isunique, isuniquekey, uniqueindex, uniquekey,
|
|
446
|
-
unique_index, unique_key,
|
|
447
|
-
primarykey, primary_key, primary, isprimary, isprimarykey,
|
|
448
|
-
indexkey, index_key, indexing
|
|
449
|
-
```
|
|
450
|
-
|
|
451
|
-
**Comments / Description / Notes**
|
|
452
|
-
You can use these as key when declearing column property inside table object inside JSON object without caring for case sensitivity.
|
|
453
|
-
```js
|
|
454
|
-
comment, comments, columncomment, column_comment,
|
|
455
|
-
description, label, helptext, hint, note
|
|
456
|
-
```
|
|
457
137
|
|
|
138
|
+
## 🛡️ Compatibility & Engines
|
|
139
|
+
- **Node.js:** 16.x or higher
|
|
140
|
+
- **MySQL:** 5.7+ / 8.0+
|
|
141
|
+
- **Engines:** InnoDB, MyISAM, MEMORY, RocksDB, TokuDB, and more.
|
|
458
142
|
|
|
459
|
-
**Foreign Key Definition**
|
|
460
|
-
Foreign keys are defined inline to reference another table’s column:
|
|
461
|
-
```js
|
|
462
|
-
foreign_key: {
|
|
463
|
-
table: "user",
|
|
464
|
-
column: "id",
|
|
465
|
-
delete: true,
|
|
466
|
-
update: true
|
|
467
|
-
}
|
|
468
|
-
```
|
|
469
|
-
## Foreign Key Aliases (Case-Insensitive)
|
|
470
|
-
DBTASKER accepts:
|
|
471
|
-
```js
|
|
472
|
-
"fk", "fuck", "foreign_key", "foreignkey"
|
|
473
|
-
```
|
|
474
143
|
|
|
475
|
-
Foreign Key Properties & Aliases
|
|
476
|
-
Property
|
|
477
|
-
### Alias Options
|
|
478
|
-
#### Purpose
|
|
479
|
-
**Table**
|
|
480
|
-
```js
|
|
481
|
-
"table", "fktable", "fk_table", "foreignkeytable", "foreign_key_table"
|
|
482
|
-
```
|
|
483
|
-
Referenced table name
|
|
484
144
|
|
|
485
|
-
|
|
145
|
+
## 🏁 Quick Start
|
|
146
|
+
`JavaScript`
|
|
486
147
|
```js
|
|
487
|
-
|
|
488
|
-
```
|
|
489
|
-
Referenced column name
|
|
148
|
+
const dbtasker = require("dbtasker");
|
|
490
149
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
150
|
+
const config = {
|
|
151
|
+
host: "localhost",
|
|
152
|
+
user: "root",
|
|
153
|
+
password: "password"
|
|
154
|
+
};
|
|
496
155
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
156
|
+
const schema = {
|
|
157
|
+
store_db: {
|
|
158
|
+
products: {
|
|
159
|
+
id: { type: "int", primarykey: true, autoincrement: true },
|
|
160
|
+
name: { type: "varchar", length: 100, required: true }
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
502
164
|
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
```js
|
|
506
|
-
null, "NULL", "SET NULL", true, "DL", "DEL", "DELETE", "CASCADE", "DEFAULT", "SET DEFAULT", "RESTRICT", "NO ACTION"
|
|
165
|
+
// Connect, Validate, and Automate
|
|
166
|
+
dbtasker(config, schema);
|
|
507
167
|
```
|
|
508
168
|
|
|
509
|
-
> Notes:
|
|
510
|
-
> DBTASKER automatically normalizes all keys internally.
|
|
511
|
-
|
|
512
|
-
Foreign key constraints are generated safely and include necessary values automatically.
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
#### Use Cases
|
|
517
|
-
Schema builders and migration tools
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
#### Admin dashboards
|
|
521
|
-
Low-code / no-code backends
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
#### License
|
|
526
|
-
MIT
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
#### Author
|
|
530
|
-
Md Nasiruddin Ahmed (Manik)
|
|
531
169
|
|
|
532
|
-
---Designed for safe, flexible, and high-quality MySQL schema automation.
|
|
533
170
|
|
|
171
|
+
## 👨💻 Author
|
|
172
|
+
**Md Nasiruddin Ahmed (Manik)** Designed for safe, flexible, and high-quality MySQL schema automation. 🌐 Visit us at: kormoi.com
|
|
534
173
|
|
|
174
|
+
**License:** MIT
|
package/addcolumn.js
CHANGED
|
@@ -249,7 +249,7 @@ async function addColumnIfNeeded(config, jsondata, separator) {
|
|
|
249
249
|
}
|
|
250
250
|
}
|
|
251
251
|
}
|
|
252
|
-
console.log(cstyler.
|
|
252
|
+
console.log(cstyler.bold.underline.hex("#b700ffff")("Adding Column to tables are completed successfully."));
|
|
253
253
|
return true;
|
|
254
254
|
} catch (err) {
|
|
255
255
|
console.error(err.message);
|
package/altercolumn.js
CHANGED
|
@@ -582,7 +582,7 @@ async function alterColumnIfNeeded(config, jsondata, forceupdatecolumn, separato
|
|
|
582
582
|
}
|
|
583
583
|
}
|
|
584
584
|
}
|
|
585
|
-
console.log(cstyler.
|
|
585
|
+
console.log(cstyler.bold.underline.hex("#b700ffff")("Alter Column process to tables are completed successfully."));
|
|
586
586
|
return true;
|
|
587
587
|
} catch (err) {
|
|
588
588
|
console.error(err.message);
|
package/dbop.js
CHANGED
|
@@ -209,9 +209,9 @@ async function databaseAddDeleteAlter(allconfig, jsondata, dropdb = false, dontt
|
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
211
|
if (count > 0) {
|
|
212
|
-
console.log(cstyler.
|
|
212
|
+
console.log(cstyler.bold.underline.hex("#b700ffff")("All useless database have dropped"));
|
|
213
213
|
} else {
|
|
214
|
-
console.log(cstyler.
|
|
214
|
+
console.log(cstyler.bold.underline.hex("#b700ffff")("No database found to be dropped"));
|
|
215
215
|
}
|
|
216
216
|
}
|
|
217
217
|
return true;
|
package/index.js
CHANGED
|
@@ -157,7 +157,7 @@ module.exports = async function (allconfig, table_json) {
|
|
|
157
157
|
}
|
|
158
158
|
// lets create tables if needed
|
|
159
159
|
const tableop = require("./tableop");
|
|
160
|
-
const createtable = await tableop.
|
|
160
|
+
const createtable = await tableop.createTableIfNeeded(config, jsondata, separator);
|
|
161
161
|
if (createtable === null) {
|
|
162
162
|
console.log(cstyler.bold.underline.red("Error occurred during creating tables."));
|
|
163
163
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dbtasker",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.5",
|
|
4
4
|
"description": "dbtasker is a powerful and developer-friendly MySQL schema automation tool that converts JSON-based definitions into validated SQL that allows developers to declaratively define databases, tables, columns, indexes, and foreign keys using a flexible, case-insensitive JSON schema.",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "Md Nasiruddin Ahmed",
|
|
@@ -8,13 +8,27 @@
|
|
|
8
8
|
"main": "index.js",
|
|
9
9
|
"keywords": [
|
|
10
10
|
"database",
|
|
11
|
+
"db",
|
|
11
12
|
"mysql",
|
|
12
13
|
"table",
|
|
13
14
|
"create",
|
|
14
|
-
"mysql2",
|
|
15
15
|
"alter",
|
|
16
16
|
"modify",
|
|
17
|
-
"mysql2"
|
|
17
|
+
"mysql2",
|
|
18
|
+
"dbtasker",
|
|
19
|
+
"automation",
|
|
20
|
+
"schema",
|
|
21
|
+
"dbtask",
|
|
22
|
+
"database-automation",
|
|
23
|
+
"schema-sync",
|
|
24
|
+
"schema-builder",
|
|
25
|
+
"database-task",
|
|
26
|
+
"database-utility",
|
|
27
|
+
"database-tool",
|
|
28
|
+
"create-table",
|
|
29
|
+
"alter-table",
|
|
30
|
+
"drop-table",
|
|
31
|
+
"foreign-key"
|
|
18
32
|
],
|
|
19
33
|
"scripts": {
|
|
20
34
|
"test": "echo \"Error: no test specified\" && exit 1"
|
package/tableop.js
CHANGED
|
@@ -152,7 +152,7 @@ async function createTableQuery(config, tabledata, tableName, dbname) {
|
|
|
152
152
|
return null;
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
|
-
async function
|
|
155
|
+
async function createTableIfNeeded(config, jsondata, separator) {
|
|
156
156
|
try {
|
|
157
157
|
if (!fncs.isJsonObject(jsondata)) {
|
|
158
158
|
return false;
|
|
@@ -209,9 +209,10 @@ async function createTableifNeeded(config, jsondata, separator) {
|
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
|
+
console.log(cstyler.bold.underline.hex("#b700ffff")("Create table if needed process completed successfully."));
|
|
212
213
|
return true;
|
|
213
214
|
} catch (err) {
|
|
214
|
-
console.error(cstyler.bold.red("Error occurred in
|
|
215
|
+
console.error(cstyler.bold.red("Error occurred in createTableIfNeeded function of ", moduleName, " module. Error details: "), err);
|
|
215
216
|
return null;
|
|
216
217
|
}
|
|
217
218
|
}
|
|
@@ -268,6 +269,6 @@ async function dropTable(config, json_data, separator = "_") {
|
|
|
268
269
|
|
|
269
270
|
|
|
270
271
|
module.exports = {
|
|
271
|
-
|
|
272
|
+
createTableIfNeeded,
|
|
272
273
|
dropTable
|
|
273
274
|
};
|
package/validation.js
CHANGED
|
@@ -236,7 +236,7 @@ async function JSONchecker(table_json, config, separator = "_") {
|
|
|
236
236
|
return null;
|
|
237
237
|
}
|
|
238
238
|
// get all mysql table engines
|
|
239
|
-
|
|
239
|
+
let mysqlEngines = await fncs.getMySQLEngines(config);
|
|
240
240
|
if (!fncs.isJsonObject(mysqlEngines)) {
|
|
241
241
|
console.error(cstyler.red.bold("There is a problem connecting to the database. Please check database info or connection."));
|
|
242
242
|
return null;
|
|
@@ -496,7 +496,7 @@ async function JSONchecker(table_json, config, separator = "_") {
|
|
|
496
496
|
/**
|
|
497
497
|
* ZEROFILL
|
|
498
498
|
*/
|
|
499
|
-
const zerofkeys = ['zerofill', 'zero_fill', 'iszerofill', 'zerofillup'];
|
|
499
|
+
const zerofkeys = ['zerofill', 'zero_fill', 'iszerofill', 'zerofillup', 'zero', 'fillzero', 'fill_zero', 'iszero'];
|
|
500
500
|
for (const item of Object.keys(deepColumn)) {
|
|
501
501
|
if (zerofkeys.includes(item.toLowerCase())) {
|
|
502
502
|
zerofill = deepColumn[item];
|
|
@@ -1220,9 +1220,16 @@ async function JSONchecker(table_json, config, separator = "_") {
|
|
|
1220
1220
|
}
|
|
1221
1221
|
} else if (enginekey.includes(columnName.toLowerCase())) {
|
|
1222
1222
|
const engvalue = table_json[databaseName][tableName][columnName];
|
|
1223
|
-
if (Object.keys(mysqlEngines).includes(engvalue)) {
|
|
1224
|
-
|
|
1225
|
-
|
|
1223
|
+
if (Object.keys(mysqlEngines).map(e => e.toUpperCase()).includes(fncs.stringifyAny(engvalue).toUpperCase())) {
|
|
1224
|
+
let ifyes;
|
|
1225
|
+
for(const key of Object.keys(mysqlEngines)) {
|
|
1226
|
+
if(key.toUpperCase() === fncs.stringifyAny(engvalue).toUpperCase()) {
|
|
1227
|
+
ifyes = key;
|
|
1228
|
+
break;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
if (mysqlEngines[ifyes].support === "YES") {
|
|
1232
|
+
contentObj[databaseName][tableName]._engine_ = ifyes;
|
|
1226
1233
|
} else {
|
|
1227
1234
|
badengine.push(
|
|
1228
1235
|
`${cstyler.blue('Database:')} ${cstyler.hex("#00d9ffff")(databaseName)} ${cstyler.blue('> Table:')} ${cstyler.hex("#00d9ffff")(tableName)} ${cstyler.blue('> Engine key:')} ${cstyler.hex("#00d9ffff")(columnName)} ${cstyler.blue("Engine name: ")} ${cstyler.hex("#00d9ffff")(engvalue)} ${cstyler.red('- The storage engine exists in MySQL source code but is not available on this server.')}`
|
|
@@ -1230,7 +1237,7 @@ async function JSONchecker(table_json, config, separator = "_") {
|
|
|
1230
1237
|
}
|
|
1231
1238
|
} else {
|
|
1232
1239
|
badengine.push(
|
|
1233
|
-
`${cstyler.blue('Database:')} ${cstyler.hex("#00d9ffff")(databaseName)} ${cstyler.blue('> Table:')} ${cstyler.hex("#00d9ffff")(tableName)} ${cstyler.blue('> Engine key:')} ${cstyler.hex("#00d9ffff")(columnName)} ${cstyler.blue("Engine name:")} ${cstyler.hex("#00d9ffff")(engvalue)} ${cstyler.red('-
|
|
1240
|
+
`${cstyler.blue('Database:')} ${cstyler.hex("#00d9ffff")(databaseName)} ${cstyler.blue('> Table:')} ${cstyler.hex("#00d9ffff")(tableName)} ${cstyler.blue('> Engine key:')} ${cstyler.hex("#00d9ffff")(columnName)} ${cstyler.blue("Engine name:")} ${cstyler.underline.bold.hex("#00d9ffff")(engvalue)} ${cstyler.bold.red('- is not a valid MySQL storage engine name.')}`
|
|
1234
1241
|
);
|
|
1235
1242
|
}
|
|
1236
1243
|
} else if (commetnkey.includes(columnName.toLowerCase())) {
|