Skip to content

An iteration for pg-models v1.0.7. Its a complete rewrite, introduces typescript, unit tests and the Query Class, so that in future it can be used with other SQL databases. Added more hooks and utilities.

Notifications You must be signed in to change notification settings

teckmk/pg-models-v2

Repository files navigation

Classes

Column
Model
Options
Query
Table

Column

Kind: global class

new Column(colName, sql, validations, formatter)

Creates instance of Column

Param Description
colName

name of the column

sql

sql statement to create column

validations

Array of validation functions, which throw error on validation failure

formatter

Function which formats the column value, returns formatted value

column.name

Gets column name

Kind: instance property of Column

column.name

Sets column name

Kind: instance property of Column

column.nameInTable

Gets the name of the column which is used to identify it in the sql

Kind: instance property of Column

column.dataType

Gets data type of the column

Kind: instance property of Column

column.sql

Get sql statement which is used for column creation

Kind: instance property of Column

column.format(val) ⇒

Runs column's formatter function for given value

Kind: instance method of Column
Returns:

formatted value

Param Description
val

Any value for column input

column.runValidations(val, allInputs)

Runs column's validator functions for given value

Kind: instance method of Column

Param Description
val

Any input value

allInputs

Optional - inputs of other columns

Model

Kind: global class

new Model(tableName, config)

Creates Model instance

Param Description
tableName

name of the database table

config

model configurations

model.tableName

Returns table name (with prefix)

Kind: instance property of Model

model.table

Returns Table instance

Kind: instance property of Model

model.beforeCreate(hook)

Creates a hook on model which runs before every insert query

Kind: instance method of Model

Param Description
hook

A function, which runs before every insertion in DB

model.afterCreate(hook)

Creates a hook on model which runs after every insert query

Kind: instance method of Model

Param Description
hook

A function, which runs after every insertion in DB

model.beforeUpdate(hook)

Creates a hook on model which runs before every update query

Kind: instance method of Model

Param Description
hook

A function, which runs before every update action in DB

model.afterUpdate(hook)

Creates a hook on model which runs after every update query

Kind: instance method of Model

Param Description
hook

A function, which runs after every update action in DB

model.beforeDestroy(hook)

Creates a hook on model which runs before every delete query

Kind: instance method of Model

Param Description
hook

A function, which runs before every delete action in DB

model.afterDestroy(hook)

Creates a hook on model which runs after every delete query

Kind: instance method of Model

Param Description
hook

A function, which runs after every delete action in DB

model.useHook(type, hook)

Adds specified hook to the Model, you can add as many hooks as you want

Kind: instance method of Model

Param Description
type

Name of the hook, i.e 'beforeCreate,afterCreate,beforeUpdate,afterUpdate,beforeDestroy,afterDestroy

hook

A function, to add as hook

model.addForeignKey(foreignKey)

Creates foreign key on the Model

Kind: instance method of Model

Param Description
foreignKey

An object containing foregin key config options

model.define(columns)

Creates new tables with specified columns, or alters table if exists already (and alter:true is set in model options)

Kind: instance method of Model

Param Description
columns

An object cotaining columns schema and other options

model.runWithHooks(operation, allInputs, fn) ⇒

Takes user inputs, operation name and runs given function with all the hooks matching operation

Kind: instance method of Model
Returns:

any value returned by 'fn'

Param Description
operation

Name of the database operation i.e 'Create, Update or Destroy'

allInputs

User inputs

fn

Function which does some database operation

model.findAll(options) ⇒

Finds all records in the table, or records matching given conditions

Kind: instance method of Model
Returns:

Found rows or empty array

Param Description
options

Search options

model.findAllWhere(whereClause, params) ⇒

Finds all records matching given conditions

Kind: instance method of Model
Returns:

Found rows or empty array

Param Description
whereClause

String containing where clause

params

Values to replace in where clause

model.findOne(needle) ⇒

Finds first record in the table, or record matching given conditions

Kind: instance method of Model
Returns:

Found row or null

Param Description
needle

Object containing search conditions, column-value pairs

model.findByPk(pkey) ⇒

Finds first record matching primary key

Kind: instance method of Model
Returns:

Found row or null

Param Description
pkey

Primary key value

model.create(data) ⇒

Inserts new record in the table

Kind: instance method of Model
Returns:

Inserted row

Param Description
data

Object containing data to insert, column-value pairs

model.createMany(data) ⇒

Inserts multiple records in the table

Kind: instance method of Model
Returns:

Inserted rows

Param Description
data

Array of objects containing data to insert, column-value pairs

model.update(data, where) ⇒

Updates all records matching given conditions

Kind: instance method of Model
Returns:

Updated row

Param Type Description
data

Object containing data to update, column-value pairs

where whereOptions

Object containing where options

model.updateByPk(pkey, data) ⇒

Updates first record matching given conditions

Kind: instance method of Model
Returns:

Updated row

Param Description
pkey

Primary key value

data

Object containing data to update, column-value pairs

model.patch(data, where) ⇒

Partially updates all records matching given conditions

Kind: instance method of Model
Returns:

Updated row

Param Type Description
data

Object containing data to update, column-value pairs

where whereOptions

Object containing where options

model.patchByPk(pkey, data) ⇒

Partially updates first record matching primary key

Kind: instance method of Model
Returns:

Updated row

Param Description
pkey

Primary key value

data

Object containing data to update, column-value pairs

model.destroy(wher) ⇒

Deletes all records matching given conditions

Kind: instance method of Model
Returns:

Deleted rows

Param Type Description
wher whereOptions

e Object containing where options

model.destroyByPk(pkey) ⇒

Deletes first record matching primary key

Kind: instance method of Model
Returns:

Deleted row

Param Description
pkey

Primary key value

Model.useConnection(pgClient)

Connects Model class to database using pg, postgresql driver for nodejs

Kind: static method of Model

Param Description
pgClient

client instance returned by pg package

Options

Kind: global class

new Options(input, defaults)

Takes an input object and a default object, merges both objects and generates new config, that can be accessed using config property

Param Description
input

input object

defaults

default values for missing input values

options.config

Returns merged input and defaults objects

Kind: instance property of Options

Query

Kind: global class

new Query(tableName, columns)

Creates a new Query instance

Param Description
tableName

Name of the table

columns

Names of the columns for the table

query.query

Gets query string

Kind: instance property of Query

query.values

Gets query values

Kind: instance property of Query

query.client

Gets the client object

Kind: instance property of Query

query.prepare(query, values, columns, where) ⇒

Prepares the query for the given values, that can be get with query.query and query.values

Kind: instance method of Query
Returns:

reference to the instance

Param Type Description
query

Query string

values

Values to be replaced in the query

columns

Columns to be selected

where whereOptions

Where clause

query.returning(cols) ⇒

Appends returning clause to the query

Kind: instance method of Query
Returns:

reference to the instance

Param Description
cols

string (comma seperated) or an array of column names

query.count(column) ⇒

Creates a query to count values of given column in the table

Kind: instance method of Query
Returns:

reference to the instance

Param Description
column

Name of the column

query.sum(column, where) ⇒

Create a query to sum the values of given column

Kind: instance method of Query
Returns:

reference to the instance

Param Type Description
column

Name of the column

where whereOptions

An object of filters

query.orderBy(columns) ⇒

Appends order by clause for given columns to the query

Kind: instance method of Query
Returns:

reference to the instance

Param Description
columns

List of column names, a comma seperated string, or an array of strings

query.groupBy(columns) ⇒

Appends group by clause for given columns to the query

Kind: instance method of Query
Returns:

reference to the instance

Param Description
columns

List of column names, a comma seperated string, or an array of strings

query.insert(values) ⇒

Creates an insert query

Kind: instance method of Query
Returns:

reference to the instance

Param Description
values

Values to be inserted

query.select(options) ⇒

Creates a select query

Kind: instance method of Query
Returns:

reference to the instance

Param Description
options

Select options like limit, offset, orderBy, where

query.update(values, columns, where) ⇒

Creates a query to update a row

Kind: instance method of Query
Returns:

reference to the instance

Param Description
values

Values to be replaced in the query

columns

Columns to be selected

where

Where clause

query.delete(where) ⇒

Creates a query to delete a row

Kind: instance method of Query
Returns:

reference to the instance

Param Description
where

Where clause

query.tableExists() ⇒

Creates a query to check if the table exists

Kind: instance method of Query
Returns:

reference to the instance


query.columnExists(name, type) ⇒

Creates a query to check if the column exists

Kind: instance method of Query
Returns:

reference to the instance

Param Description
name

Name of the column

type

Data type of the column

query.constraintExists(name) ⇒

Creates a query to check if the constraint exists

Kind: instance method of Query
Returns:

reference to the instance

Param Description
name

Name of the constraint

query.createTable(colsSchema) ⇒

Creates a query to create a table

Kind: instance method of Query
Returns:

reference to the instance

Param Description
colsSchema

Schema of the columns

query.addColumns(colsSchema) ⇒

Creates a query to alter a table to add columns

Kind: instance method of Query
Returns:

reference to the instance

Param Description
colsSchema

Schema of the columns

query.dropTable() ⇒

Creates a query to drop a table

Kind: instance method of Query
Returns:

reference to the instance


query.dropColumn(name) ⇒

Creates a query to drop a column

Kind: instance method of Query
Returns:

reference to the instance

Param Description
name

Name of the column

query.foreignKey(options) ⇒

Creates a query to add a foreign key constraint

Kind: instance method of Query
Returns:

reference to the instance

Param Description
options

Options for the constraint

query.run() ⇒

Runs the query

Kind: instance method of Query
Returns:

reference to the instance


query.log() ⇒

Logs the query and its values

Kind: instance method of Query
Returns:

reference to the instance


Query.setClient(pgClient)

Connects to the database

Kind: static method of Query

Param Description
pgClient

Client object returned by pg.Client.connect()

Table

Kind: global class

new Table(tableName, columns, options)

Creates a new Table instance

Param Description
tableName

Name of the table to be created

columns

Columns to be added to the table

options

Configuration options for the table

table.options

Gets configurable object

Kind: instance property of Table

table.name

Gets the name of the table

Kind: instance property of Table

table.columns

Gets the columns of the table

Kind: instance property of Table

table.query

Gets the query instance

Kind: instance property of Table

table.name

Sets the name of the table

Kind: instance property of Table

table.run(query) ⇒

Runs a query and returns the result

Kind: instance method of Table
Returns:

result of the query

Param Description
query

Query to be run

table.create()

Creates the table in the database

Kind: instance method of Table

table.select(options) ⇒

Selects data from the table

Kind: instance method of Table
Returns:

selected rows

Param Description
options

Set options for the query

table.insert(data) ⇒

Inserts a row into the table

Kind: instance method of Table
Returns:

inserted row

Param Description
data

Data to be inserted

table.insertMany(data) ⇒

Inserts multiple rows into the table

Kind: instance method of Table
Returns:

inserted rows

Param Description
data

Data to be inserted

table.update(data, pkey) ⇒

Updates all rows in the table, or a single row if pkey is provided

Kind: instance method of Table
Returns:

updated row

Param Description
data

Data to be updated

pkey

primary key of the row to be updated

table.delete(pkey) ⇒

Deletes row(s) from the table

Kind: instance method of Table
Returns:

deleted rows

Param Type Description
pkey string | whereOptions

primary key of the row to be deleted or where options

table.exists() ⇒

Checks if the table exists in the database

Kind: instance method of Table
Returns:

true if the table exists, false otherwise


table.columnExists(name, type) ⇒

Checks if the column exists in the table

Kind: instance method of Table
Returns:

true if the column exists, false otherwise

Param Description
name

Name of the column

type

Data type of the column

table.contraintExists(name) ⇒

Check if the constraint exists in the table

Kind: instance method of Table
Returns:

true if constraint exists, false otherwise

Param Description
name

Name of the table

table.drop()

Drops the table from database

Kind: instance method of Table

table.alter() ⇒

Alters the table in the database

Kind: instance method of Table
Returns:

true if table is altered, false otherwise


table.addForeignKey(foreignKey)

Creates a foreign key

Kind: instance method of Table

Param Description
foreignKey

Object containing foreign key configuration

table.addTimestamps()

Alters table to add timestamps (created_at, updated_at) columns in it

Kind: instance method of Table

table.setParanoid()

Adds deleted_at column in the table

Kind: instance method of Table

table.setColumns(columns)

Sets new columns on table instance

Kind: instance method of Table

Param Description
columns

Object containing table columns configuration

table.getColumn(colName) ⇒

Gets column object

Kind: instance method of Table
Returns:

matched column

Param Description
colName

Column name

table.getColumnNames(includeTimestamps) ⇒

Get array of column names

Kind: instance method of Table
Returns:

array of column names

Param Default Description
includeTimestamps false

true for including timestamp columns names, false otherwise

table.getValidInputs(allInputs, nulls) ⇒

Returns a list of valid inputs

Kind: instance method of Table
Returns:

object of valid inputs, columnName-input pairs

Param Default Description
allInputs

User inputs object

nulls false

If true, will return nulls for missing inputs

table.getInputsArrangedAsColumns(allInputs) ⇒

Arranges keys in input object according to arrangment of columns object

Kind: instance method of Table
Returns:

object of arranged inputs

Param Description
allInputs

object containing user inputs

table.runValidations(allInputs)

Runs validation functions of all columns on corresponding input value

Kind: instance method of Table

Param Description
allInputs

object containing user inputs

Table.setClient(client)

Connects to the database

Kind: static method of Table

Param Description
client

Client to be used for the query (to connect to the database)

About

An iteration for pg-models v1.0.7. Its a complete rewrite, introduces typescript, unit tests and the Query Class, so that in future it can be used with other SQL databases. Added more hooks and utilities.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published