async def postgresql_pool(app: web.Application) -> AsyncGenerator[None, None]: dialect = get_dialect(json_serializer=json.dumps, json_deserializer=json.loads) pg = await asyncpgsa.create_pool(dsn=app["DATABASE_URL"], dialect=dialect) _register_in_app(app, "pg", pg) yield await pg.close()
def get_json_connection_params(): dialect = get_dialect( json_serializer=functools.partial(ujson.dumps, ensure_ascii=False)) return { 'init': set_db_json_charset, 'dialect': dialect, }
def test_compile_jsonb_with_custom_json_encoder(): jsonb_table = sa.Table( 'meowsb', sa.MetaData(), sa.Column('data', postgresql.JSONB), ) class JSONEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, uuid.UUID): return str(o) else: return super().default(o) dialect = connection.get_dialect( json_serializer=partial(json.dumps, cls=JSONEncoder) ) data = { 'uuid4': uuid.uuid4(), } query = jsonb_table.insert().values(data=data) q, p = connection.compile_query(query, dialect=dialect) assert q == 'INSERT INTO meowsb (data) VALUES ($1)' assert p == ['{"uuid4": "%s"}' % data['uuid4']]