Skip to content

Commit 3f27d4c

Browse files
authored
refactor: Change gstore instantiation to be consistent with es modules (#149)
BREAKING CHANGE: The new way to create gstore instances is with "new Gstore(<config>)". Refer to the documentation.
1 parent e4cfaa6 commit 3f27d4c

17 files changed

+867
-876
lines changed

README.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,34 @@ It is not a replacement of @google-cloud/datastore but a layer on top of it to h
2727
- pre & post **middleware** (hooks)
2828
- **custom methods** on entity instances
2929
- **[Joi](https://github.com/hapijs/joi)** schema definition and validation
30-
- **NEW** Advanced **[cache layer](https://sebloix.gitbook.io/gstore-node/cache-dataloader/cache)** (since v3.0.0)
31-
- :tada: **NEW** **[Typescript support](https://sebloix.gitbook.io/gstore-node/typescript)** (since v4.1.0)
30+
- Advanced **[cache layer](https://sebloix.gitbook.io/gstore-node/cache-dataloader/cache)**
31+
- **[Typescript support](https://sebloix.gitbook.io/gstore-node/typescript)**
32+
- :tada: **NEW** [**populate()**](https://sebloix.gitbook.io/gstore-node/populate) support to fetch reference entities and do cross Entity Type "joins" when querying one or multiple entities (since v5.0.0)
3233

3334
This library is in active development, please report any issue you might find.
3435

3536
> Please don’t forget to star this repo if you found it useful :)
3637
3738
# Installation
3839

39-
```js
40+
```shell
4041
npm install gstore-node --save
42+
# or
43+
yarn add gstore-node
4144
```
4245

43-
Info: gstore-node requires Node version **6+**
46+
**Important**: gstore-node requires Node version **8+**
4447

4548
# Getting started
4649

4750
Import gstore-node and @google-cloud/datastore and configure your project.
48-
For the information on how to configure @google-cloud/datastore [read the docs here](https://cloud.google.com/nodejs/docs/reference/datastore/2.0.x/Datastore).
51+
For the information on how to configure @google-cloud/datastore [read the docs here](https://cloud.google.com/nodejs/docs/reference/datastore/3.0.x/Datastore).
4952

5053
```js
51-
const gstore = require('gstore-node')();
52-
const Datastore = require('@google-cloud/datastore');
54+
const { Gstore } = require('gstore-node');
55+
const { Datastore } = require('@google-cloud/datastore');
5356

57+
const gstore = new Gstore();
5458
const datastore = new Datastore({
5559
projectId: 'my-google-project-id',
5660
});
@@ -70,7 +74,7 @@ The @google/datastore instance. This means that you can access **all the API** o
7074

7175
# Documentation
7276
The [complete documentation](https://sebloix.gitbook.io/gstore-node/) of gstore-node is in gitbook.
73-
If you find any mistake or would like to improve it, [feel free to open a PR](https://github.com/sebelga/gstore-node-docs/pulls).
77+
If you find any mistake in the docs or would like to improve it, [feel free to open a PR](https://github.com/sebelga/gstore-node-docs/pulls).
7478

7579
<a name="example"/>
7680

@@ -80,24 +84,30 @@ Initialize gstore-node in your server file
8084
```js
8185
// server.js
8286

83-
const gstore = require('gstore-node')();
84-
const Datastore = require('@google-cloud/datastore');
87+
const { Gstore, instances } = require('gstore-node');
88+
const { Datastore } = require('@google-cloud/datastore');
8589

90+
const gstore = new Gstore();
8691
const datastore = new Datastore({
8792
projectId: 'my-google-project-id',
8893
});
8994

9095
gstore.connect(datastore);
96+
97+
// Save the gstore instance
98+
instances.set('unique-id', gstore);
9199
```
92100

93101
Create your Model
94102

95103
```js
96104
// user.model.js
97105

98-
const gstore = require('gstore-node')();
106+
const { instances } = require('gstore-node');
99107
const bscrypt = require('bcrypt-nodejs');
100108

109+
// Retrieve the gstore instance
110+
const gstore = instances.get('unique-id');
101111
const { Schema } = gstore;
102112

103113
/**
@@ -125,6 +135,7 @@ const userSchema = new Schema({
125135
email: { type: String, validate: 'isEmail', required: true },
126136
password: { type: String, read: false, required: true },
127137
createdOn: { type: String, default: gstore.defaultValues.NOW, write: false, read: false },
138+
address: { type: Schema.Types.Key, ref: 'Address' }, // Entity reference
128139
dateOfBirth: { type: Date },
129140
bio: { type: String, excludeFromIndexes: true },
130141
website: { validate: 'isURL', optional: true },
@@ -231,6 +242,7 @@ const getUsers = (req ,res) => {
231242
const getUser = (req, res) => {
232243
const userId = +req.params.id;
233244
User.get(userId)
245+
.populate('address') // Retrieve the reference entity
234246
.then((entity) => {
235247
res.json(entity.plain());
236248
})

0 commit comments

Comments
 (0)