gstore-node
v8.x
v8.x
  • Introduction
  • Getting Started
    • Motivation
    • Installation
    • Create a gstore instance
  • Schema
    • About
    • Type validation
    • Value validation
    • Additional properties settings
    • Schema options
    • Joi Schema
    • Methods
      • path()
      • virtual()
    • Custom methods
  • Model
    • Creation
    • Methods
      • GET
      • UPDATE
      • DELETE
      • excludeFromIndexes()
      • key()
      • sanitize()
      • clearCache()
  • Entity
    • Creation
    • Properties
    • Methods
      • SAVE
      • plain()
      • populate()
      • model()
      • datastoreEntity()
      • validate()
  • Queries
    • @google-cloud Query
    • list()
    • findOne()
    • deleteAll()
    • findAround()
  • Populate
  • Middleware (hooks)
    • About
    • Pre hooks
    • Post hooks
  • Cache / Dataloader
    • Dataloader
    • Cache
  • gstore Methods
    • save()
  • Typescript
  • Appendix
    • Error Codes
    • Credits
Powered by GitBook
On this page
  1. Model
  2. Methods

key()

PreviousexcludeFromIndexes()Nextsanitize()

Last updated 5 years ago

Was this helpful?

CtrlK

Was this helpful?

Generates one or several entity key(s) for the Model. This method accepts the following arguments (all arguments are optional):

MyModel.key(
    /*
     {string | { id: string|number } | { name: string }}
     It can also be an Array of ids to generate
     */
    <id>,
    /* {Array} -- optional. ex: ['ParentEntity', 1234 ] */
    <ancestors>,
    /* {string} -- optional. A specific namespace */
    <namespace>
)

Example:

const User = require('./user.model');

// Providing a Key "id"
const entityKey = User.key({ id: 123 });
// "string" id will automatically be converted to integer
const entityKey = User.key({ id: '123' });

// You can also provide a key "name"
const entityKey = User.key({ name: 'myEntity' });
// or passing the string name directly
const entityKey = User.key('myEntity');

// with ancestors and namespace
const entityKey = User.key({ id: 123 }, ['Parent', 'keyname'], 'dev.domain.com');

// with an Array of ids
const entityKeys = User.key([{ id: '123' }, 'myEntity']);
console.log(entityKeys.length); // 2