0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Keystonejs Tutorials #6/Creating relationships3

Last updated at Posted at 2020-05-04

前ページ 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');
const TodoSchema = require('./lists/Todo.js');
const UserSchema = require('./lists/User.js');


const keystone = new Keystone({
  name: 'New Project 3',
  adapter: new MongooseAdapter(),
});

keystone.createList('Todo', TodoSchema);
keystone.createList('User', UserSchema);

const authStrategy = keystone.createAuthStrategy({
  type: PasswordAuthStrategy,
  list: 'User',
});

module.exports = {
  keystone,
  apps: [new GraphQLApp(),new AdminUIApp({ enableDefaultRoute: true, authStrategy })],
  
};
lists/Todo.js
const { CalendarDay, Checkbox, Relationship, Text } = require('@keystonejs/fields');

module.exports = {
  fields: {
    // existing fields
    description: {
      type: Text,
      isRequired: true,
    },
    isComplete: {
      type: Checkbox,
      defaultValue: false,
    },
    // added fields
    deadline: {
      type: CalendarDay,
      format: 'Do MMMM YYYY',
      yearRangeFrom: '2019',
      yearRangeTo: '2029',
      isRequired: false,
      defaultValue: new Date().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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?