Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8868

Keystonejs Tutorials #6/Creating relationships3

$
0
0

前ページ https://qiita.com/bontensuzuki/items/7f306f0d6bed05adc891
次ページ https://qiita.com/bontensuzuki/items/8f91e3f0128b6be1ecbb

Keystonejsのtutorialsから学んでいきます。(ほとんど機械翻訳です)
https://www.keystonejs.com/tutorials/relationships

多対多の関係(To-many relationship)

ユーザーが複数のタスクを実行できる必要がある場合はどうなりますか? Keystoneはこれを簡単に行う方法を提供します。 User.jsの次のコードを見てください。

/lists/User.js

tasks:{type:Relationship,ref:'Todo.assignee',many:true,}

many:trueオプションは、ユーザーがタスクへの複数の参照を保存できることを示します。

/lists/Todo.js

assignee:{type:Relationship,-ref:'User.task',+ref:'User.tasks',}

注:フィールドの名前をtaskからtasksに更新し、関係の性質を示しました。

スクリーンショット 2020-05-04 15.25.46.png

taskが2つ登録できました

index.js
const{Keystone}=require('@keystonejs/keystone');const{MongooseAdapter}=require('@keystonejs/adapter-mongoose');const{PasswordAuthStrategy}=require('@keystonejs/auth-password');const{GraphQLApp}=require('@keystonejs/app-graphql');const{AdminUIApp}=require('@keystonejs/app-admin-ui');const{Text,CalendarDay,Checkbox,Password}=require('@keystonejs/fields');constTodoSchema=require('./lists/Todo.js');constUserSchema=require('./lists/User.js');constkeystone=newKeystone({name:'New Project 3',adapter:newMongooseAdapter(),});keystone.createList('Todo',TodoSchema);keystone.createList('User',UserSchema);constauthStrategy=keystone.createAuthStrategy({type:PasswordAuthStrategy,list:'User',});module.exports={keystone,apps:[newGraphQLApp(),newAdminUIApp({enableDefaultRoute:true,authStrategy})],};

lists/Todo.js
const{CalendarDay,Checkbox,Relationship,Text}=require('@keystonejs/fields');module.exports={fields:{// existing fieldsdescription:{type:Text,isRequired:true,},isComplete:{type:Checkbox,defaultValue:false,},// added fieldsdeadline:{type:CalendarDay,format:'Do MMMM YYYY',yearRangeFrom:'2019',yearRangeTo:'2029',isRequired:false,defaultValue:newDate().toISOString('YYYY-MM-DD').substring(0,10),},assignee:{type:Relationship,ref:'User.tasks',isRequired:true,},},};

lists/User.js
const{Text,Password,Checkbox,Relationship}=require('@keystonejs/fields');module.exports={fields:{username:{type:Text,isRequired:true,},email:{type:Text,isUnique:true,},isAdmin:{type:Checkbox},password:{type:Password,isRequired:true,},tasks:{type:Relationship,ref:'Todo.assignee',many:true,},},};

前ページ https://qiita.com/bontensuzuki/items/7f306f0d6bed05adc891
次ページ https://qiita.com/bontensuzuki/items/8f91e3f0128b6be1ecbb


Viewing all articles
Browse latest Browse all 8868

Trending Articles