Skip to content

Commit

Permalink
refactor: Change gstore instantiation to be consistent with es modules (
Browse files Browse the repository at this point in the history
#149)


BREAKING CHANGE: The new way to create gstore instances is with "new Gstore(<config>)". Refer to the
documentation.
  • Loading branch information
sebelga committed Feb 4, 2019
1 parent e4cfaa6 commit 3f27d4c
Show file tree
Hide file tree
Showing 17 changed files with 867 additions and 876 deletions.
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,34 @@ It is not a replacement of @google-cloud/datastore but a layer on top of it to h
- pre & post **middleware** (hooks)
- **custom methods** on entity instances
- **[Joi](https://github.com/hapijs/joi)** schema definition and validation
- **NEW** Advanced **[cache layer](https://sebloix.gitbook.io/gstore-node/cache-dataloader/cache)** (since v3.0.0)
- :tada: **NEW** **[Typescript support](https://sebloix.gitbook.io/gstore-node/typescript)** (since v4.1.0)
- Advanced **[cache layer](https://sebloix.gitbook.io/gstore-node/cache-dataloader/cache)**
- **[Typescript support](https://sebloix.gitbook.io/gstore-node/typescript)**
- :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)

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

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

```js
```shell
npm install gstore-node --save
# or
yarn add gstore-node
```

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

# Getting started

Import gstore-node and @google-cloud/datastore and configure your project.
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).
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).

```js
const gstore = require('gstore-node')();
const Datastore = require('@google-cloud/datastore');
const { Gstore } = require('gstore-node');
const { Datastore } = require('@google-cloud/datastore');

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

# Documentation
The [complete documentation](https://sebloix.gitbook.io/gstore-node/) of gstore-node is in gitbook.
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).
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).

<a name="example"/>

Expand All @@ -80,24 +84,30 @@ Initialize gstore-node in your server file
```js
// server.js

const gstore = require('gstore-node')();
const Datastore = require('@google-cloud/datastore');
const { Gstore, instances } = require('gstore-node');
const { Datastore } = require('@google-cloud/datastore');

const gstore = new Gstore();
const datastore = new Datastore({
projectId: 'my-google-project-id',
});

gstore.connect(datastore);

// Save the gstore instance
instances.set('unique-id', gstore);
```

Create your Model

```js
// user.model.js

const gstore = require('gstore-node')();
const { instances } = require('gstore-node');
const bscrypt = require('bcrypt-nodejs');

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

/**
Expand Down Expand Up @@ -125,6 +135,7 @@ const userSchema = new Schema({
email: { type: String, validate: 'isEmail', required: true },
password: { type: String, read: false, required: true },
createdOn: { type: String, default: gstore.defaultValues.NOW, write: false, read: false },
address: { type: Schema.Types.Key, ref: 'Address' }, // Entity reference
dateOfBirth: { type: Date },
bio: { type: String, excludeFromIndexes: true },
website: { validate: 'isURL', optional: true },
Expand Down Expand Up @@ -231,6 +242,7 @@ const getUsers = (req ,res) => {
const getUser = (req, res) => {
const userId = +req.params.id;
User.get(userId)
.populate('address') // Retrieve the reference entity
.then((entity) => {
res.json(entity.plain());
})
Expand Down
Loading

0 comments on commit 3f27d4c

Please sign in to comment.