# hq-pg

> Lightweight modeling and express routes example for PostgreSQL data

Latest version **1.1.1** (published 2019-04-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install hq-pg
pnpm add hq-pg
yarn add hq-pg
bun add hq-pg
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2019-04-09 |
| First published | 2018-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 49 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Haseeb Qureshi |
| Maintainers | hq |
| Keywords | postgres, psql, pg, express, routes, model, modeling, api, knex, knexjs, bcrypt |

## Links

- npm: https://www.npmjs.com/package/hq-pg
- Repository: https://github.com/haseebnqureshi/hq-pg
- Homepage: https://github.com/haseebnqureshi/hq-pg#readme
- Issues: https://github.com/haseebnqureshi/hq-pg/issues
- npm.io page: https://npm.io/package/hq-pg

## Dependencies (5)

- [uuid](https://npm.io/package/uuid.md) ^3.3.2
- [moment](https://npm.io/package/moment.md) ^2.22.2
- [bcryptjs](https://npm.io/package/bcryptjs.md) ^2.4.3
- [request-ip](https://npm.io/package/request-ip.md) ^2.1.3
- [underscore](https://npm.io/package/underscore.md) ^1.9.1

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2019-04-09
- 1.1.0 — 2019-01-20
- 1.0.0 — 2018-11-26

## README

# hq-pg

### Installation
```
npm install hq-pg --save
```

### Getting Started
```
//initialize the module with your knex instance
var HQ = require('hq-pg')(knex)

//start defining your models
var Items = new HQ.Model('items')

var Locations = new HQ.Model('locations')

var Users = new HQ.UserModel('users')

var Devices = new HQ.DeviceModel('devices', {
	expiryCount: 1,
	expiryUnits: 'day'
})

var ItemsLocations = new HQ.Model('items_locations', {
	filterKnex(knex) {
		return knex
			.join('items', 'items_locations.item_id', '=', 'items.id')
			.join('locations', 'items_locations.location_id', '=', 'locations.id')
	}
})

//see them in action
Users.create({ email: 'me@domain.com', password: 'test' }, '*', console.log)
```


### require('hq-pg')(knex)
Returns three classes: ```Model```, ```UserModel```, and ```DeviceModel```:


### HQ.Model Methods
All of these methods can be extended or redefined through normal model inheritance.

```
create(data, returning, onDone(err, data, status, message) )

batchCreate(data, onDone(err, data) )
/ /where on conflict, the row attempted to be inserted does nothing

select(returning, onDone(err, data, status) )

findAll(where, returning, onDone(err, data, status) )

find(where, returning, onDone(err, data, status) )

findById(id, returning, onDone(err, data, status) )

findOrCreate(data, returning, onDone(err, data, status, message) )

updateAll(where, data, returning, onDone(err, data, status, message) )

update(where, data, returning, onDone(err, data, status, message) )

destroy(where, onDone(err, data, status) )
```

### HQ.UserModel Methods
All of these methods can be extended or redefined through normal model inheritance. (These all are in addition to those available in the Model class.)

```
generatePassword(password)

passwordMatches(password, hash)

safe(data)

validateLoginAndFind(email, password, onDone(err, data, status) )
```

### HQ.DeviceModel Methods
All of these methods can be extended or redefined through normal model inheritance. (These all are in addition to those available in the Model class.)

```
generateToken()

timeFormatted(m)

dataForCreate(req, user_id)

getTokenFromReq(req, header) 

findByActiveToken(token, returning, onDone(err, data, status) )

expireAllByUserId(user_id, returning, onDone(err, data, status, message) )
```

### Example
Comes with a fully fleshed out example, so you can see this in action and start implementing in your project.

#### Getting Started with the Example

1. Clone this repo and start there;
1. ```npm install knex -g```, if you don't have Knex CLI already installed;
1. ```npm install``` if you haven't already;
1. Create your ```.env``` file in the ```example``` folder of this repo. Make sure it contains the variables specified in ```example.env```
1. ```npm run example:migrate:latest``` to ensure our table schemas;
1. ```npm run example```

For cURL samples using the example application, look to the README in the ```example``` folder.

---
_Source: https://npm.io/package/hq-pg · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
