Overview
PersistedModel is the base class for models connected to persistent data sources such as databases and is also the base class for all built-in models (except Email). It provides all the standard create, read, update, and delete (CRUD) operations and exposes REST endpoints for them.
By default, LoopBack uses /api
as the URI root for the REST API. You can change this by changing the restApiRoot
property in the application /server/config.json
file. See config.json for more information.
The model names in the REST API are generally the plural form of the model name. By default this is simply the name with an "s" appended; for example, if the model is "car" then "cars" is the plural form. The plural form can be customized to be of any value in the model definition JSON file.
Create model instance
Create a new instance of the model and persist it to the data source.
Arguments
- Form data - Model instance data. Can be JSON representing a single model instance or an array of model instances.
Example
Request URL: POST http://localhost:3000/api/locations
{"name": "L1", "street": "107 S B St", "city": "San Mateo", "zipcode": "94401"}
Response status code: 200
{ "id": "96", "street": "107 S B St", "city": "San Mateo", "zipcode": 94401, "name": "L1" }
Update / insert instance
Update an existing model instance or insert a new one into the data source. The update will override any specified attributes in the request data object. It won’t remove existing ones unless the value is set to null.
Performs upsert to detect if there is a matching instance; if not, then inserts (creates) a new instance. If there is a matching instance, updates it.
Arguments
- Form data - model instance data in JSON format.
Examples
Insert
Request URL: PUT http://localhost:3000/api/locations
{"name": "L1", "street": "107 S B St", "city": "San Mateo", "zipcode": "94401"}
Response status code: 200
{ "id": 98, "street": "107 S B St", "city": "San Mateo", "zipcode": 94401, "name": "L1" }
Update
Request URL: PUT http://localhost:3000/api/locations
{"id": "98", "name": "L4", "street": "107 S B St", "city": "San Mateo", "zipcode": "94401"}
Response status code: 200
{ "id": 98, "street": "107 S B St", "city": "San Mateo", "zipcode": 94401, "name": "L4" }
Check instance existence
Check whether a model instance exists by ID in the data source.
Arguments
- modelID - model instance ID
Example
Request URL: GET http://localhost:3000/api/locations/88/exists
Response status code: 200
{ "exists": true }
Find instance by ID
Find a model instance by ID from the data source.
Arguments
- modelID - Model instance ID
Example
Request URL: GET http://localhost:3000/api/locations/88
Response status code: 200
{ "id": 88, "street": "390 Lang Road", "city": "Burlingame", "zipcode": 94010, "name": "Bay Area Firearms" }
Find matching instances
Find all instances of the model matched by filter from the data source.
Arguments
Pass the arguments as the value of the filter
HTTP query parameters, where:
- filterType1, filterType2, and so on, are the filter types.
- val1, val2 are the corresponding values.
See Querying data for an explanation of filter syntax.
Example
Request without filter:
Request URL: GET http://localhost:3000/api/locations
Request with a filter to limit response to two records:
Request URL: GET http://localhost:3000/api/locations?filter[limit]=2
Response status code: 200
[ { "id": "87", "street": "7153 East Thomas Road", "city": "Scottsdale", "zipcode": 85251, "name": "Phoenix Equipment Rentals" }, { "id": "88", "street": "390 Lang Road", "city": "Burlingame", "zipcode": 94010, "name": "Bay Area Firearms" } ]
Find first instance
Find first instance of the model matched by filter from the data source.
Arguments
Query parameters:
- filter - Filter that defines where, order, fields, skip, and limit. It's same as find's filter argument. See Querying data details.
Example
Request URL: GET http://localhost:3000/api/locations/findOne?filter[where][city]=Scottsdale
Response status code: 200
{ "id": "87", "street": "7153 East Thomas Road", "city": "Scottsdale", "zipcode": 85251, "name": "Phoenix Equipment Rentals" }
Delete model instance
Delete a model instance by ID from the data source
Arguments
- modelID - model instance ID
Example
Request URL: DELETE http://localhost:3000/api/locations/88
Response status code: 204
Get instance count
Count instances of the model from the data source matched by where clause.
Arguments
- where - criteria to match model instances. See Where过滤器 for more information.
Example
Count without "where" filter
Request URL: GET http://localhost:3000/api/locations/count
Count with a "where" filter
Request URL: GET http://localhost:3000/api/locations/count?where[city]=Burlingame
Response status code: 200
{ count: 6 }
Update model instance attributes
Update attributes of a model instance and persist into the data source.
Arguments
- data - An object containing property name/value pairs
- modelID - The model instance ID
Example
Request URL: PUT http://localhost:3000/api/locations/88
{"name": "L2"}
Response status code: 200
{ "id": "88", "street": "390 Lang Road", "city": "Burlingame", "zipcode": 94010, "name": "L2" }
Update matching model instances
Update attributes of matching model instances and persist into the data source.
Arguments
- data - An object containing property name/value pairs.
- where - The where object to select matching instances. See Where过滤器 for more information.
Example
Request URL: POST http://localhost:3000/api/locations/update?where[city]=Burlingame
{"city": "San Jose"}
Response status code: 204