# Class: Db

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

# Hierarchy

  • Db

# Properties

# id

id: number = Math.random()

Defined in pg-structure/db.ts:75

Random assigned id to db.


# name

name: string

Defined in pg-structure/db.ts:78

Name of database.


# schemas

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

Defined in pg-structure/db.ts:104

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

# Accessors

# entities

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

Defined in pg-structure/db.ts:143

All entities of the database. 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›


# indexes

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

Defined in pg-structure/db.ts:163

All indexes of the database. 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 pg-structure/db.ts:198

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

Returns: CollisionsByTable | undefined


# tables

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

Defined in pg-structure/db.ts:133

All tables of the database. 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", true›

Defined in pg-structure/db.ts:153

All types of the database. 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 }).

Returns: IndexableArray‹Type, "name", "oid" | "classOid", true›

# Methods

# get

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

Defined in pg-structure/db.ts:183

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.

# 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 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 relationNameFunction 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