Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Rajivkumar1234 opened this issue Sep 12, 2019 · 23 comments
Open

Comments

@Rajivkumar1234
Copy link

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
Copy link

shash7 commented Sep 18, 2019

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
Copy link

Does this works on the keystone 4 ?

@shash7
Copy link

shash7 commented Sep 20, 2019

Yep, this is for keystone 4

@jelordreygulle
Copy link

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
Copy link

shash7 commented Sep 20, 2019

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
Copy link

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

@shash7
Copy link

shash7 commented Sep 23, 2019

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
Copy link

jelordreygulle commented Sep 23, 2019

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
Copy link

I am using Keystone 4

@shash7
Copy link

shash7 commented Sep 23, 2019

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
Copy link

@shash7 , does not work either

@shash7
Copy link

shash7 commented Sep 26, 2019

@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
Copy link

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
Copy link

shash7 commented Sep 26, 2019

Yep

@jelordreygulle
Copy link

Yep

Yeah2, I know it wont appear.

@jelordreygulle
Copy link

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

@shash7
Copy link

shash7 commented Sep 26, 2019

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
Copy link

shash7 commented Sep 26, 2019

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
Copy link

jelordreygulle commented Oct 2, 2019

Data is not being added , checked the mongo db
\

@shash7
Copy link

shash7 commented Oct 2, 2019

Data is not being added , checked the mongo db
\

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

@wenchao1020
Copy link

wenchao1020 commented Oct 15, 2019

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

@ishan123456789
Copy link

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
Copy link

shash7 commented Mar 19, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants