how to design the infinity in sqlite3 with sequelize and nodejs? - database

How should i define the infinity in my database.
i use
nodejs
fastify
sequelize
sqlite3
There are a few data should deal with:
deadline: unlimited time or a date, type:string format:date
usetimes: infinity type:number
Do i need to change the type and validation to fix with the "infinity" words.such as:
//model
usetimes: {
type : DataTypes.Integer, ======> DataTypes.['string','Interger']
allowNull: false,
// validate : {
// min: -1
// }
},
when i change the type,how can i do the validation?
Please tell me how to deal with the infinity.

Related

SequelizeDatabaseError Data truncated for column position at row 4

I am getting a SequelizeDatabaseError Data truncated for column 'position' at row 4 while sending an update request as below: Any advise on how to fix the database error ?
Executing (default): UPDATE user SET photo=?,name=?,email=?,phonenumber=?,position=?,password=?,updatedAt=? WHERE email = ?
SequelizeDatabaseError: Data truncated for column 'position' at row 4
at Query.formatError (C:\Project\soccerpep\node_modules\sequelize\lib\dialects\mysql\query.js:244:16)
at Execute.handler [as onResult] (C:\Project\soccerpep\node_modules\sequelize\lib\dialects\mysql\query.js:51:23)
module.exports = function(sequelize, DataTypes) {
return sequelize.define('user', {
name: {
type: DataTypes.STRING(30),
allowNull: false
},
email: {
type: DataTypes.STRING(100),
allowNull: false
},
phonenumber: {
type: DataTypes.STRING(50),
},
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
password: {
type: DataTypes.STRING(50),
allowNull: false
},
privilege: {
type: DataTypes.ENUM('PLAYER','ADMIN'),
},
photo: {
type: DataTypes.STRING(30),
},
position: {
type: DataTypes.ENUM('FORWARD','MID-FIELD','DEFENDER','GK'),
}
}, {
tableName: 'user'
});
};
server.js
const UserModel = userSchema(sequelize, DataTypes);
app.put('/service/profile', async (req, res, next) => {
try {
const userEmail = req.query.email;
var selector = {
where: { email: userEmail }
};
const updatePlayer = await UserModel.update(req.body, selector);
console.log("Server side update method log:" + updatePlayer);
res.status(200).json({ success: true });
} catch (err) {
return next(err);
}
});
I think the issue is with ENUM, you can use that only with Postgres and not with mysql , ( Ref )
position: {
type: DataTypes.ENUM('FORWARD','MID-FIELD','DEFENDER','GK'),
}
Change it to simple string type and check again,
position: {
type: DataTypes.STRING(30)
}
NOTE : You might need to create table again or update the field
manually after changing this in model else you will still get the
error
If you want to use ENUMs in mysql you should use it as so
position: {
type: DataTypes.ENUM,
values: ['player', 'admin],
defaultValue: 'monthly' // A default value just incase you fail to provide one...
},
I do hope this helps...
Change datatype is not the solution. You have to check the data that you try to save. eg: if ENUM("0", "1") and you try to saved data 0 then it's showing that type of error. You have to save like that "0". Then it's not showing such type of error message again.
i have a lot spending my day to tracing this error.
i have following all solution but doesnt work.
in my case and my bad is...
i forgot have clone table/duplicate/history/trigger that save old and new data.
make sure your history table have same data type.
i have table like these
form3
hs_form3 // its mean history with revision (number of revisi data) column and action column (create/update/delete/any)
just for description
CREATE TABLE hs_form3 LIKE form3;
ALTER TABLE hs_form3 MODIFY COLUMN id int(11) NOT NULL,
DROP PRIMARY KEY, ENGINE = InnoDB, ADD action VARCHAR(8) DEFAULT 'insert' FIRST,
ADD revision INT(6) NOT NULL AUTO_INCREMENT AFTER action,
ADD dt_datetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER revision,
ADD PRIMARY KEY (revision),
ADD KEY `user_id` (`user_id`),
and i didnt run migration on server because a lot of change , i only run on localhost. in server i just put a new table by export import or added manual. before i got issue about migration on server and doesnt capture last change in sql table and dont have time to resolve.

Field 'name' on type 'RemoteDependencyData' is too long

I've just added Application Insights to my React application. When it attempts to contact the Application Insights server to notify it about Google Adverts loading, I get a 400 error which says: "Field 'name' on type 'RemoteDependencyData' is too long"
I've looked through the config for ApplicationInsights and don't find a way to shorten the name or to ignore certain fetches.
Here is my configuration of the plugin:
initialize(reactPluginConfig) {
const instrumentationKey = publicRuntimeConfig.AppInsights.InstrumentKey
this.appInsights = new ApplicationInsights({
config: {
instrumentationKey,
maxBatchInterval: 0,
disableFetchTracking: false,
extensions: [this.reactPlugin],
extensionConfig: {
[this.reactPlugin.identifier]: reactPluginConfig
}
}
})
this.appInsights.loadAppInsights()
}
Here's the kind of value I'm seeing being passed:
name: "GET https://securepubads.g.doubleclick.net/gampad/ads?gdfp_req=1&pvsid=3899864230170460&correlator=1176166438184394&output=json_html&callback=googletag.impl.pubads.callbackProxy3&impl=fifs&adsid=AGt39rSWeAHAYcWHd55Pv2v0Hn4qiAL6Sl5QTuM8A7Vy0061-z2dWmbQdMXwo-8N5G4e1e5VaGTTm0lLSoER&jar=2019-6-18-23&json_a=1&eid=21063203%2C21063317%2C22316438&vrg=2019061301&guci=2.2.0.0.2.2.0.0&plat=1%3A32776%2C2%3A32776%2C8%3A32776&sc=0&sfv=1-0-33&ecs=20190618&iu_parts=12671339%2Caa_sbdept_bottom_728x90&enc_prev_ius=0%2F1&prev_iu_szs=728x90&prev_scp=department%3Dbody-armor%26category%3DBody%2520Armor%2520%2526%2520Protection&cookie_enabled=1&bc=31&abxe=1&lmt=1560900694&dt=1560900694440&dlt=1560900618064&idt=11742&frm=20&biw=2543&bih=400&oid=3&adxs=1244&adys=2379&adks=1434505235&ucis=d&ifi=9&u_tz=-420&u_his=4&u_h=1440&u_w=2560&u_ah=1440&u_aw=2560&u_cd=24&u_nplug=3&u_nmime=4&u_sd=1&flash=0&url=http%3A%2F%2Flocalhost%3A3000%2Fdepartment%2F10A56330.aspx&dssz=15&icsg=48955391&std=20&vis=2&dmc=8&scr_x=0&scr_y=0&psz=728x90&msz=728x90&blev=0.98&bisch=1&ga_vid=1753854358.1560900630&ga_sid=1560900630&ga_hid=551901891&fws=0"
You can add this telemetry initializer below your call to loadAppInsights to modify your telemetry items on the fly. In this case, it will truncate the RemoteDependencyData name field.
appInsights.addTelemetryInitializer(item => {
if (item.baseType === 'RemoteDependencyData') {
if (item.baseData.name.length > 1024) {
item.baseData.name = item.baseData.name.substr(0, 1024);
}
}
});

sequelize model versioning and optimistic

I am wondering if there was an easy way to add versioning to model for easy optimistic concurrency. I was curious if anyone here has integrated that into their project with sequelize and got it to work seamless, without having to manually add the version to the where of every update ect.
I started with something like this
export const User = sequelize.define('user', {
id: {type: Sequelize.STRING, primaryKey: true},
name: {type: Sequelize.STRING, allowNull: false}
}, {
underscored: true,
tableName: 'r_users',
version: true // <- here
});
but the version doesn't change when updating the record or migration
The migration version can be found in SequelizeMeta but to select from it you need to add quotes 'SequelizeMeta' or change the name of the table to sequelize_meta by add
"migrationStorageTableName": "sequelize_meta",
to the config

How do I get Sails.js to query an existing database?

There's a dev database already set up for another project. I'm trying to create a sails.js server to connect to this database and act as a RESTful API. I'm also using SQL Workbench with the profile below to connect to the database and verify my query statements. On that tool, I'm able to send queries like select top 10 * from advisor and get the data I expect in response.
My connection configuration in sails.js seems to be alright, since I'm able to start the server. I've gotten simple static actions to work, like hi: function (req, res) { return res.send("Hi there!"); }. However, I can't figure out what to do to get a response from the database served by sails. My goal (at this point) is to have http://localhost:1337/advisor return JSON for the results of select top 10 * from advisor.
I initially tried using the freshly-generated model. Then, I tried adding attributes to the model file. Then, I tried adding my own code to the controller. In each case, the browser never received a response. At the end, I tested /advisor/list to run my own code and it doesn't look like the query() callback was ever executed. In case it's the first question, I have run npm install sails-sqlserver and I've double-checked that my host, db, username, & password are identical to what was used in Workbench.
connections.js
sqlserver: {
adapter: 'sails-sqlserver',
user: 'myusername',
password: 'mypassword',
host: 'mysubdomain.mydomain.net:1433',
database: 'frontofficedev'
}
models.js
module.exports.models = {
connection: 'sqlserver',
migrate: 'safe'
};
api\models\Advisor.js
module.exports = {
attributes: {
advcode: 'string',
advname: 'string',
'adv-default': 'boolean',
"user-id": 'string',
"pc-code": 'string',
"adv-tag": 'string',
"is-group": 'boolean',
"trade-grouping": 'string',
AdvisorId: 'int',
orgcode: 'string',
BranchId: 'int',
OrdPrnBranchId: 'int',
zdec1: 'float',
zdec2: 'float',
zchar1: 'string',
zchar2: 'string',
zchar3: 'string',
zchar4: 'string',
AdvStatus: 'string'
}
};
api\controllers
module.exports = {
hi: function (req, res) {
return res.send("Hi there!");
},
list: function (req, res) {
var myQuery = "select TOP 10 * from advisor";
sails.log.debug("Query :", myQuery);
console.log(Advisor);
Advisor.query(myQuery, function (err, advisors){
console.log(advisors);
console.log(err);
if(err || !advisors.rows.length){
return res.json({"status": 0, "error": err});
}
else{
return res.json(advisors);
}
});
}
};
Any ideas on what I'm doing wrong? Is JDBC causing problems? Thanks in advance.
Im assuming you've already run: npm install sails-sqlserver --save
You have to specify your connection and the table you will be using in the model, the variables in the model should match with your DB variables, like this:
api\models\Advisor.js
module.exports = {
schema: true,
connection: 'sqlserver',
tableName: 'yourTableName',
attributes: {
advcode:{
type: 'string',
primaryKey: true //if this is a primary key
},
advname:{
type: 'string'
},
'adv-default':{
type: 'boolean'
}
};
In your controller you can use the Sails ORM waterline like this:
api\controllers
module.exports = {
list: function (req, res) {
Advisor.query('SELECT * FROM advisor', function(err, results) {
if (err) {
res.send(400);
} else {
res.send(results);
}
});
}
};
Where Advisor is the model.
For more specific information about models and ORM waterline i recommend you read the sails docs: http://sailsjs.org/documentation/reference/waterline-orm/models
My colleague spotted the problem. The port that database lives on needs to be a separate attribute in sails' connection.js (instead of including it in the host string). No need for extra libraries, like node-jdbc.
config/connections.js
sqlserver: {
adapter: 'sails-sqlserver',
user: 'myusername',
password: 'mypassword',
host: 'mysubdomain.mydomain.net',
port: 1433,
database: 'frontofficedev'
}
After making that change, I was able to delete all my custom code from the controller and almost everything from the model (I still need to specify a primary key, since sails looks for id by default and the database was using AdvisorId.
api/models/Advisor.js
module.exports = {
attributes: {
AdvisorId: {primaryKey: true}
}
};

Encrypt all data with nodejs + angularjs + mongoose

I would like to encrypt all the datas that is being saved in mongoose. Do you know some plugins or some moduls, in nodejs, that does this ? And how could i do it efficiently with angularjs in frontend ?
For example : i'm using a chat system, using socket.io. And i store the messages in this model :
var messageSchema = new Schema({
type: {
type: String,
required: false
},
user: {
type: String,
default: '',
trim: true
},
content: {
type: String,
default: '',
trim: true
},
slug: {
type: String,
lowercase: true,
trim: true
},
created: Date,
updated: [Date],
roomCreator: {
type: Schema.ObjectId,
ref: 'Room'
},
});
I want all this data to be encrypted in the most secure way as possible, so nobody should be able to see any of the contents of the messages.
Thanks in advance
There is this:
ChatSafe
Though I am not sure how I feel about it though. It has the ability to use different cipher keys, but it has no inherent way to get keys from one client to the other, which is necessary to decrypt messages (apparently you have to send them a url, which is how other clients get the cipher key). It is cool that it does all the encryption client-side though.
I think I would work this one though: Implement AES Strength Encryption With JavaScript
It shows you how to build a client-side angular based encryption service.
Encrypt everything client side > shoot it to node > save/do whatever > send it to other clients > decrypt client side.

Resources