|
| 1 | +import {Model as ORMModel} from "@vuex-orm/core"; |
| 2 | +import {createStore} from "./helpers"; |
| 3 | + |
| 4 | +export class User extends ORMModel { |
| 5 | + static entity = 'users'; |
| 6 | + |
| 7 | + static fields () { |
| 8 | + return { |
| 9 | + id: this.increment(0), |
| 10 | + name: this.string(''), |
| 11 | + posts: this.hasMany(Post, 'userId'), |
| 12 | + comments: this.hasMany(Comment, 'userId') |
| 13 | + }; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +export class Video extends ORMModel { |
| 18 | + static entity = 'videos'; |
| 19 | + static eagerLoad = ['comments']; |
| 20 | + static skipFields = ['ignoreMe']; |
| 21 | + |
| 22 | + static fields () { |
| 23 | + return { |
| 24 | + id: this.increment(null), |
| 25 | + content: this.string(''), |
| 26 | + title: this.string(''), |
| 27 | + userId: this.number(0), |
| 28 | + otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation |
| 29 | + ignoreMe: this.string(''), |
| 30 | + user: this.belongsTo(User, 'userId'), |
| 31 | + comments: this.morphMany(Comment, 'subjectId', 'subjectType') |
| 32 | + }; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +export class Post extends ORMModel { |
| 37 | + static entity = 'posts'; |
| 38 | + static eagerLoad = ['comments']; |
| 39 | + |
| 40 | + static fields () { |
| 41 | + return { |
| 42 | + id: this.increment(null), |
| 43 | + content: this.string(''), |
| 44 | + title: this.string(''), |
| 45 | + userId: this.number(0), |
| 46 | + otherId: this.number(0), // This is a field which ends with `Id` but doesn't belong to any relation |
| 47 | + user: this.belongsTo(User, 'userId'), |
| 48 | + comments: this.morphMany(Comment, 'subjectId', 'subjectType') |
| 49 | + }; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +export class Comment extends ORMModel { |
| 55 | + static entity = 'comments'; |
| 56 | + |
| 57 | + static fields () { |
| 58 | + return { |
| 59 | + id: this.increment(0), |
| 60 | + content: this.string(''), |
| 61 | + userId: this.number(0), |
| 62 | + user: this.belongsTo(User, 'userId'), |
| 63 | + |
| 64 | + subjectId: this.number(0), |
| 65 | + subjectType: this.string('') |
| 66 | + }; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export class ContractContractOption extends ORMModel { |
| 71 | + static entity = 'contractContractOptions'; |
| 72 | + |
| 73 | + static primaryKey = ['contractId', 'contractOptionId']; |
| 74 | + |
| 75 | + static fields () { |
| 76 | + return { |
| 77 | + contractId: this.attr(null), |
| 78 | + contractOptionId: this.attr(null), |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +export class Contract extends ORMModel { |
| 84 | + static entity = 'contracts'; |
| 85 | + |
| 86 | + static fields () { |
| 87 | + return { |
| 88 | + id: this.increment(), |
| 89 | + name: this.attr(''), |
| 90 | + displayName: this.attr(''), |
| 91 | + slug: this.attr(''), |
| 92 | + |
| 93 | + contractOptions: this.belongsToMany(ContractOption, ContractContractOption, 'contractId', |
| 94 | + 'contractOptionId'), |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +export class ContractOption extends ORMModel { |
| 101 | + static entity = 'contractOptions'; |
| 102 | + |
| 103 | + static fields () { |
| 104 | + return { |
| 105 | + id: this.increment(), |
| 106 | + name: this.attr(''), |
| 107 | + description: this.attr(''), |
| 108 | + |
| 109 | + contracts: this.belongsToMany(Contract, ContractContractOption, 'contractOptionId', 'contractId') |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +export async function setupMockData() { |
| 116 | + let store, vuexOrmApollo; |
| 117 | + |
| 118 | + [store, vuexOrmApollo] = createStore([ |
| 119 | + { model: User }, |
| 120 | + { model: Post }, |
| 121 | + { model: Video }, |
| 122 | + { model: Comment }, |
| 123 | + { model: ContractOption }, |
| 124 | + { model: Contract }, |
| 125 | + { model: ContractContractOption } |
| 126 | + ]); |
| 127 | + |
| 128 | + await User.insert({ data: { id: 1, name: 'Charlie Brown' }}); |
| 129 | + await User.insert({ data: { id: 1, name: 'Charlie Brown' }}); |
| 130 | + await User.insert({ data: { id: 2, name: 'Peppermint Patty' }}); |
| 131 | + await Post.insert({ data: { id: 1, otherId: 9, userId: 1, title: 'Example post 1', content: 'Foo' }}); |
| 132 | + await Post.insert({ data: { id: 2, otherId: 10, userId: 1, title: 'Example post 2', content: 'Bar' }}); |
| 133 | + await Video.insert({ data: { id: 1, otherId: 11, userId: 1, title: 'Example video', content: 'Video' }}); |
| 134 | + await Comment.insert({ data: { id: 1, userId: 1, subjectId: 1, subjectType: 'videos', content: 'Example comment 1' }}); |
| 135 | + await Comment.insert({ data: { id: 2, userId: 2, subjectId: 1, subjectType: 'posts', content: 'Example comment 2' }}); |
| 136 | + await Comment.insert({ data: { id: 3, userId: 2, subjectId: 2, subjectType: 'posts', content: 'Example comment 3' }}); |
| 137 | + |
| 138 | + return [store, vuexOrmApollo]; |
| 139 | +} |
0 commit comments