# Database Actions

# query()

Queries the local database, handles error handling and converts results into JavaScript objects.

import { query } from '@pitcher/core'

const result = await query('select * from tbl_event_v1') // deprecated please use query options object version of the function
console.log(result)

const result = await query({
  query: 'select * from tbl_event_v1',
  db: 'pitcher',
  removeNull: false,
  source: 'modal',
  cacheEnabled: false,
  longQueryWarning: false,
  extractJsonFields: false // when set to true fields in the json strings flattened into the parent object
})

# contextQuery()

contextQuery uses a mustache template syntax and enriches the query function dramatically. As a prerequisite it needs both loadConfig() and loadParams() to be executed before calling it the first time.

As a result contextQuery has knowledge about the current environment and provides you with:

  • The names of all the table names. {{ sfdcObjectName }} will be replaced with the tablename

  • The following objects:

    • account
    • user
    • locale

    example: {{ account.Id }}

  • Today: TODAY

  • Extra context via parameters

  • Ability to execute JavaScript and function from the context

// Example 1
contextQuery(`
  SELECT * from {{ Account }} WHERE Id="{{ account.id }}" 
  AND  ModifiedDate < TODAY AND Name="{{ Name }}"`, 
  { Name: 'ABCDEF1234' }
)
// Example 2
contextQuery(`
  SELECT * from {{ Account }} 
  WHERE Id IN ({{ ids.join(",") }})`, 
  { ids: [1,2,3] }
)
// Example 3
contextQuery(`
  SELECT AccountId__c="{{ account.id }}{% if account.isVIP %} 
  AND IsVIP=TRUE{% endif %}`,
  { ids: [1,2,3] }
)

You can either reference API names of Salesforce objects or normal names. It's a good idea to call loadConfig() and loadParams() in your App.vue -> onMounted() function first before using contextQuery

# saveLocal()

To save something locally in the database:

const data = { hello: 'world' }
saveLocal('myId', data)

# loadLocal()

To load the same data when the app resumes:

const data = await loadLocal('myId')

# Example

// example.js
function getId() {
  const store = useParamsStore()
  return 'myApp_' + store.state.account.Id
}

export function save() {
  const store = useMyStore()
  const data = { myData:store.state.myData }
  saveLocal(getId(), data)
}

export async function resume() {
  const data = await loadLocal(getId())
  const store = useMyStore()
  store.state.myData = data.myData
}
// App.vue
setup() {
  const loaded = ref(false)
  onMounted(async () => {
    await loadSomeData()
    try {
      await resume()
    } catch(e) {
      console.info('no resume found')
    }

    watchEffect(save)
    console.info('data loading finished')
    loaded.value = true
  })
}

# sfdcSchema

TO DO: Needs docs

# sfdcLayout

TO DO: Needs docs

# sfdcField

TO DO: Needs docs