Class Table<TModel>
Class created from a model derived from BaseModel
that can generate query requests to a Postgrest Endpoint.
Representative of a USE $TABLE
command.
public class Table<TModel> : IPostgrestTable<TModel>, IGettableHeaders where TModel : BaseModel, new()
Type Parameters
TModel
Model derived from
BaseModel
.
- Inheritance
-
Table<TModel>
- Implements
-
IPostgrest
Table <TModel>
- Derived
- Inherited Members
Constructors
Table(string, JsonSerializerSettings, ClientOptions?)
Typically called from the Client new Client.Table<ModelType>
public Table(string baseUrl, JsonSerializerSettings serializerSettings, ClientOptions? options = null)
Parameters
baseUrl
stringApi Endpoint (ex: "http://localhost:8000"), no trailing slash required.
serializerSettings
JsonSerializer Settings options
ClientOptions Optional client configuration.
Properties
BaseUrl
API Base Url for subsequent calls.
Property Value
GetHeaders
An executable Func
that returns a dictionary of headers to be appended onto a request.
Property Value
TableName
Name of the Table parsed by the Model.
Property Value
Methods
And(List<IPostgrestQueryFilter>)
Adds an AND Filter to the current query args.
Parameters
filters
List<IPostgrestQuery >Filter
Returns
- IPostgrest
Table <TModel>
Clear()
Clears currently defined query values.
Columns(Expression<Func<TModel, object[]>>)
By using the columns query parameter it’s possible to specify the payload keys that will be inserted and ignore the rest of the payload.
The rest of the JSON keys will be ignored. Using this also has the side-effect of being more efficient for Bulk Insert since PostgREST will not process the JSON and it’ll send it directly to PostgreSQL.
See: https://postgrest.org/en/stable/api.html#specifying-columns
Parameters
predicate
Expression<Func<TModel, object[]>>
Returns
- IPostgrest
Table <TModel>
Columns(string[])
By using the columns query parameter it’s possible to specify the payload keys that will be inserted and ignore the rest of the payload.
The rest of the JSON keys will be ignored. Using this also has the side-effect of being more efficient for Bulk Insert since PostgREST will not process the JSON and it’ll send it directly to PostgreSQL.
See: https://postgrest.org/en/stable/api.html#specifying-columns
Parameters
columns
string[]
Returns
- IPostgrest
Table <TModel>
Count(CountType, CancellationToken)
Returns ONLY a count from the specified query.
See: https://postgrest.org/en/v7.0.0/api.html?highlight=count
Parameters
type
Constants.CountType The kind of count.
cancellationToken
CancellationToken
Returns
Delete(QueryOptions?, CancellationToken)
Executes a delete request using the defined query params on the current instance.
Parameters
options
QueryOptions cancellationToken
CancellationToken
Returns
Delete(TModel, QueryOptions?, CancellationToken)
Executes a delete request using the model's primary key as the filter for the request.
public Task<ModeledResponse<TModel>> Delete(TModel model, QueryOptions? options = null, CancellationToken cancellationToken = default)
Parameters
model
TModeloptions
QueryOptions cancellationToken
CancellationToken
Returns
- Task<Modeled
Response <TModel>>
Filter<TCriterion>(Expression<Func<TModel, object>>, Operator, TCriterion?)
Add a filter to a query request using a predicate to select column.
public IPostgrestTable<TModel> Filter<TCriterion>(Expression<Func<TModel, object>> predicate, Constants.Operator op, TCriterion? criterion)
Parameters
predicate
Expression<Func<TModel, object>>Expects a columns from the Model to be returned
op
Constants.OperatorOperation to perform.
criterion
TCriterionValue to filter with, must be a
string
,List<object>
,Dictionary<string, object>
,FullTextSearchConfig
, orRange
Returns
- IPostgrest
Table <TModel>
Type Parameters
TCriterion
Exceptions
Filter<TCriterion>(string, Operator, TCriterion?)
Add a Filter to a query request
public IPostgrestTable<TModel> Filter<TCriterion>(string columnName, Constants.Operator op, TCriterion? criterion)
Parameters
columnName
stringColumn Name in Table.
op
Constants.OperatorOperation to perform.
criterion
TCriterionValue to filter with, must be a
string
,List<object>
,Dictionary<string, object>
,FullTextSearchConfig
, orRange
Returns
- IPostgrest
Table <TModel>
Type Parameters
TCriterion
GenerateUrl()
Generates the encoded URL with defined query parameters that will be sent to the Postgrest API.
Returns
Get(CancellationToken)
Executes the query using the defined filters on the current instance.
Parameters
cancellationToken
CancellationToken
Returns
- Task<Modeled
Response <TModel>>
Insert(ICollection<TModel>, QueryOptions?, CancellationToken)
Executes a BULK INSERT query using the defined query params on the current instance.
public Task<ModeledResponse<TModel>> Insert(ICollection<TModel> models, QueryOptions? options = null, CancellationToken cancellationToken = default)
Parameters
models
ICollection<TModel>options
QueryOptions cancellationToken
CancellationToken
Returns
- Task<Modeled
Response <TModel>> A typed model response from the database.
Insert(TModel, QueryOptions?, CancellationToken)
Executes an INSERT query using the defined query params on the current instance.
public Task<ModeledResponse<TModel>> Insert(TModel model, QueryOptions? options = null, CancellationToken cancellationToken = default)
Parameters
model
TModeloptions
QueryOptions cancellationToken
CancellationToken
Returns
- Task<Modeled
Response <TModel>> A typed model response from the database.
Limit(int, string?)
Sets a limit with an optional foreign table reference.
Parameters
Returns
- IPostgrest
Table <TModel>
Match(Dictionary<string, string>)
Finds all rows whose columns match the specified query
object.
Parameters
query
Dictionary<string, string>The object to filter with, with column names as keys mapped to their filter values.
Returns
- IPostgrest
Table <TModel>
Match(TModel)
Fills in query parameters based on a given model's primary key(s).
Parameters
model
TModelA model with a primary key column
Returns
- IPostgrest
Table <TModel>
Not(IPostgrestQueryFilter)
Adds a NOT filter to the current query args.
Parameters
filter
IPostgrestQuery Filter
Returns
- IPostgrest
Table <TModel>
Not(Expression<Func<TModel, object>>, Operator, Dictionary<string, object>)
Adds a NOT filter to the current query args.
public IPostgrestTable<TModel> Not(Expression<Func<TModel, object>> predicate, Constants.Operator op, Dictionary<string, object> criteria)
Parameters
predicate
Expression<Func<TModel, object>>Expects a column from the model to be returned.
op
Constants.Operatorcriteria
Dictionary<string, object>
Returns
- IPostgrest
Table <TModel>
Not(string, Operator, Dictionary<string, object>)
Adds a NOT filter to the current query args.
public IPostgrestTable<TModel> Not(string columnName, Constants.Operator op, Dictionary<string, object> criteria)
Parameters
Returns
- IPostgrest
Table <TModel>
Not<TCriterion>(Expression<Func<TModel, object>>, Operator, List<TCriterion>)
Adds a NOT filter to the current query args. Allows queries like:
await client.Table<User>().Not("status", Operators.In, new List<string> {"AWAY", "OFFLINE"}).Get();
public IPostgrestTable<TModel> Not<TCriterion>(Expression<Func<TModel, object>> predicate, Constants.Operator op, List<TCriterion> criteria)
Parameters
predicate
Expression<Func<TModel, object>>Expects a column from the model to be returned.
op
Constants.Operatorcriteria
List<TCriterion>
Returns
- IPostgrest
Table <TModel>
Type Parameters
TCriterion
Not<TCriterion>(Expression<Func<TModel, object>>, Operator, TCriterion?)
Adds a NOT filter to the current query args.
Allows queries like:
await client.Table<User>().Not("status", Operators.Equal, "OFFLINE").Get();
public IPostgrestTable<TModel> Not<TCriterion>(Expression<Func<TModel, object>> predicate, Constants.Operator op, TCriterion? criterion)
Parameters
predicate
Expression<Func<TModel, object>>Expects a column from the model to be returned.
op
Constants.Operatorcriterion
TCriterion
Returns
- IPostgrest
Table <TModel>
Type Parameters
TCriterion
Not<TCriterion>(string, Operator, List<TCriterion>)
Adds a NOT filter to the current query args. Allows queries like:
await client.Table<User>().Not("status", Operators.In, new List<string> {"AWAY", "OFFLINE"}).Get();
public IPostgrestTable<TModel> Not<TCriterion>(string columnName, Constants.Operator op, List<TCriterion> criteria)
Parameters
Returns
- IPostgrest
Table <TModel>
Type Parameters
TCriterion
Not<TCriterion>(string, Operator, TCriterion?)
Adds a NOT filter to the current query args.
Allows queries like:
await client.Table<User>().Not("status", Operators.Equal, "OFFLINE").Get();
public IPostgrestTable<TModel> Not<TCriterion>(string columnName, Constants.Operator op, TCriterion? criterion)
Parameters
Returns
- IPostgrest
Table <TModel>
Type Parameters
TCriterion
Offset(int, string?)
Sets an offset with an optional foreign table reference.
Parameters
Returns
- IPostgrest
Table <TModel>
OnConflict(Expression<Func<TModel, object>>)
Set an onConflict query parameter for UPSERTing on a column that has a UNIQUE constraint using a linq predicate.
Parameters
predicate
Expression<Func<TModel, object>>Expects a column from the model to be returned.
Returns
- IPostgrest
Table <TModel>
OnConflict(string)
By specifying the onConflict query parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint.
Parameters
columnName
string
Returns
- IPostgrest
Table <TModel>
Or(List<IPostgrestQueryFilter>)
Adds a OR Filter to the current query args.
Parameters
filters
List<IPostgrestQuery >Filter
Returns
- IPostgrest
Table <TModel>
Order(Expression<Func<TModel, object>>, Ordering, NullPosition)
Adds an ordering to the current query args using a predicate function.
NOTE: If multiple orderings are required, chain this function with another call to
public IPostgrestTable<TModel> Order(Expression<Func<TModel, object>> predicate, Constants.Ordering ordering, Constants.NullPosition nullPosition = NullPosition.First)
Parameters
predicate
Expression<Func<TModel, object>>ordering
Constants.OrderingExpects a columns from the Model to be returned
nullPosition
Constants.NullPosition
Returns
- IPostgrest
Table <TModel>
Order(string, Ordering, NullPosition)
Adds an ordering to the current query args.
NOTE: If multiple orderings are required, chain this function with another call to
public IPostgrestTable<TModel> Order(string column, Constants.Ordering ordering, Constants.NullPosition nullPosition = NullPosition.First)
Parameters
column
stringColumn Name
ordering
Constants.OrderingnullPosition
Constants.NullPosition
Returns
- IPostgrest
Table <TModel>
Order(string, string, Ordering, NullPosition)
Adds an ordering to the current query args.
NOTE: If multiple orderings are required, chain this function with another call to
public IPostgrestTable<TModel> Order(string foreignTable, string column, Constants.Ordering ordering, Constants.NullPosition nullPosition = NullPosition.First)
Parameters
Returns
- IPostgrest
Table <TModel>
Range(int)
Sets a FROM range, similar to a StartAt
query.
Parameters
from
int
Returns
- IPostgrest
Table <TModel>
Range(int, int)
Sets a bounded range to the current query.
Parameters
Returns
- IPostgrest
Table <TModel>
Select(Expression<Func<TModel, object[]>>)
Select columns using a predicate function.
For example:
Table<Movie>().Select(x => new[] { x.Id, x.Name, x.CreatedAt }).Get();
Parameters
predicate
Expression<Func<TModel, object[]>>Expects an array of columns from the Model to be returned.
Returns
- IPostgrest
Table <TModel>
Select(string)
Select columns for query.
Parameters
columnQuery
string
Returns
- IPostgrest
Table <TModel>
Set(Expression<Func<TModel, KeyValuePair<object, object?>>>)
Specifies a KeyValuePair to be updated. Should be combined with filters/where clauses.
Can be called multiple times to set multiple values.
public IPostgrestTable<TModel> Set(Expression<Func<TModel, KeyValuePair<object, object?>>> keyValuePairExpression)
Parameters
keyValuePairExpression
Expression<Func<TModel, KeyValue <object, object>>>Pair
Returns
- IPostgrest
Table <TModel>
Exceptions
Set(Expression<Func<TModel, object>>, object?)
Specifies a key and value to be updated. Should be combined with filters/where clauses.
Can be called multiple times to set multiple values.
Parameters
keySelector
Expression<Func<TModel, object>>value
object
Returns
- IPostgrest
Table <TModel>
Single(CancellationToken)
Executes a query that expects to have a single object returned, rather than returning list of models it will return a single model.
Parameters
cancellationToken
CancellationToken
Returns
- Task<TModel>
Update(QueryOptions?, CancellationToken)
Calls an Update function after Set
has been called.
public Task<ModeledResponse<TModel>> Update(QueryOptions? options = null, CancellationToken cancellationToken = default)
Parameters
options
QueryOptions cancellationToken
CancellationToken
Returns
- Task<Modeled
Response <TModel>>
Exceptions
Update(TModel, QueryOptions?, CancellationToken)
Executes an UPDATE query using the defined query params on the current instance.
public Task<ModeledResponse<TModel>> Update(TModel model, QueryOptions? options = null, CancellationToken cancellationToken = default)
Parameters
model
TModeloptions
QueryOptions cancellationToken
CancellationToken
Returns
- Task<Modeled
Response <TModel>> A typed response from the database.
Upsert(ICollection<TModel>, QueryOptions?, CancellationToken)
Executes an UPSERT query using the defined query params on the current instance.
By default the new record is returned. Set QueryOptions.ReturnType to Minimal if you don't need this value. By specifying the QueryOptions.OnConflict parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint. QueryOptions.DuplicateResolution.IgnoreDuplicates Specifies if duplicate rows should be ignored and not inserted.
public Task<ModeledResponse<TModel>> Upsert(ICollection<TModel> model, QueryOptions? options = null, CancellationToken cancellationToken = default)
Parameters
model
ICollection<TModel>options
QueryOptions cancellationToken
CancellationToken
Returns
- Task<Modeled
Response <TModel>>
Upsert(TModel, QueryOptions?, CancellationToken)
Executes an UPSERT query using the defined query params on the current instance.
By default the new record is returned. Set QueryOptions.ReturnType to Minimal if you don't need this value. By specifying the QueryOptions.OnConflict parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint. QueryOptions.DuplicateResolution.IgnoreDuplicates Specifies if duplicate rows should be ignored and not inserted.
public Task<ModeledResponse<TModel>> Upsert(TModel model, QueryOptions? options = null, CancellationToken cancellationToken = default)
Parameters
model
TModeloptions
QueryOptions cancellationToken
CancellationToken
Returns
- Task<Modeled
Response <TModel>>
Where(Expression<Func<TModel, bool>>)
Filter a query based on a predicate function.
Note: Chaining multiple Where(Expression<Func<TModel, bool>>) calls will be parsed as an "AND" query.
Examples:
Table<Movie>().Where(x => x.Name == "Top Gun").Get();
Table<Movie>().Where(x => x.Name == "Top Gun" || x.Name == "Mad Max").Get();
Table<Movie>().Where(x => x.Name.Contains("Gun")).Get();
Table<Movie>().Where(x => x.CreatedAt <= new DateTime(2022, 08, 21)).Get();
Table<Movie>().Where(x => x.Id > 5 && x.Name.Contains("Max")).Get();
Parameters
predicate
Expression<Func<TModel, bool>>
Returns
- IPostgrest
Table <TModel>