{"id":21150417,"url":"https://github.com/frectonz/pglite-fusion","last_synced_at":"2025-04-14T23:28:38.587Z","repository":{"id":263252552,"uuid":"889637874","full_name":"frectonz/pglite-fusion","owner":"frectonz","description":"Embed an SQLite database in your PostgreSQL table. AKA multitenancy has been solved.","archived":false,"fork":false,"pushed_at":"2025-04-05T21:46:49.000Z","size":156,"stargazers_count":775,"open_issues_count":1,"forks_count":8,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-07T16:13:22.659Z","etag":null,"topics":["multitenancy","postgres-extension","postgresql","sqlite","sqlite-in-postgres"],"latest_commit_sha":null,"homepage":"","language":"Nix","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/frectonz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2024-11-16T20:43:45.000Z","updated_at":"2025-04-05T21:46:52.000Z","dependencies_parsed_at":"2024-11-17T10:31:24.475Z","dependency_job_id":"96f1243d-5899-4449-84ed-b78cf3c70476","html_url":"https://github.com/frectonz/pglite-fusion","commit_stats":null,"previous_names":["frectonz/pglite-fusion"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frectonz%2Fpglite-fusion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frectonz%2Fpglite-fusion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frectonz%2Fpglite-fusion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frectonz%2Fpglite-fusion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frectonz","download_url":"https://codeload.github.com/frectonz/pglite-fusion/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248977253,"owners_count":21192552,"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":["multitenancy","postgres-extension","postgresql","sqlite","sqlite-in-postgres"],"created_at":"2024-11-20T10:01:00.438Z","updated_at":"2025-04-14T23:28:38.580Z","avatar_url":"https://github.com/frectonz.png","language":"Nix","funding_links":[],"categories":["Nix","Tools"],"sub_categories":[],"readme":"# pglite-fusion\n\nEmbed an SQLite database in your PostgreSQL table. AKA multitenancy has been solved.\n\n## Usage\n\nRun a PostgreSQL 17 database that has `pglite-fusion` already installed.\n\n```bash\ndocker run --network=host frectonz/pglite-fusion\n```\n\n`pglite-fusion` is also distributed with other PostgreSQL versions.\n\n### PostgreSQL 12\n\n```bash\ndocker run --network=host frectonz/pglite-fusion:pg12\n```\n\n### PostgreSQL 13\n\n```bash\ndocker run --network=host frectonz/pglite-fusion:pg13\n```\n\n### PostgreSQL 14\n\n```bash\ndocker run --network=host frectonz/pglite-fusion:pg14\n```\n\n### PostgreSQL 15\n\n```bash\ndocker run --network=host frectonz/pglite-fusion:pg15\n```\n\n### PostgreSQL 16\n\n```bash\ndocker run --network=host frectonz/pglite-fusion:pg16\n```\n\n### PostgreSQL 17\n\n```bash\ndocker run --network=host frectonz/pglite-fusion:pg17\n```\n\nConnect to the PostgreSQL database using `psql`.\n\n```bash\npsql postgresql://postgres@localhost:5432/\n```\n\nHere's some demo usage.\n\n```sql\n-- Load PG extension\nCREATE EXTENSION pglite_fusion;\n\n-- Create a table with an SQLite column\nCREATE TABLE people (\n    name     TEXT NOT NULL,\n    database SQLITE DEFAULT init_sqlite('CREATE TABLE todos (task TEXT)')\n);\n\n-- Insert a row into the people table\nINSERT INTO people VALUES ('frectonz');\n\n-- Create a todo for \"frectonz\"\nUPDATE people\nSET database = execute_sqlite(\n    database,\n    'INSERT INTO todos VALUES (''solve multitenancy'')'\n)\nWHERE name = 'frectonz';\n\n-- Create a todo for \"frectonz\"\nUPDATE people\nSET database = execute_sqlite(\n    database,\n    'INSERT INTO todos VALUES (''buy milk'')'\n)\nWHERE name = 'frectonz';\n\n-- Fetch frectonz's info\nSELECT \n    name, \n    (\n        SELECT json_agg(get_sqlite_text(sqlite_row, 0))\n        FROM query_sqlite(\n            database, \n            'SELECT * FROM todos'\n        )\n    ) AS todos\nFROM \n    people \nWHERE \n    name = 'frectonz';\n```\n\n## Build it from source\n\n```bash\ngit clone git@github.com:frectonz/pglite-fusion.git \ncd pglite-fusion\nnix develop\ncargo pgrx init --pg13 download # this will take some time, since you are compiling postgres from source\ncargo pgrx run # this will drop you into a psql repl, you can then follow the example shown above\n```\n\n## API Documentation\n\nEvery function is parallel-safe except for `import_sqlite_from_file` and `export_sqlite_to_file`.\n\n### `empty_sqlite`\n\nCreates an empty SQLite database and returns it as a binary object. This can be used to initialize an empty SQLite database in a PostgreSQL column.\n\n#### Example Usage:\n\n```sql\nSELECT empty_sqlite();\n```\n\n-----\n\n### `query_sqlite`\n\nExecutes a SQL query on a SQLite database stored as a binary object and returns the result as a table of JSON-encoded rows. This function is useful for querying SQLite databases stored in PostgreSQL columns.\n\n#### Parameters:\n\n- `sqlite`: The SQLite database to query, stored as a binary object.\n- `query`: The SQL query string to execute on the SQLite database.\n\n#### Example Usage:\n\n```sql\nSELECT * FROM query_sqlite(\n    database, \n    'SELECT * FROM todos'\n);\n```\n\n-----\n\n### `execute_sqlite`\n\nExecutes a SQL statement (such as `INSERT`, `UPDATE`, or `DELETE`) on a SQLite database stored as a binary object. The updated SQLite database is returned as a binary object, allowing further operations on it.\n\n#### Parameters:\n\n- `sqlite`: The SQLite database to execute the SQL query on, stored as a binary object.\n- `query`: The SQL statement to execute on the SQLite database.\n\n#### Example Usage:\n\n```sql\nUPDATE people\nSET database = execute_sqlite(\n    database,\n    'INSERT INTO todos VALUES (''solve multitenancy'')'\n)\nWHERE name = 'frectonz';\n```\n\n-----\n\n### `init_sqlite`\n\nCreates an SQLite database with an initialization query already applied on it. This can be used to initialize a SQLite database with the expected tables already created.\n\n#### Parameters:\n\n- `query`: The SQL statement to execute on the SQLite database.\n\n#### Example Usage:\n\n```sql\n\nCREATE TABLE people (\n    name     TEXT NOT NULL,\n    database SQLITE DEFAULT init_sqlite('CREATE TABLE todos (task TEXT)')\n);\n```\n\n-----\n\n### `get_sqlite_text`\nExtracts a text value from a specific column in a row returned by `query_sqlite`. Use this function to retrieve text values from query results.\n\n#### Parameters:\n\n- `sqlite_row`: A row from the results of `query_sqlite`.\n- `index`: The index of the column to extract from the row.\n\n#### Example Usage:\n\n```sql\nSELECT get_sqlite_text(sqlite_row, 0) \nFROM query_sqlite(database, 'SELECT * FROM todos');\n```\n\n----\n\n### `get_sqlite_integer`\n\nExtracts an integer value from a specific column in a row returned by `query_sqlite`. Use this function to retrieve integer values from query results.\n\n#### Parameters:\n\n- `sqlite_row`: A row from the results of `query_sqlite`.\n- `index`: The index of the column to extract from the row.\n\n#### Example Usage:\n\n```sql\nSELECT get_sqlite_integer(sqlite_row, 1) \nFROM query_sqlite(database, 'SELECT * FROM todos');\n```\n\n----\n\n### `get_sqlite_real`\n\nExtracts a real (floating-point) value from a specific column in a row returned by `query_sqlite`. Use this function to retrieve real number values from query results.\n\n#### Parameters:\n\n- `sqlite_row`: A row from the results of `query_sqlite`.\n- `index`: The index of the column to extract from the row.\n\n#### Example Usage:\n\n```sql\nSELECT get_sqlite_real(sqlite_row, 2) \nFROM query_sqlite(database, 'SELECT * FROM todos');\n```\n\n---\n\n### `list_sqlite_tables`\n\nLists all table names present in the SQLite database.\n\n#### Parameters:\n\n- `sqlite`: A SQLite database stored as a binary blob.\n\n#### Example Usage:\n\n```sql\nSELECT table_name FROM list_sqlite_tables(database);\n```\n\n---\n\n### `sqlite_schema`\n\nRetrieves the SQL schema of each table in the SQLite database, the CREATE TABLE statements for each table in the SQLite database.\n\n#### Parameters:\n\n- `sqlite`: A SQLite database stored as a binary blob.\n\n#### Example Usage:\n\n```sql\nSELECT schema_sql FROM sqlite_schema(database);\n```\n\n---\n\n### `import_sqlite_from_file`\n\nLoads a SQLite database from a file into PostgreSQL.\n\n#### Parameters:\n\n- `path`: The file path of the SQLite database to load.\n\n#### Example Usage:\n\n```sql\nSELECT import_sqlite_from_file('/tmp/mydb.sqlite');\n```\n\n---\n\n### `export_sqlite_to_file`\n\nSaves the SQLite database to a file on disk.\n\n#### Parameters:\n\n- `sqlite`: The SQLite database.\n- `path`: The file path where the database should be saved.\n\n#### Example Usage:\n\n```sql\nSELECT export_sqlite_to_file(database, '/tmp/mydb.sqlite');\n```\n\n---\n\n### `vacuum_sqlite`\n\nRuns the SQLite `VACUUM` command to optimize storage by reclaiming unused space.\n\n#### Parameters:\n\n- `sqlite`: The SQLite database.\n\n#### Example Usage:\n\n```sql\nSELECT vacuum_sqlite(database);\n```\n\n---\n\n### `count_sqlite_rows`\n\nCounts the number of rows in a specified table within the SQLite database.\n\n#### Parameters:\n\n- `sqlite`: The SQLite database.\n- `table`: The name of the table to count rows from.\n\n#### Example Usage:\n\n```sql\nSELECT count_sqlite_rows(database, 'users');\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrectonz%2Fpglite-fusion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrectonz%2Fpglite-fusion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrectonz%2Fpglite-fusion/lists"}