- Source:
Methods for views.
Methods
(static) createRecord(viewName, recordValuesObj, optionsopt) → {Promise.<({status: 201, statusText: 'Created', message: string}|{status: 201, statusText: 'Created', message: string, createdRecord: object})>}
- Source:
- Since:
- 1.0.0
Creates a record in view viewName
(generally not a recommended method to use due to the notes that follow). Directly creating a record in a table should always be the preferred route since creating a record in a view ultimately results in creating a single record in an underlying table.
Note 1 (required conditions for creating a record in a view): Records may only be created in a view if the view itself only contains a single table or if a particular table in the view has been specified as editable within the view's configuration. This can be done by selecting a table under the "Do you need to edit data using this View?" question when editing a view within Caspio.
Note 2 (creating a record in a view where the record does not appear in the view): It is possible for a record to be "created" in the view but not appear as the value for the createdRecord
property of the response object. How? If the record is no longer included in the view after its creation, then the record does not appear as having been created in the view. How could this happen? If a field value is being used to make the record appear in the view, such as the Account_Status
field needing to be true
in a Demo_Physicians_Active
view, then creating a record where the Account_Status
field value was false
would result in the record being excluded from the view but created in the underlying Demo_Physicians
table; hence, the record would correspondingly be excluded from the createdRecord
property of the response object.
Example
// create one record that remains in the view after creation (i.e., email of "oscarmartinez@google.com")
// create another record that ends up being filtered out of the view (i.e., email of "jimhalpert@google.com")
const caspio = require('caspio-sdk')(caspioCredentials);
async function createViewRecord() {
const newRecordObjGood = {
First_Name: "Oscar",
Last_Name: "Martinez",
Email: "oscarmartinez@google.com",
Account_Status: true
}
const newRecordObjBad = {
First_Name: "Jim",
Last_Name: "Halpert",
Email: "jimhalpert@google.com",
Account_Status: false
}
const VIEW_NAME = 'Demo_Physicians_Active';
const createResultOne = await caspio.views.createRecord(VIEW_NAME, newRecordObjGood, { row: true });
const createResultTwo = await caspio.views.createRecord(VIEW_NAME, newRecordObjBad, { row: true });
console.log(createResultOne);
console.log(createResultTwo);
return [ createResultOne, createResultTwo ]
}
createViewRecord();
// example return value(s)
[
{
status: 201,
statusText: 'Created',
message: 'Record successfully created.',
createdRecord: {
Physician_ID: 'PVFHYYQH',
Date_Created: null,
First_Name: 'Oscar',
Last_Name: 'Martinez',
Full_Name: 'Oscar Martinez',
Gender: '',
Email: 'oscarmartinez@google.com',
Account_Status: true,
Profile_Status: false,
Profile_Picture: null,
Specialties: null,
About_Section: '',
Education_and_Training: '',
Languages_Spoken: null,
Office_Address: '',
Office_City: '',
Office_State: '',
Office_Zip: '',
Title: '',
Accepting_New_Patients: false,
Affiliations: '',
Board_Certifications: '',
Office_Name: '',
Office_Phone: '',
ViewCount: null
}
},
{
status: 201,
statusText: 'Created',
message: 'Record successfully created.',
createdRecord: undefined
}
]
Parameters:
Name | Type | Attributes | Default | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
viewName |
string | Name of view (case-insensitive) |
||||||||||||
recordValuesObj |
object | Object with key-value pairs in the form |
||||||||||||
options |
object |
<optional> |
{ 'row': false }
|
The Properties
|
Returns:
Object with information about the attempted creation of the provided record (i.e., status
, statusText
, message
, and createdRecord
if the options
argument was passed as { 'row': true }
). If { 'row': true }
is not specified as the third argument, then the response object will not have a createRecord
property; if, however, { 'row': true }
is specified but the record was created in an underlying table but does not appear in the view itself, then undefined
will be the value for the createdRecord
property of the response object.
- Type
- Promise.<({status: 201, statusText: 'Created', message: string}|{status: 201, statusText: 'Created', message: string, createdRecord: object})>
(static) deleteRecords(viewName, whereClause) → {Promise.<{status: 200, statusText: 'OK', message: string, recordsAffected: number}>}
- Source:
- Since:
- 1.0.0
Deletes all records from viewName
matched by whereClause
(i.e., the provided WHERE
clause). This method is generally not recommended due to the notes that follow. Directly deleting records in a table should always be the preferred route since deleting records in a view ultimately results in deleting records from a single underlying table.
Note (required conditions for deleting a record from a view): Records may only be deleted in a view if the view itself only contains a single table or if a particular table in the view has been specified as editable within the view's configuration. This can be done by selecting a table under the "Do you need to edit data using this View?" question when editing a view within Caspio.
Example
// Delete all records from the 'Demo_Physicians_Active' view
// that have an 'Email' field value of 'oscarmartinez@google.com'
const caspio = require('caspio-sdk')(caspioCredentials);
async function deleteRecordsFromView() {
const whereClause = `Email = 'oscarmartinez@google.com'`;
const VIEW_NAME = 'Demo_Physicians_Active';
const deleteResult = await caspio.views.deleteRecords(VIEW_NAME, whereClause);
console.log(deleteResult);
return deleteResult;
}
deleteRecordsFromView();
// sample return value
{
status: 200,
statusText: 'OK',
message: '1 record(s) successfully deleted.',
recordsAffected: 1
}
Parameters:
Name | Type | Description |
---|---|---|
viewName |
string | Name of view (case-insensitive) |
whereClause |
string |
|
Returns:
Object with information about the attempted deletion of the records matched by the WHERE
clause (i.e., status
, statusText
, message
, and recordsAffected
)
- Type
- Promise.<{status: 200, statusText: 'OK', message: string, recordsAffected: number}>
(static) description(viewName) → {Promise.<{Name: string, Note: string}>}
- Source:
- Since:
- 1.0.0
Returns the description of the view (i.e., a description of view viewName
).
Example
// get description of the 'Demo_Physicians_Active' view
const caspio = require('caspio-sdk')(caspioCredentials);
async function getViewDescription() {
const VIEW_NAME = 'Demo_Physicians_Active';
const description = await caspio.views.description(VIEW_NAME);
console.log(description);
return description;
}
getViewDescription();
// sample return value
{ Name: 'Demo_Physicians_Active', Note: '' }
Parameters:
Name | Type | Description |
---|---|---|
viewName |
string | Name of the view (case-insensitive) |
Returns:
Object with the name of view (i.e., Name
) and a note about the view (i.e., Note
)
- Type
- Promise.<{Name: string, Note: string}>
(static) getRecords(viewName, selectionCriteriaObjopt) → {Promise.<Array.<Object>>}
- Source:
- Since:
- 1.0.0
Returns all records from the view that satisfy the provided query criteria (i.e., selectionCriteriaObj
). Pagination is automatically handled to ensure all records matching the provided criteria are returned.
Note (potential strain on memory resources): If the query provided results in many thousands or millions of records needing to be returned, then this may cause a strain on memory resources (since all returned records are held in memory when using this method). Consider using the getRecordsStreamToFile
method in such an instance where all returned records can be streamed to a file in batches of 1000
records (i.e., the maximum number of records Caspio's REST API will respond with for any request).
Example
// get all records from the 'Demo_Physicians_Active' view
const caspio = require('caspio-sdk')(caspioCredentials);
async function getViewRecords() {
const VIEW_NAME = 'Demo_Physicians_Active';
const viewRecords = await caspio.views.getRecords(VIEW_NAME);
console.log(viewRecords);
return viewRecords;
}
getViewRecords();
// sample return value
[
...,
{
Physician_ID: '6T2C9HW8',
Date_Created: '2020-10-07T04:54:19',
First_Name: 'Lelah',
Last_Name: 'Hoppe',
Full_Name: 'Lelah Hoppe',
Gender: 'Other',
Email: 'Lelah.Hoppe@gmail.com',
Account_Status: true,
Profile_Status: false,
Profile_Picture: '/Demo/LelahHoppe.png',
Specialties: {
'3': 'Family Medicine',
'6': 'Obstetrics and Gynecology',
'8': 'Pain Management',
'9': 'Pathology'
},
About_Section: 'Exercitationem ... labore.',
Education_and_Training: 'David Geffen School of Medicine - MD\n' +
'Yale - Residency, Ophthalmology\n' +
'Yale - Fellowship, Cataracts and Refractive Surgery',
Languages_Spoken: { '2': 'English', '4': 'French', '7': 'Japanese', '9': 'Spanish' },
Office_Address: '0519 Mustafa Via',
Office_City: 'New Bradlyhaven',
Office_State: 'MI',
Office_Zip: '50198',
Title: 'Adolescent Medicine Specialist',
Accepting_New_Patients: true,
Affiliations: 'American College of Endocrinology\n' +
'The Endocrine Society\n' +
'American Diabetes Association',
Board_Certifications: 'American Board of Pediatrics',
Office_Name: 'Davis Family Medical Group',
Office_Phone: '967-841-6054',
ViewCount: 3654
},
...
]
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
viewName |
string | Name of view (case-insensitive) |
|||||||||||||||||||||||||||
selectionCriteriaObj |
Object |
<optional> |
{}
|
Object that specifies the criteria to be used in constructing the query. Valid properties include the following: In large part, the query being constructed resembles most queries constructed using SQL (specifically the SQL Server dialect since Caspio uses SQL Server under the hood). Consult a Caspio rep to find which version your account is running on. Note: Usage of window functions when expecting more than Properties
|
Returns:
An array of objects representing the records retrieved from the specified view (i.e., viewName
) that were obtained by the query provided (i.e., selectionCriteriaObj
).
- Type
- Promise.<Array.<Object>>
(static) getRecordsPaginated(viewName, selectionCriteriaObjopt) → {Promise.<Array.<Object>>}
- Source:
- Since:
- 1.0.0
Returns records in a paginated fashion from view viewName
that satisfy the provided query criteria (i.e., selectionCriteriaObj
).
Example
// get a single record from the 'Demo_Physicians_Active' view
const caspio = require('caspio-sdk')(caspioCredentials);
async function getPaginatedViewRecords() {
const criteriaObj = {
limit: 1
}
const VIEW_NAME = 'Demo_Physicians_Active';
const viewRecords = await caspio.views.getRecordsPaginated(VIEW_NAME, criteriaObj);
console.log(viewRecords);
return viewRecords;
}
getPaginatedViewRecords();
// sample return value
[
{
Physician_ID: '6T2C9HW8',
Date_Created: '2020-10-07T04:54:19',
First_Name: 'Lelah',
Last_Name: 'Hoppe',
Full_Name: 'Lelah Hoppe',
Gender: 'Other',
Email: 'Lelah.Hoppe@gmail.com',
Account_Status: true,
Profile_Status: false,
Profile_Picture: '/Demo/LelahHoppe.png',
Specialties: {
'3': 'Family Medicine',
'6': 'Obstetrics and Gynecology',
'8': 'Pain Management',
'9': 'Pathology'
},
About_Section: 'Exercitationem ... labore.',
Education_and_Training: 'David Geffen School of Medicine - MD\n' +
'Yale - Residency, Ophthalmology\n' +
'Yale - Fellowship, Cataracts and Refractive Surgery',
Languages_Spoken: { '2': 'English', '4': 'French', '7': 'Japanese', '9': 'Spanish' },
Office_Address: '0519 Mustafa Via',
Office_City: 'New Bradlyhaven',
Office_State: 'MI',
Office_Zip: '50198',
Title: 'Adolescent Medicine Specialist',
Accepting_New_Patients: true,
Affiliations: 'American College of Endocrinology\n' +
'The Endocrine Society\n' +
'American Diabetes Association',
Board_Certifications: 'American Board of Pediatrics',
Office_Name: 'Davis Family Medical Group',
Office_Phone: '967-841-6054',
ViewCount: 3654
}
]
Parameters:
Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
viewName |
string | Name of view (case-insensitive) |
||||||||||||||||||||||||||||||||||||||||||
selectionCriteriaObj |
Object |
<optional> |
{}
|
Object that specifies the criteria to be used in constructing the query. Valid properties include the following: In large part, the query being constructed resembles most queries constructed using SQL (specifically the SQL Server dialect since Caspio uses SQL Server under the hood). Consult a Caspio rep to find which version your account is running on. Note: Usage of window functions (e.g., Properties
|
Returns:
An array of objects representing the records retrieved from the specified view (i.e., viewName
) that were obtained by the query provided (i.e., selectionCriteriaObj
).
- Type
- Promise.<Array.<Object>>
(static) getRecordsStreamToFile(viewName, filePath, selectionCriteriaObjopt) → {void}
- Source:
- Since:
- 1.0.0
Streams all records from the view (i.e., viewName
) that satisfy the provided query criteria (i.e., selectionCriteriaObj
) to a file (i.e., filePath
). Pagination is automatically handled to ensure all records matching the provided criteria are streamed to the specified file. Records are streamed in batches of 1000
records (Caspio's rate limit for returning records). Useful when you need to process huge amounts of data but do not want to hold everything in memory.
Example
// stream all records from the 'Demo_Physicians_Active' view
// to the 'Demo_Physicians_Active.json' file
const caspio = require('caspio-sdk')(caspioCredentials);
async function streamViewRecordsToFile() {
const VIEW_NAME = 'Demo_Physicians_Active';
const FILE_PATH = 'Demo_Physicians_Active.json';
await caspio.views.getRecordsStreamToFile(VIEW_NAME, FILE_PATH);
}
streamViewRecordsToFile();
// no return value
Parameters:
Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
viewName |
string | Name of view (case-insensitive) |
|||||||||||||||||||||||||||
filePath |
string | Path of file to write to (file path should have an extension of |
|||||||||||||||||||||||||||
selectionCriteriaObj |
Object |
<optional> |
{}
|
Object that specifies the criteria to be used in constructing the query. Valid properties include the following: In large part, the query being constructed resembles most queries constructed using SQL (specifically the SQL Server dialect since Caspio uses SQL Server under the hood). Consult a Caspio rep to find which version your account is running on. Note: Usage of window functions when expecting more than Properties
|
Returns:
No value is returned
- Type
- void
(static) listing() → {Promise.<Array.<string>>}
- Source:
- Since:
- 1.0.0
Returns an array of strings where each string represents an available view for a Caspio account.
Example
// get list of all view names linked to a Caspio account
const caspio = require('caspio-sdk')(caspioCredentials);
async function getViewNames() {
const viewNames = await caspio.views.listing();
console.log(viewNames);
return viewNames;
}
getViewNames();
// sample return value
[
...
'Demo_Admins_Active',
'Demo_Physicians_Active',
'Demo_Physicians_Published',
...
]
Returns:
Array of strings representing view names for a Caspio account
- Type
- Promise.<Array.<string>>
(static) updateRecords(viewName, whereClause, newRecordValuesObj, optionsopt) → {Promise.<({status: 200, statusText: 'OK', message: string, recordsAffected: number}|{status: 200, statusText: 'OK', message: string, recordsAffected: number, updatedRecords: Array.<Object>})>}
- Source:
- Since:
- 1.0.0
Updates all records in view viewName
that match the provided WHERE
clause. This method is generally not recommended due to the notes that follow. Directly updating records in a table should always be the preferred route since updating records in a view ultimately results in updating records in a single underlying table.
Note 1 (required conditions for updating view records): Records may only be updated in a view if the view itself only contains a single table or if a particular table in the view has been specified as editable within the view's configuration. This can be done by selecting a table under the "Do you need to edit data using this View?" question when editing a view within Caspio.
Note 2 (updating a view record that results in the record's disappearance from the view): It is possible for a record to be updated in the view but not appear in the updatedRecords
array. How? If the record is no longer included in the view after the update, then the record does not appear as one of the records that was updated. For example, if a field value is being used to make the record appear in the view (e.g., Account_Status
of true
for the Demo_Physicians_Active
view), then changing that field value may result in the field being excluded from the view (e.g., changing Account_Status
from true
to false
), which would correspondingly exclude the updated record from the updatedRecords
array in the response.
Example
// update records in the 'Demo_Physicians_Active' to have a 'Profile_Status'
// field value of false for all records where the 'Email' field value is 'Lelah.Hoppe@gmail.com'
const caspio = require('caspio-sdk')(caspioCredentials);
async function updateViewRecords() {
const whereClause = `Email = 'Lelah.Hoppe@gmail.com'`;
const newValuesObj = {
Profile_Status: false
}
const VIEW_NAME = 'Demo_Physicians_Active';
const updateResult = await caspio.views.updateRecords(VIEW_NAME, whereClause, newValuesObj, { rows: true });
console.log(updateResult);
return updateResult;
}
updateViewRecords();
// sample return value
{
status: 200,
statusText: 'OK',
message: '1 record(s) affected.',
recordsAffected: 1,
updatedRecords: [
{
Physician_ID: '6T2C9HW8',
Date_Created: '2020-10-07T04:54:19',
First_Name: 'Lelah',
Last_Name: 'Hoppe',
Full_Name: 'Lelah Hoppe',
Gender: 'Other',
Email: 'Lelah.Hoppe@gmail.com',
Account_Status: true,
Profile_Status: false,
Profile_Picture: '/Demo/LelahHoppe.png',
Specialties: [Object],
About_Section: 'Exercitationem ... labore.',
Education_and_Training: 'David Geffen School of Medicine - MD\n' +
'Yale - Residency, Ophthalmology\n' +
'Yale - Fellowship, Cataracts and Refractive Surgery',
Languages_Spoken: [Object],
Office_Address: '0519 Mustafa Via',
Office_City: 'New Bradlyhaven',
Office_State: 'MI',
Office_Zip: '50198',
Title: 'Adolescent Medicine Specialist',
Accepting_New_Patients: true,
Affiliations: 'American College of Endocrinology\n' +
'The Endocrine Society\n' +
'American Diabetes Association',
Board_Certifications: 'American Board of Pediatrics',
Office_Name: 'Davis Family Medical Group',
Office_Phone: '967-841-6054',
ViewCount: 3654
}
]
}
Parameters:
Name | Type | Attributes | Default | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
viewName |
string | Name of view (case-insensitive) |
||||||||||||
whereClause |
string |
|
||||||||||||
newRecordValuesObj |
object | Object with key-value pairs in the form |
||||||||||||
options |
object |
<optional> |
{ 'rows': '' }
|
The Properties
|
Returns:
Object with information about the attempted update of the records matched by the WHERE
clause (i.e., status
, statusText
, message
, recordsAffected
, and updatedRecords
if the options
argument was passed as { 'rows': true }
)
- Type
- Promise.<({status: 200, statusText: 'OK', message: string, recordsAffected: number}|{status: 200, statusText: 'OK', message: string, recordsAffected: number, updatedRecords: Array.<Object>})>