{"id":13516737,"url":"https://github.com/graphile-contrib/postgraphile-plugin-upload-field","last_synced_at":"2026-03-15T18:14:35.097Z","repository":{"id":30967557,"uuid":"126526868","full_name":"graphile-contrib/postgraphile-plugin-upload-field","owner":"graphile-contrib","description":"Upload fields in PostGraphile","archived":false,"fork":false,"pushed_at":"2023-07-18T21:57:09.000Z","size":1306,"stargazers_count":43,"open_issues_count":10,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-12-07T15:43:22.017Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/graphile-contrib.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":"2018-03-23T18:50:55.000Z","updated_at":"2024-06-13T17:38:56.000Z","dependencies_parsed_at":"2024-06-02T03:41:25.081Z","dependency_job_id":null,"html_url":"https://github.com/graphile-contrib/postgraphile-plugin-upload-field","commit_stats":null,"previous_names":["mattbretl/postgraphile-plugin-upload-field"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile-contrib%2Fpostgraphile-plugin-upload-field","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile-contrib%2Fpostgraphile-plugin-upload-field/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile-contrib%2Fpostgraphile-plugin-upload-field/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile-contrib%2Fpostgraphile-plugin-upload-field/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphile-contrib","download_url":"https://codeload.github.com/graphile-contrib/postgraphile-plugin-upload-field/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229670339,"owners_count":18104861,"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":[],"created_at":"2024-08-01T05:01:25.366Z","updated_at":"2026-03-15T18:14:35.053Z","avatar_url":"https://github.com/graphile-contrib.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[![Package on npm](https://img.shields.io/npm/v/postgraphile-plugin-upload-field.svg)](https://www.npmjs.com/package/postgraphile-plugin-upload-field) [![CircleCI](https://circleci.com/gh/graphile-contrib/postgraphile-plugin-upload-field.svg?style=svg)](https://circleci.com/gh/graphile-contrib/postgraphile-plugin-upload-field)\n\n# postgraphile-plugin-upload-field\nUpload fields in PostGraphile\n\n\u003e Requires `graphql-upload@^10.0.0`\n\n## Usage\n\n```js\nconst fs = require(\"fs\");\nconst path = require(\"path\");\nconst express = require(\"express\");\nconst { postgraphile } = require(\"postgraphile\");\nconst PostGraphileUploadFieldPlugin = require(\"postgraphile-plugin-upload-field\");\nconst { graphqlUploadExpress } = require(\"graphql-upload\");\n\nconst app = express();\n\nconst UPLOAD_DIR_NAME = 'uploads';\n\n// Serve uploads as static resources\napp.use(`/${UPLOAD_DIR_NAME}`, express.static(path.resolve(UPLOAD_DIR_NAME)));\n\n// Attach multipart request handling middleware\napp.use(graphqlUploadExpress());\n\napp.use(\n  postgraphile(\"postgres://localhost:5432/upload_example\", \"public\", {\n    graphiql: true,\n    enableCors: true,\n    appendPlugins: [PostGraphileUploadFieldPlugin],\n    graphileBuildOptions: {\n      uploadFieldDefinitions: [\n        {\n          match: ({ schema, table, column, tags }) =\u003e\n            column === \"header_image_file\",\n          resolve: resolveUpload\n        }\n      ]\n    }\n  })\n);\n\napp.listen(5000, () =\u003e {\n  console.log('Server listening on port 5000');\n});\n\nasync function resolveUpload(upload, _args, _context, _info) {\n  const { filename, mimetype, encoding, createReadStream } = upload;\n  const stream = createReadStream();\n  // Save file to the local filesystem\n  const { id, filepath } = await saveLocal({ stream, filename });\n  // Return metadata to save it to Postgres\n  return filepath;\n}\n\nfunction saveLocal({ stream, filename }) {\n  const timestamp = new Date().toISOString().replace(/\\D/g, \"\");\n  const id = `${timestamp}_${filename}`;\n  const filepath = path.join(UPLOAD_DIR_NAME, id);\n  const fsPath = path.join(process.cwd(), filepath);\n  return new Promise((resolve, reject) =\u003e\n    stream\n      .on(\"error\", error =\u003e {\n        if (stream.truncated)\n          // Delete the truncated file\n          fs.unlinkSync(fsPath);\n        reject(error);\n      })\n      .on(\"end\", () =\u003e resolve({ id, filepath }))\n      .pipe(fs.createWriteStream(fsPath))\n  );\n}\n```\n\n## Examples\n\nSee https://github.com/mattbretl/postgraphile-upload-example/ for an example implementation.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphile-contrib%2Fpostgraphile-plugin-upload-field","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphile-contrib%2Fpostgraphile-plugin-upload-field","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphile-contrib%2Fpostgraphile-plugin-upload-field/lists"}