I am using node express in a project. I am getting or setting data like
Service.find({ $and:
[{ name: req.body.service },
{ order: req.body.order }]
})
Now I want to add a column in the same table and set a default value. For example, in service table I want to add a column 'deleted' and set it to false for all the current rows. How to do that?
You have to add the column in your model definition and set the default value.
columnName : {
type : String,
default : 'Abcd'
},
// For your case
isDeleted : {
type : Boolean,
default : false
},
Related
I have a model where I have a column name data_numbers and its type is DataTypes.RANGE(sequelize.INTEGER)
I am using the following line of code to create a new row in table
models.TABLE.create({
data_numbers: ?? //here is what I should write to save a range of numbers.
})
Here you go. While saving data in column type range you have to assign an array of Range type, like following
const TABLE = sequelize.define(
"table",
{
data_numbers: DataTypes.RANGE(sequelize.INTEGER),
},
{
underscored: true,
paranoid: true,
defaultScope: {
attributes: [
"id",
"data_numbers",
],
},
}
);
and while creating new object I added value like following
models.TABLE.create({
data_numbers: [1, 199]
})
I'm using the expires field in a mongoose schema in order to set expiration on a given field. For instance:
var Crumbs = new Schema({
...
timestamp: {
type: Date,
expires: 3600,
required: true
},
...
});
The effect is the expected one and the following index is created at MongoDB if the index doesn't previously exist:
> db.crumbs.getIndexes()
[
...
{
"v" : 2,
"key" : {
"timestamp" : 1
},
"name" : "timestamp_1",
"ns" : "mydb.crumbs",
"expireAfterSeconds" : 36000,
"background" : true
}
]
However, if the index previously exists it is not created. For instance, if I change to expires: 360 in my code and I re-run my program, "expireAfterSeconds" at MongoDB keeps being 36000.
Is there any way to force the index to use the value defined in the schema, even if the index already exists?
You cannot do it from schema definition.
The best way is to drop index directly in the db.
Alternatively, drop it from the script. E.g.
mongoose.connections[0].collections("collectionname")
.dropIndex("propertyName",callback)
Thanks to Alex Blex hint, I have finally solved this. First droping the index, then recreating with the new expiration value:
mongoose.connections[0].collections.crumbs.dropIndex({timestamp: 1}, function (error) {
if (error) {
// This happens typically if the index doesn't exist but it doesn't hurt anyway
}
mongoose.connections[0].collections.crumbs.ensureIndex({timestamp: 1}, {expireAfterSeconds: 360});
});
What I want to do:
Whenever I add a new item to the collection (in my case a game), it will have a incremented "index"-like value (in my case I'm naming it index too).
My games collection should looks like:
[
{ "index":0, ... data }
{ "index":1, ... data }
{ "index":2, ... data }
]
The term is so hard to search. I always end up with:
$inc for update. Not this, I want to have incremented number on create.
Schema.index does look like what I want, but somehow it doesn't work at all:
const gameModel = new Schema({
index: {
type: Number,
default: 0
},
players: [{
name: String,
score: Number
}]
}, {
timestamps: {
createdAt: 'date'
}
});
gameModel.index({
index: 1
});
With this I always get index: 0. If I turn off default no index is created.
What do I do now? (I would prefer to keep the _id intact)
You can use a npm package named mongoose-auto-increment which provides this functionality. It is also very easy and well documented
I'm just trying to add a number to a number set in DynamoDB. This expression was working with an untyped list. But to save space since it will just be storing numbers I moved everything to plain number sets. Now no matter how much I tinker with it I can't get it to go through.
var phoneID = req.body.PhoneID;
var category = req.body.ratingCategory;
var ratingToAdd = [Number(req.body.rating)]
var dbparams = {
"TableName": "Venue_Ratings",
Key: {
"PhoneID" : phoneID
},
"UpdateExpression": "SET #categoryName = list_append(#categoryName, :rating)",
"ExpressionAttributeNames" : {
"#categoryName" : category
},
"ExpressionAttributeValues": {
":rating": ratingToAdd
},
"ReturnValues": "ALL_NEW"
};
This error is being thrown An operand in the update expression has an incorrect data type
I have also tried changing the update expression to an ADD expression instead like so ADD #categoryName :rating.
I've tried changing ratingToAdd to a plain number not in an array, a string in an array, and a plain string not in an array.
I'm calling the db using the docClient.update method.
I have verified that the sets in the db are in fact number sets and that they exist.
What am I missing here? Thanks for the help.
The below code should add the number to the Number Set (i.e. DynamoDB data type 'NS').
Use this function with ADD in UpdateExpression:-
docClient.createSet([Number(5)])
Code:-
var params = {
TableName : "Movies",
Key : {
"yearkey" : 2016,
"title" : "The Big New Movie 1"
},
UpdateExpression : "ADD #category :categorySet",
ExpressionAttributeNames: {
'#category' : 'category'
},
ExpressionAttributeValues: {':categorySet' : docClient.createSet( [Number(5)])},
ReturnValues: 'UPDATED_NEW'
};
I use the test framework Frisby and I want to check that two propery have the same value as in the exampel below where count must be equal to total_count.
I have been looking around but haven't any way to check that two property have
the same value.
Here is the code that I use to validate the GET.The call to the API list all the users that are registered in the system.
frisby.create('GET Method')
.get(urlMultiNetworkAdminUser)
.expectStatus(200)
.expectJSON({
})
.expectJSONTypes({
rows : Array,
count : String,
total_count : String
})
.expectJSONTypes('rows.*' ,{
id : Number,
setting_group_id: function(val) { expect(val).toBeTypeOrNull(Number); },
login : String,
email : String,
language_code : String,
role : String,
ismultinetworkadmin : Boolean,
real_name : String,
subscribe_to_issues : Boolean,
external_account : Boolean,
href : String
}).toss();
{
"rows":
[
{
"id":109,
"setting_group_id":null,
"login":"Admin",
"email":"dise.server#dise.com",
"language_code":"en",
"role":"user",
"ismultinetworkadmin":false,
"real_name":"Amin",
"subscribe_to_issues":false,
"external_account":false,
"href":"/api/v1/users/109"
},
{
"id":110,
"setting_group_id":null,
"login":"User",
"email":"dise.server#dise.com",
"language_code":"en",
"role":"user",
"ismultinetworkadmin":false,
"real_name":"User",
"subscribe_to_issues":false,
"external_account":false,
"href":"/api/v1/users/110"
}
],
"count":"2",
"total_count":"2"
}
Many Thanks
to check deep equality - which i think is what you want to do - use underscore ...
expect(_.isEqual(actual, expected)).toBe(true);
you could also use lodash or write your own function to check for deep equality.