keystone-classic icon indicating copy to clipboard operation
keystone-classic copied to clipboard

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

Open Rajivkumar1234 opened this issue 6 years ago • 23 comments

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"
       }
    ]

Rajivkumar1234 avatar Sep 12 '19 09:09 Rajivkumar1234

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.

shash7 avatar Sep 18 '19 09:09 shash7

Does this works on the keystone 4 ?

jelordreygulle avatar Sep 20 '19 03:09 jelordreygulle

Yep, this is for keystone 4

shash7 avatar Sep 20 '19 03:09 shash7

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 },
    },
});

jelordreygulle avatar Sep 20 '19 03:09 jelordreygulle

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 },
    },
});

shash7 avatar Sep 20 '19 04:09 shash7

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

jelordreygulle avatar Sep 23 '19 00:09 jelordreygulle

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.

shash7 avatar Sep 23 '19 00:09 shash7

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 avatar Sep 23 '19 00:09 jelordreygulle

I am using Keystone 4

jelordreygulle avatar Sep 23 '19 01:09 jelordreygulle

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
	}
});

shash7 avatar Sep 23 '19 06:09 shash7

@shash7 , does not work either

jelordreygulle avatar Sep 26 '19 01:09 jelordreygulle

@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.

shash7 avatar Sep 26 '19 07:09 shash7

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 ?

jelordreygulle avatar Sep 26 '19 08:09 jelordreygulle

Yep

shash7 avatar Sep 26 '19 08:09 shash7

Yep

Yeah2, I know it wont appear.

jelordreygulle avatar Sep 26 '19 08:09 jelordreygulle

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

jelordreygulle avatar Sep 26 '19 08:09 jelordreygulle

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 avatar Sep 26 '19 08:09 shash7

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.

shash7 avatar Sep 26 '19 08:09 shash7

Data is not being added , checked the mongo db \

jelordreygulle avatar Oct 02 '19 07:10 jelordreygulle

Data is not being added , checked the mongo db \

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

shash7 avatar Oct 02 '19 11:10 shash7

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

wenchao1020 avatar Oct 15 '19 08:10 wenchao1020

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

ishan123456789 avatar Mar 19 '20 10:03 ishan123456789

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.

shash7 avatar Mar 19 '20 22:03 shash7