Sails.Js - Simple Exercise with 2 Relations on the ORM - javascript

I'm new to Sails.js and Node.js, so if i'm asking a simple question please forgive me.
I'm doing an API that must have a database, and i wanted to implement MySQL because i have some relations between tables. So i wrote the relational model by hand, like it is on the figure:
And i tried to implement is using sails.js, but for some reason i can't get the relation Level to Score working while the User-Score relationship is working fine.
Here are my models:
User.JS
module.exports = {
attributes: {
name:{
type:'string',
required: true
},
address:{
type: 'string'
},
email:{
type: 'string',
email:true,
unique: true,
required: true
},
encryptedPassword:{
type: 'string'
},
tranusertoscore:{
collection:'scores',
via: 'transcoretouser'
}
}
}
Level.js
module.exports = {
attributes: {
lvl_name:{
type: 'string',
required: true
},
tranleveltoscore:{
collection: 'scores',
via: 'transcoretolevels'
}
}
};
and finally
Score.js
module.exports = {
attributes: {
lvl:{
type: 'string',
required: true
},
score:{
type: 'integer',
required: true
},
transcoretouser:{
model: 'user'
},
transcoretolevels:{
model: 'level'
}
}
};
So, can you guys tell me what i'm doing wrong?
Sorry to take your time if the answer is easy, but i tried to follow documentation and search about the subject and didn't find any relevant answers.

Related

how can i efficiently model my Mongoose Schema's?

Good Morning all! - i'll try to keep it short.
Coming from a begginers SQL/EF background i definitely went into mongoose with the wrong mindset.
I have 3 tables:
Users /
Boards /
BoardObjects
Now originally i thought about storing the boards as an array in the users document. But i think i'm approaching this wrong and i need community advice.
i need to be able to query what users what own what boards, and what objects are on those boards, and the arrays i put in were a nightmare. Then i thought, do i even need to cross these over?
for example, if i store a User, and then create a board, and in the board has a UserId that has to be unique to the user, and same for the BoardObjects, UserId and a BoardId, from what i've read so far seems it might be possible this way using joins and whatnot, which ive still got to learn..
Whats the community view here? do you embed or normalise often? or is this a valid way of managing related data in mongoose?
Providing Boards model for context
const Board = mongoose.model('Board', new mongoose.Schema({
Ownerid: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
name: {
type: String,
required: true,
minlength: 5,
maxlength: 50
},
datecreated: {
type: Date,
},
datemodified: {
type: Date
},
BoardObjects: [{ boardobjectid: { type: mongoose.Types.ObjectId, ref: 'BoardObject' } }],
}));
if i am understanding you correctly, you need to create a relationship between the two tables. in the user model you can add something like.
userSchema.virtual('boards', {
ref: 'Boards',
localField: '_id',
foreignField: 'owner'
});
then as part of your boards model build your relationship with the user as an object on the model like this.
owner:{
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
}
I hope this helps!
As per my knowledge,You can go with following answer as below:
const Board= new mongoose.Schema({
Ownerid: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
name: {
type: String,
required: true,
minlength: 5,
maxlength: 50
},
datecreated: {
type: Date,
},
datemodified: {
type: Date
}
})
const Board=mongoose.model('Board',userSchema)

User, post, and comment model in mongoose

I am making a forum app that is quite similar to Reddit. I am using mongoose here for schema design. I have some doubts regarding models that I have- User, Post and Comment.
Does the schema look all right of all the models?
In User, I want to show the friends on the user profile. Like when you go to his profile, there will be like Friends(200), and when I click the friend list will appear(like Facebook, then on clicking you can access friends' profiles). I haven't created Friend schema, but does it needed actually?
Question about comments. So far, I have the basic comment schema. What happens to the threaded comments or replies. How do I store them?
All the models that I have currently:
const userSchema = new Schema(
{
username: { type: String, required: true },
email: { type: String, reuired: true },
password: { type: String, required: true },
country: { type: String, required: true },
friends: [{ type: Schema.Types.ObjectId, ref: "Friend" }], // is it needed?
posts: [{ type: Schema.Types.ObjectId, ref: "Post" }],
},
{ timestamps: true }
)
const postSchema = new Schema(
{
title: { type: String, required: true },
description: { type: String, required: true },
user: { type: Schema.Types.ObjectId, ref: "User" },
slug: { type: String, slug: "title" },
upvotes: { type: Number },
downvotes: { type: Number },
city: { type: String }, // post will have a tag by the city like NewYork, LA etc.
},
{ timestamps: true }
)
const commentSchema = new Schema(
{
comment: { type: String, required: true },
post: { type: Schema.Types.ObjectId, ref: "Post"},
user: { type: Schema.Types.ObjectId, ref: "User"},
upvotes: { type: Number },
downvotes: { type: Number }
},
{ timestamps: true }
)
For the Second Question you don't need friend schema since friend is also a user.
For the first question, Why do intend to store all the posts of a user inside the User object as a list of posts, this would mean fetching 100s of posts even when something basic like username or email is required. Instead, you can place user_id in the Post Schema, helping to identify posts from a particular User.

Can't add new attributes to existing model sails.js

I'm quite newbie in sails.js. I need to add fields to an existing model in sails js.
Here it is
module.exports = {
attributes: {
id: {
columnName: 'id',
type: 'integer',
autoIncrement: true,
primaryKey: true,
unique: true
},
username: {
columnName: 'username',
type: 'STRING',
required: true,
},
userLevel: {
columnName: 'user_level',
type: 'integer',
required: true,
defaultsTo: 1
},
...
But as soon as I add the fields in my model js like
newAttribute: {
columnName: 'new_attribute',
type: 'integer',
required: true
}
I get an error "E_UKNOWN".
To be more precise, when I try to login with updated model, I get this message:
rr {"data":{"err":{"error":"E_UNKNOWN","status":500,"summary":"Encountered an unexpected error","raw":{"code":"ER_BAD_FIELD_ERROR","errno":1054,"sqlState":"42S22","index":0}}},"status":401,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"data":{*email and password here*},"url":"auth/login","headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}},"statusText":"Unauthorized"}
Appreciate your help.
UPD It's working if I set NODE_ENV to development. May it could be the problem with connection.js file?
Seems that I've found the solution of my problem. At first I set NODE_ENV to development, and, I guess, the tables were created. Then I change NODE_ENV to production. And now it's working without odd errors.

Sails.js with mongodb design

just starting out in sails.
Currently I have a User Model set up. I also have another Founder Model to store information specific to Users who are also Models.
attributes: {
companyName: {
type: 'string',
required: true
},
tagLine: {
type: 'string',
required: true
},
location: {
type: 'string',
required: true
},
market: {
type: 'string',
required: true
},
psName: {
type: 'string',
required: true
},
psDesc: {
type: 'string'.
required: true
}
}
So my question in the FounderController...would inputting the unique id for the user from User for the database be implemented in the models or in the controller? The Founder model will essentially create it's own unique id.
My first approach would to basically find the ID from the User documents, store it into a variable, update the Founder document's id field with the user id?
I felt this isn't very efficient. Thanks.

Sails.js - One to Many mapping

Is there any way to do relation mapping between models in Sails.js?
Here is what I would like:
Video.js:
module.exports = {
attributes: {
filename: 'STRING',
length: 'INTEGER',
watchCount: 'INTEGER',
extension: 'STRING'
user: // I wan to reference to my User.js model
}
};
And in my User.js:
module.exports = {
attributes: {
username: {
type: 'email',
required: true
},
password: 'STRING',
videos: // I would like to have an array of videos after querying a user
}
};
You can use associations in sailsJs now by using the v0.10 branch
https://stackoverflow.com/a/21822843/1585332
The mapping would be something like this..
Video.js
module.exports = {
attributes: {
filename: 'STRING',
length: 'INTEGER',
watchCount: 'INTEGER',
extension: 'STRING'
user:{
model: "user"
}
}
};
User.js
module.exports = {
attributes: {
username: {
type: 'email',
required: true
},
password: 'STRING',
videos:{
collection: "video",
via: "user"
},
}
};
Sails.js doesn't support association yet, but they're working on it:
https://github.com/balderdashy/sails/issues/124#issuecomment-21690561
Also see: How to perform SQL Joins and Relations in Sails.js and Waterline?
For now I would just reference ID's and/or use the .query() method.

Categories

Resources