pg-structure / Db

# Class: Db

Class which represent a database. Provides attributes and methods for details of the database.

# Properties

# id

Readonly id: number

Random assigned id to db.

Defined in: pg-structure/db.ts:91


# name

Readonly name: string

Name of database.

Defined in: pg-structure/db.ts:94


# schemas

Readonly schemas: default<Schema, name, oid, true>

All schemas in the database as an {@link IndexableArray indexable array} ordered by their name.

# Example

const schemaArray  = db.schemas;
const isAvailable  = db.schemas.has('another_schema');
const public       = db.schemas.get('public');
const name         = public.name;
const names = db.schemas.map(schema => schema.name);

Defined in: pg-structure/db.ts:123


# serverVersion

Readonly serverVersion: string

Version of the PostgreSQL Engine

Defined in: pg-structure/db.ts:97

# Accessors

# allTypes

• get allTypes(): default<Type, name, oid | classOid | arrayOid | internalName, true>

All types of the database including system types and entities. Returned array have all objects, you may loop over them. However two PostgreSQL schemas may have same named type. get method of {@link IndexableArray https://www.npmjs.com/package/indexable-array} returns first one. You may also use getAll or get(1234, { key: oid }).

see types, typesIncludingEntities, systemTypes

Returns: default<Type, name, oid | classOid | arrayOid | internalName, true>

Defined in: pg-structure/db.ts:236


# entities

• get entities(): default<Entity, name, oid, true>

All entities of the database. Returned array have all objects, you may loop over them. However two PostgreSQL schemas may have same named entity. get method of {@link IndexableArray https://www.npmjs.com/package/indexable-array} returns first one. You may also use getAll or get(1234, { key: oid }).

Returns: default<Entity, name, oid, true>

Defined in: pg-structure/db.ts:167


# functions

• get functions(): default<Func, name, oid, true>

All entities of the database. Returned array have all objects, you may loop over them. However two PostgreSQL schemas may have same named entity. get method of {@link IndexableArray https://www.npmjs.com/package/indexable-array} returns first one. You may also use getAll or get(1234, { key: oid }).

Returns: default<Func, name, oid, true>

Defined in: pg-structure/db.ts:261


# indexes

• get indexes(): default<Index, name, oid, true>

All indexes of the database. Returned array have all objects, you may loop over them. However two PostgreSQL schemas may have same named index. get method of {@link IndexableArray https://www.npmjs.com/package/indexable-array} returns first one. You may also use getAll or get(1234, { key: oid }).

Returns: default<Index, name, oid, true>

Defined in: pg-structure/db.ts:247


# relationNameCollisions

• get relationNameCollisions(): undefined | CollisionsByTable

Name collisions of table relations if there are any, otherwise undefined.

Returns: undefined | CollisionsByTable

Defined in: pg-structure/db.ts:296


# systemTypes

• get systemTypes(): default<Type, name, oid | classOid | arrayOid | internalName, true>

All system types of the database. Returned array have all objects, you may loop over them. However two PostgreSQL schemas may have same named type. get method of {@link IndexableArray https://www.npmjs.com/package/indexable-array} returns first one. You may also use getAll or get(1234, { key: oid }).

see types, typesIncludingEntities, allTypes

Returns: default<Type, name, oid | classOid | arrayOid | internalName, true>

Defined in: pg-structure/db.ts:216


# tables

• get tables(): default<Table, name, oid, true>

All tables of the database. Returned array have all objects, you may loop over them. However Returned array have all objects, you may loop over them. However two PostgreSQL schemas may have same named table. get method of {@link IndexableArray https://www.npmjs.com/package/indexable-array} returns first one. You may also use getAll or get(1234, { key: oid }).

Returns: default<Table, name, oid, true>

Defined in: pg-structure/db.ts:156


# types

• get types(): default<Type, name, oid | classOid | arrayOid | internalName, true>

All user defined types of the database excluding entities such as table, {@link Views view}, materialized view and sequence types. Entities are also composite types in PostgreSQL. To get all types including entities use typesIncludingEntities method. Returned array have all objects, you may loop over them. However two PostgreSQL schemas may have same named type. get method of {@link IndexableArray https://www.npmjs.com/package/indexable-array} returns first one. You may also use getAll or get(1234, { key: oid }).

see typesIncludingEntities, systemTypes, allTypes

Returns: default<Type, name, oid | classOid | arrayOid | internalName, true>

Defined in: pg-structure/db.ts:183


# typesIncludingEntities

• get typesIncludingEntities(): default<Type, name, oid | classOid | arrayOid | internalName, true>

All user defined types of the database including entities such as table, {@link Views view}, materialized view and sequence types. Entities are also composite types in PostgreSQL. To get all types excluding entities use types method. {@link IndexableArray https://www.npmjs.com/package/indexable-array} returns first one. You may also use getAll or get(1234, { key: oid }).

see types, systemTypes, allTypes

Returns: default<Type, name, oid | classOid | arrayOid | internalName, true>

Defined in: pg-structure/db.ts:197

# Methods

# get

get(path: string): Column | Entity | Schema

Returns schema, table or column for given path. Path should be in dot (.) notation. If no schema is provided looks into public schema as PostgreSQL does.

Note for TypeScript users: Since get() could return one of the many possible types, you may need to specify your expected type using as. i.e. const result = db.get("public") as Schema;

# Example

const schema   = db.get('public');               // Returns public schema.
const table    = db.get('public.contact');       // Returns contact table in public schema.
const table2   = db.get('contact');              // Returns contact table in public schema.
const column   = db.get('public.contact.name');  // Returns name column of the contact table in public schema.
const column2  = db.get('contact.name');         // Returns name column of the contact table in public schema.

# Parameters:

Name Type Description
path string is the path of the requested item in dot (.) notation such as public.contact

Returns: Column | Entity | Schema

requested database object.

Defined in: pg-structure/db.ts:281


# serialize

serialize(): string

Serializes object.

CAVEATS:

  • Serialized data may or may not be deserialized with another version of pg-structure. (Even between minor versions are not guaranteed).
  • Serialized data is not direct stringified version of objects.
  • Ignores relation name function provided using relationNameFunctions args, if it is not a module name.

# Example

import pgStructure, { deserialize } from "pg-structure";
const db = await pgStructure({ database: "db", user: "u", password: "pass" });
const serialized = db.serialize();
const otherDb = deserialize(serialized);

Returns: string

Defined in: pg-structure/db.ts:77