keystonejs / keystone-classic

Node.js CMS and web app framework
http://v4.keystonejs.com
MIT License
14.65k stars 2.21k forks source link

Keystone JS , what field type we can you on saving an array of objects #4966

Open Rajivkumar1234 opened 4 years ago

Rajivkumar1234 commented 4 years ago

In my program I generated an array of objects which is the ImageData , Now I want to save that data to mongo Db , I have no problem with other Keys except the ImageData key because I dont know what field type to use to be able to insert those data below which are an array of objects . What field type in keystone we could use to save those array of object example below ?. Thank you.

Model

Data.add({
    name: { type: String, required: false },
    Type: { type: Types.Select, options: 'New, Used,', index: true },
    ImageData: { type: Types.TextArray },
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 },
    },
});

JSON DATA - Key and value


 ImageData :  [ 
       { 
          "Uri":"Test.com",
          "Hash":"42e04950d6f11cd5350e3179083c7c7f",
          "Path":"/public/server/img/de29d68ab3594032bef70ead0b0d8fc2.jpg"
       },
       { 
          "Uri":"Test.com",
          "Hash":"42e04950d6f11cd5350e3179083c7c7f",
          "Path":"/public/server/img/de29d68ab3594032bef70ead0b0d8fc2.jpg"
       }
    ]
shash7 commented 4 years ago

There's no field type for something like that. Ideally, it would look like a repeater field type if you're coming from Wordpress.

You can always store the array as a type : Schema.Types.Mixed Eg:

var Schema = mongoose.Schema;
Data.schema.add({
ImageData : {
type : Schema.Types.Mixed
}
});

Keep in mind that it won't render on the admin UI. You can still use it as an array-of-objects field programmatically.

jelordreygulle commented 4 years ago

Does this works on the keystone 4 ?

shash7 commented 4 years ago

Yep, this is for keystone 4

jelordreygulle commented 4 years ago

So a seperate model that would handle the the mixed schema ?

var Schema = mongoose.Schema;
Data.schema.add({
ImageData : {
type : Schema.Types.Mixed
}
});

Data.add({
    name: { type: String, required: false },
    Type: { type: Types.Select, options: 'New, Used,', index: true },
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 },
    },
});
shash7 commented 4 years ago

So a seperate model that would handle the the mixed schema ?

var Schema = mongoose.Schema;
Data.schema.add({
ImageData : {
type : Schema.Types.Mixed
}
});

Data.add({
    name: { type: String, required: false },
    Type: { type: Types.Select, options: 'New, Used,', index: true },
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 },
    },
});

No, not a seperate schema(you can do that too if it floats your boat) but just a mixed types field. For example:

Data.add({
    name: { type: String, required: false },
    Type: { type: Types.Select, options: 'New, Used,', index: true },
    ImageData : { type : Schema.Types.Mixed },
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 },
    },
});
jelordreygulle commented 4 years ago

That would raise an error throw new Error('Unrecognised field constructor: ' + options.type);****

shash7 commented 4 years ago

That would raise an error throw new Error('Unrecognised field constructor: ' + options.type);****

Are you using keystone 4? If you can show me your model file I can try to figure out the problem.

jelordreygulle commented 4 years ago
var keystone = require('keystone');
var Types = keystone.Field.Types;
var mongoose = require('mongoose');

/**
    Vehicle
    =====
 */

var Vehicle = new keystone.List('Vehicle', {
    label: 'Vehicle',
});

var Schema = mongoose.Schema;

var vehicleImgStorage = new keystone.Storage({
    adapter: keystone.Storage.Adapters.FS,
    fs: {
        // required; path where the files should be stored  
        path: keystone.expandPath('server/public/img'),
        generateFilename: function (file, index) {
            return file.originalname;
        },
        schema: {
            url: true,

        },
        whenExists: 'error',
        // path where files will be served
        publicPath: '/public/img',
    },
});

Vehicle.add({
    name: { type: String, required: false },
    Type: { type: Types.Select, options: 'New, Used,', index: true },
    Stock: { type: String, required: false },
    ImageData : { type : Schema.Types.Mixed },
    Comment: { type: String, required: false },
    FuelType: { type: String, required: false },
    DriveType: { type: String, required: false },
    state: { type: Types.Select, options: 'draft, published, archived', default: 'published', index: true },
    author: { type: Types.Relationship, initial: true, ref: 'User', index: true },
    publishedDate: { type: Types.Date, index: true, dependsOn: { state: 'published' } },
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 },
    },
});

// Vehicle.schema.add({
//  ImageData: {
//      type: Schema.Types.Mixed
//  },
//  TestData:{
//      type: Schema.Types.Mixed

//  }
// });

Vehicle.schema.post('save', function () {
    if (!this.wasNew) return;
    if (this.author) {
        keystone.list('User').model.findById(this.author).exec(function (err, user) {
            if (user) {
                user.wasActive().save();
            }
        });
    }
});

Vehicle.track = true;
Vehicle.defaultColumns = 'Type , Model , VIN , Stock ';
Vehicle.register();
jelordreygulle commented 4 years ago

I am using Keystone 4

shash7 commented 4 years ago

I am using Keystone 4

Try this:

Vehicle.add({
    name: { type: String, required: false },
    Type: { type: Types.Select, options: "New, Used,", index: true },
    Stock: { type: String, required: false },
    //ImageData: { type: Schema.Types.Mixed },
    Comment: { type: String, required: false },
    FuelType: { type: String, required: false },
    DriveType: { type: String, required: false },
    state: {
        type: Types.Select,
        options: "draft, published, archived",
        default: "published",
        index: true
    },
    author: { type: Types.Relationship, initial: true, ref: "User", index: true },
    publishedDate: {
        type: Types.Date,
        index: true,
        dependsOn: { state: "published" }
    },
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 }
    }
});

Vehicle.schema.add({
    ImageData: {
        type: Schema.Types.Mixed
    }
});
jelordreygulle commented 4 years ago

@shash7 , does not work either

shash7 commented 4 years ago

@shash7 , does not work either

As in, does keystone crash or does the model not show imageData. Cause there won't be a UI for imageData as there's no field available for the mixed data type.

jelordreygulle commented 4 years ago

Cause there won't be a UI for imageData as there's no field available for the mixed data type , you are talking about the UI on the admin of keystone right ?

shash7 commented 4 years ago

Yep

jelordreygulle commented 4 years ago

Yep

Yeah2, I know it wont appear.

jelordreygulle commented 4 years ago

Just don't know why it does not seem to work , no error either .

shash7 commented 4 years ago

Hi sir , ah , may i ask a question about configuring keystone js plus mongo grid fs? On Thu, Sep 26, 2019 at 4:42 PM Shashwat amin @.***> wrote: Yep — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#4966?email_source=notifications&email_token=ADSFLSDIYZVDKJGVJMCXVVLQLRYXXA5CNFSM4IWBD5LKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7UZZWQ#issuecomment-535403738>, or mute the thread https://github.com/notifications/unsubscribe-auth/ADSFLSCVANTSQQBKYS57N5TQLRYXXANCNFSM4IWBD5LA .

I don't know much about mongo fs but I highly recommend using cloudinary or s3 as they make it way easier to handle files.

shash7 commented 4 years ago

Just don't know why it does not seem to work , no error either .

What do you mean it doesn't work. Give me some specifics.

jelordreygulle commented 4 years ago

Data is not being added , checked the mongo db \

shash7 commented 4 years ago

Data is not being added , checked the mongo db \

How are you adding the data? Do you have some code you can show?

wenchao1020 commented 4 years ago

you can use Types.List (in branch nested-lists-fork), and I have forked for the master in my github

ishan123456789 commented 4 years ago

Any solution for this I need to do the same? @wenchao1020 @shash7 I have an unknown object instead which may vary so can't define a schema for it

shash7 commented 4 years ago

I use this to add an object with mixed values:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var User = new keystone.List("User", {
});

User.add({
// .. add your fields here
});

User.schema.add({
    meta: {
        type: Schema.Types.Mixed
    }
});

Add this after your keystone schema definition. Keep in mind that there will be no admin UI for this.