Home > Back-end >  Kotlin:How to add arraylist into an Entity in data class
Kotlin:How to add arraylist into an Entity in data class

Time:12-04

I am trying to make a facebook clone app . And from the very begining ,I will need to define a user class . And I got stuck here , every users ,supose to have followers list and following list .How do it able to make this model ? Could you please take a look my code ,thank you so much in advance !!

@Entity(tableName="user_table")
@Parcelize
data class User(
    @PrimaryKey(autoGenerate = true)
    val userId : Int=0 ,
    val username :String,
    val email :String,
    val password : String,
    var followers: Array<User>?,
    var followings: Array<User>?,


) : Parcelable

data class UserResponse(val user:User?)

Also ,I attach my node.js model schema :

const mongoose = require('mongoose');

const router = require("express").Router();

const userSchema = new mongoose.Schema({
    username:{
        type:String,
        required : true,
        min:3,
        max:50,
    },

    email:{
        type:String,
        required:true,
        unique:true
    },
    password:{
        type:String,
        required : true,
    },
    profilePicture:{
        type:String,
        default:''
    },
    coverPicture:{
        type : String,
        default:''
    },
    followers:{
        type:Array,
        default :[]
    },
    followings:{
        type:Array,
        default:[]
    },
    isAdmin:{
        type:Boolean,
        default : false
    }

},
{timestamps:true}
);

const User = mongoose.model('User',userSchema);
exports.userSchema = userSchema;
exports.User = User;

CodePudding user response:

The first thing you should do is to change

var followers: Array<User>?,
var followings: Array<User>?

To

var followers: Array<Int>?,
var followings: Array<Int>?

Int may be the type of your userId or make it to String By doing this you will not be recursively fetching your whole database and looping for getting followers and following.

And if you are using Room to store your data , add TypeConverters.

  • Related