【问题标题】:How do I create a schema with google datastore (in javascript)?如何使用 google 数据存储(在 javascript 中)创建架构?
【发布时间】:2016-11-20 16:41:30
【问题描述】:

我正在编写一个使用 Google Datastore 的 Nodejs 应用程序。我只想知道如何使用谷歌数据存储为身份验证过程设置架构。基本上,我如何使用 Google Datastore 执行 下面的代码

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 Datastore,如果这个问题看起来很基础,我很抱歉。

【问题讨论】:

    标签: node.js google-app-engine express google-cloud-datastore nosql


    【解决方案1】:

    您使用 gstore-node 库的示例与使用 Mongoose 的示例几乎相同:

    // 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);
    

    【讨论】:

      【解决方案2】:

      看看这个库,我刚刚开始自己​​,它是迄今为止我发现的最接近的:https://github.com/sebelga/gstore-node

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-15
        • 2016-11-08
        • 1970-01-01
        • 2020-02-11
        • 2014-10-24
        • 2010-12-15
        相关资源
        最近更新 更多