Code Monkey home page Code Monkey logo

postgrest-google-translate's Introduction

postgrest-google-translate

PostgreSQL/PostgrREST proxy to Google Translate API, with caching. It allows to work with Google Translate API right from Postgres or via REST API calls.

Build Status

This tiny project consists of 2 parts:

  1. SQL objects to enable calling Google API right from SQL environment (uses plsh extension)
  2. API method (uses PostgREST)

Part (1) can be used without part (2).

Table google_translate.cache is used to cache Google API responses to speedup work and reduce costs.

⚠️ Limitations

In general, the idea to call external things (even pretty predictable and fast like Google API) might introduce significant limitations to capability to scale for your master. However, this project shows how powerful PostgreSQL is: you don't need to use PHP/Python/Java/Ruby to work with external JSON API.

To make it scalable, one could run PostgREST on multiple slave nodes to avoid this limitations. The ony thing is to think about – writing to cache table (TODO: check if it is possible to call master's wrinting functions from plpgsql code being executed on slave nodes).

Installation

For your database (here we assume that it's called dbname), install [plsh](https://github.com/petere/plsh extension and then execute two SQL scripts, after what configure your database setting google_translate.api_key (take it from Google Could Platform Console):

psql dbname -c 'create extension if not exists plsh;'
psql dbname -f install_core.sql
psql dbname -f install_api.sql
psql -c "alter dbname set google_translate.api_key = 'YOU_GOOGLE_API_KEY';"

Alternatively, you can use ALTER ROLE ... SET google_translate.api_key = 'YOU_GOOGLE_API_KEY'; or put this setting to postgresql.conf (in these cases, it will be available cluster-wide).

Uninstallation

psql dbname -f uninstall_api.sql
psql dbname -f uninstall_core.sql
psql dbname -c 'drop extension plsh;'

Usage

In SQL environment:

-- Translate from English to Russian
SELECT google_translate('en', 'ru', 'Hello world'); 

REST API:

curl -X POST -H "Content-Type: application/json" \
    -H "Cache-Control: no-cache" \
    -d '{"source": "en", "target": "ru", "q": "Hello world"}' \
    "http://localhost:3000/rpc/google_translate"

postgrest-google-translate's People

Contributors

nikolays avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.