Skip to content

v7.2.0

Choose a tag to compare

@loixlab loixlab released this 26 Sep 10:45
· 66 commits to master since this release

Features

  • Typescript: Improve typings support (43d9dc2)

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
});