meadow-connection-mysql 1.0.9 → 1.0.11
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/1 +10 -0
- package/README.md +17 -16
- package/docs/README.md +21 -1
- package/docs/_cover.md +10 -4
- package/docs/_sidebar.md +17 -2
- package/docs/_topbar.md +3 -1
- package/docs/api/connect.md +92 -0
- package/docs/api/connectAsync.md +118 -0
- package/docs/api/createTable.md +93 -0
- package/docs/api/createTables.md +124 -0
- package/docs/api/generateCreateTableStatement.md +122 -0
- package/docs/api/generateDropTableStatement.md +77 -0
- package/docs/api/pool.md +148 -0
- package/docs/api/reference.md +275 -0
- package/docs/api.md +9 -217
- package/docs/architecture.md +259 -0
- package/docs/quickstart.md +332 -0
- package/docs/retold-catalog.json +20 -1
- package/docs/retold-keyword-index.json +5124 -5
- package/package.json +2 -2
- package/source/Meadow-Connection-MySQL.js +79 -120
- package/source/Meadow-Schema-MySQL.js +1128 -0
- package/test/MySQL_tests.js +833 -0
- package/test/docker-init/02-chinook-schema.sql +190 -0
package/1
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
NPM Version bump
|
|
2
|
+
# Please enter the commit message for your changes. Lines starting
|
|
3
|
+
# with '#' will be ignored, and an empty message aborts the commit.
|
|
4
|
+
#
|
|
5
|
+
# On branch main
|
|
6
|
+
# Your branch is up to date with 'origin/main'.
|
|
7
|
+
#
|
|
8
|
+
# Changes to be committed:
|
|
9
|
+
# modified: package.json
|
|
10
|
+
#
|
package/README.md
CHANGED
|
@@ -10,13 +10,13 @@ A MySQL connection pool provider for the Meadow ORM. Wraps [mysql2](https://gith
|
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
|
-
- **MySQL2 Connection Pooling**
|
|
14
|
-
- **Fable Service Provider**
|
|
15
|
-
- **Named Placeholders**
|
|
16
|
-
- **Auto-Connect**
|
|
17
|
-
- **Schema-Driven DDL**
|
|
18
|
-
- **Connection Safety**
|
|
19
|
-
- **Settings Coercion**
|
|
13
|
+
- **MySQL2 Connection Pooling** -- High-performance pooled connections via [mysql2](https://github.com/sidorares/node-mysql2)
|
|
14
|
+
- **Fable Service Provider** -- Registers with a Fable instance for dependency injection, logging, and configuration
|
|
15
|
+
- **Named Placeholders** -- Automatically enables named placeholders for parameterized queries
|
|
16
|
+
- **Auto-Connect** -- Optional automatic connection on instantiation via the `MeadowConnectionMySQLAutoConnect` flag
|
|
17
|
+
- **Schema-Driven DDL** -- Generates `CREATE TABLE IF NOT EXISTS` statements from Meadow table schemas with primary keys, UTF-8 charset, and proper column types
|
|
18
|
+
- **Connection Safety** -- Guards against creating duplicate connection pools with descriptive logging (passwords are never leaked)
|
|
19
|
+
- **Settings Coercion** -- Accepts both Meadow-style (`Server`, `Port`, `User`) and mysql2-native (`host`, `port`, `user`) configuration formats
|
|
20
20
|
|
|
21
21
|
## Installation
|
|
22
22
|
|
|
@@ -167,13 +167,13 @@ Generate a `DROP TABLE IF EXISTS` SQL statement for the given table name.
|
|
|
167
167
|
|
|
168
168
|
Meadow Connection MySQL is a database connector for the Meadow data access layer:
|
|
169
169
|
|
|
170
|
-
- [meadow](https://github.com/stevenvelozo/meadow)
|
|
171
|
-
- [foxhound](https://github.com/stevenvelozo/foxhound)
|
|
172
|
-
- [stricture](https://github.com/stevenvelozo/stricture)
|
|
173
|
-
- [meadow-endpoints](https://github.com/stevenvelozo/meadow-endpoints)
|
|
174
|
-
- [meadow-connection-mssql](https://github.com/stevenvelozo/meadow-connection-mssql)
|
|
175
|
-
- [meadow-connection-sqlite](https://github.com/stevenvelozo/meadow-connection-sqlite)
|
|
176
|
-
- [fable](https://github.com/stevenvelozo/fable)
|
|
170
|
+
- [meadow](https://github.com/stevenvelozo/meadow) -- ORM and data access framework
|
|
171
|
+
- [foxhound](https://github.com/stevenvelozo/foxhound) -- Query DSL used by Meadow
|
|
172
|
+
- [stricture](https://github.com/stevenvelozo/stricture) -- Schema definition tool
|
|
173
|
+
- [meadow-endpoints](https://github.com/stevenvelozo/meadow-endpoints) -- RESTful endpoint generation
|
|
174
|
+
- [meadow-connection-mssql](https://github.com/stevenvelozo/meadow-connection-mssql) -- MSSQL connector
|
|
175
|
+
- [meadow-connection-sqlite](https://github.com/stevenvelozo/meadow-connection-sqlite) -- SQLite connector
|
|
176
|
+
- [fable](https://github.com/stevenvelozo/fable) -- Application services framework
|
|
177
177
|
|
|
178
178
|
## Testing
|
|
179
179
|
|
|
@@ -191,8 +191,9 @@ npm run coverage
|
|
|
191
191
|
|
|
192
192
|
## Related Packages
|
|
193
193
|
|
|
194
|
-
- [meadow](https://github.com/stevenvelozo/meadow)
|
|
195
|
-
- [
|
|
194
|
+
- [meadow](https://github.com/stevenvelozo/meadow) -- Data access and ORM
|
|
195
|
+
- [meadow-connection-rocksdb](https://github.com/stevenvelozo/meadow-connection-rocksdb) -- RocksDB connection provider
|
|
196
|
+
- [fable](https://github.com/stevenvelozo/fable) -- Application services framework
|
|
196
197
|
|
|
197
198
|
## License
|
|
198
199
|
|
package/docs/README.md
CHANGED
|
@@ -152,9 +152,29 @@ tmpBookDAL.doReads(tmpBookDAL.query, (pError, pQuery, pRecords) =>
|
|
|
152
152
|
|
|
153
153
|
The service can generate and execute CREATE TABLE statements from Meadow schema definitions. See [Schema & Table Creation](schema.md) for details.
|
|
154
154
|
|
|
155
|
+
## How It Fits Together
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
Application Code
|
|
159
|
+
|
|
|
160
|
+
Meadow ORM ← setProvider('MySQL')
|
|
161
|
+
|
|
|
162
|
+
FoxHound ← Generates SQL queries
|
|
163
|
+
|
|
|
164
|
+
Meadow-Connection-MySQL ← This module
|
|
165
|
+
|
|
|
166
|
+
mysql2 Pool ← Connection pooling, named placeholders
|
|
167
|
+
|
|
|
168
|
+
MySQL Server ← Your database
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Meadow Connection MySQL sits between the Meadow ORM layer and the MySQL server. It manages the mysql2 connection pool, which Meadow's MySQL provider uses to execute queries generated by FoxHound.
|
|
172
|
+
|
|
155
173
|
## Learn More
|
|
156
174
|
|
|
175
|
+
- [Quickstart](quickstart.md) -- Step-by-step walkthrough with code examples
|
|
176
|
+
- [Architecture](architecture.md) -- Design, data flow, and component responsibilities
|
|
157
177
|
- [Schema & Table Creation](schema.md) -- Generate MySQL tables from Meadow schemas
|
|
158
|
-
- [API Reference](api.md) -- Complete method and property documentation
|
|
178
|
+
- [API Reference](api/reference.md) -- Complete method and property documentation
|
|
159
179
|
- [Meadow](/meadow/meadow/) -- The data access layer
|
|
160
180
|
- [FoxHound](/meadow/foxhound/) -- Query DSL used by Meadow
|
package/docs/_cover.md
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
# Meadow Connection MySQL
|
|
2
2
|
|
|
3
|
+
<small>v1.0.8</small>
|
|
4
|
+
|
|
3
5
|
> MySQL connection pooling as a Fable service
|
|
4
6
|
|
|
5
|
-
A lightweight wrapper around mysql2 that manages connection pooling, supports auto-connect, and integrates with Meadow for provider-based data access. Register once, query from anywhere.
|
|
7
|
+
A lightweight wrapper around [mysql2](https://github.com/sidorares/node-mysql2) that manages connection pooling, supports auto-connect, and integrates with Meadow for provider-based data access. Register once, query from anywhere.
|
|
6
8
|
|
|
7
9
|
- **Connection Pooling** -- Managed mysql2 pool with configurable connection limits
|
|
8
10
|
- **Auto-Connect** -- Set one flag and the pool is ready at service instantiation
|
|
9
|
-
- **Schema
|
|
10
|
-
- **
|
|
11
|
+
- **Schema-Driven DDL** -- Generate and execute CREATE TABLE statements from Meadow schema definitions
|
|
12
|
+
- **Named Placeholders** -- Automatic named placeholder support for parameterized queries
|
|
13
|
+
- **Flexible Config** -- Accepts both Meadow-style and mysql2-native configuration formats
|
|
14
|
+
- **Connection Safety** -- Guards against duplicate pools with cleansed logging
|
|
11
15
|
|
|
12
16
|
[Get Started](README.md)
|
|
17
|
+
[Quickstart](quickstart.md)
|
|
18
|
+
[Architecture](architecture.md)
|
|
13
19
|
[Schema & Tables](schema.md)
|
|
14
|
-
[API Reference](api.md)
|
|
20
|
+
[API Reference](api/reference.md)
|
|
15
21
|
[GitHub](https://github.com/stevenvelozo/meadow-connection-mysql)
|
package/docs/_sidebar.md
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
- Getting Started
|
|
2
2
|
|
|
3
3
|
- [Overview](README.md)
|
|
4
|
+
- [Quickstart](quickstart.md)
|
|
5
|
+
|
|
6
|
+
- Architecture
|
|
7
|
+
|
|
8
|
+
- [Design & Data Flow](architecture.md)
|
|
4
9
|
- [Schema & Table Creation](schema.md)
|
|
5
10
|
|
|
6
|
-
- Reference
|
|
11
|
+
- API Reference
|
|
7
12
|
|
|
8
|
-
- [
|
|
13
|
+
- [Full Reference](api/reference.md)
|
|
14
|
+
- [connect()](api/connect.md)
|
|
15
|
+
- [connectAsync()](api/connectAsync.md)
|
|
16
|
+
- [pool](api/pool.md)
|
|
17
|
+
- [generateCreateTableStatement()](api/generateCreateTableStatement.md)
|
|
18
|
+
- [createTable()](api/createTable.md)
|
|
19
|
+
- [createTables()](api/createTables.md)
|
|
20
|
+
- [generateDropTableStatement()](api/generateDropTableStatement.md)
|
|
9
21
|
|
|
10
22
|
- Retold Ecosystem
|
|
11
23
|
|
|
@@ -13,4 +25,7 @@
|
|
|
13
25
|
- [FoxHound](/meadow/foxhound/)
|
|
14
26
|
- [Stricture](/meadow/stricture/)
|
|
15
27
|
- [Meadow Endpoints](/meadow/meadow-endpoints/)
|
|
28
|
+
- [Meadow Connection MSSQL](/meadow/meadow-connection-mssql/)
|
|
29
|
+
- [Meadow Connection SQLite](/meadow/meadow-connection-sqlite/)
|
|
30
|
+
- [Meadow Connection RocksDB](/meadow/meadow-connection-rocksdb/)
|
|
16
31
|
- [Fable](/fable/fable/)
|
package/docs/_topbar.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# Meadow Connection MySQL
|
|
2
2
|
|
|
3
3
|
- [Overview](README.md)
|
|
4
|
+
- [Quickstart](quickstart.md)
|
|
5
|
+
- [Architecture](architecture.md)
|
|
4
6
|
- [Schema & Tables](schema.md)
|
|
5
|
-
- [API Reference](api.md)
|
|
7
|
+
- [API Reference](api/reference.md)
|
|
6
8
|
- [GitHub](https://github.com/stevenvelozo/meadow-connection-mysql)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# connect()
|
|
2
|
+
|
|
3
|
+
Creates the mysql2 connection pool synchronously.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
connect()
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
None.
|
|
14
|
+
|
|
15
|
+
## Return Value
|
|
16
|
+
|
|
17
|
+
None.
|
|
18
|
+
|
|
19
|
+
## Behavior
|
|
20
|
+
|
|
21
|
+
1. Builds a connection settings object from `this.options.MySQL` (host, port, user, password, database, connectionLimit) plus `namedPlaceholders: true`
|
|
22
|
+
2. If a pool already exists (`this._ConnectionPool` is truthy):
|
|
23
|
+
- Logs an error with cleansed settings (password replaced with `*****************`)
|
|
24
|
+
- Skips pool creation to prevent duplicate connections
|
|
25
|
+
3. If no pool exists:
|
|
26
|
+
- Logs the connection details (host, port, user, database, connection limit)
|
|
27
|
+
- Calls `mysql2.createPool()` with the settings
|
|
28
|
+
- Sets `this.connected = true`
|
|
29
|
+
|
|
30
|
+
## Basic Usage
|
|
31
|
+
|
|
32
|
+
```javascript
|
|
33
|
+
const libFable = require('fable');
|
|
34
|
+
const libMeadowConnectionMySQL = require('meadow-connection-mysql');
|
|
35
|
+
|
|
36
|
+
let _Fable = new libFable(
|
|
37
|
+
{
|
|
38
|
+
"MySQL":
|
|
39
|
+
{
|
|
40
|
+
"Server": "127.0.0.1",
|
|
41
|
+
"Port": 3306,
|
|
42
|
+
"User": "root",
|
|
43
|
+
"Password": "secret",
|
|
44
|
+
"Database": "bookstore",
|
|
45
|
+
"ConnectionPoolLimit": 20
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
_Fable.serviceManager.addServiceType('MeadowMySQLProvider', libMeadowConnectionMySQL);
|
|
50
|
+
_Fable.serviceManager.instantiateServiceProvider('MeadowMySQLProvider');
|
|
51
|
+
|
|
52
|
+
_Fable.MeadowMySQLProvider.connect();
|
|
53
|
+
|
|
54
|
+
console.log(_Fable.MeadowMySQLProvider.connected);
|
|
55
|
+
// => true
|
|
56
|
+
|
|
57
|
+
// Query using the pool
|
|
58
|
+
_Fable.MeadowMySQLProvider.pool.query('SELECT * FROM Book LIMIT 5',
|
|
59
|
+
(pError, pRows) =>
|
|
60
|
+
{
|
|
61
|
+
console.log(`Found ${pRows.length} books.`);
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Double-Connect Guard
|
|
66
|
+
|
|
67
|
+
Calling `connect()` a second time does not create a new pool:
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
_Fable.MeadowMySQLProvider.connect();
|
|
71
|
+
// First call -- pool created, connected = true
|
|
72
|
+
|
|
73
|
+
_Fable.MeadowMySQLProvider.connect();
|
|
74
|
+
// Second call -- logs error, pool unchanged
|
|
75
|
+
// "Meadow-Connection-MySQL trying to connect to MySQL but is already connected"
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
This prevents connection leaks from accidental double-initialization.
|
|
79
|
+
|
|
80
|
+
## Password Safety
|
|
81
|
+
|
|
82
|
+
When the double-connect guard triggers, the error log includes the connection settings with the password masked:
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
{ connectionLimit: 20, host: '127.0.0.1', port: 3306, user: 'root',
|
|
86
|
+
password: '*****************', database: 'bookstore', namedPlaceholders: true }
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Related
|
|
90
|
+
|
|
91
|
+
- [connectAsync](connectAsync.md) -- Async connection with callback
|
|
92
|
+
- [pool](pool.md) -- Access the connection pool
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# connectAsync(fCallback)
|
|
2
|
+
|
|
3
|
+
Async wrapper around `connect()` that provides a callback interface for consistency with other Meadow connection providers (MSSQL, SQLite, RocksDB).
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
connectAsync(fCallback)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Parameter | Type | Required | Description |
|
|
14
|
+
|-----------|------|----------|-------------|
|
|
15
|
+
| `fCallback` | function | Yes | Callback with signature `(pError, pConnectionPool)` |
|
|
16
|
+
|
|
17
|
+
## Return Value
|
|
18
|
+
|
|
19
|
+
None (result provided via callback).
|
|
20
|
+
|
|
21
|
+
## Callback Arguments
|
|
22
|
+
|
|
23
|
+
| Argument | Type | Description |
|
|
24
|
+
|----------|------|-------------|
|
|
25
|
+
| `pError` | Error or null | Connection error, if any |
|
|
26
|
+
| `pConnectionPool` | mysql2 Pool | The mysql2 connection pool instance |
|
|
27
|
+
|
|
28
|
+
## Behavior
|
|
29
|
+
|
|
30
|
+
1. If `fCallback` is not a function, logs a warning and substitutes a no-op callback
|
|
31
|
+
2. If a pool already exists, returns it immediately via the callback (no reconnection)
|
|
32
|
+
3. Otherwise calls `connect()` to create the pool, then returns it via the callback
|
|
33
|
+
4. Wraps the entire operation in try-catch -- any errors are passed to the callback
|
|
34
|
+
|
|
35
|
+
## Basic Usage
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
const libFable = require('fable');
|
|
39
|
+
const libMeadowConnectionMySQL = require('meadow-connection-mysql');
|
|
40
|
+
|
|
41
|
+
let _Fable = new libFable(
|
|
42
|
+
{
|
|
43
|
+
"MySQL":
|
|
44
|
+
{
|
|
45
|
+
"Server": "127.0.0.1",
|
|
46
|
+
"Port": 3306,
|
|
47
|
+
"User": "root",
|
|
48
|
+
"Password": "secret",
|
|
49
|
+
"Database": "bookstore",
|
|
50
|
+
"ConnectionPoolLimit": 20
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
_Fable.serviceManager.addServiceType('MeadowMySQLProvider', libMeadowConnectionMySQL);
|
|
55
|
+
_Fable.serviceManager.instantiateServiceProvider('MeadowMySQLProvider');
|
|
56
|
+
|
|
57
|
+
_Fable.MeadowMySQLProvider.connectAsync(
|
|
58
|
+
(pError, pConnectionPool) =>
|
|
59
|
+
{
|
|
60
|
+
if (pError)
|
|
61
|
+
{
|
|
62
|
+
console.error('Connection failed:', pError);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// pConnectionPool is the mysql2 pool
|
|
67
|
+
pConnectionPool.query('SELECT * FROM Book LIMIT 5',
|
|
68
|
+
(pQueryError, pRows) =>
|
|
69
|
+
{
|
|
70
|
+
console.log(`Found ${pRows.length} books.`);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Idempotent Calls
|
|
76
|
+
|
|
77
|
+
Calling `connectAsync()` multiple times is safe -- if the pool already exists, it is returned without creating a new one:
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
_Fable.MeadowMySQLProvider.connectAsync(
|
|
81
|
+
(pError, pPool1) =>
|
|
82
|
+
{
|
|
83
|
+
// First call creates the pool
|
|
84
|
+
|
|
85
|
+
_Fable.MeadowMySQLProvider.connectAsync(
|
|
86
|
+
(pError, pPool2) =>
|
|
87
|
+
{
|
|
88
|
+
// Second call returns the same pool
|
|
89
|
+
console.log(pPool1 === pPool2);
|
|
90
|
+
// => true
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Why connectAsync() Exists
|
|
96
|
+
|
|
97
|
+
MySQL connection via mysql2 is inherently synchronous (the pool is created immediately, and TCP connections are managed lazily). The `connectAsync()` method exists for API consistency across Meadow connection providers:
|
|
98
|
+
|
|
99
|
+
- **MSSQL** -- Requires async handshake (TDS protocol)
|
|
100
|
+
- **SQLite** -- Requires async file open
|
|
101
|
+
- **RocksDB** -- Requires async database open
|
|
102
|
+
|
|
103
|
+
Using `connectAsync()` makes your code portable across database backends.
|
|
104
|
+
|
|
105
|
+
## Missing Callback Warning
|
|
106
|
+
|
|
107
|
+
If called without a callback, `connectAsync()` logs a warning:
|
|
108
|
+
|
|
109
|
+
```javascript
|
|
110
|
+
// Not recommended
|
|
111
|
+
_Fable.MeadowMySQLProvider.connectAsync();
|
|
112
|
+
// Logs: "Meadow MySQL connectAsync() called without a callback; this could lead to connection race conditions."
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Related
|
|
116
|
+
|
|
117
|
+
- [connect](connect.md) -- Synchronous connection
|
|
118
|
+
- [pool](pool.md) -- Access the connection pool
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# createTable(pMeadowTableSchema, fCallback)
|
|
2
|
+
|
|
3
|
+
Generates and executes a `CREATE TABLE IF NOT EXISTS` statement against the connected MySQL pool.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
createTable(pMeadowTableSchema, fCallback)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Parameter | Type | Required | Description |
|
|
14
|
+
|-----------|------|----------|-------------|
|
|
15
|
+
| `pMeadowTableSchema` | object | Yes | Schema with `TableName` and `Columns` array |
|
|
16
|
+
| `fCallback` | function | Yes | Callback with signature `(pError)` |
|
|
17
|
+
|
|
18
|
+
## Return Value
|
|
19
|
+
|
|
20
|
+
None (result provided via callback).
|
|
21
|
+
|
|
22
|
+
## Behavior
|
|
23
|
+
|
|
24
|
+
1. Calls `generateCreateTableStatement(pMeadowTableSchema)` to produce the DDL string
|
|
25
|
+
2. Executes the DDL against `this._ConnectionPool` via `pool.query()`
|
|
26
|
+
3. On success: logs a success message and calls `fCallback()` with no error
|
|
27
|
+
4. On error:
|
|
28
|
+
- If the error indicates the table already exists (detected via `originalError.info.message`), logs a warning and calls `fCallback()` with no error
|
|
29
|
+
- Otherwise logs the error and calls `fCallback(pError)`
|
|
30
|
+
|
|
31
|
+
## Basic Usage
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
let tmpSchema =
|
|
35
|
+
{
|
|
36
|
+
TableName: 'Book',
|
|
37
|
+
Columns:
|
|
38
|
+
[
|
|
39
|
+
{ Column: 'IDBook', DataType: 'ID' },
|
|
40
|
+
{ Column: 'GUIDBook', DataType: 'GUID', Size: 36 },
|
|
41
|
+
{ Column: 'Title', DataType: 'String', Size: 256 },
|
|
42
|
+
{ Column: 'Author', DataType: 'String', Size: 128 },
|
|
43
|
+
{ Column: 'Year', DataType: 'Numeric' },
|
|
44
|
+
{ Column: 'Price', DataType: 'Decimal', Size: '10,2' }
|
|
45
|
+
]
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
_Fable.MeadowMySQLProvider.createTable(tmpSchema,
|
|
49
|
+
(pError) =>
|
|
50
|
+
{
|
|
51
|
+
if (pError)
|
|
52
|
+
{
|
|
53
|
+
console.error('Table creation failed:', pError);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
console.log('Book table created successfully.');
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Idempotent Operation
|
|
61
|
+
|
|
62
|
+
Because the generated DDL uses `CREATE TABLE IF NOT EXISTS`, calling `createTable()` for an existing table does not produce an error. A warning is logged instead:
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
Meadow-MySQL CREATE TABLE Book executed but table already existed.
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
This makes `createTable()` safe to call during application startup without worrying about pre-existing tables.
|
|
69
|
+
|
|
70
|
+
## Prerequisites
|
|
71
|
+
|
|
72
|
+
The connection pool must be established before calling `createTable()`:
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
_Fable.MeadowMySQLProvider.connectAsync(
|
|
76
|
+
(pError) =>
|
|
77
|
+
{
|
|
78
|
+
if (pError) { return; }
|
|
79
|
+
|
|
80
|
+
_Fable.MeadowMySQLProvider.createTable(tmpSchema,
|
|
81
|
+
(pCreateError) =>
|
|
82
|
+
{
|
|
83
|
+
if (pCreateError) { console.error(pCreateError); }
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Related
|
|
89
|
+
|
|
90
|
+
- [generateCreateTableStatement](generateCreateTableStatement.md) -- Generate DDL without executing
|
|
91
|
+
- [createTables](createTables.md) -- Create multiple tables sequentially
|
|
92
|
+
- [generateDropTableStatement](generateDropTableStatement.md) -- Generate DROP TABLE DDL
|
|
93
|
+
- [Schema & Table Creation](../schema.md) -- Full walkthrough
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# createTables(pMeadowSchema, fCallback)
|
|
2
|
+
|
|
3
|
+
Creates all tables defined in a Meadow schema object by calling `createTable()` for each table sequentially.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
createTables(pMeadowSchema, fCallback)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Parameter | Type | Required | Description |
|
|
14
|
+
|-----------|------|----------|-------------|
|
|
15
|
+
| `pMeadowSchema` | object | Yes | Schema with a `Tables` array of table schema objects |
|
|
16
|
+
| `fCallback` | function | Yes | Callback with signature `(pError)` |
|
|
17
|
+
|
|
18
|
+
### Schema Object Format
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
{
|
|
22
|
+
Tables:
|
|
23
|
+
[
|
|
24
|
+
{
|
|
25
|
+
TableName: 'Author',
|
|
26
|
+
Columns:
|
|
27
|
+
[
|
|
28
|
+
{ Column: 'IDAuthor', DataType: 'ID' },
|
|
29
|
+
{ Column: 'Name', DataType: 'String', Size: 200 }
|
|
30
|
+
]
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
TableName: 'Book',
|
|
34
|
+
Columns:
|
|
35
|
+
[
|
|
36
|
+
{ Column: 'IDBook', DataType: 'ID' },
|
|
37
|
+
{ Column: 'Title', DataType: 'String', Size: 256 },
|
|
38
|
+
{ Column: 'IDAuthor', DataType: 'ForeignKey' }
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Return Value
|
|
46
|
+
|
|
47
|
+
None (result provided via callback).
|
|
48
|
+
|
|
49
|
+
## Behavior
|
|
50
|
+
|
|
51
|
+
1. Iterates `pMeadowSchema.Tables` using `fable.Utility.eachLimit()` with a concurrency of 1 (serial)
|
|
52
|
+
2. Calls `createTable()` for each table in the array
|
|
53
|
+
3. If any table creation fails, logs the error and passes it to the final callback
|
|
54
|
+
4. On completion, logs a success message and calls `fCallback()`
|
|
55
|
+
|
|
56
|
+
## Basic Usage
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
let tmpFullSchema =
|
|
60
|
+
{
|
|
61
|
+
Tables:
|
|
62
|
+
[
|
|
63
|
+
{
|
|
64
|
+
TableName: 'Author',
|
|
65
|
+
Columns:
|
|
66
|
+
[
|
|
67
|
+
{ Column: 'IDAuthor', DataType: 'ID' },
|
|
68
|
+
{ Column: 'GUIDAuthor', DataType: 'GUID', Size: 36 },
|
|
69
|
+
{ Column: 'Name', DataType: 'String', Size: 200 },
|
|
70
|
+
{ Column: 'Bio', DataType: 'Text' }
|
|
71
|
+
]
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
TableName: 'Book',
|
|
75
|
+
Columns:
|
|
76
|
+
[
|
|
77
|
+
{ Column: 'IDBook', DataType: 'ID' },
|
|
78
|
+
{ Column: 'GUIDBook', DataType: 'GUID', Size: 36 },
|
|
79
|
+
{ Column: 'Title', DataType: 'String', Size: 256 },
|
|
80
|
+
{ Column: 'Year', DataType: 'Numeric' },
|
|
81
|
+
{ Column: 'Price', DataType: 'Decimal', Size: '10,2' },
|
|
82
|
+
{ Column: 'IDAuthor', DataType: 'ForeignKey' }
|
|
83
|
+
]
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
TableName: 'Review',
|
|
87
|
+
Columns:
|
|
88
|
+
[
|
|
89
|
+
{ Column: 'IDReview', DataType: 'ID' },
|
|
90
|
+
{ Column: 'GUIDReview', DataType: 'GUID', Size: 36 },
|
|
91
|
+
{ Column: 'IDBook', DataType: 'ForeignKey' },
|
|
92
|
+
{ Column: 'Rating', DataType: 'Numeric' },
|
|
93
|
+
{ Column: 'Body', DataType: 'Text' },
|
|
94
|
+
{ Column: 'ReviewDate', DataType: 'DateTime' }
|
|
95
|
+
]
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
_Fable.MeadowMySQLProvider.createTables(tmpFullSchema,
|
|
101
|
+
(pError) =>
|
|
102
|
+
{
|
|
103
|
+
if (pError)
|
|
104
|
+
{
|
|
105
|
+
console.error('Schema creation failed:', pError);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
console.log('All tables created.');
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Serial Execution
|
|
113
|
+
|
|
114
|
+
Tables are created one at a time (concurrency of 1). This avoids race conditions with foreign key constraints and ensures predictable execution order. The tables are created in the order they appear in the `Tables` array.
|
|
115
|
+
|
|
116
|
+
## Error Handling
|
|
117
|
+
|
|
118
|
+
If a table creation fails with an error other than "table already exists", execution stops and the error is passed to the callback. Tables created before the failure remain in the database.
|
|
119
|
+
|
|
120
|
+
## Related
|
|
121
|
+
|
|
122
|
+
- [createTable](createTable.md) -- Create a single table
|
|
123
|
+
- [generateCreateTableStatement](generateCreateTableStatement.md) -- Generate DDL without executing
|
|
124
|
+
- [Schema & Table Creation](../schema.md) -- Full walkthrough
|