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, nullable=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.2.0.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for soql-1.2.0.tar.gz
Algorithm Hash digest
SHA256 e3dfee7f9606018f36a063e974d7e57fcb7a548843cb7454dde803caabec3642
MD5 a36bbd951ee5663744dac2b4fac3cfe8
BLAKE2b-256 2f8b5dc2daa2e13122366c908313407165904aa1a58e58c65d31c941d8ea0fa7

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for soql-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d2c3878f263dcd46b7726b7a53ea307d6bb8be7f92e8c2388850c54a9a73956
MD5 32be3fe2ceec7261defb25b32c97547f
BLAKE2b-256 360d62e6d08f37faca5a646750f5e1a6359fdf701dcee6ca56b2e518325de3af

See more details on using hashes here.

Supported by

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