Sequelize how to check if entry exists in database
Update: see the answer which suggests using findOne()
below. I personally prefer; this answer though describes an alternative approach.
You are not returning from the isIdUnique
function:
function isIdUnique (id) {
return db.Profile.count({ where: { id: id } })
.then(count => {
if (count != 0) {
return false;
}
return true;
});
}
isIdUnique(id).then(isUnique => {
if (isUnique) {
// ...
}
});
Sequelize : How to check if a substring exists in a table
Credit to @alx for the SQL I didn't know was possible - this is how you generate the appropriate SQL with Sequelize. Note that this may not be efficient with large datasets.
const sample_fruit_string = "apple_shake";
const gsp_config = await model.fruit.findOne({
attributes: ['id'],
where: Sequelize.where(
Sequelize.fn('LOCATE', Sequelize.col('fruit_name'), sample_fruit_string),
Sequelize.Op.ne,
0
),
logging: true,
});
Generates the following:
SELECT `id` FROM `fruit` AS `fruit` WHERE LOCATE(`fruit_name`, 'apple_shake') != 0 LIMIT 1;
How to update if key exists - sequelize
Here you go :
let request = {
name: req.body.name,
province: req.body.province,
city: req.body.city,
address: req.body.address,
username: req.body.username,
type: req.body.type,
...
}
// With the help of Lodash
request = _.pickBy(request, _.identity); // <--- Will remove empty | null | undefined
Or you can use Remove blank attributes from an Object in Javascript , and use that value for updating model
And then just simply :
shopsModel.Shop.update(request,...);
NOTE :
As I can see that key name and
req.body
names are almost same , so you
can directly uselet request = req.body;
Sequelize check whether query has result or not
findAll
always return the result from the model. If the rows don't exist it returns an empty array. So
if(result){ /* do somthing */ } else { res.status(404) }
the above statement will always return true. You can check for empty array if you want to return status 404, something like below
if(result.length != 0){ /* do somthing */ } else { res.status(404) }
sequelize check if table got created
A very simple method:
SELECT 1 FROM schema_name.table_name LIMIT 0;
Raises an exception if the table does not exist. schema_name is optional. If omitted, it defaults to the "current" schema, the first schema in the search_path
.
Or:
SELECT 'schema_name.table_name'::regclass;
Raises an exception if the name isn't used at all, yet (regular table, sequence, view, materialized view, composite type or TOAST table).
Or you can query the system catalogs or the unified information schema to find out without raising an exception:
SELECT EXISTS (
SELECT 1
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'schema_name'
AND c.relname = 'table_name'
AND c.relkind = 'r' -- only tables ...(?)
);
Related answer for more details:
- How to check if a table exists in a given schema
Related Topics
Pass Dynamic Object in Onclick - Javascript/Jquery
How to Add Node Module to Angular Project If Is Not Schematics Enabled
How to Render a Word Document (.Doc, .Docx) in the Browser Using JavaScript
How to Reset the Bootstrap Modal When It Gets Closed and Open It Fresh Again
React-Native: Application Has Not Been Registered Error
How to Check If a Textbox Is Empty Using JavaScript
Remove Insignificant Trailing Zeros from a Number
How to Change Value of Object Which Is Inside an Array Using JavaScript or Jquery
How to Display a Success Message After Redirecting the User Back to My Home Page
Filtering Multiple Items With Es6 Array Functions
React Js Error: Is Not Defined React/Jsx-No-Undef
Click on Pseudo Element Using Selenium
How to Insert Dashes Between Each Two Even Digits of a Specific Number
Trouble Converting Dates String to Yyyy-Mm-Ddthh:Mm:Ss-Hh:Mm Format
Method for Splitting String into Separate Cells in Sheets/App Script
How to Make a Bot Delete Messages After a Certain Time Period