Sequelize - get fields in database - node.js

The Model.rawAttributes returns the list of fields defined in sequelize.define() method. However, I want to check the fields for a particular model once the table has been created in the database. How do I do this?

Related

Is there any way to find distinct documents in mongo using nodejs

I actually want To display Full documents Without duplication
When I use distinct i can only display one particular field
How to display all fields without selecting that document which is repeated
Each document in MongoDB contains an _id field which must be unique in the collection. Hence, it is not possible to have two identical documents in a collection. Ergo, iterating a collection with no conditions will return all unique documents (which are all documents in the collection).

Mongoose How to update if exists, based on custom fields. Otherwise insert

I'm building a mongoDB database that holds sales data from multiple different systems. Each system is integrated via an node/mongoose/Express API that I'm creating for the database. Typically, you'd check the id to determine if a record already exists, and insert it if it doesn't. But since the ID from these different sources could technically overlap, I need a system to make sure that a source can only update records that originally came from that source. So I've added a column called "external_ID" where the record id from the source is saved, and another column called "integration ID", which will be unique to the specific system that sends data. But for that idea to work, I'd need to update only if those two columns matches, and otherwise insert a new record. Is that possible with MongoDB, or am I approaching this wrong?
Thank you so much.
Use upsert on update(). It will creates a new document when no document matches the query criteria.
db.collection.update(<query>, <update>, { upsert: true })
You can find more detail at Upsert Behavior documentation

How to query solr to get the complete result rather eliminating the empty values in the result

I have application which has solr search facility. When i query for search result comes only fields which has the values. How to get the result with empty field values in solr? How to get all the fields in result even though the value of a field is empty?
Only the fields present is stored with each document, so if you want to keep empty fields actually available, you'll have to explicitly index empty content into the field. How you do that depends on how you're indexing documents to Solr today.
You can work around this by fetching the schema for the core / collection you're querying first, retrieve the field names and adding empty fields for each field present in the schema, if necessary.
It'll probably be easier to assume that missing fields are empty, and wrap your code in a check to see if the field is present, and if not, return an empty value instead.
The reason for this is that while Solr uses a schema, the underlying Lucene library does not - any document can contain a set of field names unrelated to the other documents present in the index. Since the schema can change independent of the content of the index as well (a schema change will not update existing documents), returning non-existing fields isn't straight forward - and for most cases the document returned should be as close to possible to what was actually indexed.

Azure Table Storage - remove columns

I think this is not possible, but however I ask the question, maybe I have missed something.
Can we add/remove columns from an azure table?
For example by default we get those columns: PartitionKey, RowKey, Timestamp, ETag. Can I add for example another 3: FirstName, LastName, Email columns?
After that I will insert some values and I want to remove column Email and add instead column Address. Can we do this?
As Igor rightly said, Azure Tables do not have the concept of rows and columns. A table can contain zero or more entities and each entity can have a maximum of 255 attributes (an attribute is a name/value/type). Off of these 255 attributes, 3 of them are system attributes (PartitionKey, RowKey and Timestamp) which you can't update through your code. When creating an entity you define PartitionKey and RowKey and after that they become readonly properties. So when it comes to updating an entity, you can only update 252 attributes.
To manage data in an Azure Table, there's a REST API and you provide attributes of an entity in the request body. Azure Storage provides two ways by which you can update an entity - Update and Merge.
When you tell Azure Table Service to Update an entity, it simply drops all the existing attributes for that entity, and inserts the attributes defined in the request payload.
When you tell Azure Table Service to Merge an entity, it looks at the existing entity attributes and compares them with the attributes defined in the request payload. If it finds matching attributes, it simply updates those attributes. If the attributes are not present in existing entity but are defined in request payload, those attributes get added to the entity. If the attribute are present in existing entity but are not defined in request payload, they are not changed.
Now coming to your problem.
So let's say you already have an entity where you just defined PartitionKey and RowKey. Now you want to add FirstName, LastName, Email attributes. Because these attributes are not there in the entity, you can either use Merge or Replace to update the entity and these attributes will be added to the entity.
Now you want to drop Email attribute from an entity and instead add Address attribute to that entity. What you will do is perform an Update operation on that entity where your request body will have FirstName, LastName and Address attributes only (no Email attribute). When you update the entity with this request payload, Email attribute will be removed from the entity.
Azure Tables do not have "columns" as SQL tables do. Azure Tables have entities. Each entity has up to 255 properties. Most tools that let you look at an Azure table, choose to visualize the data in it tabular with columns. However, in reality, each entity (row) is a collection of properties.
Therefore, you can save objects/entities into an Azure Table with different properties, everytime you do a save. It makes things somewhat confusing, but you can do it.
HTH
I think programmatic answers given above is the best solution in terms of achieving the functionality.
But in case, you are working in the development mode and you have inserted an unnecessary attribute which you want to remove. you can do the same by using azure storage explorer.
You can export the table of choice, delete the property in the CSV file and import it back in new table. drop the existing table and rename the new table to the existing one. This is kind of a work around.

sequelize: retrieve model of a manually created table

If i create tables with sequelize API (sequelize.define), it returns a model object (User in the following example) that i can use to do queries (User.find) and other kind of operations:
var User = sequelize.define('User', {/* ... */})
If i need to create a table in the db without that api, but i need to do it with a pure sql query, is there a way to retrieve the same model object for my manual table and use it like the others?
In sequelize.models i see all my tables but not the custom one.
You need to define a model using define with the tableName property set to your manually defined table.
The DB columns that you want to retrieve as a model's attributes must meet a number of criteria. Each should have a datatype that matches the column's type. They should have the same constraints (cascade, null etc) as the DB columns. The column name should either match what Sequelize automatically generates or be specified manually using field.
Since the table for this model has already been manually created, make sure that it is not sync'ed to the DB. To test that the model you have specified will work the manually specified table you should sync the model to a test database and compare the automatically generated SQL with the manually generated SQL You are using.
This answer is based on the documentation here.

Resources