I am wondering if there was an easy way to add versioning to model for easy optimistic concurrency. I was curious if anyone here has integrated that into their project with sequelize and got it to work seamless, without having to manually add the version to the where of every update ect.
I started with something like this
export const User = sequelize.define('user', {
id: {type: Sequelize.STRING, primaryKey: true},
name: {type: Sequelize.STRING, allowNull: false}
}, {
underscored: true,
tableName: 'r_users',
version: true // <- here
});
but the version doesn't change when updating the record or migration
The migration version can be found in SequelizeMeta but to select from it you need to add quotes 'SequelizeMeta' or change the name of the table to sequelize_meta by add
"migrationStorageTableName": "sequelize_meta",
to the config
Related
I have been having trouble figuring out how to update a User with graphQL. The functionality I'm currently aiming for is for the user to be able to update their account/profile information. I have some things set up for the user like a bio field for their profile, and a profile picture field that's set up to take a URL and display that as their profile picture.
I have no problems when it comes to creating using graphQL. A user can sign up, log in, make posts, etc without issue. I can also update the User in regards to other models, for example, a new post pushes to the users Post data just fine.
I have not been able to figure out how to update a user directly though. Essentially I can get around this by creating a new model for "profile pic" and pushing that to the User, but that seems like it's just extra steps that might slow things down, as well as shortchanging myself being able to learn something new.
This is the User model. I have omitted a few fields due to the exact block of code being large, but this includes the "image" and "bio" fields (the fields I would like to update) as well as the reference to the Post model which I mentioned above that functions appropriately.
User.js
const userSchema = new Schema(
{
username: {
type: String,
required: true,
unique: true,
trim: true
},
email: {
type: String,
required: true,
unique: true,
match: [/.+#.+\..+/, 'Must match an email address!']
},
password: {
type: String,
required: true,
minlength: 8
},
image: {
type: String
},
bio: {
type: String,
maxLength: 500
},
posts: [
{
type: Schema.Types.ObjectId,
ref: 'Post'
}
],
},
Below is the mutation in Explorer, including the variables and the result.
Profile Pic Resolver
addProfilePic: async (parent, { image }, context) => {
if (context.user) {
const updatedUser = await User.findOneAndUpdate(
{ _id: context.user._id },
{ image: image },
{ new: true, runValidators: true }
);
return updatedUser;
}
throw new AuthenticationError('You need to be logged in!');
},
typeDefs.js (relevant only)
type Mutation {
addProfilePic(_id: ID!, image: String!): Auth
}
I notice that in the Explorer page it returns "null" for user with a 200 status. I am led to believe that means that it's not able to even access the "image" field on the user to be able to update it. When compared to my other mutations in regards to users, this is set up very similarly and I'm not sure what the difference is.
I feel like I am missing something very basic here in regards to being able to update. I haven't been able to find an update mutation example that works. Could anyone assist? My main questions would be:
Why does the mutation return "null" for user?
How can I set up my resolver to appropriately update information on an already-created object?
Thank you to anyone who is able to take a look and assist, I will be closely watching this post for replies and will update any other code someone may need to be able to assist. I've been stuck in regards to updating information for a long time, but my site is getting to the point where it's nearly ready and I need to tackle this updating issue in order to progress. Thank you!
Quick Edit: I want to add that "Auth" is referenced. The appropriate authorization headers are in place to retrieve the data. Just wanted to add that in as I highly doubt authorization has anything to do with this!
I have solved this issue and would like to leave the answer here for anyone who may find it useful.
In the mutation typeDefs, I changed the "Auth" to "User",
type Mutation {
addProfilePic(_id: ID!, image: String!): User
}
and then in the mutation itself, took away the user field like such:
mutation addProfilePic($_id: ID!, $image: String!) {
addProfilePic(_id: $_id, image: $image) {
_id
username
image
}
}
This has allowed the user to update their profile photo information. Hope this helps!
I created a post Schema and I have trouble implementing the comment and comment reply schema since you can not predict how often one comment reply has it own reply.
I am using mongoose and express.
So how can I implement this type of schema design?
I think you're looking for something like this where you are referencing comments from within your comment schema.
I added a middleware to pre-populate the replies array when you call .find(). You can add more middleware for other calls like .findOne() etc.
const mongoose = require("mongoose");
const commentSchema = mongoose.Schema(
{
comment: {
type: String,
required: true
},
author: { // To reference the user that left the comment. If needed
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User',
},
replies:[{type: Schema.Types.ObjectId, ref: "Comment"}] // Array of comment replies
},
{
timestamps: true,
}
);
// Middleware to populate the replies when you call `find()`
commentSchema.pre('find', function() {
this.populate('replies');
});
module.exports = mongoose.model('Comment', commentSchema);
You can do more in-depth on this post which will show you how to pre-populate the replies field when returning comments etc.
https://www.makeschool.com/academy/track/standalone/reddit-clone-in-node-js/comments-on-comments
I have a UI component that generates a mongo schema like this
{
content: String,
date: { type: Date, default: Date.now },
author: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}
the idea is take this schema and generate the end-point to get the info, the question is where do you recomend storage the schema in mongoDB or in the files somehow run programmatically
yo angular-fullstack:endpoint mySchema
Thank You
So I'm fairly new to the mean-stack and I'm using mean.js as a framework.
Using Yeoman I've made a new CRUD module called groups. In a group I want to be able to have an owner, the user who made the group, and a collection of members. According to the mongoose docs I gave the GroupSchema the following structure:
var GroupSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill Group name',
trim: true
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
members: [{type: Schema.ObjectId, ref: 'Member'}]
});
Now, when a group is created the only member will be the owner. I've got the following app structure:
Project:
- app (server side code)
- controllers
- models
- routes
- public (client side code)
- modules
- groups
-controllers
Where do I put the method to add the current user to a certain group? My best guess is that the controller should take care of that, but is it the server or the client side controller?
I wouldn't set it on the client or trust the client. Your server side create function should ignore members completely and just push the current user onto the members array.
I have what I assume is a configuration problem with my Doctrine schema.yml, but I can't see to strike the right answer here.
I have two tables, BetaMeeting and ProjectTester, that form a many-to-many relationship through BetaMeetingAttendee. Everything works fine, and I can edit a beta meeting for example to include several project testers, and the relationships are all saved correctly. However, when I edit a project tester that already has existing relationships with a beta meeting(s), upon save the M:N relationships are lost. Using Symfony 1.4.13 and the admin generator, and Doctrine 1.2, and the edit page for a project tester makes no mention of the many-to-many relationships, no hidden fields, etc. Could this be the reason, the data's not there so Doctrine removes it? I didn't think it would be necessary to include it.
My schema.yml is as follows, with irrelevant details removed.
BetaMeeting:
connection: doctrine
tableName: BetaMeeting
columns:
id: { type: integer(4), primary: true, autoincrement: true }
project_id: { type: integer(4) }
date: { type: date }
relations:
Project:
local: project_id
foreign: id
foreignAlias: BetaMeetings
ProjectTester:
class: ProjectTester
refClass: BetaMeetingAttendee
foreignAlias: BetaMeetings
BetaMeetingAttendee:
connection: doctrine
tableName: BetaMeetingAttendee
columns:
beta_meeting_id: { type: integer(4), primary: true, autoincrement: false }
project_tester_id: { type: integer(4), primary: true, autoincrement: false }
relations:
BetaMeeting:
foreignAlias: BetaMeetingAttendees
ProjectTester:
foreignAlias: BetaMeetingAttendees
ProjectTester:
connection: doctrine
tableName: ProjectTester
columns:
id: { type: integer(4), primary: true, autoincrement: true }
tester_id: { type: integer(4) }
project_id: { type: integer(4) }
relations:
Tester:
local: tester_id
foreign: id
foreignAlias: Projects
Project:
local: project_id
foreign: id
foreignAlias: ProjectTesters
Any clue as to why the relationships get cleared out after an edit which is concerned only with the immediate attributes of the ProjectTester object?
If you have a field defined in the Form but you excluded it from the generator.yml it's like submitting an empty field and therefore it clears the relations.
You have to unset that field in the Form.class so the field retains the current values.
public function configure()
{
unset($this['beta_meeting_list']); // or the correct value
}