1

Google Datastoreを使用するNodejsアプリケーションを作成しています。私はちょうど私が認証プロセスのためのGoogleのデータストアを持つスキーマをどのように設定できるか知りたい。基本的に、どのように私はを行うことができますGoogleのデータストアと以下のコード:Googleのデータストアを使用してスキーマを作成するにはどうすればよいですか?

var mongoose = require('mongoose'); 
var bcrypt = require('bcrypt-nodejs'); 

var userSchema = mongoose.Schema({ 

local   : { 
    email  : String, 
    password  : String, 
} 

}); 

// generating a hash 
userSchema.methods.generateHash = function(password) { 
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); 
}; 

// checking if password is valid 
userSchema.methods.validPassword = function(password) { 
return bcrypt.compareSync(password, this.local.password); 
}; 

// create the model for users and expose it to our app 
module.exports = mongoose.model('User', userSchema); 

私はちょうどNodejsとGoogleのデータストアを使用することを始めていますので、この質問は非常に基本的なようであれば、私は申し訳ありません。

答えて

2

私はちょうど自分自身を始めていますし、それは私がこれまでに見つけた最も近いですが、このライブラリをチェックアウト:gstoreノードライブラリとhttps://github.com/sebelga/gstore-node

4

あなたの例では、マングースとほぼ同じになります:

// The connection should probably done in your server.js start script 
// ------------------------------------------------------------------ 
var ds = require('@google-cloud/datastore') 
var gstore = require('gstore-node'); 
gstore.connect(ds); 

// In your user.model.js file 
// -------------------------- 
var gstore = require('gstore-node'); 
var bcrypt = require('bcrypt-nodejs'); 

var Schema = gstore.Schema; 
var userSchema = new Schema({ 
    email : {type: 'string', validate:'isEmail'}, 
    password : {type: 'string'}, 
}); 

// custom methods (https://github.com/sebelga/gstore-node#custom-methods) 
// generating a hash 
userSchema.methods.texts = function(password) { 
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); 
}; 

// checking if password is valid 
userSchema.methods.validPassword = function(password) { 
    return bcrypt.compareSync(password, this.get('password')); 
}; 

// create the model for users and expose it to our app 
module.exports = gstore.model('User', userSchema); 
関連する問題