Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Google Datastore Entities Modeling for Node.js

License

NotificationsYou must be signed in to change notification settings

sebelga/gstore-node

Repository files navigation

gstore-nodeTweet

Entities modeling for Google's Datastore

NPM VersionBuild Statuscoveralls-imageCommitizen friendly

🎉NEWS: new maintainer!

A few weeks ago I announced thatgstore-node was deprecated as I currently don't have bandwidth to work on it. A few days later,Hendrik Schalekamp stepped in and offered to be a maintainer of the project. I was thrilled! 😊Hendrik will be the lead of the project and I will be around to provide any necessary guidance. Thanks Hendrik!

Documentation |Example |Demo Application |Support |Changelog

gstore-node is a Google Datastore entities modeling library for Node.js inspired by Mongoose and builton top of the@google-cloud/datastore client.
It is not a replacement of @google-cloud/datastore but a layer on top of it to help modeling your entities through Schemas and to help validating the data saved in the Datastore.

Highlight

  • explicitSchema declaration for entities
  • propertiestype validation
  • propertiesvalue validation
  • shortcuts queries
  • pre & postmiddleware (hooks)
  • custom methods on entity instances
  • Joi schema definition and validation
  • Advancedcache layer
  • Typescript support
  • populate() support to fetch reference entities and do cross Entity Type "joins" when querying one or multiple entities (since v5.0.0)

Please don’t forget to star this repo if you find it useful :)

Installation

npm install gstore-node --save# oryarn add gstore-node

Important: gstore-node requires Node version8+

Getting started

Import gstore-node and @google-cloud/datastore and configure your project.
For the information on how to configure @google-cloud/datastoreread the docs here.

const{ Gstore}=require('gstore-node');const{ Datastore}=require('@google-cloud/datastore');constgstore=newGstore();constdatastore=newDatastore({projectId:'my-google-project-id',});// Then connect gstore to the datastore instancegstore.connect(datastore);

After connecting gstore to the datastore, gstore has 2 aliases set up

  • gstore.ds
    The @google/datastore instance. This means that you can accessall the API of the Google library when needed.

  • gstore.transaction. Alias of the same google-cloud/datastore method

Thecomplete documentation of gstore-node is in gitbook.
If you find any mistake in the docs or would like to improve it,feel free to open a PR.

Initialize gstore-node in your server file

// server.jsconst{ Gstore, instances}=require('gstore-node');const{ Datastore}=require('@google-cloud/datastore');constgstore=newGstore();constdatastore=newDatastore({projectId:'my-google-project-id',});gstore.connect(datastore);// Save the gstore instanceinstances.set('unique-id',gstore);

Create your Model

// user.model.jsconst{ instances}=require('gstore-node');constbscrypt=require('bcrypt-nodejs');// Retrieve the gstore instanceconstgstore=instances.get('unique-id');const{ Schema}=gstore;/** * A custom validation function for an embedded entity */constvalidateAccessList=(value,validator)=>{if(!Array.isArray(value)){returnfalse;}returnvalue.some((item)=>{constisValidIp=!validator.isEmpty(item.ip)&&validator.isIP(item.ip,4);constisValidHostname=!validator.isEmpty(item.hostname);returnisValidHostname&&isValidIp;});}/** * Create the schema for the User Model*/constuserSchema=newSchema({firstname:{type:String,required:true},lastname:{type:String,optional:true},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 referencedateOfBirth:{type:Date},bio:{type:String,excludeFromIndexes:true},website:{validate:'isURL',optional:true},ip:{validate:{rule:'isIP',args:[4],}},accessList:{validate:{rule:validateAccessList,}},});// Or with **Joi** schema definition// You need to have joi as a dependency of your project ("npm install joi --save")constuserSchema=newSchema({firstname:{joi:Joi.string().required()},email:{joi:Joi.string().email()},password:{joi:Joi.string()},    ...},{joi:{extra:{// validates that when "email" is present, "password" must be toowhen:['email','password'],},}});/** * List entities query shortcut */constlistSettings={limit:15,order:{property:'lastname'}};userSchema.queries('list',listSettings);/** * Pre "save" middleware * Each time the entity is saved or updated, if there is a password passed, it will be hashed*/functionhashPassword(){// scope *this* is the entity instanceconst_this=this;constpassword=this.password;if(!password){returnPromise.resolve();}returnnewPromise((resolve,reject)=>{bcrypt.genSalt(5,functiononSalt(err,salt){if(err){returnreject(err);};bcrypt.hash(password,salt,null,functiononHash(err,hash){if(err){// reject will *not* save the entityreturnreject(err);};_this.password=hash;// resolve to go to next middleware or save methodreturnresolve();});});});}// add the "pre" middleware to the save methoduserSchema.pre('save',hashPassword);/** * Export the User Model * It will generate "User" entity kind in the Datastore*/module.exports=gstore.model('User',userSchema);

Use it in your Controller

// user.constroller.jsconstUser=require('./user.model');constgetUsers=(req,res)=>{constpageCursor=req.query.cursor;// List users with the Query settings defined on SchemaUser.list({start:pageCursor}).then((entities)=>{res.json(entities);}).catch(err=>res.status(400).json(err));};constgetUser=(req,res)=>{constuserId=+req.params.id;User.get(userId).populate('address')// Retrieve the reference entity.then((entity)=>{res.json(entity.plain());}).catch(err=>res.status(400).json(err));};constcreateUser=(req,res)=>{constentityData=User.sanitize(req.body);constuser=newUser(entityData);user.save().then((entity)=>{res.json(entity.plain());}).catch((err)=>{// If there are any validation error on the schema// they will be in this error objectres.status(400).json(err);})};constupdateUser=(req,res)=>{constuserId=+req.params.id;constentityData=User.sanitize(req.body);// { email: 'john@snow.com'}/**     * This will fetch the entity, merge the data and save it back to the Datastore    */User.update(userId,entityData).then((entity)=>{res.json(entity.plain());}).catch((err)=>{// If there are any validation error on the schema// they will be in this error objectres.status(400).json(err);});};constdeleteUser=(req,res)=>{constuserId=+req.params.id;User.delete(userId).then((response)=>{res.json(response);}).catch(err=>res.status(400).json(err));};module.exports={    getUsers,    getUser,    createUser,    updateUser,    deleteUser};

If you want to see an example on how to use gstore-node in your Node.js app, check thedemo blog application repository.

Development

  1. Install npm dependencies
yarn install
  1. Run the unit tests
yarn test:unit
  1. Run the integration tests

Prerequise:
In order to run the integration tests you need to have theGoogle Datastore Emulator installed as well asRedis.

  • Launch the Redis server
# From the folder where you've installed the Redis SDK run: cd src && ./redis-server
  • Launch the Datastore Emulator (separate terminal window)
# From the root of the projectyarn local-datastore
  • Execute the integration tests (separate terminal window)
# From the root of the projectyarn test:integration

Meta

Sébastien Loix –@sebloix
Hendrik Schalekamp -@carnun

Distributed under the MIT license. SeeLICENSE for more information.

Contributing

  1. Fork it (https://github.com/sebelga/gstore-node/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Generate a conventional commit message (npm run commit)
  4. Push to the branch (git push origin feature/fooBar)
  5. Rebase your feature branch and squash (git rebase -i master)
  6. Create a new Pull Request

Credits

I have been heavily inspired byMongoose to write gstore. Credits to them for the Schema, Model and Entitydefinitions, as well as 'hooks', custom methods and other similarities found here.Not much could neither have been done without the great work of the guys atgoogleapis.


[8]ページ先頭

©2009-2025 Movatter.jp