These are chat archives for frictionlessdata/chat

26th
Jul 2018
roll
@roll
Jul 26 2018 10:23
@anuveyatsu it's as simple as package.save(storage='sql', engine=create_engine(DATABASE_URL)) using the datapackage-py library
Full example (it was initially for check_relationships so it's also present but of course fully optional) :
from pprint import pprint
from datapackage import Package
from sqlalchemy import create_engine

DESCRIPTOR = {
  'resources': [
    {
      'name': 'teams',
      'data': [
        ['id', 'name', 'city'],
        ['1', 'Arsenal', 'London'],
        ['2', 'Real', 'Madrid'],
        ['3', 'Bayern', 'Munich'],
      ],
      'schema': {
        'fields': [
          {'name': 'id', 'type': 'integer'},
          {'name': 'name', 'type': 'string'},
          {'name': 'city', 'type': 'string'},
        ],
        'primaryKey': 'id',
        'foreignKeys': [
          {
            'fields': 'city',
            'reference': {'resource': 'cities', 'fields': 'name'},
          },
        ],
      },
    }, {
      'name': 'cities',
      'data': [
        ['name', 'country'],
        ['London', 'England'],
        ['Madrid', 'Spain'],
      ],
      'schema': {
        'fields': [
          {'name': 'name', 'type': 'string'},
          {'name': 'country', 'type': 'string'},
        ],
        'primaryKey': 'name',
      },
    },
  ],
}

# Check
package = Package(DESCRIPTOR)
teams = package.get_resource('teams')
# teams.check_relations()
# will raise FK error

# Tweak/check
package.descriptor['resources'][1]['data'].append(['Munich', 'Germany'])
package.commit()
teams = package.get_resource('teams')
print(teams.check_relations())

# Read
print(teams.read(keyed=True, relations=True))
# [{'id': 1, 'name': 'Arsenal', 'city': {'name': 'London', 'country': 'England'}}, {'id': 2, 'name': 'Real', 'city': {'name': 'Madrid', 'country': 'Spain'}}, {'id': 3, 'name': 'Bayern', 'city': {'name': 'Munich', 'country': 'Germany'}}]

# Save to Postgres
DATABASE_URL = 'postgresql://<usel>:<pass>@localhost:5432/<name>'
package.save(storage='sql', engine=create_engine(DATABASE_URL))
Anuar Ustayev
@anuveyatsu
Jul 26 2018 10:27
@roll :+1: thanks a lot!
Michael Amadi
@michaelamadi
Jul 26 2018 18:42
@serahrono Looks like this can be closed now? frictionlessdata/specs#566