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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for list_append #544

Merged
merged 2 commits into from
Jan 20, 2019
Merged

Adding support for list_append #544

merged 2 commits into from
Jan 20, 2019

Conversation

fishcharlie
Copy link
Member

Summary:

This PR adds support for list_append when updating a document.

Code sample:

Schema

{
	id: {
		type: Number,
		validate: function(v) {
			return v > 0;
		}
	},
	items: {
		type: "list",
		list: [{
			name: {
				type: String,
				required: true
			},
			amount: {
				type: Number,
				required: true
			}
		}]
	}
}

Model

dynamoose.model('Cat', schema);

General

Cats.Cat13.create({
	id: 1000,
	items: [{
		name: "item 2",
		amount: 25
	}]
}, function() {
	Cats.Cat13.update({
		id: 1000
	}, {
		$ADD: {
			items: [{
				name: "item 1",
				amount: 50
			}]
		}
	}, function(err, data) {
		should.not.exist(err);
		should.exist(data);
		data.id.should.eql(1000);
		data.items.should.eql([{
			name: "item 2",
			amount: 25
		}, {
			name: "item 1",
			amount: 50
		}]);
		Cats.Cat13.get(1000, function(err, cat) {
			should.not.exist(err);
			should.exist(cat);
			cat.id.should.eql(1000);
			cat.items.should.eql([{
				name: "item 2",
				amount: 25
			}, {
				name: "item 1",
				amount: 50
			}]);
			done();
		});
	});
});

Other information:

Type (select 1):

  • Bug fix
  • Feature implementation
  • Documentation improvement
  • Testing improvement
  • Test added to report bug (GitHub issue #--- @---)
  • Something not listed here

Is this a breaking change? (select 1):

  • 馃毃 YES 馃毃
  • No
  • I'm not sure

Is this ready to be merged into Dynamoose? (select 1):

  • Yes
  • No

Are all the tests currently passing on this PR? (select 1):

  • Yes
  • No

Other:

  • I have searched through the GitHub pull requests to ensure this PR has not already been submitted
  • I have updated the Dynamoose documentation (if required) given the changes I made
  • I have added/updated the Dynamoose test cases (if required) given the changes I made
  • I have run npm test from the root of the project directory to ensure all tests continue to pass
  • I agree that all changes made in this pull request may be distributed and are made available in accordance with the Dynamoose license
  • All of my commits and commit messages are detailed, explain what changes were made, and are easy to follow and understand
  • I have confirmed that all my code changes are indented properly using 2 spaces
  • I have filled out all fields above

@fishcharlie fishcharlie changed the title Adding support for list append Adding support for list_append Jan 20, 2019
@coveralls
Copy link

coveralls commented Jan 20, 2019

Pull Request Test Coverage Report for Build 863

  • 15 of 15 (100.0%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.05%) to 85.593%

Files with Coverage Reduction New Missed Lines %
lib/Table.js 2 78.59%
Totals Coverage Status
Change from base Build 853: -0.05%
Covered Lines: 2217
Relevant Lines: 2513

馃挍 - Coveralls

@fishcharlie fishcharlie merged commit ec865ea into dynamoose:master Jan 20, 2019
@fishcharlie fishcharlie deleted the listappendsupport branch January 20, 2019 23:36
@patrickianco
Copy link

patrickianco commented Mar 15, 2019

Hello I'm having problems with the append to list function. I keep getting the error

message: 'Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: STRING'

I have the following schema
const schema = new dynamoose.Schema( { messenger_id: { type: String, hashKey: true }, is_active: { type: Boolean, required: true }, first_name: { type: String }, last_name: { type: String }, body: { type: "list", list: [ { text: { type: String, required: true }, attachments: { type: [Object], required: true }, type: { type: String, enum: ["text", "image", "template"], required: true }, is_echo: { type: Boolean, required: true }, timestamp: { type: Date, required: true } } ] } }, { useDocumentTypes: false } );

const model = dynamoose.model('livechat', schema);

Here is the create item
model .create({ messenger_id: 12345, is_active: true, first_name: "Patrick", last_name: "Co", body: [ { text: "Hello", attachments: [], type: "text", is_echo: true, timestamp: Date.now() } ] })

Here is the update
model .update( { messenger_id: 12345 }, { $ADD: { body: [ { text: "Hello po", attachments: [], type: "text", is_echo: true, timestamp: Date.now() } ] } } )

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

Successfully merging this pull request may close these issues.

None yet

3 participants