gstore-node
v7.x
v7.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

Was this helpful?

  1. Queries

findOne()

Quickly find an entity by passing key/value pairs. You can optionally pass an ancestors array or a namespace.

@Returns an gstore entity instance of the Model.

This method accepts the following arguments:

MyModel.findOne(
    /* {object}. -- Key/Value pairs to look for */
    <propsValues>,
    /* {Array} -- optional. ex: ['ParentEntity', 1234 ] */
    <ancestors>,
    /* {string} -- optional. A specific namespace */
    <namespace>,
    /* {object}. -- optional. Options for the query */
    <options>
)

options

The options argument accepts the following properties:

  • cache (default: the "global" cache configuration) "true" = read from the cache and prime the cache with the query response.

  • ttl (default: the global cache.ttl.queries value) Custom TTL value for the cache. For multi-store it can be an Object of TTL values.

  • readAll (default: false). Return all the properties, even the ones marked as read: false on the Schema.

Example:

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

User.findOne({ email: 'john@snow.com' })
    .then((entity) => {
        console.log(entity.plain()); // entityData + id
        console.log(entity.firstname)); // 'John'
});

// Disable cache and read all properties
User.findOne({ email: 'john@snow.com' }, null, null, { cache: false, readAll: true })
    .then((entity) => {
        console.log(entity.plain()); // entityData + id
        console.log(entity.firstname)); // 'John'
});

// Custom TTL for cache
// ttl set to "-1" means to not cache for this store
User.findOne({ email: 'john@snow.com' }, null, null, { ttl: { memory: -1, redis: 300 } })
    .then((entity) => {
        console.log(entity.plain()); // entityData + id
        console.log(entity.firstname)); // 'John'
});
Previouslist()NextdeleteAll()

Last updated 5 years ago

Was this helpful?