Memory Database Select Queries v7 - nodeGame/nodegame GitHub Wiki

Querying the Memory Database

Here are a few examples of database queries.

First of all, let's store a convenient reference to the memory database to be used in all examples.

// Store reference.
let memory = node.game.memory;

Select items based on properties

Fetch all items that contain a property called 'value'.

memory.select('value').fetch();

// [
//     {
//         value: 1,
//         player: "A",
//         stage: { stage: 2, step: 1, round : 1 },
//         timestamp: 1477.13729710
//     },
//     {
//         value: 2,
//         player: "A",
//         stage: { stage: 2, step: 1, round : 1 },
//         timestamp: 17.147.33111
//     },
//     {
//         value: 3,
//         player: "B",
//         stage: { stage: 2, step: 1, round : 1 },
//         timestamp: 177.47899812
//     },
// ]

Select items based on properties and values

All items that contain a property named 'foo' equal to 'bar' from player 'A'.

memory
    .select('foo', '=', 'bar')
    .and('player', '=', 'A')
    .fetch();

// [ {
//     foo: "bar",
//     player: "A",
//     stage: { stage: 1, step: 1, round : 1 },
//     timestamp: 1477.13729710
// } ]


// All items with a property 'value' between 0 and 5.
memory
    .select('value', '>', 0)
    .and('value', '<', 5)
    // Consider also the '<>' operator for the same result.
    .fetch();

// [
//     {
//         value: 1,
//         player: "A",
//         stage: { stage: 2, step: 1, round : 1 },
//         timestamp: 1477.13729710
//     },
//     {
//         value: 2,
//         player: "A",
//         stage: { stage: 2, step: 1, round : 1 },
//         timestamp: 17.147.33111
//     },
//     {
//         value: 3,
//         player: "B",
//         stage: { stage: 2, step: 1, round : 1 },
//         timestamp: 177.47899812
//     }
// ]

Tag and retrieve an entry

Sometimes you need fast access to an item just added.

// Creates a direct link to the last item added onto the database.
memory.tag("last_decision");

// Retrieves tag.
memory.resolveTag("last_decision");
//   {
//         value: 3,
//         player: "B",
//         stage: { stage: 2, step: 1, round : 1 },
//         timestamp: 177.47899812
//    }

Hashes and Views

Hashes and views provide convenient access to collections of items already inserted in the memory database.

  • View: A sub-database which contains semantically coherent items. Example: a database with all items from the player with id "A".

  • Hash: An automatic way to dynamically create views based on incoming data, when the criteria for subsetting the database are not known in advance. A hash function takes an item from the database and returns an alphanumeric id, that is a "hash". Example: you normally do not know the id of players in advance (it is randomly assigned), but you want to create a new view for every new id.

There are three predefined sub-databases inside the memory database:

  • memory.player: hashes items by player id,
  • memory.stage: hashes items by the stage of creation,
  • memory.done: a view containing all items sent along via node.done.

Here are a few examples about how to use hashes and views:

// Store reference.
let memory = node.game.memory;

// Items indexed by the player id.
memory.player.A.size(); // 3
memory.player.B.size(); // 2

// Get all items from previous step.
memory.stage[node.game.getPreviousStep()].size(); // 2;

// Get items indexed from given stages.
memory.stage['1.1.1'].size(); // 2
memory.stage['2.1.1'].size(); // 3

// This is equivalent to using a GameStage object
// (first import the GameStage class).
const ngc = require('nodegame-client');
const GameStage = ngc.GameStage;
memory.stage[new GameStage('1.1.1')].size(); // 2

Defining Custom Hash Databases

It is often convenient to define a custom hash database.

// In the init function of logic.

// A hash function containing all items from player A that have a property
// 'foo' with value 'bar'.
memory.hash('bar_A', function(item) {
    if (item.player === 'A' && item.foo ==='bar') {
        return item;
    }
});

// Needed if the hash is created after items have been already inserted.
// memory.rebuildIndexes();

memory.bar_A.size(); // 1

Next

Sources

⚠️ **GitHub.com Fallback** ⚠️