Google Datastore Entities Modeling for Node.js
Caching - when not using in memory caching (e.g. Redis cache) Date objects data is not being correctly deserialized to Date objects
new Date()
JavaScript constructorhttps://github.com/sebelga/gstore-node/compare/v7.2.6...v7.2.7
The code base has been converted to Typescript, as a result the typings have been improved.
const gstore = new Gstore();
const { Schema } = gstore;
interface User {
name: string;
age: number;
}
// Schema
const userSchema = new Schema<User>({
email: { type: String },
age: { type: Number }
});
// Model
const User = gstore.model('User', userSchema); // No need to provide the types here
// Entity
const user = new User({ name: 'John' }); // TS error as "age" is not provided
// Queries
const query = User.query();
query.filter('age', '<', 'wrong string'); // TS error as "age" should be a number
query.filter('wrongProperty', 'some value'); // TS error as "wrongProperty" does not exist in User
// We can also specify the format of the response ("JSON" is the default and thus optional)
const query = User.query<'ENTITY'>(); // Provide the response ("ENTITY")
query.run({ format: 'ENTITY' }).then(result => {
const { entities } = result;
const [user] = entities;
// "user" is an Entity instance with its methods
user.save();
});
// Shortcut queries (`list()` and `findAround()`) response are automatically typed from the `format` option
User.list({ format: 'ENTITY' }).populate().then((result) => {
const { entities } = result;
const [user] = entities;
const age = user.age; // number
user.save(); // method from the entity instance
});
entitykey: The "keyType" Schema option has been removed as it is no longer needed. Also, as gstore does not parse the id anymore, running your project against the Datastore emulator locally might break as the emulator treats differently User.get(123) than User.get("123"). Auto-allocated ids are integers and need to be provided as integers for the Emulator.
Dependencies: Node version 8.x not supported anymore. Upgrade to v10.x or superior.
Dependencies: The @google-cloud/datastore package is not defined anymore as a dependency of gstore-node. You will need to manually install it in your project.
Dependencies: Set google-cloud datastore as peerDependency (#177) (ac52ffb)
This release mainly addresses a bug on the Model.get()
method.
When an Array of ids is provided to Model.get(), gstore will now consistently return an Array. In earlier versions, if an array of one id was provided, gstore would return a single entity instead of an array containing the entity.
// The old way
const gstore = require('gstore-node')();
// the new way
const { Gstore, instances } = require('gstore-node');
const gstore = new Gstore(/*optional*/ config);
// You can then save that instance...
instances.set('default', gstore); // "default" is a unique id for that instance
// ... and retrieve it anywhere in your app
const { instances } = require('gstore-node');
const gstore = instances.get('default');
errorOnEntityNotFound
You can now define if gstore should throw an error when an entity is not found.
If you prefer to simply return null
when an entity is not found, pass errorOnEntityNotFound: false
when creating the gstore instance
const Gstore = require('gstore-node');
const gstore = Gstore({ errorOnEntityNotFound: false });
...
MyModel.get('id-that-does-not-exist').then((entity) => {
console.log(entity); // null
});
excludeFromRead
You can now specify in your schema properties you don't want to output from you embedded entities by specifying an array of properties to exclude when calling entity.plain()
schema = new Schema({
embedded: { type: Object, excludeFromRead: ['prop1', 'prop2'] },
});
...
const entity = new MyModel({
embedded: { prop1: '1', prop2: '2', prop3: '3' }
});
const output = entity.plain();
console.log(output); // { embedded: { prop3: '3' } }
You can also remove nested properties with a "." notation
schema = new Schema({
embedded: { type: Object, excludeFromRead: ['prop1.nested'] },
});
...
const entity = new MyModel({
embedded: {
prop1: { something: 'abc', nested: 'def' },
prop2: '2'
}
});
const output = entity.plain();
console.log(output); // { embedded: { prop1: { something: 'abc' }, prop2: '2' } }
Many thanks to @huysamen for both these new features!