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
-
IPostgrestTable<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
JsonSerializerSettingsoptions
ClientOptionsOptional client configuration.
Properties
BaseUrl
API Base Url for subsequent calls.
public string BaseUrl { get; }
Property Value
GetHeaders
An executable Func
that returns a dictionary of headers to be appended onto a request.
public Func<Dictionary<string, string>>? GetHeaders { get; set; }
Property Value
TableName
Name of the Table parsed by the Model.
public string TableName { get; }
Property Value
Methods
And(List<IPostgrestQueryFilter>)
Adds an AND Filter to the current query args.
public IPostgrestTable<TModel> And(List<IPostgrestQueryFilter> filters)
Parameters
filters
List<IPostgrestQueryFilter>
Returns
- IPostgrestTable<TModel>
Clear()
Clears currently defined query values.
public void Clear()
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
public IPostgrestTable<TModel> Columns(Expression<Func<TModel, object[]>> predicate)
Parameters
predicate
Expression<Func<TModel, object[]>>
Returns
- IPostgrestTable<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
public IPostgrestTable<TModel> Columns(string[] columns)
Parameters
columns
string[]
Returns
- IPostgrestTable<TModel>
Count(CountType, CancellationToken)
Returns ONLY a count from the specified query.
See: https://postgrest.org/en/v7.0.0/api.html?highlight=count
public Task<int> Count(Constants.CountType type, CancellationToken cancellationToken = default)
Parameters
type
Constants.CountTypeThe kind of count.
cancellationToken
CancellationToken
Returns
Delete(QueryOptions?, CancellationToken)
Executes a delete request using the defined query params on the current instance.
public Task Delete(QueryOptions? options = null, CancellationToken cancellationToken = default)
Parameters
options
QueryOptionscancellationToken
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
QueryOptionscancellationToken
CancellationToken
Returns
- Task<ModeledResponse<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
- IPostgrestTable<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
- IPostgrestTable<TModel>
Type Parameters
TCriterion
GenerateUrl()
Generates the encoded URL with defined query parameters that will be sent to the Postgrest API.
public string GenerateUrl()
Returns
Get(CancellationToken)
Executes the query using the defined filters on the current instance.
public Task<ModeledResponse<TModel>> Get(CancellationToken cancellationToken = default)
Parameters
cancellationToken
CancellationToken
Returns
- Task<ModeledResponse<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
QueryOptionscancellationToken
CancellationToken
Returns
- Task<ModeledResponse<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
QueryOptionscancellationToken
CancellationToken
Returns
- Task<ModeledResponse<TModel>>
A typed model response from the database.
Limit(int, string?)
Sets a limit with an optional foreign table reference.
public IPostgrestTable<TModel> Limit(int limit, string? foreignTableName = null)
Parameters
Returns
- IPostgrestTable<TModel>
Match(Dictionary<string, string>)
Finds all rows whose columns match the specified query
object.
public IPostgrestTable<TModel> Match(Dictionary<string, string> query)
Parameters
query
Dictionary<string, string>The object to filter with, with column names as keys mapped to their filter values.
Returns
- IPostgrestTable<TModel>
Match(TModel)
Fills in query parameters based on a given model's primary key(s).
public IPostgrestTable<TModel> Match(TModel model)
Parameters
model
TModelA model with a primary key column
Returns
- IPostgrestTable<TModel>
Not(IPostgrestQueryFilter)
Adds a NOT filter to the current query args.
public IPostgrestTable<TModel> Not(IPostgrestQueryFilter filter)
Parameters
filter
IPostgrestQueryFilter
Returns
- IPostgrestTable<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
- IPostgrestTable<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
- IPostgrestTable<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
- IPostgrestTable<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
- IPostgrestTable<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
- IPostgrestTable<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
- IPostgrestTable<TModel>
Type Parameters
TCriterion
Offset(int, string?)
Sets an offset with an optional foreign table reference.
public IPostgrestTable<TModel> Offset(int offset, string? foreignTableName = null)
Parameters
Returns
- IPostgrestTable<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.
public IPostgrestTable<TModel> OnConflict(Expression<Func<TModel, object>> predicate)
Parameters
predicate
Expression<Func<TModel, object>>Expects a column from the model to be returned.
Returns
- IPostgrestTable<TModel>
OnConflict(string)
By specifying the onConflict query parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint.
public IPostgrestTable<TModel> OnConflict(string columnName)
Parameters
columnName
string
Returns
- IPostgrestTable<TModel>
Or(List<IPostgrestQueryFilter>)
Adds a OR Filter to the current query args.
public IPostgrestTable<TModel> Or(List<IPostgrestQueryFilter> filters)
Parameters
filters
List<IPostgrestQueryFilter>
Returns
- IPostgrestTable<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
- IPostgrestTable<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
- IPostgrestTable<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
- IPostgrestTable<TModel>
Range(int)
Sets a FROM range, similar to a StartAt
query.
public IPostgrestTable<TModel> Range(int from)
Parameters
from
int
Returns
- IPostgrestTable<TModel>
Range(int, int)
Sets a bounded range to the current query.
public IPostgrestTable<TModel> Range(int from, int to)
Parameters
Returns
- IPostgrestTable<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();
public IPostgrestTable<TModel> Select(Expression<Func<TModel, object[]>> predicate)
Parameters
predicate
Expression<Func<TModel, object[]>>Expects an array of columns from the Model to be returned.
Returns
- IPostgrestTable<TModel>
Select(string)
Select columns for query.
public IPostgrestTable<TModel> Select(string columnQuery)
Parameters
columnQuery
string
Returns
- IPostgrestTable<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, KeyValuePair<object, object>>>
Returns
- IPostgrestTable<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.
public IPostgrestTable<TModel> Set(Expression<Func<TModel, object>> keySelector, object? value)
Parameters
keySelector
Expression<Func<TModel, object>>value
object
Returns
- IPostgrestTable<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.
public Task<TModel?> Single(CancellationToken cancellationToken = default)
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
QueryOptionscancellationToken
CancellationToken
Returns
- Task<ModeledResponse<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
QueryOptionscancellationToken
CancellationToken
Returns
- Task<ModeledResponse<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
QueryOptionscancellationToken
CancellationToken
Returns
- Task<ModeledResponse<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
QueryOptionscancellationToken
CancellationToken
Returns
- Task<ModeledResponse<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();
public IPostgrestTable<TModel> Where(Expression<Func<TModel, bool>> predicate)
Parameters
predicate
Expression<Func<TModel, bool>>
Returns
- IPostgrestTable<TModel>