Skip to content

Instantly share code, notes, and snippets.

@daffl
Created November 6, 2019 18:06
Show Gist options
  • Save daffl/62cca7830ac8e244d517926be101bfec to your computer and use it in GitHub Desktop.
Save daffl/62cca7830ac8e244d517926be101bfec to your computer and use it in GitHub Desktop.
Feathers Schema proposal ideas
const { setSchema } = require('@feathersjs/schema');
class User {}
setSchema(User, {
id: {
type: Number
},
email: {
description: 'The user email',
type: String
},
firstName: {
type: String
},
lastName: {
type: String
},
todos: {
type: [ Todo ],
async resolve (user, context) {
const { params: { query = {} }, app } = context;
return app.service('todos').find({
paginate: false,
query: {
...query,
userId: user.id
}
});
}
}
});
class Todo {}
setSchema(Todo, {
text: {
type: String
},
completed: {
type: Boolean,
defaultValue: false
},
userId: {
type: Number
},
user: {
type: User,
resolve: async (todo, context) => {
const { params, app } = context;
return app.service('users').get(todo.userId, params);
}
}
});
const { property } = require('@feathersjs/schema');
class User {
@property()
id: number;
@property({
description: 'The user email'
})
email: string;
@property()
firstName: string;
@property()
lastName: string;
@property({
resolve: async (user, context) => {
const { params: { query = {} }, app } = context;
return app.service('todos').find({
paginate: false,
query: {
...query,
userId: user.id
}
});
}
})
todos: Todo[];
}
class Todo {
@property()
text: string;
@property({
defaultValue: false
})
completed: boolean;
@property()
userId: User['id'];
@property({
resolve: async (todo, context) => {
const { params, app } = context;
return app.service('users').get(todo.userId, params);
}
})
user: User;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment