meadow-connection-postgresql 1.0.0 → 1.0.1
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 +133 -0
- package/docker-compose.yml +20 -0
- package/docs/.nojekyll +0 -0
- package/docs/README.md +96 -0
- package/docs/_cover.md +17 -0
- package/docs/_sidebar.md +33 -0
- package/docs/_topbar.md +5 -0
- package/docs/api/connect.md +100 -0
- package/docs/api/connectAsync.md +92 -0
- package/docs/api/createTable.md +116 -0
- package/docs/api/createTables.md +128 -0
- package/docs/api/generateCreateTableStatement.md +136 -0
- package/docs/api/generateDropTableStatement.md +71 -0
- package/docs/api/pool.md +171 -0
- package/docs/api/reference.md +112 -0
- package/docs/api.md +3 -0
- package/docs/architecture.md +168 -0
- package/docs/css/docuserve.css +73 -0
- package/docs/index.html +39 -0
- package/docs/quickstart.md +181 -0
- package/docs/retold-catalog.json +62 -0
- package/docs/retold-keyword-index.json +4964 -0
- package/docs/schema.md +148 -0
- package/package.json +5 -2
- package/source/Meadow-Connection-PostgreSQL.js +79 -99
- package/source/Meadow-Schema-PostgreSQL.js +1038 -0
- package/start-postgresql.sh +21 -0
- package/stop-postgresql.sh +9 -0
- package/test/PostgreSQL_tests.js +865 -1
- package/test/docker-init/01-chinook-schema.sql +177 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# createTables(pMeadowSchema, fCallback)
|
|
2
|
+
|
|
3
|
+
Creates multiple PostgreSQL tables sequentially from a Stricture schema object.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
createTables(pMeadowSchema, fCallback)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Parameter | Type | Description |
|
|
14
|
+
|-----------|------|-------------|
|
|
15
|
+
| `pMeadowSchema` | `object` | Schema with a `Tables` array of Meadow table schemas |
|
|
16
|
+
| `fCallback` | `function` | Callback receiving `(error)` |
|
|
17
|
+
|
|
18
|
+
## Return Value
|
|
19
|
+
|
|
20
|
+
Returns the result of the callback invocation.
|
|
21
|
+
|
|
22
|
+
## Behavior
|
|
23
|
+
|
|
24
|
+
1. Iterates over `pMeadowSchema.Tables` using `fable.Utility.eachLimit` with concurrency of 1
|
|
25
|
+
2. Calls `this.createTable(table, callback)` for each table
|
|
26
|
+
3. On completion: logs info, calls `fCallback()` with no error
|
|
27
|
+
4. On error: logs the error, calls `fCallback(pError)`
|
|
28
|
+
|
|
29
|
+
## Basic Usage
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
let tmpSchema =
|
|
33
|
+
{
|
|
34
|
+
Tables:
|
|
35
|
+
[
|
|
36
|
+
{
|
|
37
|
+
TableName: 'Animal',
|
|
38
|
+
Columns:
|
|
39
|
+
[
|
|
40
|
+
{ Column: 'IDAnimal', DataType: 'ID' },
|
|
41
|
+
{ Column: 'GUIDAnimal', DataType: 'GUID', Size: 36 },
|
|
42
|
+
{ Column: 'Name', DataType: 'String', Size: 128 },
|
|
43
|
+
{ Column: 'IDFarm', DataType: 'ForeignKey' }
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
TableName: 'Farm',
|
|
48
|
+
Columns:
|
|
49
|
+
[
|
|
50
|
+
{ Column: 'IDFarm', DataType: 'ID' },
|
|
51
|
+
{ Column: 'GUIDFarm', DataType: 'GUID', Size: 36 },
|
|
52
|
+
{ Column: 'FarmName', DataType: 'String', Size: 256 }
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
TableName: 'Veterinarian',
|
|
57
|
+
Columns:
|
|
58
|
+
[
|
|
59
|
+
{ Column: 'IDVeterinarian', DataType: 'ID' },
|
|
60
|
+
{ Column: 'GUIDVeterinarian', DataType: 'GUID', Size: 36 },
|
|
61
|
+
{ Column: 'LastName', DataType: 'String', Size: 128 },
|
|
62
|
+
{ Column: 'IDFarm', DataType: 'ForeignKey' }
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
_Fable.MeadowPostgreSQLProvider.createTables(tmpSchema,
|
|
69
|
+
(pError) =>
|
|
70
|
+
{
|
|
71
|
+
if (pError)
|
|
72
|
+
{
|
|
73
|
+
console.error('Schema creation failed:', pError);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
console.log('All tables created!');
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Sequential Processing
|
|
81
|
+
|
|
82
|
+
Tables are created one at a time (concurrency of 1) using `fable.Utility.eachLimit`. This ensures:
|
|
83
|
+
|
|
84
|
+
- Deterministic creation order (important for foreign key dependencies)
|
|
85
|
+
- Clear log output showing each table as it is created
|
|
86
|
+
- Predictable error reporting -- the first failure stops the sequence
|
|
87
|
+
|
|
88
|
+
## Error Handling
|
|
89
|
+
|
|
90
|
+
If any table creation fails (other than the expected `42P07` duplicate_table error), the error is passed to the callback and remaining tables are skipped:
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
_Fable.MeadowPostgreSQLProvider.createTables(tmpSchema,
|
|
94
|
+
(pError) =>
|
|
95
|
+
{
|
|
96
|
+
if (pError)
|
|
97
|
+
{
|
|
98
|
+
// Only the first error is reported
|
|
99
|
+
console.error('Failed during schema creation:', pError);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Application Startup Pattern
|
|
105
|
+
|
|
106
|
+
Since `createTable()` handles existing tables gracefully, `createTables()` is safe to call on every application startup:
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
_Fable.MeadowPostgreSQLProvider.connectAsync(
|
|
110
|
+
(pError) =>
|
|
111
|
+
{
|
|
112
|
+
if (pError) { return console.error(pError); }
|
|
113
|
+
|
|
114
|
+
_Fable.MeadowPostgreSQLProvider.createTables(appSchema,
|
|
115
|
+
(pSchemaError) =>
|
|
116
|
+
{
|
|
117
|
+
if (pSchemaError) { return console.error(pSchemaError); }
|
|
118
|
+
console.log('Database ready -- starting application');
|
|
119
|
+
startApp();
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Related
|
|
125
|
+
|
|
126
|
+
- [createTable](createTable.md) -- Create a single table
|
|
127
|
+
- [generateCreateTableStatement](generateCreateTableStatement.md) -- Generate DDL without executing
|
|
128
|
+
- [Schema & Column Types](../schema.md) -- Full type mapping reference
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# generateCreateTableStatement(pMeadowTableSchema)
|
|
2
|
+
|
|
3
|
+
Generates a PostgreSQL `CREATE TABLE IF NOT EXISTS` DDL string from a Meadow table schema. Returns the SQL string without executing it.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
generateCreateTableStatement(pMeadowTableSchema)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Parameter | Type | Description |
|
|
14
|
+
|-----------|------|-------------|
|
|
15
|
+
| `pMeadowTableSchema` | `object` | Meadow table schema with `TableName` and `Columns` array |
|
|
16
|
+
|
|
17
|
+
## Return Value
|
|
18
|
+
|
|
19
|
+
| Type | Description |
|
|
20
|
+
|------|-------------|
|
|
21
|
+
| `string` | The complete `CREATE TABLE IF NOT EXISTS` SQL statement |
|
|
22
|
+
|
|
23
|
+
## Schema Object Format
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
let tmpSchema =
|
|
27
|
+
{
|
|
28
|
+
TableName: 'Animal',
|
|
29
|
+
Columns:
|
|
30
|
+
[
|
|
31
|
+
{ Column: 'IDAnimal', DataType: 'ID' },
|
|
32
|
+
{ Column: 'GUIDAnimal', DataType: 'GUID', Size: 36 },
|
|
33
|
+
{ Column: 'Name', DataType: 'String', Size: 128 },
|
|
34
|
+
{ Column: 'Age', DataType: 'Numeric' },
|
|
35
|
+
{ Column: 'Weight', DataType: 'Decimal', Size: '10,2' },
|
|
36
|
+
{ Column: 'Description', DataType: 'Text' },
|
|
37
|
+
{ Column: 'CreateDate', DataType: 'DateTime' },
|
|
38
|
+
{ Column: 'Deleted', DataType: 'Boolean' }
|
|
39
|
+
]
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Basic Usage
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
let tmpDDL = _Fable.MeadowPostgreSQLProvider.generateCreateTableStatement(tmpSchema);
|
|
47
|
+
console.log(tmpDDL);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Output:
|
|
51
|
+
|
|
52
|
+
```sql
|
|
53
|
+
-- [ Animal ]
|
|
54
|
+
CREATE TABLE IF NOT EXISTS
|
|
55
|
+
"Animal"
|
|
56
|
+
(
|
|
57
|
+
"IDAnimal" SERIAL PRIMARY KEY,
|
|
58
|
+
"GUIDAnimal" VARCHAR(36) DEFAULT '0xDe',
|
|
59
|
+
"Name" VARCHAR(128) NOT NULL DEFAULT '',
|
|
60
|
+
"Age" INTEGER NOT NULL DEFAULT 0,
|
|
61
|
+
"Weight" DECIMAL(10,2),
|
|
62
|
+
"Description" TEXT,
|
|
63
|
+
"CreateDate" TIMESTAMP,
|
|
64
|
+
"Deleted" BOOLEAN NOT NULL DEFAULT false
|
|
65
|
+
);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Type Mapping
|
|
69
|
+
|
|
70
|
+
| Meadow DataType | PostgreSQL Type | Constraints |
|
|
71
|
+
|-----------------|-----------------|-------------|
|
|
72
|
+
| `ID` | `SERIAL` | `PRIMARY KEY` |
|
|
73
|
+
| `GUID` | `VARCHAR(Size)` | `DEFAULT '0xDe'` |
|
|
74
|
+
| `ForeignKey` | `INTEGER` | `NOT NULL DEFAULT 0` |
|
|
75
|
+
| `Numeric` | `INTEGER` | `NOT NULL DEFAULT 0` |
|
|
76
|
+
| `Decimal` | `DECIMAL(Size)` | -- |
|
|
77
|
+
| `String` | `VARCHAR(Size)` | `NOT NULL DEFAULT ''` |
|
|
78
|
+
| `Text` | `TEXT` | -- |
|
|
79
|
+
| `DateTime` | `TIMESTAMP` | -- |
|
|
80
|
+
| `Boolean` | `BOOLEAN` | `NOT NULL DEFAULT false` |
|
|
81
|
+
|
|
82
|
+
## DDL Structure
|
|
83
|
+
|
|
84
|
+
The generated DDL has these parts:
|
|
85
|
+
|
|
86
|
+
1. **Comment line** -- `-- [ TableName ]`
|
|
87
|
+
2. **CREATE TABLE IF NOT EXISTS** -- idempotent creation
|
|
88
|
+
3. **Quoted table name** -- double-quoted for reserved word safety
|
|
89
|
+
4. **Column definitions** -- each column on its own line, comma-separated
|
|
90
|
+
5. **Closing parenthesis and semicolon**
|
|
91
|
+
|
|
92
|
+
### Quoted Identifiers
|
|
93
|
+
|
|
94
|
+
All table and column names are wrapped in double quotes (`"Name"`). This ensures safe handling of PostgreSQL reserved words (e.g., `"User"`, `"Order"`, `"Group"`).
|
|
95
|
+
|
|
96
|
+
## GUID Size Handling
|
|
97
|
+
|
|
98
|
+
The `GUID` type defaults to `VARCHAR(36)` if no `Size` is specified. If the `Size` value is `NaN`, it also falls back to 36:
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
// Explicit size
|
|
102
|
+
{ Column: 'GUIDAnimal', DataType: 'GUID', Size: 36 }
|
|
103
|
+
// => "GUIDAnimal" VARCHAR(36) DEFAULT '0xDe'
|
|
104
|
+
|
|
105
|
+
// No size -- defaults to 36
|
|
106
|
+
{ Column: 'GUIDAnimal', DataType: 'GUID' }
|
|
107
|
+
// => "GUIDAnimal" VARCHAR(36) DEFAULT '0xDe'
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Inspecting Without Executing
|
|
111
|
+
|
|
112
|
+
Use this method to preview the DDL without applying it to the database:
|
|
113
|
+
|
|
114
|
+
```javascript
|
|
115
|
+
let tmpDDL = _Fable.MeadowPostgreSQLProvider.generateCreateTableStatement(tmpSchema);
|
|
116
|
+
console.log('Would execute:');
|
|
117
|
+
console.log(tmpDDL);
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Differences from MySQL DDL
|
|
121
|
+
|
|
122
|
+
| Feature | PostgreSQL | MySQL |
|
|
123
|
+
|---------|-----------|-------|
|
|
124
|
+
| Auto-increment | `SERIAL PRIMARY KEY` | `INT UNSIGNED NOT NULL AUTO_INCREMENT` |
|
|
125
|
+
| Boolean | `BOOLEAN NOT NULL DEFAULT false` | `TINYINT NOT NULL DEFAULT 0` |
|
|
126
|
+
| DateTime | `TIMESTAMP` | `DATETIME` |
|
|
127
|
+
| Identifier quoting | `"column"` | `` `column` `` |
|
|
128
|
+
| Integer signing | Signed only | `UNSIGNED` support |
|
|
129
|
+
| Character set | Inherited from database | Per-table `CHARSET`/`COLLATE` |
|
|
130
|
+
|
|
131
|
+
## Related
|
|
132
|
+
|
|
133
|
+
- [createTable](createTable.md) -- Generate and execute the DDL
|
|
134
|
+
- [createTables](createTables.md) -- Create multiple tables
|
|
135
|
+
- [generateDropTableStatement](generateDropTableStatement.md) -- Generate DROP TABLE DDL
|
|
136
|
+
- [Schema & Column Types](../schema.md) -- Full type mapping reference
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# generateDropTableStatement(pTableName)
|
|
2
|
+
|
|
3
|
+
Generates a PostgreSQL `DROP TABLE IF EXISTS` DDL string for the named table.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
generateDropTableStatement(pTableName)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Parameter | Type | Description |
|
|
14
|
+
|-----------|------|-------------|
|
|
15
|
+
| `pTableName` | `string` | The name of the table to drop |
|
|
16
|
+
|
|
17
|
+
## Return Value
|
|
18
|
+
|
|
19
|
+
| Type | Description |
|
|
20
|
+
|------|-------------|
|
|
21
|
+
| `string` | The `DROP TABLE IF EXISTS` SQL statement |
|
|
22
|
+
|
|
23
|
+
## Basic Usage
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
let tmpDrop = _Fable.MeadowPostgreSQLProvider.generateDropTableStatement('Animal');
|
|
27
|
+
console.log(tmpDrop);
|
|
28
|
+
// => 'DROP TABLE IF EXISTS "Animal";'
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## DDL Format
|
|
32
|
+
|
|
33
|
+
The generated statement uses:
|
|
34
|
+
|
|
35
|
+
- **`IF EXISTS`** -- prevents errors when the table does not exist
|
|
36
|
+
- **Quoted identifier** -- double-quoted table name for reserved word safety
|
|
37
|
+
|
|
38
|
+
## Executing the Drop
|
|
39
|
+
|
|
40
|
+
The method only generates the SQL string. To execute it, use the `pool` getter:
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
let tmpDrop = _Fable.MeadowPostgreSQLProvider.generateDropTableStatement('Animal');
|
|
44
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
45
|
+
|
|
46
|
+
tmpPool.query(tmpDrop,
|
|
47
|
+
(pError, pResult) =>
|
|
48
|
+
{
|
|
49
|
+
if (pError) { return console.error(pError); }
|
|
50
|
+
console.log('Table dropped.');
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Cascading Drops
|
|
55
|
+
|
|
56
|
+
The generated statement does not include `CASCADE`. If the table has dependent objects (foreign keys, views), add `CASCADE` manually:
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
let tmpDrop = _Fable.MeadowPostgreSQLProvider.generateDropTableStatement('Farm');
|
|
60
|
+
// => 'DROP TABLE IF EXISTS "Farm";'
|
|
61
|
+
|
|
62
|
+
// To cascade:
|
|
63
|
+
let tmpCascadeDrop = tmpDrop.replace(';', ' CASCADE;');
|
|
64
|
+
// => 'DROP TABLE IF EXISTS "Farm" CASCADE;'
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Related
|
|
68
|
+
|
|
69
|
+
- [generateCreateTableStatement](generateCreateTableStatement.md) -- Generate CREATE TABLE DDL
|
|
70
|
+
- [createTable](createTable.md) -- Create a table
|
|
71
|
+
- [Schema & Column Types](../schema.md) -- Full type mapping reference
|
package/docs/api/pool.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# pool (getter)
|
|
2
|
+
|
|
3
|
+
Returns the `pg.Pool` connection pool instance for executing queries against PostgreSQL.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
get pool()
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Return Value
|
|
12
|
+
|
|
13
|
+
| Type | Description |
|
|
14
|
+
|------|-------------|
|
|
15
|
+
| `pg.Pool` | The node-postgres connection pool (after connecting) |
|
|
16
|
+
| `false` | Before connection |
|
|
17
|
+
|
|
18
|
+
## Primary Use
|
|
19
|
+
|
|
20
|
+
The `pool` getter is the main entry point for all PostgreSQL operations. It returns the `pg.Pool` instance which manages a pool of connections and provides automatic connection checkout/return.
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Simple Query
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
30
|
+
|
|
31
|
+
tmpPool.query('SELECT * FROM "Animal" WHERE "Deleted" = false',
|
|
32
|
+
(pError, pResult) =>
|
|
33
|
+
{
|
|
34
|
+
if (pError) { return console.error(pError); }
|
|
35
|
+
console.log(pResult.rows);
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Parameterized Query
|
|
40
|
+
|
|
41
|
+
PostgreSQL uses `$1, $2, $3, ...` for parameter placeholders:
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
45
|
+
|
|
46
|
+
tmpPool.query(
|
|
47
|
+
'SELECT * FROM "Animal" WHERE "Age" > $1 AND "Deleted" = $2',
|
|
48
|
+
[3, false],
|
|
49
|
+
(pError, pResult) =>
|
|
50
|
+
{
|
|
51
|
+
if (pError) { return console.error(pError); }
|
|
52
|
+
console.log(pResult.rows);
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Insert with Returning
|
|
57
|
+
|
|
58
|
+
PostgreSQL supports `RETURNING` to get the inserted row:
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
62
|
+
|
|
63
|
+
tmpPool.query(
|
|
64
|
+
'INSERT INTO "Animal" ("Name", "Age") VALUES ($1, $2) RETURNING "IDAnimal"',
|
|
65
|
+
['Luna', 5],
|
|
66
|
+
(pError, pResult) =>
|
|
67
|
+
{
|
|
68
|
+
if (pError) { return console.error(pError); }
|
|
69
|
+
console.log('Inserted ID:', pResult.rows[0].IDAnimal);
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Update
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
77
|
+
|
|
78
|
+
tmpPool.query(
|
|
79
|
+
'UPDATE "Animal" SET "Name" = $1, "Age" = $2 WHERE "IDAnimal" = $3',
|
|
80
|
+
['Luna Belle', 6, 1],
|
|
81
|
+
(pError, pResult) =>
|
|
82
|
+
{
|
|
83
|
+
if (pError) { return console.error(pError); }
|
|
84
|
+
console.log('Rows updated:', pResult.rowCount);
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Promise-Based Query
|
|
89
|
+
|
|
90
|
+
The `pg.Pool` supports both callbacks and Promises:
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
94
|
+
|
|
95
|
+
tmpPool.query('SELECT * FROM "Animal" WHERE "IDAnimal" = $1', [1])
|
|
96
|
+
.then((pResult) =>
|
|
97
|
+
{
|
|
98
|
+
console.log(pResult.rows[0]);
|
|
99
|
+
})
|
|
100
|
+
.catch((pError) =>
|
|
101
|
+
{
|
|
102
|
+
console.error(pError);
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Client Checkout
|
|
107
|
+
|
|
108
|
+
For transactions or multiple queries on the same connection, check out a client:
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
112
|
+
|
|
113
|
+
tmpPool.connect((pError, pClient, pDone) =>
|
|
114
|
+
{
|
|
115
|
+
if (pError) { return console.error(pError); }
|
|
116
|
+
|
|
117
|
+
pClient.query('BEGIN');
|
|
118
|
+
pClient.query('INSERT INTO "Animal" ("Name") VALUES ($1)', ['Fido']);
|
|
119
|
+
pClient.query('UPDATE "Farm" SET "AnimalCount" = "AnimalCount" + 1 WHERE "IDFarm" = $1', [42]);
|
|
120
|
+
pClient.query('COMMIT', (pCommitError) =>
|
|
121
|
+
{
|
|
122
|
+
pDone(); // Return client to pool
|
|
123
|
+
if (pCommitError) { console.error(pCommitError); }
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Pool Methods
|
|
129
|
+
|
|
130
|
+
| Method | Returns | Purpose |
|
|
131
|
+
|--------|---------|---------|
|
|
132
|
+
| `pool.query(sql, params, callback)` | `void` | Execute a query (auto-checkout) |
|
|
133
|
+
| `pool.query(sql, params)` | `Promise` | Execute a query (Promise) |
|
|
134
|
+
| `pool.connect(callback)` | `void` | Check out a client from the pool |
|
|
135
|
+
| `pool.end()` | `Promise` | Close all connections in the pool |
|
|
136
|
+
| `pool.on(event, handler)` | `Pool` | Listen for pool events |
|
|
137
|
+
|
|
138
|
+
## Pool Events
|
|
139
|
+
|
|
140
|
+
| Event | Description |
|
|
141
|
+
|-------|-------------|
|
|
142
|
+
| `'connect'` | Fired when a new client is created |
|
|
143
|
+
| `'acquire'` | Fired when a client is checked out from the pool |
|
|
144
|
+
| `'remove'` | Fired when a client is closed and removed |
|
|
145
|
+
| `'error'` | Fired on idle client error |
|
|
146
|
+
|
|
147
|
+
## Before Connection
|
|
148
|
+
|
|
149
|
+
Returns `false` before `connect()` or `connectAsync()` is called:
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
153
|
+
// tmpPool => false (not connected yet)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Always check `connected` before using `pool`:
|
|
157
|
+
|
|
158
|
+
```javascript
|
|
159
|
+
if (!_Fable.MeadowPostgreSQLProvider.connected)
|
|
160
|
+
{
|
|
161
|
+
console.error('Not connected to PostgreSQL.');
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Related
|
|
169
|
+
|
|
170
|
+
- [connectAsync](connectAsync.md) -- Establish the connection
|
|
171
|
+
- [connect](connect.md) -- Synchronous connection
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# API Reference
|
|
2
|
+
|
|
3
|
+
Complete reference for `meadow-connection-postgresql`.
|
|
4
|
+
|
|
5
|
+
## Service Information
|
|
6
|
+
|
|
7
|
+
| Property | Value |
|
|
8
|
+
|----------|-------|
|
|
9
|
+
| Service Type | `MeadowConnectionPostgreSQL` |
|
|
10
|
+
| Extends | `fable-serviceproviderbase` |
|
|
11
|
+
| Driver | `pg` ^8.13.0 |
|
|
12
|
+
|
|
13
|
+
## Connection Methods
|
|
14
|
+
|
|
15
|
+
### [connectAsync(fCallback)](connectAsync.md)
|
|
16
|
+
|
|
17
|
+
Callback-style connection method (recommended). Creates the `pg.Pool` instance. If already connected, returns the existing pool immediately.
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
_Fable.MeadowPostgreSQLProvider.connectAsync(
|
|
21
|
+
(pError, pPool) =>
|
|
22
|
+
{
|
|
23
|
+
if (pError) { return console.error(pError); }
|
|
24
|
+
// pPool is the pg.Pool instance
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### [connect()](connect.md)
|
|
29
|
+
|
|
30
|
+
Synchronous connection method. Creates the `pg.Pool` from the resolved settings. Called automatically when `MeadowConnectionPostgreSQLAutoConnect` is `true`.
|
|
31
|
+
|
|
32
|
+
```javascript
|
|
33
|
+
_Fable.MeadowPostgreSQLProvider.connect();
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Accessors
|
|
37
|
+
|
|
38
|
+
### [pool](pool.md)
|
|
39
|
+
|
|
40
|
+
Getter that returns the `pg.Pool` instance. Provides access to queries, parameterized statements, and client checkout.
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
|
|
44
|
+
tmpPool.query('SELECT * FROM "Animal"', (pErr, pRes) =>
|
|
45
|
+
{
|
|
46
|
+
console.log(pRes.rows);
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Schema Management
|
|
51
|
+
|
|
52
|
+
### [generateCreateTableStatement(pMeadowTableSchema)](generateCreateTableStatement.md)
|
|
53
|
+
|
|
54
|
+
Generates a PostgreSQL `CREATE TABLE IF NOT EXISTS` DDL string from a Meadow table schema. Returns the SQL string without executing it.
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
let tmpDDL = _Fable.MeadowPostgreSQLProvider.generateCreateTableStatement(tmpSchema);
|
|
58
|
+
console.log(tmpDDL);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### [createTable(pMeadowTableSchema, fCallback)](createTable.md)
|
|
62
|
+
|
|
63
|
+
Generates and executes a `CREATE TABLE` statement via the pool. Handles duplicate table errors gracefully.
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
_Fable.MeadowPostgreSQLProvider.createTable(tmpAnimalSchema,
|
|
67
|
+
(pError) =>
|
|
68
|
+
{
|
|
69
|
+
if (pError) { console.error(pError); }
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### [createTables(pMeadowSchema, fCallback)](createTables.md)
|
|
74
|
+
|
|
75
|
+
Creates multiple tables sequentially from a Stricture schema object.
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
_Fable.MeadowPostgreSQLProvider.createTables(tmpSchema,
|
|
79
|
+
(pError) =>
|
|
80
|
+
{
|
|
81
|
+
if (pError) { console.error(pError); }
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### [generateDropTableStatement(pTableName)](generateDropTableStatement.md)
|
|
86
|
+
|
|
87
|
+
Generates a `DROP TABLE IF EXISTS` DDL string for the named table.
|
|
88
|
+
|
|
89
|
+
```javascript
|
|
90
|
+
let tmpDrop = _Fable.MeadowPostgreSQLProvider.generateDropTableStatement('Animal');
|
|
91
|
+
// => 'DROP TABLE IF EXISTS "Animal";'
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Properties
|
|
95
|
+
|
|
96
|
+
| Property | Type | Description |
|
|
97
|
+
|----------|------|-------------|
|
|
98
|
+
| `connected` | `boolean` | `true` after successful connection |
|
|
99
|
+
| `serviceType` | `string` | Always `'MeadowConnectionPostgreSQL'` |
|
|
100
|
+
| `options.PostgreSQL` | `object` | Resolved connection settings |
|
|
101
|
+
|
|
102
|
+
## Method Summary
|
|
103
|
+
|
|
104
|
+
| Method | Returns | Description |
|
|
105
|
+
|--------|---------|-------------|
|
|
106
|
+
| `connect()` | `void` | Synchronous connection |
|
|
107
|
+
| `connectAsync(fCallback)` | `void` | Callback-style connection |
|
|
108
|
+
| `pool` | `pg.Pool` / `false` | Connection pool instance |
|
|
109
|
+
| `generateCreateTableStatement(schema)` | `string` | SQL DDL for CREATE TABLE |
|
|
110
|
+
| `createTable(schema, fCallback)` | `void` | Execute CREATE TABLE |
|
|
111
|
+
| `createTables(schema, fCallback)` | `void` | Create multiple tables |
|
|
112
|
+
| `generateDropTableStatement(name)` | `string` | SQL DDL for DROP TABLE |
|
package/docs/api.md
ADDED