小编Sla*_*waj的帖子

ndb按ID检索实体密钥而没有父项

我想获得一个知道实体ID和祖先的实体密钥.ID在祖先定义的实体组中是唯一的.在我看来,使用ndb接口是不可能的.据我所知,数据存储可能是由于此操作需要执行完整索引扫描.我使用的解决方法是在模型中创建一个计算属性,它将包含键的id部分.我现在可以做一个祖先查询并获取密钥

class SomeModel(ndb.Model):
    ID = ndb.ComputedProperty( lambda self: self.key.id() )

    @classmethod
    def id_to_key(cls, identifier, ancestor):
        return cls.query(cls.ID == identifier,
                         ancestor = ancestor.key ).get( keys_only = True)
Run Code Online (Sandbox Code Playgroud)

它似乎有效,但有没有更好的解决方案来解决这个问题?

更新 似乎对于数据存储,自然的解决方案是使用完整路径而不是标识符.最初我认为这太麻烦了.阅读dragonx后,我重新设计了我的应用程序.令我惊讶的是,现在看起来一切都变得简单了.额外的好处是我的实体将使用更少的空间,我不需要额外的索引.

google-app-engine app-engine-ndb

7
推荐指数
1
解决办法
4799
查看次数

Meteor._wrapAsync和节点子进程

我尝试从Meteor环境中的child_process命令获取结果.似乎child_process中有一些特殊的东西,我不明白.这是我用于测试的代码

Meteor.startup(function () {
    exec = Npm.require('child_process').exec;
});

function bind_environment_callback(error) {
    console.log('Error binding environment for a callback', error.stack);
}

function get_git_commit_hash(cb) {
    exec( 
        'git rev-parse HEAD',
        Meteor.bindEnvironment(
            function(error, stdout, stderr) {
                if (error) {
                    cb('Error retrieving commit hash', null);
                } else {
                    console.log("Inside get_git_commit_hash:" + stdout.slice(0,stdout-1).toString());
                    cb(null, stdout.slice(0,stdout-1).toString());
                }
            },
            bind_environment_callback
        )
    );
}

function dummy(cb){
    setTimeout(function(){
        cb(null, 'Dummy result');
    },
    100);
}

Meteor.methods({
    test: function() {
        var get_git_commit_hash_sync = Meteor._wrapAsync(get_git_commit_hash);
        var result= get_git_commit_hash_sync();
        console.log('Call 1:' + result);

        var dummy_sync …
Run Code Online (Sandbox Code Playgroud)

javascript asynccallback meteor

1
推荐指数
1
解决办法
1045
查看次数