Skip to main content

Models and query generator for Salesforce Object Query Language (SOQL)

Project description

CI Status PyPI status

This package provides declarative models for Salesforce objects and utilities for generating Salesforce Object Query Language (SOQL) queries from these models.

This package works well with Simple Salesforce.

Usage

from simple_salesforce import Salesforce
from soql import attributes
from soql import load_models_from_salesforce_data
from soql import Model
from soql import select


class Account(Model):
    id = attributes.String('Id')
    deleted = attributes.Boolean('IsDeleted')
    name = attributes.String('Name')
    owner = attributes.Relationship('Owner', related_model=User)
    custom_field = attributes.String('CustomField__c', nullable=True)

class User(Model):
    id = attributes.String('Id')
    email = attributes.String('Email')

sf = Salesforce(...)

query = select(Account) \
    .where(Account.id == '50130000000014c') \
    .join(Account.owner)

resp = sf.query(str(query))

account = load_models_from_salesforce_data(resp)[0]

print(account.id)
print(account.owner.id)

Models

Models define in-memory representations of Salesforce object, and provide an idiomatic way to access the data.

from soql import attributes
from soql import Model

class User(Model):
    # The first argument to an attribute is its name in Salesforce.
    id = attributes.String('Id')
    email = attributes.String('Email')

user = User(id='123', email='a@b.com')

assert user.id == '123'

Helpers are available to load models directly from simple_salesforce:

query = select(User)

resp = sf.query(str(query))

users = load_models_from_salesforce_data(resp)

Relationships can also be declared:

class Account(Model):
    id = attributes.String('Id')
    owner = attributes.Relationship('Owner', related_model=User)
    contacts = attributes.Relationship('Contacts', related_model=User, many=True)

Queries

SOQL queries can be generated from models:

from soql import select

query = select(User).where(User.id == '123')

assert str(query) == "SELECT User.Id, User.Email FROM User WHERE User.Id = '123'"

Most of SOQL is supported, including…

Joins:

from soql import select

query = select(Account).join(Account.contacts)

assert str(query) == "SELECT Account.Id, (SELECT User.Id, User.Email FROM Account.Contacts) FROM Account"

Subqueries:

from soql import select

subquery = select(User).columns(User.email).subquery()
query = select(User).where(User.email.in_(subquery))

assert str(query) == "SELECT User.Id, User.Email FROM User WHERE User.Email IN (SELECT User.Email FROM User)"

And more!

Installation

pip install soql

Contributing

There is still work to be done, and contributions are encouraged! Check out the contribution guide for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

soql-1.1.0.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

soql-1.1.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file soql-1.1.0.tar.gz.

File metadata

  • Download URL: soql-1.1.0.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for soql-1.1.0.tar.gz
Algorithm Hash digest
SHA256 0d40299329913cfad2cda171a78ceac5d7d642fa776c03926c47e8b6509d8394
MD5 fec21c69e4fd3d73e36c28d4ca0f5b29
BLAKE2b-256 4ef22b3a909b89ea08547071c5fd06c87266e4ac360f5a81d6e83ebb258b0651

See more details on using hashes here.

File details

Details for the file soql-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: soql-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for soql-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82405599e3d59e235700d57401dde6d1389d51744a28625018049736b27658b9
MD5 d2b05d085639511aa8cd0c85caac637c
BLAKE2b-256 5a8476324755ce3e9e935b83dcbba47e844b9cc24b677434c81e67867d4e0b48

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page