Created
October 2, 2019 05:56
-
-
Save ldiebold/7a1d15ae4ed0e7cbeff1f4bc15ee364a to your computer and use it in GitHub Desktop.
Files for the vuetiful life, vuex-orm series (video 18). 1. Create insertUsers.js in your "src" directory 2. update User.js, Post.js and Comment.js
This file contains 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
import { Model } from '@vuex-orm/core' | |
import User from './User' | |
export default class Comment extends Model { | |
static entity = 'comments' | |
static fields() { | |
return { | |
id: this.increment(), | |
body: this.attr(null), | |
commentable_id: this.attr(null), | |
commentable_type: this.attr(null), | |
type: this.attr('review'), | |
user_id: this.attr(null), | |
// relationships | |
user: this.belongsTo(User, 'user_id'), | |
commentable: this.morphTo('commentable_id', 'commentable_type'), | |
} | |
} | |
} |
This file contains 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
import User from './classes/User' | |
export default () => { | |
// Luke | |
User.insertOrUpdate({ | |
data: { | |
id: 1, | |
first_name: 'Luke', | |
age: 28, | |
posts: [ | |
{ | |
published: true, | |
id: 1, | |
title: 'Vue Stuff', | |
tags: [ | |
{ | |
id: 1, | |
name: 'vue' | |
}, | |
{ | |
id: 2, | |
name: 'tutorials' | |
} | |
], | |
comments: [ | |
{ | |
user_id: 2, | |
id: 1, | |
body: 'Cool post bro!' | |
}, | |
{ | |
user_id: 3, | |
id: 2, | |
body: 'I thought it was terrible' | |
} | |
] | |
}, | |
{ | |
id: 2, | |
title: 'Piano Stuff', | |
tags: [ | |
{ | |
id: 3, | |
name: 'piano', | |
} | |
], | |
comments: [ | |
{ | |
user_id: 3, | |
id: 3, | |
body: 'mmm, I love piano!', | |
} | |
] | |
}, | |
{ | |
id: 3, | |
title: 'boring post' | |
} | |
] | |
} | |
}) | |
// Shannen | |
User.insertOrUpdate({ | |
data: { | |
id: 2, | |
first_name: 'Shannen', | |
age: 27, | |
posts: [ | |
{ | |
id: 4, | |
published: true, | |
title: 'Nutrition Stuff', | |
tags: [ | |
{ | |
id: 2, | |
title: 'tutorials' | |
} | |
], | |
comments: [ | |
{ | |
user_id: 3, | |
id: 4, | |
body: 'I love to eat healthy!', | |
} | |
] | |
} | |
] | |
} | |
}) | |
// Prince | |
User.insertOrUpdate({ | |
data: { | |
id: 3, | |
first_name: 'Prince', | |
age: 14, | |
} | |
}) | |
} |
This file contains 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
import { Model } from '@vuex-orm/core' | |
import Image from './Image' | |
import Comment from './Comment' | |
import Tag from './Tag' | |
import Taggable from './Taggable' | |
import User from './User' | |
export default class Post extends Model { | |
static entity = 'posts' | |
static fields() { | |
return { | |
id: this.increment(), | |
title: this.attr(null), | |
image: this.morphOne(Image, 'imageable_id', 'imageable_type'), | |
user_id: this.attr(null), | |
published: this.boolean(false), | |
// relationships | |
user: this.belongsTo(User, 'user_id'), | |
comments: this.morphMany(Comment, 'commentable_id', 'commentable_type'), | |
tags: this.morphToMany(Tag, Taggable, 'tag_id', 'taggable_id', 'taggable_type'), | |
} | |
} | |
} |
This file contains 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
import { Model } from '@vuex-orm/core' | |
import Profile from './Profile' | |
import List from './List' | |
import Item from './Item' | |
import Role from './Role' | |
import RoleUser from './RoleUser' | |
import Image from './Image' | |
import moment from 'moment' | |
import Post from './Post' | |
import Comment from './Comment' | |
export default class User extends Model { | |
static entity = 'users' | |
get full_name() { | |
return `${this.first_name} ${this.last_name}` | |
} | |
static mutators() { | |
return { | |
date_born(value) { | |
return moment(value) | |
} | |
} | |
} | |
static fields() { | |
return { | |
id: this.increment(), | |
first_name: this.attr(''), | |
last_name: this.attr(''), | |
email: this.attr(''), | |
date_born: this.attr(null), | |
age: this.attr(null), | |
// relationships | |
posts: this.hasMany(Post, 'user_id'), | |
comments: this.hasMany(Comment, 'user_id'), | |
profile: this.hasOne(Profile, 'user_id'), | |
lists: this.hasMany(List, 'user_id'), | |
items: this.hasManyThrough(Item, List, 'user_id', 'list_id'), | |
roles: this.belongsToMany(Role, RoleUser, 'user_id', 'role_id'), | |
image: this.morphOne(Image, 'imageable_id', 'imageable_type'), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment