You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+27-11Lines changed: 27 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,11 +24,12 @@ If you create a PostgreSQL data source using the data source generator as descri
24
24
25
25
Use the [Data source generator](http://loopback.io/doc/en/lb3/Data-source-generator.html) to add a PostgreSQL data source to your application.
26
26
The generator will prompt for the database server hostname, port, and other settings
27
-
required to connect to a PostgreSQL database. It will also run the `npm install` command above for you.
27
+
required to connect to a PostgreSQL database. It will also run the `npm install` command above for you.
28
28
29
29
The entry in the application's `/server/datasources.json` will look like this:
30
30
31
31
{% include code-caption.html content="/server/datasources.json" %}
32
+
32
33
```javascript
33
34
"mydb": {
34
35
"name":"mydb",
@@ -50,6 +51,7 @@ Edit `datasources.json` to add other properties that enable you to connect the
50
51
You can also specify connection pool settings in `datasources.json`. For instance you can specify the minimum and the maximum pool size, and the maximum pool client's idle time before closing the client.
51
52
52
53
Example of `datasource.json`:
54
+
53
55
```
54
56
{
55
57
"mypostgresdb": {
@@ -68,6 +70,7 @@ Example of `datasource.json`:
68
70
}
69
71
}
70
72
```
73
+
71
74
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.
72
75
73
76
### Properties
@@ -210,6 +213,7 @@ The model definition consists of the following properties.
210
213
For example:
211
214
212
215
{% include code-caption.html content="/common/models/model.json" %}
216
+
213
217
```javascript
214
218
{
215
219
"name":"Inventory",
@@ -404,18 +408,18 @@ You can query the nested fields with dot notation:
404
408
```javascript
405
409
Customer.find({
406
410
where: {
407
-
'address.state':'California'
411
+
"address.state":"California"
408
412
},
409
-
order:'address.city'
410
-
})
413
+
order:"address.city"
414
+
});
411
415
```
412
416
413
417
## Discovery and auto-migration
414
418
415
419
### Model discovery
416
420
417
421
The PostgreSQL connector supports _model discovery_ that enables you to create LoopBack models
418
-
based on an existing database schema using the unified [database discovery API](http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-discoverandbuildmodels). For more information on discovery, see [Discovering models from relational databases](https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html).
422
+
based on an existing database schema using the unified [database discovery API](http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-discoverandbuildmodels). For more information on discovery, see [Discovering models from relational databases](https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html).
419
423
420
424
### Auto-migration
421
425
@@ -429,8 +433,8 @@ model: a table, for example, PRODUCT under the 'public' schema within the databa
429
433
430
434
The auto-migrate method:
431
435
432
-
* Defines a primary key for the properties whose `id` property is true (or a positive number).
433
-
* Creates a column with 'SERIAL' type if the `generated` property of the `id` property is true.
436
+
- Defines a primary key for the properties whose `id` property is true (or a positive number).
437
+
- Creates a column with 'SERIAL' type if the `generated` property of the `id` property is true.
434
438
435
439
Destroying models may result in errors due to foreign key integrity. First delete any related models by calling delete on models with relationships.
436
440
@@ -440,7 +444,7 @@ Foreign key constraints can be defined in the model `options`. Removing or updat
440
444
441
445
If there is a reference to an object being deleted then the `DELETE` will fail. Likewise if there is a create with an invalid FK id then the `POST` will fail.
442
446
443
-
**Note**: The order of table creation is important. A referenced table must exist before creating a foreign key constraint.
447
+
**Note**: The order of table creation is important. A referenced table must exist before creating a foreign key constraint.
444
448
445
449
For **LoopBack 4** users, define your models under the `models/` folder as follows:
446
450
@@ -464,6 +468,7 @@ export class Customer extends Entity {
464
468
name:string;
465
469
}
466
470
```
471
+
467
472
`order.model.ts`:
468
473
469
474
```ts
@@ -494,7 +499,7 @@ export class Order extends Entity {
494
499
For **LoopBack 3** users, you can define your model JSON schema as follows:
495
500
496
501
```json
497
-
{
502
+
({
498
503
"name": "Customer",
499
504
"options": {
500
505
"idInjection": false
@@ -512,7 +517,6 @@ For **LoopBack 3** users, you can define your model JSON schema as follows:
512
517
}
513
518
}
514
519
},
515
-
516
520
{
517
521
"name": "Order",
518
522
"options": {
@@ -542,7 +546,7 @@ For **LoopBack 3** users, you can define your model JSON schema as follows:
542
546
"length": 40
543
547
}
544
548
}
545
-
}
549
+
})
546
550
```
547
551
548
552
Auto-migrate supports the automatic generation of property values. For PostgreSQL, the default id type is _integer_. If you have `generated: true` in the id property, it generates integers by default:
@@ -571,6 +575,7 @@ It is common to use UUIDs as the primary key in PostgreSQL instead of integers.
571
575
},
572
576
}
573
577
```
578
+
574
579
The setting uses `uuid-ossp` extension and `uuid_generate_v4()` function as default.
575
580
576
581
If you'd like to use other extensions and functions, you can do:
@@ -596,24 +601,35 @@ WARNING: It is the users' responsibility to make sure the provided extension and
596
601
## Running tests
597
602
598
603
### Own instance
604
+
599
605
If you have a local or remote PostgreSQL instance and would like to use that to run the test suite, use the following command:
606
+
600
607
- Linux
608
+
601
609
```bash
602
610
POSTGRESQL_HOST=<HOST> POSTGRESQL_PORT=<PORT> POSTGRESQL_USER=<USER> POSTGRESQL_PASSWORD=<PASSWORD> POSTGRESQL_DATABASE=<DATABASE> CI=true npm test
603
611
```
612
+
604
613
- Windows
614
+
605
615
```bash
606
616
SET POSTGRESQL_HOST=<HOST> SET POSTGRESQL_PORT=<PORT> SET POSTGRESQL_USER=<USER> SET POSTGRESQL_PASSWORD=<PASSWORD> SET POSTGRESQL_DATABASE=<DATABASE> SET CI=true npm test
607
617
```
608
618
609
619
### Docker
620
+
610
621
If you do not have a local PostgreSQL instance, you can also run the test suite with very minimal requirements.
622
+
611
623
- Assuming you have [Docker](https://docs.docker.com/engine/installation/) installed, run the following script which would spawn a PostgreSQL instance on your local:
where `<HOST>`, `<PORT>`, `<USER>`, `<PASSWORD>` and `<DATABASE>` are optional parameters. The default values are `localhost`, `5432`, `root`, `pass` and `testdb` respectively.
0 commit comments