{"id":23219643,"url":"https://github.com/barcek/progpgq","last_synced_at":"2026-05-09T10:13:58.357Z","repository":{"id":157419478,"uuid":"332255711","full_name":"barcek/progpgQ","owner":"barcek","description":"abstract away SQL queries | table \u0026 CRUD query creation classes | Node.js \u0026 PostgreSQL","archived":false,"fork":false,"pushed_at":"2021-07-22T20:51:51.000Z","size":128,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-11T11:53:02.154Z","etag":null,"topics":["backend","crud","dx","library","node-postgres","nodejs","object-oriented-programming","postgresql","sql","web-development"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/barcek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-23T16:29:29.000Z","updated_at":"2025-01-13T21:30:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"27a87d3d-c6b7-4afe-acb5-788f67ea672a","html_url":"https://github.com/barcek/progpgQ","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barcek%2FprogpgQ","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barcek%2FprogpgQ/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barcek%2FprogpgQ/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/barcek%2FprogpgQ/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/barcek","download_url":"https://codeload.github.com/barcek/progpgQ/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247353742,"owners_count":20925329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backend","crud","dx","library","node-postgres","nodejs","object-oriented-programming","postgresql","sql","web-development"],"created_at":"2024-12-18T21:36:16.921Z","updated_at":"2026-05-09T10:13:58.309Z","avatar_url":"https://github.com/barcek.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# progpgQ\n\nA fileset for programmatic table and CRUD query creation in Node.js using the `pg` package for PostgreSQL.\n\nAll or part can be incorporated into a larger project to abstract away the core query code. In addition, the 'config' folder applies a useful destructure-truncate-export approach to environment variables to reduce environment variable identifier length and uses `NODE_ENV` to determine the correct set of database environment variables.\n\nA large part of the code is currently used in the [docNxgres](https://github.com/barcek/docNxgres) containerized back end.\n\nFor the whole at a glance, see [the current repository tree](#repository-tree).\n\n- [Getting started](#getting-started)\n    - [Table creation](#table-creation)\n    - [CRUD generation](#crud-generation)\n    - [Running operations](#running-operations)\n    - [.summarize \u0026 .summarizeAll](#summarize--summarizeall)\n- [Content summary](#content-summary)\n    - [config/](#config)\n        - [index.js](#indexjs)\n    - [db/](#db)\n        - [index.js](#indexjs)\n        - [pool.js](#pooljs)\n        - [crud.js](#crudjs)\n        - [table.js](#tablejs)\n    - [utils/](#utils)\n        - [index.js](#indexjs)\n        - [format.js](#formatjs)\n    - [test/](#test)\n    - [.env](#env)\n- [Making changes](#making-changes)\n    - [Unit tests](#unit-tests)\n    - [npm audit](#npm-audit)\n- [Development plan](#development-plan)\n- [Repository tree](#repository-tree)\n\n## Getting started\n\n### Table creation\n\nA table can be created by creating an instance of the `Table` class, passing the following:\n\n- the name of the table as a string;\n- an array of strings, each containing the SQL for a single column.\n\nNote that the constructor function returns an asynchronous function, which resolves to the Table instance only after the response is received from the table creation query.\n\n```js\nconst entriesTable = await new Table('entries', [\n    'id SERIAL PRIMARY KEY',\n    'column_1 VARCHAR(255) NOT NULL',\n    'column_2 VARCHAR(255) NOT NULL',\n    'column_3 VARCHAR(255) NOT NULL'\n]);\n```\n\n### CRUD generation\n\nA set of CRUD operations for the table can then be instantiated using the `CRUD` class, passing an array of strings, each string the name of a column in the table.\n\n```js\nconst oddColumnsCRUD = entriesTable.generateCRUD(['column_1', 'column_3']);\n```\n\nThe CRUD operations `readById`, `deleteById` and `update` assume that an 'id' column is present and is the column to be used in the `WHERE` clause. If an alternative column is to be used, the relevant column name is passed as a string as the second argument.\n\n```js\nconst oddColumnsCRUD = entriesTable.generateCRUD(['column_1', 'column_3'], 'column_2');\n```\n\nEach CRUD instance has a `label` property, a string created by hyphenating the column names passed. The instance is assigned to the `cruds` property on the Table instance with its `label` property as the key.\n\n### Running operations\n\nA CRUD operation can be performed by calling the `run` method with the first argument the operation name as a string and the second argument the array of expected values.\n\n```js\nconst response = await oddColumnsCRUD.run('create', ['value_1', 'value_3']);\n```\n\n### .summarize \u0026 .summarizeAll\n\nEach CRUD instance also exposes a `summarize` method returning a string overview of a given operation, as well as a `summarizeAll` method returning all overviews for that operation set.\n\n```js\noddColumnsCRUD.summarize('create');\noddColumnsCRUD.summarizeAll();\n```\n\n## Content summary\n\n### config/\n\nThe 'config' directory contains a single 'index.js' file.\n\n#### index.js\n\nThe 'index.js' file requires the `dotenv` package, destructures environment variables from `process.env` - which draws also from the '.env' file in the root directory - and uses `NODE_ENV` to determine the correct set of database environment variables and export them with truncated identifiers under the `DB` key.\n\n### db/\n\nThe 'db' directory contains four files: 'index.js', 'pool.js', 'table.js' and 'crud.js'.\n\n#### index.js\n\nThe 'index.js' file destructures `Table` and `pool` from their modules and exports the `Table` class and an `endPool` function.\n\n#### pool.js\n\nThe 'pool.js' file requires the `pg` package and the `DB` environment variables to create a pool exports this as `pool`.\n\n#### crud.js\n\nThe 'crud.js' file destructures `pool` from its module, as `defaultPool` to support dependency injection, and exports the constructor function `CRUD`, which takes arguments to create a set of standard CRUD queries, specifically `create`, `readById`, `readAll`, `update`, `deleteById` and `deleteAll`.\n\n#### table.js\n\nThe 'table.js' file destructures `pool`  and `CRUD` from their modules, each with the 'default-' prefix to support dependency injection, and exports the constructor function `Table`, which takes arguments to create a table if the table does not exist.\n\n### utils/\n\nThe 'utils' directory contains two files: 'index.js' and 'format.js'.\n\n#### index.js\n\nThe 'index.js' file destructures the sole utility function from 'format.js' and exports it.\n\n#### format.js\n\nThe 'format.js' file defines and exports a single utility function, `commaSpaceJoin`, currently used in both 'table.js' and 'crud.js'.\n\n### test/\n\nThe 'test' directory contains two subdirectories: 'db/' and 'utils/'. The subdirectory 'db/' contains three files: 'db.test.js' with test resources and one unit test file each for the `Table` and `CRUD` modules. The subdirectory 'utils/' contains two files: 'utils.test.js' with test resources and 'format.test.js' for the utility function in 'utils/format.js'.\n\n### .env\n\nThe '.env' file contains two sets of database environment variables with placeholder values, one set each for development and production.\n\n## Making changes\n\nRunning the tests after making changes and adding tests to cover new behaviour is recommended, as is a regular audit of dependencies.\n\n### Unit tests\n\nThe unit tests use the npm packages `mocha` and `chai` as dev dependencies and can be run with the following command:\n\n```shell\nmocha\n```\n\nThis command along with the `--recursive` flag is the current content of the 'test' script in the 'package.json' file, which can be run with the following:\n\n```shell\nnpm test\n```\n\nThe command in the 'watch' script is set up to watch for and test on changes:\n\n```shell\nnpm run watch\n```\n\n### npm audit\n\nThe `npm audit` command can be used to run a security audit on the dependencies used, with the process returning information on updates where available. The command `npm audit fix` can be used instead or thereafter to install compatible updates. See the npm documentation for [more detail](https://docs.npmjs.com/auditing-package-dependencies-for-security-vulnerabilities).\n\n## Development plan\n\nThe following are possible next steps in the development of the code base. Pull requests are welcome for these and any other potential improvements.\n\n- allow for multiple `WHERE` clause conditions and the full range of operators\n- enable the use of `OFFSET` and `LIMIT` and the ordering of rows returned\n- add additional query types\n\n## Repository tree\n\n```\n./\n├── config\n│   └── index.js\n├── db\n│   ├── crud.js\n│   ├── index.js\n│   ├── pool.js\n│   └── table.js\n├── test\n│   ├── db\n│   │   ├── crud.test.js\n│   │   ├── db.test.js\n│   │   └── table.test.js\n│   └── utils\n│       ├── format.test.js\n│       └── utils.test.js\n├── utils\n│   ├── format.js\n│   └── index.js\n├── .env\n├── .gitignore\n├── LICENSE.txt\n├── README.md\n├── package-lock.json\n└── package.json\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbarcek%2Fprogpgq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbarcek%2Fprogpgq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbarcek%2Fprogpgq/lists"}