1.3.2 • Published 4 months ago
pg-fixtures-editor v1.3.2
pg-fixtures-editor
Library with some utilities to edit SQL fixtures for Postgres databases.
It uses pgsql-ast-parser to parse and emit SQL code.
Rationale
Have you ever had to edit a SQL fixture after a DB schema migration? If you have, you know it's a real pain. This library aims to make this process easier.
Features
The API of this library exposes these functions:
removeColumn
: removes a column fromINSERT
statements.addColumn
: adds a column and its value toINSERT
statements. Note that the value will be emitted "as is", so you need to include proper quoting and escaping if needed. Example: to insert a SQL string, you need to pass'your string'
as columnValue (i.e. enclosed with single quotes).
Installation
npm install pg-fixtures-editor
Usage
import { removeColumn } from "pg-fixtures-editor";
const sql = `
INSERT INTO users (id, name, email) VALUES
(1, 'John', 'john@example.com'),
(2, 'Jane', 'jane@example.com')
`;
const newSql = removeColumn("email", sql);
Quirks
While this project aims for a very specific and limited feature set, it's in early development stage and its API might break.
Some quirks:
- At the moment, comments are not preserved.
- Spacing and formatting is not always preserved.
- The output SQL aims to be human readable when possible (e.g. multi-line
INSERT
statements).
Hopefully those quirks shouldn't matter too much when editing SQL fixtures.
Development
Build the project:
npm run build
Run tests:
npm test
Publish a new version:
npm run pub