Rough example for how to achieve fields that have unique values within a relation, for example, projects with unique slugs within a team.
Last active
February 15, 2022 18:58
-
-
Save jerryjappinen/0982596512e6670196029ee523491232 to your computer and use it in GitHub Desktop.
Scoped uniqueness on 8base via triggers
This file contains hidden or 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
# https://docs.8base.com/docs/8base-console/custom-functions/triggers/ | |
functions: | |
beforeProjectCreate: | |
handler: | |
code: triggers/beforeProjectCreate.js | |
type: trigger.before | |
operation: Project.create | |
description: Enforce unique Project slugs within a team upon create | |
beforeProjectUpdate: | |
handler: | |
code: triggers/beforeProjectUpdate.js | |
type: trigger.before | |
operation: Project.update | |
description: Enforce unique Project slugs within a team upon update |
This file contains hidden or 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
async function slugExists (api, teamId, slug) { | |
const response = await api.gqlRequest(`{ | |
projectsList ( | |
filter: { | |
slug: { | |
equals: "${slug}" | |
} | |
team: { | |
id: { | |
equals: "${teamId}" | |
} | |
} | |
} | |
) { | |
items { | |
id | |
} | |
} | |
}`) | |
return response.projectsList.items && response.projectsList.items.length | |
} | |
export default async ({ data }, { api }) => { | |
if ( | |
data.slug && | |
data.team.connect.id && | |
await slugExists(api, data.team.connect.id, data.slug) | |
) { | |
throw new Error('Project\'s slug must be unique within a team') | |
} | |
return { | |
data | |
} | |
} |
This file contains hidden or 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
async function slugExistsByProjectId (api, projectId, slug) { | |
const response = await api.gqlRequest(`{ | |
project( | |
id: "${projectId}" | |
) { | |
team { | |
projects ( | |
filter: { | |
slug: { | |
equals: "${slug}" | |
} | |
} | |
) { | |
items { | |
id | |
} | |
} | |
} | |
} | |
}`) | |
return response.project.team.projects.items && response.project.team.projects.items.length | |
} | |
export default async ({ data, originalObject }, { api }) => { | |
if ( | |
data.slug !== originalObject.slug && | |
await slugExistsByProjectId(api, originalObject.id, data.slug) | |
) { | |
throw new Error('Project\'s slug must be unique within team') | |
} | |
return { | |
data | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment