Is Composite Primary Key in DynamoDB for Query - primary-key

As per this link:
Supported Operations on DynamoDB
"You can query only tables that have a composite primary key (partition key and sort key)."
This doesn't seem correct though. I have a table in DynamoDB called 'users' which has a Primary Key that consists of only one attribute 'username'.
And I'm able to query this table just fine in NodeJS using only a 'KeyConditionExpression' on the attribute 'username'. Please see below:
var getUserByUsername = function (username, callback) {
var dynamodbDoc = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "users",
KeyConditionExpression: "username = :username",
ExpressionAttributeValues: {
":username": username
}
};
dynamodbDoc.query(params, function (err, data) {
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
callback(err, null);
} else {
console.log("DynamoDB Query succeeded.");
callback(null, data);
}
});
}
This code works just fine. So I'm wondering if the documentation is incorrect or am I missing something?

The documentation is correct.
"Partition Key and Sort Key – A composite primary key, composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key. DynamoDB uses the partition key value as input to an internal hash function"
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataModel.html
If a table doesn't have a sort key (range attribute), then the composite key is built from the hash key only. One of the results of that is that items won't be sorted as you like (items are sorted by sort key)

Related

How to present dynamic keys in a type struct?

I have a PostgreSQL table which has a JSONB filed. The table can be created by
create table mytable
(
id uuid primary key default gen_random_uuid(),
data jsonb not null,
);
insert into mytable (data)
values ('{
"user_roles": {
"0x101": [
"admin"
],
"0x102": [
"employee",
"customer"
]
}
}
'::json);
In above example, I am using "0x101", "0x102" to present two UIDs. In reality, it has more UIDs.
I am using jackc/pgx to read that JSONB field.
Here is my code
import (
"context"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
)
type Data struct {
UserRoles struct {
UID []string `json:"uid,omitempty"`
// ^ Above does not work because there is no fixed field called "uid".
// Instead they are "0x101", "0x102", ...
} `json:"user_roles,omitempty"`
}
type MyTable struct {
ID string
Data Data
}
pg, err := pgxpool.Connect(context.Background(), databaseURL)
sql := "SELECT data FROM mytable"
myTable := new(MyTable)
err = pg.QueryRow(context.Background(), sql).Scan(&myTable.Data)
fmt.Printf("%v", myTable.Data)
As the comment inside mentions, the above code does not work.
How to present dynamic keys in a type struct or how to return all JSONB field data? Thanks!
edit your Data struct as follows,
type Data struct {
UserRoles map[string][]string `json:"user_roles,omitempty"`
}
you can also use a uuid type as the map's key type if you are using a package like https://github.com/google/uuid for uuids.
However please note that this way if you have more than one entry in the json object user_roles for a particular user(with the same uuid), only one will be fetched.

Typescript Sequelize create data with keys linked to data in a transaction

I'm strugling with something that maybe is pretty simple.
I'm using a postgrès SQL with sequelize and typescript.
what I'm trying to do is to create two things and one as Reference on the other but if the creation of one fail then I don't want to commit anythigs.
This is my code where I'm trying to create someone and assign hime some shoes.
CREATE TABLE User
(
id BIGSERIAL PRIMARY KEY,
firstname TEXT,
lastName TEXT
);
CREATE TABLE Shoes
(
id BIGSERIAL PRIMARY KEY,
size INTEGER NOT NULL,
idUser BIGINT REFERENCES User(id) NOT NULL
);
async function operations() {
const t = await sequelize.transaction();
try {
await User.create({
firstName: 'Bart',
lastName: 'Simpson'
}, { transaction: t });
await Shoes.create({
idUser: // here I want the id of my futur new creation (bart simpson)
size: 43
}, { transaction: t });
await t.commit();
} catch (error) {
await t.rollback();
}
}
operations.then(() => {/*do something*/})
the thing is, I don't know how to get the futur Id of my new user and if I'm putting something hard like 1 if the database is empty or if I get the latest id user and I'm adding 1 then I get an error violates foreign key constraint.
I think it's because the user isn't existing in the database but it exist in the transaction.
If someone could help me :)
I fact sending a transaction in a get can also return the value that will be created in the transaction so just need to use get and send the exact same transaction inside the methode

Can I update a FaunaDB document without knowing its ID?

FaunaDB's documentation covers how to update a document, but their example assumes that I'll have the id to pass into Ref:
Ref(schema_ref, id)
client.query(
q.Update(
q.Ref(q.Collection('posts'), '192903209792046592'),
{ data: { text: "Example" },
)
)
However, I'm wondering if it's possible to update a document without knowing its id. For instance, if I have a collection of users, can I find a user by their email, and then update their record? I've tried this, but Fauna returns a 400 (Database Ref expected, String provided):
client
.query(
q.Update(
q.Match(
q.Index("users_by_email", "me#example.com")
),
{ name: "Em" }
)
)
Although Bens comments are correct, (that's the way you do it), I wanted to note that the error you are receiving is because you are missing a bracket here: "users_by_email"), "me#example.com"
The error is logical if you know that Index takes an optional database reference as second argument.
To clarify what Ben said:
If you do this you'll get another error:
Update(
Match(
Index("accounts_by_email"), "test#test.com"
),
{ data: { email: "test2#test.com"} }
)
Since Match could potentially return more then one element. It returns a set of references called a SetRef. Think of setrefs as lists that are not materialized yet. If you are certain there is only one match for that e-mail (e.g. if you set a uniqueness constraint) you can materialize it using Paginate or Get:
Get:
Update(
Select(['ref'], Get(Match(
Index("accounts_by_email"), "test#test.com"
))),
{ data: { email: 'test2#test.com'} }
)
The Get returns the complete document, we need to specify that we require the ref with Select(['ref']..
Paginate:
Update(
Select(['data', 0],
Paginate(Match(
Index("accounts_by_email"), "test#test.com"
))
),
{ data: { email: "testchanged#test.com"} }
)
You are very close! Update does require a ref. You can get one via your index though. Assuming your index has a default values setting (i.e. paging a match returns a page of refs) and you are confident that the there is a single match or the first match is the one you want then you can do Select(["ref"], Get(Match(Index("users_by_email"), "me#example.com"))) to transform your set ref to a document ref. This can then be passed into update (or to any other function that wants a document ref, like Delete).

Sequelize - Rename column with index & constraint

I want create migration with Sequelize to rename column with camelCase to have a database with column in snake_case.
I use Sequelize to create migration and use migration.
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.renameColumn('my_some_table', 'totoId', 'toto_id');
},
down: function(queryInterface, Sequelize) {
//
}
};
But... I have a unique constraint on this column (totoId) and name column, named my_some_table_name_totoId_uindex, and I also have an index on this column (totoId).
How I can force renaming column who have a unique constraint and one index?
You have to drop all the constraints, rename the column and then add the constraints back. With a single constraint on totoId it would look something like this:
// 1) drop constraint
queryInterface.removeConstraint('my_some_table', 'my_constraint');
// 2) rename column
queryInterface.renameColumn('my_some_table', 'totoId', 'toto_id');
// 3) add constraint back
queryInterface.addConstraint('my_some_table', ['toto_id'], {
type: 'unique',
name: 'my_constraint'
});
Remember that migrations should be atomic operations. So you should create 3 migrations in that order. Or even better, as #Santilli pointed out in the comments, you could create a transaction.
This will prevent from any change to be applied if one of the queries fails:
return queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.removeConstraint("my_some_table", "my_constraint", {
transaction,
});
await queryInterface.renameColumn("my_some_table", "totoId", "toto_id", {
transaction,
});
await queryInterface.addConstraint("my_some_table", ["toto_id"], {
type: "unique",
name: "my_constraint",
transaction,
});
});
Also, remember to create a transaction to revert the changes in the down function.

PouchDB Query like sql

with CouchDB is possible do queries "like" SQL. http://guide.couchdb.org/draft/cookbook.html says that
How you would do this in SQL:
SELECT field FROM table WHERE value="searchterm"
How you can do this in CouchDB:
Use case: get a result (which can be a record or set of records) associated with a key ("searchterm").
To look something up quickly, regardless of the storage mechanism, an index is needed. An index is a data structure optimized for quick search and retrieval. CouchDB’s map result is stored in such an index, which happens to be a B+ tree.
To look up a value by "searchterm", we need to put all values into the key of a view. All we need is a simple map function:
function(doc) {
if(doc.value) {
emit(doc.value, null);
}
}
This creates a list of documents that have a value field sorted by the data in the value field. To find all the records that match "searchterm", we query the view and specify the search term as a query parameter:
/database/_design/application/_view/viewname?key="searchterm"
how can I do this with PouchDB? the API provide methods to create temp view, but how I can personalize the get request with key="searchterm"?
You just add your attribute settings to the options object:
var searchterm = "boop";
db.query({map: function(doc) {
if(doc.value) {
emit(doc.value, null);
}
}, { key: searchterm }, function(err, res) { ... });
see http://pouchdb.com/api.html#query_database for more info
using regex
import PouchDB from 'pouchdb';
import PouchDBFind from 'pouchdb-find';
...
PouchDB.plugin(PouchDBFind)
const db = new PouchDB(dbName);
db.createIndex({index: {fields: ['description']}})
....
const {docs, warning} = await db.find({selector: { description: { $regex: /OVO/}}})