Skip to content

Instantly share code, notes, and snippets.

@redterror
Created February 28, 2019 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redterror/026d1f0ce1e67518fb220f8aaa0e14e5 to your computer and use it in GitHub Desktop.
Save redterror/026d1f0ce1e67518fb220f8aaa0e14e5 to your computer and use it in GitHub Desktop.
dgraph image importer
/* tslint:disable:no-console */
import { TaskOptions } from 'bin/task-bin'
import { listObjects } from 'lib/apis/s3'
import { getConfig } from 'lib/config'
import { batch } from 'streaming-iterables'
import * as dgraph from 'dgraph-js'
import { getSecret } from 'lib/ssm'
export default async function({ log }: TaskOptions) {
const dgraphClient = await getDgraphClient()
const { typesetMediaBetaS3Bucket: s3Bucket } = await getConfig()
console.log('starting')
let batchCount = 0
for await (const s3Objects of batch(1000, listObjects({ s3Bucket }))) {
const transaction = dgraphClient.newTxn()
const images = s3Objects.map(({ Key: s3key, Size: bytes, ETag: s3etag, LastModified: lastModified }) => ({
_type: 'Image',
s3key,
bytes,
s3etag,
updatedAt: lastModified && new Date(lastModified),
}))
const mutation = new dgraph.Mutation()
mutation.setSetJson(images)
console.log(`committing batch ${++batchCount}`)
await transaction.mutate(mutation)
await transaction.commit()
}
}
async function getDgraphClient() {
const dgraphUrl = await getSecret('dgraphUrl')
console.log('fetched degraphUrl', { dgraphUrl })
const clientStub = new dgraph.DgraphClientStub(dgraphUrl)
const dgraphClient = new dgraph.DgraphClient(clientStub)
return dgraphClient
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment