Database
Kind: global class
-
- new Database(connection, [loader], [driverConfig])
- .attach(entities) ⇒
Array
- .detach(entityPath)
- .clean()
- .getIntrospectors() ⇒
Promise
- .reload() ⇒
Promise
- .serverAtLeast(target) ⇒
Boolean
- .listTables() ⇒
Array
- .listViews() ⇒
Array
- .listFunctions() ⇒
Array
- .listSequences() ⇒
Array
- .query(query, [params], [options]) ⇒
Promise
- .withConnection(callback, [options]) ⇒
Promise
- .withTransaction(callback, [options]) ⇒
Promise
- .clone(conn) ⇒
Database
- .createExtension(extensionName) ⇒
Promise
- .dropExtension(extensionName) ⇒
Promise
- .saveDoc(collection, doc) ⇒
Promise
- .saveDocs(collection, docs) ⇒
Promise
- .createDocumentTable(location) ⇒
Promise
- .dropTable(tablePath, options) ⇒
Promise
- .createSchema(schemaName) ⇒
Promise
- .dropSchema(schemaName, options) ⇒
Promise
new Database (connection, [loader], [driverConfig])
A database connection.
Param | Type | Description |
---|---|---|
connection | Object | String |
A pg connection object or a connection string. |
[loader] | Object |
Filter definition for including and excluding database objects. If nothing is specified, Massive loads every table, view, and function visible to the connection's user. |
[loader.scripts] | String |
Override the Massive script file location (default ./db). |
[loader.documentPkType] | String |
Override default data type (serial), set for DocumentTable primary key 'id', e.g. 'uuid' |
[loader.uuidVersion] | String |
If documentPkType is set to 'uuid', set which UUID version to use, e.g. 'uuid_generate_v1', 'uuid_generate_v4', etc. Default is 'uuid_generate_v4' |
[loader.allowedSchemas] | Array | String |
Table/view schema whitelist. |
[loader.whitelist] | Array | String |
Table/view name whitelist. |
[loader.blacklist] | Array | String |
Table/view name blacklist. |
[loader.exceptions] | Array | String |
Table/view blacklist exceptions. |
[loader.functionWhitelist] | Array | String |
Function name whitelist. |
[loader.functionBlacklist] | Array | String |
Function name blacklist. |
[loader.enhancedFunctions] | Boolean |
Streamline function return values. |
[loader.excludeFunctions] | Boolean |
Ignore functions entirely. |
[loader.excludeMatViews] | Boolean |
[DEPRECATED] Ignore materialized views. |
[driverConfig] | Object |
A |
database.attach (entities) ⇒ Array
Attach an entity to the connected instance.
Kind: instance method of Database
Returns: Array
- All added entities.
Param | Type | Description |
---|---|---|
entities | Object | Array |
New Entity or list of Entities to add to the instance. |
database.detach (entityPath)
Forget an entity.
Kind: instance method of Database
Param | Type | Description |
---|---|---|
entityPath | String |
Path to the entity. |
database.clean ()
Remove all attached entities from the instance, returning it to the pre- introspection state.
Kind: instance method of Database
database.getIntrospectors () ⇒ Promise
Get the SQL scripts used to introspect the database.
Kind: instance method of Database
Returns: Promise
- A promise resolving into a name:QueryFile map.
database.reload () ⇒ Promise
Synchronize the database API with the current state by scanning for tables, views, functions, and scripts. Objects and files which no longer exist are cleared and new objects and files added.
Kind: instance method of Database
Returns: Promise
- The refreshed database.
database.serverAtLeast (target) ⇒ Boolean
Determine whether the connected PostgreSQL server is of at least the specified version. Used for automatically enabling features in the loaders, notably procedures (in the function loader) and materialized views.
Kind: instance method of Database
Returns: Boolean
- Whether the target version has been matched or exceeded.
Param | Type | Description |
---|---|---|
target | String |
Target version to match or exceed. |
database.listTables () ⇒ Array
List all the tables attached to the connected instance.
Kind: instance method of Database
Returns: Array
- A list of table paths.
database.listViews () ⇒ Array
List all the views attached to the connected instance.
Kind: instance method of Database
Returns: Array
- A list of view paths.
database.listFunctions () ⇒ Array
List all the functions and scripts attached to the connected instance.
Kind: instance method of Database
Returns: Array
- A list of function paths.
database.listSequences () ⇒ Array
List all the
Kind: instance method of Database
Returns: Array
- A list of sequence names.
database.query (query, [params], [options]) ⇒ Promise
Execute a query.
Kind: instance method of Database
Returns: Promise
- Query results.
Param | Type | Description |
---|---|---|
query | Statement | String |
One of the four statement objects, or a string containing a prepared SQL statement. |
[params] | Array |
An array of the prepared statement parameters, if applicable. |
[options] | Object |
If using raw SQL, a subset of query options may be applied. |
options.document | Boolean |
This is a query against a document table. |
options.single | Boolean |
True to return a single result object instead of an array of results. |
options.stream | Boolean |
True to return a stream instead of a resultset. |
database.withConnection (callback, [options]) ⇒ Promise
Begin a task, returning a copy of the connected instance which will route all queries made in the callback through the task scope.
Kind: instance method of Database
Returns: Promise
- A promise for the completed task, which will be fulfilled
if it succeeds and commits or rejected if it rolls back.
Param | Type | Description |
---|---|---|
callback | function |
A callback containing Massive API calls and SQL queries to be made within the task scope. |
[options] | Object |
Task options. |
database.withTransaction (callback, [options]) ⇒ Promise
Begin a transaction, returning a copy of the connected instance which will route all queries made in the callback through the transaction scope.
Kind: instance method of Database
Returns: Promise
- A promise for the completed transaction, which will be
fulfilled if it succeeds and commits or rejected if it rolls back.
Param | Type | Description |
---|---|---|
callback | function |
A callback containing Massive API calls and SQL queries to be made within the transaction scope. |
[options] | Object |
Transaction options. |
database.clone (conn) ⇒ Database
Clones the database handle for a task or transaction, replacing the internal instance with a dedicated connection.
Kind: instance method of Database
Returns: Database
- A cloned database object.
Param | Type | Description |
---|---|---|
conn | Object |
A |
database.createExtension (extensionName) ⇒ Promise
Create an extension.
Kind: instance method of Database
Returns: Promise
- A promise which resolves when the extension has been created.
Param | Type | Description |
---|---|---|
extensionName | String |
A valid extension name. Example 'uuid-ossp' |
database.dropExtension (extensionName) ⇒ Promise
Drop an extension.
Kind: instance method of Database
Returns: Promise
- A promise which resolves when the extension has been droped.
Param | Type | Description |
---|---|---|
extensionName | String |
A valid extension name. Example 'uuid-ossp' |
database.saveDoc (collection, doc) ⇒ Promise
Save a document.
Kind: instance method of Database
Returns: Promise
- The saved document.
Param | Type | Description |
---|---|---|
collection | String |
Document table name to save to. If it does not already exist, it will be created. |
doc | Object |
A JSON document. |
database.saveDocs (collection, docs) ⇒ Promise
Save multiple documents.
Kind: instance method of Database
Returns: Promise
- The saved documents.
Param | Type | Description |
---|---|---|
collection | String |
Document table name to save to. If it does not already exist, it will be created. |
docs | Object |
JSON documents. |
database.createDocumentTable (location) ⇒ Promise
Create a new document table and attach it to the Database for usage.
Kind: instance method of Database
Returns: Promise
- The added table.
Param | Type | Description |
---|---|---|
location | String |
Name of the table to create. Unless the schema is specified in a qualified path, current schema (usually 'public') is assumed. |
database.dropTable (tablePath, options) ⇒ Promise
Drop a table and remove it from the Database.
Kind: instance method of Database
Returns: Promise
- A promise which resolves when the table has been removed.
Param | Type | Description |
---|---|---|
tablePath | String |
Path to the table (including schema, if not public). |
options | Object |
Additional options. |
options.cascade | Boolean |
True to drop any objects that depend on the table. |
database.createSchema (schemaName) ⇒ Promise
Create a new schema in the database.
Kind: instance method of Database
Returns: Promise
- A promise which resolves when the schema has been added.
Param | Type | Description |
---|---|---|
schemaName | String |
A valid schema name. |
database.dropSchema (schemaName, options) ⇒ Promise
Drop a schema and remove it and its owned objects from the Database.
Kind: instance method of Database
Returns: Promise
- A promise which resolves when the schema and all owned
objects have been removed.
Param | Type | Description |
---|---|---|
schemaName | String |
A valid schema name to remove. |
options | Object |
Additional options. |
options.cascade | Boolean |
True to drop any objects that depend on the schema. |