# Class: Db

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

# Hierarchy

  • Db

# Properties

# Readonly id

id: number = Math.random()

Defined in src/pg-structure/db.ts:81

Random assigned id to db.


# Readonly name

name: string

Defined in src/pg-structure/db.ts:84

Name of database.


# Readonly schemas

schemas: IndexableArray‹Schema, "name", "oid", true› = IndexableArray.throwingFrom([], "name", "oid")

Defined in src/pg-structure/db.ts:113

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);

# Readonly serverVersion

serverVersion: string

Defined in src/pg-structure/db.ts:87

Version of the PostgreSQL Engine

# Accessors

# allTypes

get allTypes(): IndexableArray‹Type, "name", "oid" | "classOid" | "arrayOid" | "internalName", true›

Defined in src/pg-structure/db.ts:226

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: IndexableArray‹Type, "name", "oid" | "classOid" | "arrayOid" | "internalName", true›


# entities

get entities(): IndexableArray‹Entity, "name", "oid", true›

Defined in src/pg-structure/db.ts:157

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: IndexableArray‹Entity, "name", "oid", true›


# functions

get functions(): IndexableArray‹Func, "name", "oid", true›

Defined in src/pg-structure/db.ts:251

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: IndexableArray‹Func, "name", "oid", true›


# indexes

get indexes(): IndexableArray‹Index, "name", "oid", true›

Defined in src/pg-structure/db.ts:237

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: IndexableArray‹Index, "name", "oid", true›


# relationNameCollisions

get relationNameCollisions(): CollisionsByTable | undefined

Defined in src/pg-structure/db.ts:286

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

Returns: CollisionsByTable | undefined


# systemTypes

get systemTypes(): IndexableArray‹Type, "name", "oid" | "classOid" | "arrayOid" | "internalName", true›

Defined in src/pg-structure/db.ts:206

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: IndexableArray‹Type, "name", "oid" | "classOid" | "arrayOid" | "internalName", true›


# tables

get tables(): IndexableArray‹Table, "name", "oid", true›

Defined in src/pg-structure/db.ts:146

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: IndexableArray‹Table, "name", "oid", true›


# types

get types(): IndexableArray‹Type, "name", "oid" | "classOid" | "arrayOid" | "internalName", true›

Defined in src/pg-structure/db.ts:173

All user defined types of the database excluding entities such as table, {@link Views view} and materialized view types. Entities are also composite types in PostgreSQL. To get all types including entites 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: IndexableArray‹Type, "name", "oid" | "classOid" | "arrayOid" | "internalName", true›


# typesIncludingEntities

get typesIncludingEntities(): IndexableArray‹Type, "name", "oid" | "classOid" | "arrayOid" | "internalName", true›

Defined in src/pg-structure/db.ts:187

All user defined types of the database including entities such as table, {@link Views view} and materialized view types. Entities are also composite types in PostgreSQL. To get all types excluding entites 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: IndexableArray‹Type, "name", "oid" | "classOid" | "arrayOid" | "internalName", true›

# Methods

# get

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

Defined in src/pg-structure/db.ts:271

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: Schema | Entity | Column

requested database object.


# serialize

serialize(): string

Defined in src/pg-structure/db.ts:67

Serializes object.

CAVEATS:

  • Serialized data may or may not be deserialized with another version of pg-structure. (Even between minor verisons 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 builtin function.

# 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