Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion FOOTER.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ Common issues and solutions for pgpm, PostgreSQL, and testing.
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
* [pg-ast](https://www.npmjs.com/package/pg-ast): **🔍 Low-level AST tools** and transformations for Postgres query structures.

### 🚀 API & Dev Tools

Expand Down
80 changes: 80 additions & 0 deletions packages/csv-to-pg/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [2.0.10](https://github.com/launchql/launchql/compare/[email protected]@2.0.10) (2025-05-30)

**Note:** Version bump only for package csv-to-pg





## [2.0.9](https://github.com/launchql/launchql/compare/[email protected]@2.0.9) (2025-05-20)

**Note:** Version bump only for package csv-to-pg





## [2.0.8](https://github.com/launchql/launchql/compare/[email protected]@2.0.8) (2025-05-18)

**Note:** Version bump only for package csv-to-pg





## [2.0.7](https://github.com/launchql/launchql/compare/[email protected]@2.0.7) (2025-05-18)

**Note:** Version bump only for package csv-to-pg





## [2.0.6](https://github.com/launchql/launchql/compare/[email protected]@2.0.6) (2025-05-18)

**Note:** Version bump only for package csv-to-pg





## [2.0.5](https://github.com/launchql/launchql/compare/[email protected]@2.0.5) (2025-05-18)

**Note:** Version bump only for package csv-to-pg





## [2.0.4](https://github.com/launchql/launchql/compare/[email protected]@2.0.4) (2025-05-16)

**Note:** Version bump only for package csv-to-pg





## [2.0.3](https://github.com/launchql/launchql/compare/[email protected]@2.0.3) (2025-05-16)

**Note:** Version bump only for package csv-to-pg





## [2.0.2](https://github.com/launchql/launchql/compare/[email protected]@2.0.2) (2025-05-16)

**Note:** Version bump only for package csv-to-pg





## 2.0.1 (2025-05-16)

**Note:** Version bump only for package csv-to-pg
106 changes: 106 additions & 0 deletions packages/csv-to-pg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# csv-to-pg

<p align="center" width="100%">
<img height="250" src="https://raw.githubusercontent.com/launchql/launchql/refs/heads/main/assets/outline-logo.svg" />
</p>

<p align="center" width="100%">
<a href="https://github.com/launchql/launchql/actions/workflows/run-tests.yaml">
<img height="20" src="https://github.com/launchql/launchql/actions/workflows/run-tests.yaml/badge.svg" />
</a>
<a href="https://github.com/launchql/launchql/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
<a href="https://www.npmjs.com/package/csv-to-pg"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/launchql?filename=packages%2Fcsv-to-pg%2Fpackage.json"/></a>
</p>

Create PostgreSQL statements from CSV files

## Installation

```sh
npm install -g csv-to-pg
```

## Usage

The idea for this library was to solve complex CSV scenarios, so currently each CSV requires a config file.

### config

Here is an example of a config.yaml that we can use to parse a csv that has no headers, and uses tab-delimited elements:

```yaml
input: "./myfile.tsv"
output: "./myfile.sql"
schema: myschema
table: mytable
delimeter: "\t"
headers: # order of the headers
- feature
- category
- days
- start
- end
fields:
feature: text
category: text
days: int
start: date
end:
type: date
cast: date # explicitly cast to a date type
```

You can also use JS files or JSON. If you use JS, you can also create ASTs for super custom inserts (read tests).

### run it!

Once you have a config file, simply call it and point to the config:

```sh
csv2pg ./config.yaml
```

## Related LaunchQL Tooling

### 🧪 Testing

* [launchql/pgsql-test](https://github.com/launchql/launchql/tree/main/packages/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
* [launchql/graphile-test](https://github.com/launchql/launchql/tree/main/packages/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
* [launchql/pg-query-context](https://github.com/launchql/launchql/tree/main/packages/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.

### 🧠 Parsing & AST

* [launchql/pgsql-parser](https://github.com/launchql/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
* [launchql/libpg-query-node](https://github.com/launchql/libpg-query-node): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
* [launchql/pg-proto-parser](https://github.com/launchql/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
* [@pgsql/enums](https://github.com/launchql/pgsql-parser/tree/main/packages/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
* [@pgsql/types](https://github.com/launchql/pgsql-parser/tree/main/packages/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
* [@pgsql/utils](https://github.com/launchql/pgsql-parser/tree/main/packages/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.

### 🚀 API & Dev Tools

* [launchql/server](https://github.com/launchql/launchql/tree/main/packages/server): **⚡ Express-based API server** powered by PostGraphile to expose a secure, scalable GraphQL API over your Postgres database.
* [launchql/explorer](https://github.com/launchql/launchql/tree/main/packages/explorer): **🔎 Visual API explorer** with GraphiQL for browsing across all databases and schemas—useful for debugging, documentation, and API prototyping.

### 🔁 Streaming & Uploads

* [launchql/s3-streamer](https://github.com/launchql/launchql/tree/main/packages/s3-streamer): **📤 Direct S3 streaming** for large files with support for metadata injection and content validation.
* [launchql/etag-hash](https://github.com/launchql/launchql/tree/main/packages/etag-hash): **🏷️ S3-compatible ETags** created by streaming and hashing file uploads in chunks.
* [launchql/etag-stream](https://github.com/launchql/launchql/tree/main/packages/etag-stream): **🔄 ETag computation** via Node stream transformer during upload or transfer.
* [launchql/uuid-hash](https://github.com/launchql/launchql/tree/main/packages/uuid-hash): **🆔 Deterministic UUIDs** generated from hashed content, great for deduplication and asset referencing.
* [launchql/uuid-stream](https://github.com/launchql/launchql/tree/main/packages/uuid-stream): **🌊 Streaming UUID generation** based on piped file content—ideal for upload pipelines.
* [launchql/upload-names](https://github.com/launchql/launchql/tree/main/packages/upload-names): **📂 Collision-resistant filenames** utility for structured and unique file names for uploads.

### 🧰 CLI & Codegen

* [@launchql/cli](https://github.com/launchql/launchql/tree/main/packages/cli): **🖥️ Command-line toolkit** for managing LaunchQL projects—supports database scaffolding, migrations, seeding, code generation, and automation.
* [launchql/launchql-gen](https://github.com/launchql/launchql/tree/main/packages/launchql-gen): **✨ Auto-generated GraphQL** mutations and queries dynamically built from introspected schema data.
* [@launchql/query-builder](https://github.com/launchql/launchql/tree/main/packages/query-builder): **🏗️ SQL constructor** providing a robust TypeScript-based query builder for dynamic generation of `SELECT`, `INSERT`, `UPDATE`, `DELETE`, and stored procedure calls—supports advanced SQL features like `JOIN`, `GROUP BY`, and schema-qualified queries.
* [@launchql/query](https://github.com/launchql/launchql/tree/main/packages/query): **🧩 Fluent GraphQL builder** for PostGraphile schemas. ⚡ Schema-aware via introspection, 🧩 composable and ergonomic for building deeply nested queries.

## Disclaimer

AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.

No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.

16 changes: 16 additions & 0 deletions packages/csv-to-pg/__fixtures__/config1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
schema: 'my-schema',
table: 'my-table',
headers: ['zip', 'lat', 'lon', 'box'],
fields: {
zip: 'int',
bbox: {
type: 'bbox',
from: 'box'
},
location: {
type: 'location',
from: ['lon', 'lat']
}
}
};
24 changes: 24 additions & 0 deletions packages/csv-to-pg/__fixtures__/config1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"schema": "my-schema",
"table": "my-table",
"headers": [
"zip",
"lat",
"lon",
"box"
],
"fields": {
"zip": "int",
"bbox": {
"type": "bbox",
"from": "box"
},
"location": {
"type": "location",
"from": [
"lon",
"lat"
]
}
}
}
17 changes: 17 additions & 0 deletions packages/csv-to-pg/__fixtures__/config1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
schema: my-schema
table: my-table
headers:
- zip
- lat
- lon
- box
fields:
zip: int
bbox:
type: bbox
from: box
location:
type: location
from:
- lon
- lat
3 changes: 3 additions & 0 deletions packages/csv-to-pg/__fixtures__/delimeter.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
zip@latitude@longitude@bbox
[email protected]@[email protected],34.024999,-118.495177,34.13165
[email protected]@[email protected],33.986062,-118.242772,34.030991
3 changes: 3 additions & 0 deletions packages/csv-to-pg/__fixtures__/headers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
zip,latitude,longitude,bbox
90272,34.078725,-118.542228,"-118.587533,34.024999,-118.495177,34.13165"
90011,34.00707,-118.25867,"-118.274021,33.986062,-118.242772,34.030991"
3 changes: 3 additions & 0 deletions packages/csv-to-pg/__fixtures__/parse.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pyramation,246x246.png (https://images.com/246x246.png)
brazil,246x246.png (https://images.com/246x246.png)
skate,246x246.png (https://images.com/246x246.png)
2 changes: 2 additions & 0 deletions packages/csv-to-pg/__fixtures__/test-case.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
391f5dfe-a251-43af-bee5-821e69214d20,95482a0f-ed8e-447d-ab05-318650d2f328,41c74ae0-b474-4aaf-b561-faadbadfc154,id,,,f,,f,uuid,1,,,,,,2020-12-06 01:22:56.703152+00,2020-12-06 01:22:56.703152+00
c32175b2-fe04-496a-8725-4ee35d6b2a3d,95482a0f-ed8e-447d-ab05-318650d2f328,41c74ae0-b474-4aaf-b561-faadbadfc154,owner_id,,,f,,f,uuid,2,,,,,,2020-12-06 01:22:56.703152+00,2020-12-06 01:22:56.703152+00
3 changes: 3 additions & 0 deletions packages/csv-to-pg/__fixtures__/zip.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
90272,34.078725,-118.542228,"-118.587533,34.024999,-118.495177,34.13165"
90011,34.00707,-118.25867,"-118.274021,33.986062,-118.242772,34.030991"
90014,,,"-118.274021,33.986062,-118.242772,34.030991"
41 changes: 41 additions & 0 deletions packages/csv-to-pg/__tests__/__snapshots__/export.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`test case arrays 1`] = `
"INSERT INTO collections_public.field (
id,
schemas
) VALUES
('450e3b3b-b68d-4abc-990c-65cb8a1dcdb4', '{a,b}');"
`;

exports[`test case image/attachment 1`] = `
"INSERT INTO collections_public.field (
id,
name,
image,
upload
) VALUES
('450e3b3b-b68d-4abc-990c-65cb8a1dcdb4', 'name here', '{"url":"http://path/to/image.jpg"}', '{"url":"http://path/to/image.jpg"}');"
`;

exports[`test case jsonb/json 1`] = `
"INSERT INTO collections_public.field (
id,
name,
data
) VALUES
('450e3b3b-b68d-4abc-990c-65cb8a1dcdb4', 'name here', '{"a":1}');"
`;

exports[`test case test case 1`] = `Promise {}`;

exports[`test case test case parser 1`] = `
"INSERT INTO collections_public.field (
id,
database_id,
table_id,
name,
description
) VALUES
('450e3b3b-b68d-4abc-990c-65cb8a1dcdb4', '450e3b3b-b68d-4abc-990c-65cb8a1dcdb4', '450e3b3b-b68d-4abc-990c-65cb8a1dcdb4', 'name here', 'description');"
`;
Loading