Created
November 6, 2019 18:06
-
-
Save daffl/62cca7830ac8e244d517926be101bfec to your computer and use it in GitHub Desktop.
Feathers Schema proposal ideas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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