I'm trying to write in sequelize the following SQL query:
select * from properties where parse_input(address) LIKE parse_input('%some filter%')
where parse_input is a function which I defined in my postgres DB. Basically I want to apply the same function to the filter and to the values and then run a like comparison.
I tried with something like:
Sequelize.where(Sequelize.fn('parse_input', Sequelize.col('address')), '$iLike', Sequelize.fn('parse_input', '%some filter%'))
I see the generated query:
...AND "properties"."attribute" = parse_input("address") AND "properties"."comparator" = '$iLike' AND "properties"."logic" = '%some filter%';
but I get a errorMissingColumn error. Any hint?
Found the solution:
Sequelize.where(
Sequelize.fn('parse_input', Sequelize.col('address')), {
[Op.iLike]: Sequelize.fn('parse_input', '%some filter%')
})
Related
Is it possible to use the nextval function in a pg-promise multirow insert?
I have a database (that I sadly can't change) where the id has to be inserted via the client like this:
INSERT INTO some_object (object_id, object_name)
VALUES (nextval('some_object_seq'), ${object_name})
RETURNING object_id;
This works fine for one insert. But now I have to insert multiple rows at once and tried pgp.helpers.insert:
const cs = pgp.helpers.ColumnSet(['object_id', 'object_name'], { table });
const query = pgp.helpers.insert(values, cs) + 'RETURNING object_id';
database.many(query).then(data => {
return data
}).catch(error => {
logger.error(error, query);
});
Is there any way to use nextval('some_object_seq') in this scenario? Sadly I can't change the default value of the id column in the table definition.
Your column should be defined as this:
{
name: `object_id`,
init: () => `nextval('some_object_seq')`,
mod: `:raw`
}
As opposed to the answer by #baal, you do not need to use def, because you are not providing a default value, rather a complete override for the value, which is what init for.
And it can be used within upsert queries too.
As Bergi wrote, it is possible to add a default expression to the column set like this:
const cs = pgp.helpers.ColumnSet(
[{
name: "object_id",
// setting the DEFAULT value for this column
def: "nextval('some_object_seq')",
// use raw-text modifier to inject string directly
mod: "^",
}, 'object_name'], { table });
I am trying to create a dynamoDB table, with a secondary index with partition and sort key.
I can create the table without the secondary index, but haven't been able to find a way yet to add the secondary index
I've looked at both of these resources, but haven't found anything that actually shows me what code i need in my cdk python script to create the resource with a secondary index
https://docs.aws.amazon.com/cdk/api/latest/docs/#aws-cdk_aws-dynamodb.Table.html
https://docs.aws.amazon.com/cdk/api/latest/docs/aws-dynamodb-readme.html
This is the code that will create the table
table_name = 'event-table-name'
event_table = dynamoDB.Table(self, 'EventsTable',
table_name=table_name,
partition_key=Attribute(
name='composite',
type=AttributeType.STRING
),
sort_key=Attribute(
name='switch_ref',
type=AttributeType.STRING
),
removal_policy=core.RemovalPolicy.DESTROY,
billing_mode=BillingMode.PAY_PER_REQUEST,
stream=StreamViewType.NEW_IMAGE,
)
and this is the secondary index I need to attach to it
secondaryIndex = dynamoDB.GlobalSecondaryIndexProps(
index_name='mpan-status-index',
partition_key=Attribute(
name='field1',
type=AttributeType.STRING
),
sort_key=Attribute(
name='field2',
type=AttributeType.STRING
),
)
I've tried adding the block inside the table creation and tried calling the addSecondaryindex method on the table. But both fail either saying unexpected keyword or object has no attribute addGlobalSecondaryIndex
addGlobalSecondaryIndex should be called on the Table class.
The code below (in typescript) works perfectly for me:
const table = new ddb.Table(this, "EventsTable", {
tableName: "event-table-name",
partitionKey: { name: 'composite', type: ddb.AttributeType.STRING },
sortKey: { name: 'switch_ref', type: ddb.AttributeType.STRING },
removalPolicy: cdk.RemovalPolicy.DESTROY,
billingMode: BillingMode.PAY_PER_REQUEST,
stream: StreamViewType.NEW_IMAGE
});
table.addGlobalSecondaryIndex({
indexName: 'mpan-status-idex',
partitionKey: { name: 'field1', type: ddb.AttributeType.STRING },
sortKey: { name: 'field2', type: ddb.AttributeType.STRING }
});
For anyone looking for this and stumbling on it through google search:
create your table with the usual:
from aws_cdk import aws_dynamodb as dynamodb
from aws_cdk.aws_dynamodb import Attribute, AttributeType, ProjectionType
table = dynamodb.Table(self, 'tableID',
partition_key=Attribute(name='partition_key', type = AttributeType.STRING))
then add your global secondary indexes in much the same way:
table.add_global_secondary_index(
partition_key=Attribute(name='index_hash_key', type=AttributeType.NUMBER),
sort_key=Attribute(name='range_key', type=AttributeType.STRING),
index_name='some_index')
you can add projection attributes with they kwarg arguments:
projection_type = ProjectionType.INCLUDE,
non_key_attributes= ['list', 'of', 'attribute','names']
and projection_type defaults to All if you don't include it.
I know the docs are incomplete in lots of areas, but this is found here:
https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_dynamodb/Table.html?highlight=add_global#aws_cdk.aws_dynamodb.Table.add_global_secondary_index
Have you tried using the addGlobalSecondaryIndex method as in
event_table.addGlobalSecondaryIndex({indexName: "...", partitionKey: "...", ...})
Take a look at the documentation for the method.
aws_dynamodb.Table returns an ITable. To use the addGlobalSecondaryIndex, first cast to Table like so:
table = aws_dynamodb.Table(self, "Table",
partition_key=dynamodb.Attribute(name="id", type=dynamodb.AttributeType.STRING)
aws_dynamodb.Table(table).add_global_secondary_index(...)
I have a Json like this:
"datos_personales":
{
"nombre":"Dionel",
"apellido":"Delgado",
"fechanacimiento":"1990-12-31T04:00:00.000Z",
"lugarNacimiento":"Venezuela, Maracaibo",
"edad":25,
"genero":"Masculino",
"cedula":"21076754",
"direccion":"San Carlos",
"telefonofijo":"0262-6871111",
"telefonomovil":"0262-6871111"
},
"datos_emergencia":
{
"nombre1":"Jeaynie",
"apellido1":"Valbuena",
"telefono1":"0262-6871111",
"telefono2":"0262-6871111",
"parentesco1":"Madre",
"nombre2":"Diones",
"apellido2":"Delgado",
"telefono3":"0262-6871111",
"telefono4":"0262-6871111",
"parentesco2":"Padre"
},
"datos_sociales":
{
"civil":"Soltero",
"estudios4":true,
"ocupacion":"Programador Web",
"hijos":"No"
},
"datos_medicotratante":
{
"nombre":"Naikelin",
"apellido":"Ruiz",
"telefono1":"0262-6871111",
"telefono2":"0262-6871111",
"especialidad":"PediatrĂa",
"sangre":"AB",
"rh":"Negativo",
"seguro":"No"
}
`
I need to query these kinds of documents with nodeJS using "cedula" to find them
{"datos_personales.cedula":21076754} << in this case
I tried using this but it doesn't fetch the document
var ced = 21076754;
db.getCollection('users').find({"datos_personales.cedula":ced});
any suggestions?
"cedula":"21076754",
Your result is a string (See the quotes).
Change your query to:
var ced = "21076754";
db.getCollection('users').find({"datos_personales.cedula":ced});
Another solution to the problem is -
find() query in MongoDB returns a cursor while findOne() query returns a JSON Object which matches the argument passed in the query. You can retrieve the desired record by using the query below -:
db.getCollection('users').findOne({"datos_personales.cedula":ced});
Say, I have the following query: 'document spreadsheet app*'. I want to construct the query dictionary which is equivalent to this type of query '_search?q=_all:document+spreadsheet+app*'. Say, query_words_list = ['document', 'spreadsheet', 'app']. I tried this:
{
'query': {
'terms': {
'_all': query_words_list[:-1] + [query_words_list[-1] + '*'],
}
}
}
However, if you compare results of two queries, they are not equivalent. Any hints what query dictionary equivalent to '_search?q=_all:document+spreadsheet+app*' can look like? Thanks in advance!
The q parameter is equivalent to Query String Query.
Is there a way to select distinct rows from a table using sequelize.js?
I looked through the documentation but the "finder methods" do not specify a way to accomplish this task.
Assuming you want to apply DISTINCT to the following query:
Tuple.findAll({attributes: ['key', 'value']});
then this is a (hackish) way to achieve what you want without having to write the whole query yourself:
Tuple.findAll({attributes: [[Sequelize.literal('DISTINCT `key`'), 'key'], 'value']});
(Tested with Sequelize v2.1.0)
Edit 2015-06-08: Still works with Sequelize v3.1.1
You can do the following:
myModel.findAll({
attributes: [[sequelize.fn('DISTINCT', sequelize.col('col_name')), 'alias_name']],
where:{}
}).then(data => {}).....
taken from issues and it works.
edit your "node_modules/sequelize/lib/dialects/mysql/query-generator.js"
at around line 118
change
var query = "SELECT <%= attributes %> FROM <%= table %>"
into
var query = "SELECT " + ((options.distinct)? 'DISTINCT ':'') +"<%= attributes %> FROM <%= table %>",
now you can add an option distinct: true in your sequelize request
hope it helps -_^
Model.findAll({Attributes: ['col_name1', 'col_name2'], group: ['col_name1', 'col_name2']});
it's perfectly fine with Sequelize 5.21
It's not possible automatically but if you don't mind creating the sql on your own, you could do this:
sequelize.query('sql goes here', null, { raw: plain }).success(function(data){
console.log(data)
})
Have fun :)
UPDATE
Sequelize now uses then instead of success as promise function.
Sequelize.query has been refactored to only use paramters sql and options
raw accepts true/false but no plain as value.
So, according to the new version, the code should look like this:
sequelize.query('sql goes here', { raw: true }).then(function(data){
console.log(data);
});
This is somewhat similar to the solution proposed by Pascal Ludwig, but for those landing here looking to get a list of distinct values for a given column, you can do the following:
MyModel.aggregate('teh_field', 'DISTINCT', { plain: false }).then(...)
// Resolves to: [ { DISTINCT: value1 }, { DISTINCT: value2 }, ... ]
With that, it's easy to transform it into a standard list:
MyModel.aggregate('teh_field', 'DISTINCT', { plain: false })
.map(function (row) { return row.DISTINCT })
.then(function (tehValueList) {
// tehValueList = [ value1, value2, ... ]
})
;
As of Sequelize version 1.7, the select query has been moved into lib/dialects/abstract/query-generator.js.
Around line 1167, change
mainQueryItems.push("SELECT "+mainAttributes.join ....)
to
mainQueryItems.push('SELECT ');
if (options.distinct) {
mainQueryItems.push('DISTINCT ');
}
mainQueryItems.push(mainAttributes.join(', ') + ' FROM ' + options.table);
By the way, I use Sqlite and Postgres, both of which support "DISTINCT". If you're using a dialect that doesn't support distinct, then obviously this line will cause problems for you, since it will be generated for all the SQL flavors that you're using. I suspect this is why this simple change hasn't made it into the main Sequelize source tree.