meadow-connection-postgresql
v1.0.2
Published
Meadow PostgreSQL Connection Plugin
Maintainers
Readme
meadow-connection-postgresql
PostgreSQL connection service for the Meadow data access layer.
Features
- Fable Service Provider -- integrates with the Fable dependency injection ecosystem
- Connection Pooling -- managed pool via the
pgdriver'sPoolclass - SQL DDL Generation -- produces PostgreSQL-native
CREATE TABLEandDROP TABLEstatements - Meadow-Compatible Settings -- accepts both Meadow-style (
Server,Port) and nativepgproperty names - Quoted Identifiers -- double-quoted table and column names for safe handling of reserved words
- Auto-Connect Mode -- optionally connect during service construction
- Idempotent Schema --
CREATE TABLE IF NOT EXISTSand graceful handling of duplicate table errors
Installation
npm install meadow-connection-postgresqlQuick Start
const libFable = require('fable');
const libMeadowConnectionPostgreSQL = require('meadow-connection-postgresql');
let _Fable = new libFable(
{
"PostgreSQL":
{
"Server": "localhost",
"Port": 5432,
"User": "postgres",
"Password": "secret",
"Database": "myapp"
}
});
_Fable.serviceManager.addAndInstantiateServiceType(
'MeadowPostgreSQLProvider', libMeadowConnectionPostgreSQL);
_Fable.MeadowPostgreSQLProvider.connectAsync(
(pError, pPool) =>
{
if (pError) { return console.error(pError); }
let tmpPool = _Fable.MeadowPostgreSQLProvider.pool;
tmpPool.query('SELECT NOW()', (pErr, pRes) =>
{
console.log('Server time:', pRes.rows[0].now);
});
});Configuration
Settings are read from fable.settings.PostgreSQL:
| Setting | Alias | Default | Description |
|---------|-------|---------|-------------|
| Server | host | -- | PostgreSQL host |
| Port | port | -- | PostgreSQL port |
| User | user | -- | Authentication username |
| Password | password | -- | Authentication password |
| Database | database | -- | Target database name |
| ConnectionPoolLimit | max | -- | Maximum connections in the pool |
API
| Method | Description |
|--------|-------------|
| connect() | Synchronous -- create pg.Pool instance |
| connectAsync(fCallback) | Callback-style connection (recommended) |
| pool | Getter -- returns the pg.Pool instance |
| generateCreateTableStatement(schema) | Generate a CREATE TABLE DDL string |
| createTable(schema, fCallback) | Execute CREATE TABLE via the pool |
| createTables(schema, fCallback) | Create multiple tables sequentially |
| generateDropTableStatement(name) | Generate a DROP TABLE IF EXISTS DDL string |
Column Type Mapping
| Meadow DataType | PostgreSQL Type | Constraints |
|-----------------|-----------------|-------------|
| ID | SERIAL | PRIMARY KEY |
| GUID | VARCHAR(Size) | DEFAULT '0xDe' |
| ForeignKey | INTEGER | NOT NULL DEFAULT 0 |
| Numeric | INTEGER | NOT NULL DEFAULT 0 |
| Decimal | DECIMAL(Size) | -- |
| String | VARCHAR(Size) | NOT NULL DEFAULT '' |
| Text | TEXT | -- |
| DateTime | TIMESTAMP | -- |
| Boolean | BOOLEAN | NOT NULL DEFAULT false |
Part of the Retold Framework
This module is a Meadow connector that plugs into the Retold application framework. It provides the PostgreSQL persistence layer for the Meadow data access abstraction.
Testing
npm testCoverage:
npm run coverageRelated Packages
- meadow -- Data access layer and ORM
- fable -- Application framework and service manager
- foxhound -- Query generation DSL
- stricture -- Schema definition and DDL tools
- meadow-endpoints -- RESTful endpoint generation
- meadow-connection-mysql -- MySQL / MariaDB connector
- meadow-connection-mssql -- Microsoft SQL Server connector
- meadow-connection-sqlite -- SQLite connector
- meadow-connection-mongodb -- MongoDB connector
License
MIT
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
