Comments (5)
I'm going to do this by leveraging the where
clauses:
keystone.createItems({
Users: [{
name: 'Jess',
}],
Posts: [{
title: 'Hello world',
author: {
where: {
name: 'Jess',
}
}
}],
});
The config tells us that the Posts.author
field is a Relationship, so we can then safely extract out the where
clause before we do the INSERT
into the DB.
Once we have the where
clause, we can do the lookup, and then make the relationship connections.
One caveat: If Posts.author
is set as required
, the initial insert would fail (because the Posts.author
value is null until after the insert). So we need to bypass that required
check when doing the initial INSERT
.
from keystone.
That's a pretty cool approach. What we used to do in Keystone 4 was:
keystone.createItems({
Users: [{
name: 'Jess',
_ref: 'jess',
}],
Posts: [{
title: 'Hello world',
author: 'jess'
}],
});
Create the items, keep the refs as we create them (resolve to ids) then do a second pass updating the relationships.
Definitely important to at least keep the two-pass approach; I like the idea of being able to do any where
clause when creating relationships, but if we didn't have a unique (known) value to filter them on, maybe bringing back the idea of a manual ref linking system could help.
The v4 implementation is actually reasonably well written for old code, if you want to check it out: https://github.com/keystonejs/keystone/blob/master/lib/core/createItems.js
It did some nice stats collection too, which we logged out to the console when it was executed as part of an upgrade script.
from keystone.
Two other ideas: your approach works well for resolving data that already exists; from memory, we had an alternative way of doing it where you'd basically have to query the database first then provide a function that resolved to itemIds...
Maybe there's some nice futuristic es syntax stuff we could take advantage of here? we could generate ref IDs and resolve their values like this:
const { ref } = require('@keystonejs/create-items');
keystone.createItems({
Users: {
[ref('jess')]: {
name: 'Jess',
},
},
Posts: [{
title: 'Hello world',
author: ref('jess'),
}],
});
Just thinking 🤔
from keystone.
Definitely important to at least keep the two-pass approach
💯% - it's the approach I've taken, and it looks to be working really well, here's how the code shakes out:
// 1. Split it apart
const { relationships, data } = unmergeRelationships(this.lists, itemsToCreate);
// 2. Create the items
const createdItems = await createItems(data);
// 3. Create the relationships
const createdRelationships = await createRelationships(
this.lists,
relationships,
createdItems
);
// 4. Merge the data back together again
return mergeRelationships(createdItems, createdRelationships);
if we didn't have a unique (known) value to filter them on, maybe bringing back the idea of a manual ref linking system could help.
Agreed! Do you have any specific examples so I can form some tests around them? I was thinking of also enabling the filter
/first
/skip
filtering options as well as the where
clause, but can't really think of a usecase for that either (I don't have enough experience with this kind of data modelling yet).
we could generate ref IDs and resolve their values like this:
Oooh, that's a really nice direction! I wonder how that plays into the usecase where only some items are ref'd?
const { ref } = require('@keystonejs/create-items');
keystone.createItems({
Users: [
[ref('jess')]: { // INVALID SYNTAX (within an array)
name: 'Jess',
},
{
name: 'Jed',
},
],
Posts: [{
title: 'Hello world',
author: ref('jess'),
}],
});
from keystone.
Oh, maybe it's like this?
const { ref } = require('@keystonejs/create-items');
keystone.createItems({
Users: [
ref({
name: 'Jess',
}, 'jess'),
{
name: 'Jed',
},
],
Posts: [{
title: 'Hello world',
author: ref('jess'),
}],
});
from keystone.
Related Issues (20)
- Using Turborepo with KeystoneJS with a Nextjs package version higher than 13.4.12 breaks redirects HOT 3
- Some List Types do not load generated page in dashboard. Endlessly loading, unable to CRUD HOT 1
- ListView CSS Issue with file field
- Login email field is type="text"
- Email address on login interface is case-sensitive HOT 1
- Can your add a config to customize page title
- pnpm and prisma client HOT 4
- Multiselect field required
- Keystone6 error: Field must have a selection of subfields HOT 2
- Node 20 (LTS) support HOT 7
- Keystone app breaks with interal slice call on getGqlNames HOT 21
- Calling GraphQL api consume multiple connections HOT 2
- POST body missing, invalid Content-Type, or JSON object has no keys. HOT 2
- Custom Apollo landingspage setup in config.graphql.apolloConfig gives error
- EPERM: operation not permitted, unlink query_engine-windows.dll.nod HOT 2
- WhereInput filter for multiselect field HOT 1
- Hooks not fired on relation field changes HOT 2
- Everything is nullable in the schema HOT 1
- Build Keystone inside turborepo fails HOT 4
- Unable to deploy Keystone JS app to Azure app service HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from keystone.