Skip to content

Commit 96415ea

Browse files
committed
docs: improve README organization
Signed-off-by: Miroslav Bajtoš <[email protected]>
1 parent 74ff2fe commit 96415ea

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

README.md

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const config = {
4545

4646
<details><summary markdown="span"><strong>For LoopBack 3 users</strong></summary>
4747

48-
Use the [Data source generator](http://loopback.io/doc/en/lb3/Data-source-generator.html) to add a PostgreSQL data source to your application.
48+
Use the [Data source generator](http://loopback.io/doc/en/lb3/Data-source-generator.html) to add a PostgreSQL data source to your application.
4949
The generator will prompt for the database server hostname, port, and other settings
5050
required to connect to a PostgreSQL database. It will also run the `npm install` command above for you.
5151

@@ -96,7 +96,7 @@ const config = {
9696

9797
Check out [node-pg-pool](https://github.com/brianc/node-pg-pool) and [node postgres pooling example](https://github.com/brianc/node-postgres#pooling-example) for more information.
9898

99-
### Properties
99+
### Configuration options
100100

101101
<table>
102102
<thead>
@@ -106,7 +106,7 @@ Check out [node-pg-pool](https://github.com/brianc/node-pg-pool) and [node postg
106106
<th>Description</th>
107107
</tr>
108108
</thead>
109-
<tbody>
109+
<tbody>
110110
<tr>
111111
<td>connector</td>
112112
<td>String</td>
@@ -176,6 +176,14 @@ Check out [node-pg-pool](https://github.com/brianc/node-pg-pool) and [node postg
176176
<td>Boolean/String</td>
177177
<td>Set to <code>false</code> to disable default sorting on <code>id</code> column(s). Set to <code>numericIdOnly</code> to only apply to IDs with a number type <code>id</code>.</td>
178178
</tr>
179+
<tr>
180+
<td>allowExtendedOperators</td>
181+
<td>Boolean</td>
182+
<td>Set to <code>true</code> to enable PostgreSQL-specific operators
183+
such as <code>contains</code>. Learn more in
184+
<a href="#extended-operators">Extended operators</a> below.
185+
</td>
186+
</tr>
179187
</tbody>
180188
</table>
181189

@@ -202,29 +210,6 @@ const config = {
202210
};
203211
```
204212

205-
206-
## Additional properties
207-
208-
<table>
209-
<thead>
210-
<tr>
211-
<th width="160">Property</th>
212-
<th width="90">Type</th>
213-
<th>Default</th>
214-
<th>Description</th>
215-
</tr>
216-
</thead>
217-
<tbody>
218-
<tr>
219-
<td>allowExtendedOperators</td>
220-
<td>Boolean</td>
221-
<td><code>false</code></td>
222-
<td>Set to <code>true</code> to enable using Postgres operators such as <code>contains</code> which is used to perform filter on postgres array type field.</td>
223-
</tr>
224-
</tbody>
225-
</table>
226-
227-
228213
## Defining models
229214

230215
LoopBack allows you to specify some database settings through the model definition and/or the property definition. These definitions would be mapped to the database. Please check out the CLI [`lb4 model`](https://loopback.io/doc/en/lb4/Model-generator.html) for generating LB4 models. The following is a typical LoopBack 4 model that specifies the schema, table and column details through model definition and property definitions:
@@ -295,7 +280,7 @@ The model definition consists of the following properties.
295280
<th>Description</th>
296281
</tr>
297282
</thead>
298-
<tbody>
283+
<tbody>
299284
<tr>
300285
<td>name</td>
301286
<td>Camel-case of the database table name</td>
@@ -560,43 +545,65 @@ CustomerRepository.find({
560545
});
561546
```
562547
563-
## Querying Postgres Array type fields
548+
## Extended operators
549+
550+
PostgreSQL supports the following PostgreSQL-specific operators:
551+
552+
- [`contains`](#operator-contains)
553+
554+
Please note extended operators are disabled by default, you must enable
555+
them at datasource level or model level by setting `allowExtendedOperators` to
556+
`true`.
557+
558+
### Operator `contains`
564559
565-
**Note** The fields you are querying should be setup to use the postgresql array data type - see Defining models
560+
The `contains` operator allow you to query array properties and pick only
561+
rows where the stored value contains all of the items specified by the query.
562+
563+
The operator is implemented using PostgreSQL [array operator
564+
`@>`](https://www.postgresql.org/docs/current/functions-array.html).
565+
566+
**Note** The fields you are querying must be setup to use the postgresql array data type - see [Defining models](#defining-models) above.
566567
567568
Assuming a model such as this:
568569
569570
```ts
571+
@model({
572+
settings: {
573+
allowExtendedOperators: true,
574+
}
575+
})
576+
class Post {
570577
@property({
571578
type: ['string'],
572579
postgresql: {
573-
dataType: 'varchar[]',
574-
},
580+
dataType: 'varchar[]',
581+
},
575582
})
576583
categories?: string[];
584+
}
577585
```
578586
579-
You can query the tags fields via setting allowExtendedOperators to true
587+
You can query the tags fields as follows:
580588
581589
```ts
582-
Model.dataSource.settings.allowExtendedOperators = true;
583-
const post = await Post.find({where: {
584-
{
585-
categories: {'contains': ['AA']},
586-
}
587-
}
588-
});
590+
const posts = await postRepository.find({
591+
where: {
592+
{
593+
categories: {'contains': ['AA']},
594+
}
595+
}
596+
});
589597
```
590598
591-
592599
## Discovery and auto-migration
593600
594601
### Model discovery
595602
596603
The PostgreSQL connector supports _model discovery_ that enables you to create LoopBack models
597604
based on an existing database schema. Once you defined your datasource:
598-
- LoopBack 4 users could use the commend [`lb4 discover`](https://loopback.io/doc/en/lb4/Discovering-models.html) to discover models.
599-
- For LB3 users, please check [Discovering models from relational databases](https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html).
605+
- LoopBack 4 users could use the commend [`lb4 discover`](https://loopback.io/doc/en/lb4/Discovering-models.html) to discover models.
606+
- For LB3 users, please check [Discovering models from relational databases](https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html).
600607
601608
(See [database discovery API](http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-discoverandbuildmodels) for related APIs information)
602609
@@ -670,7 +677,7 @@ Here are some limitations and tips:
670677
671678
### Auto-migrate/Auto-update models with foreign keys
672679
673-
Foreign key constraints can be defined in the model definition.
680+
Foreign key constraints can be defined in the model definition.
674681
675682
**Note**: The order of table creation is important. A referenced table must exist before creating a foreign key constraint.
676683

0 commit comments

Comments
 (0)