Closed Olya-Yer closed 5 years ago
Great question! Accessing the model directly is discouraged as it will tie your code directly to the database you're using.
Until we have programatic querying sorted out (#1275), we don't have a good answer to achieve what you're after, but here are 2 ways you could do it:
keystone.getListByKey('Field').adapter._findAll()
. This has a high risk of breaking in a non-major version release as they're meant to be private methods.const { request } = require('graphql-request');
const { allUsers } = await request('http://localhost:3000/admin/api', `{
allUsers {
id
name
}
}`);
Just to follow up on this quickly, the list adapter .model
will be null
until you perform a keystone.connect()
, at which point the database connection is established and the models become available. This is the expected behaviour. If you're seeing a null
model after performing a .connect()
please let us know, as this would be a bug.
As @jesstelford pointed out above, in general you're better off using the graphQL or adapter methods to interact with your backend. You should probably only drop down to the .model
if you're doing something really specific which the other APIs don't support.
If you do use the .model
then a) your code won't work if you switch to a different database adapter and b) you might bypass certain rules such as validation, access control, etc, which keystone is handling.
If you're OK with these considerations then by all means use .model
directly. The general principle here is that Keystone will try to provide APIs for common operations (CRUD) while not preventing you from directly interacting with your database if that's what you need.
I tried to query list items
but adapter's model field is
null
Is there a way to query the model other than
getListByKey
?this is the console.log of the adapter is this an expected behavior ?