How to apply limit on sequlize table rows number? - javascript

I want to apply limit on Sequelize table declaration.
This would actually limit the rows total of the table to 1 by forbidding to add more rows into it.
How can I achieve this in the table model definition?

I don't think that it is a good idea to use database table with only one row possible.
But a technical solution could be to use a default primary key (e.g. id: 1).
This primary key won't be auto increment or anything but a static default value like integer 1.
So that you could not add any other rows than the very first one, as a primary key is unique and it's value is always equals to 1. So you would only be able to update or read its value, if that's why you're trying to achieve.
But again this sounds like an anti-SQL thing to do.

You can use the 'limit' option in sequelize:
findAll({
limit: 2,
where: { YOUR QUERY }
)}

If you want to limit records, why don't do this in the controller / service / business logic part?
Before creating a record, you can findAll or count the records/rows what the current user have and apply your rule
Or even, you can add a beforeCreate hook if apply...

Related

How to create an item with an index in DynamoDB?

I have been looking everywhere on AWS docs for any information on this and can find absolutely none. The only answer I keep getting everywhere I look is how to query or scan using a secondary index, on already-indexed data. But how do you add a value to the index-attribute of an item in the first place? I am using AWS SDK for JavaScript so JS-specific info would be most helpful, but any info on this would be so much better than what AWS has provided.
I tried to add an item with params like the following, where I simply used the names of indexes as attributes (date and timestamp):
const params = {
TableName: 'Posts_Table',
Item: {
'username' : user,
'image_id' : uuid(),
'date' : date,
'timestamp' : timestamp
}
}
But what ended up happening is date and timestamp were simply added as normal attributes that aren't able to be queried.
You've got some fundamental misunderstanding going on. You don't give enough code or examples for me to guess what you're really attempting. For example, I don't know what your table's keys are. So here's a primer:
You only write items to the base table (never directly to an index). Items can have a variety of attributes. Each item must have unique key attributes in the base table.
You can create a GSI against the table, including after the table has data. When constructing the GSI you select what its key attributes will be.
When you want to use the GSI you must specify it in the query as your Scan or Query target.
Are you trying to write to the index? You can't.
Are you trying to query the index by pointing at the base table? You can't.
Are you trying to write an item to the base table without specifying its primary keys? You can't.
How to create an item with an index in DynamoDB?
You can not create an item without an index in DynamoDB.
When you create a table, you specify the Primary Key which is your index.
When you add an item, you have to provide the Primary Key.
You can also make use of Global Secondary Indexes which technically create a new table with that index under the hood.
But what ended up happening is date and timestamp were simply added as normal attributes that aren't able to be queried.
If you want to be able to query an attribute, that attribute has to be a Primary Key (Partition or Composite) or a Global Secondary Index.

postgreSQL - how can i return all rows from 3 tables?

im actually a frontend developer not a backend developer. so believe me, its really hard for me to work with postgreSQL and i searched a lot for my question but i couldnt find exactly what i want. i want to create a query in supabase that returns all rows of 3 tables. my goal is to search in these rows for search results base on users entered text in my app. so i want to return all rows from 3 tables in a function and a request. then by using the methods of supabase js library i can filter the rows. you can understand my point better with this code example:
create
or replace function search()
returns /* what type of data should i return? */
language sql as $$
/*
* select all rows of table artists -> select all rows of table musics -> select all rows of
* table playlists
*/
$$;
then i can do something like this in js:
let query = supabase.rpc('search').ilike('name', search_text)
so how can i do something like this?
thanks for helping.
UPDATE
here are the rows with some of theie columns that are in my 3 tables:
note: the rows of my 3 tables are totally different from each other.
a row of table *musics*:
id | name | singer | listenTimes | link
1|without me|eminem|10000|www.test.com
a row of table *artists*:
id | name | monthlyListens | bio
1|eminem|123431|{bio infos}
a row of table *playlists*:
id | name | musicsId | musics | cover
1|your favorite playlist|[1,2,3]|3|www.test.com/cover.png
base on jiri baums answer, i want to create a function that has an argument. what this function does is to check the columns of rows from 3 tables to check if the argument is something like the value of a column and returns the rows that at least have a column like the value of argument. for example: the function has an argument with the value of emvnem. the rows 1 and 2 in the exampla above will be returned. because emvnem is like eminem. if its is exactly like eminem, the returned rows are row 1 and 2. so how can i implement something like this?
As a general rule, this would be a poor design:
In terms of efficiency, it's best to push filtering as close as possible to the data; PostgreSQL has ilike functionality built in, indeed the queries in supabase are based on SQL syntax. See the PostgreSQL docs for LIKE and ILIKE
SELECT *
FROM table_name
WHERE name ILIKE 'search text'
With any real amount of data, sending everything to the front-end for filtering will be prohibitive.
In terms of access to data, if you send the whole table to the front end, the user will be able to copy it, save it, do other things with it; in most cases, you probably prefer to avoid that.
If you nevertheless want to do this, the operator you're looking for is UNION; the PostgreSQL docs have an example of doing pretty much exactly this:
SELECT distributors.name
FROM distributors
WHERE distributors.name LIKE 'W%'
UNION
SELECT actors.name
FROM actors
WHERE actors.name LIKE 'W%';
Link: https://www.postgresql.org/docs/14/sql-select.html

Each Player has 50 items [saving that in SQL]

I have two issues that both resemble each other.
Each player has access to 50 inventory slots. 1 slot holds 1 item.
I figured that I can make a table of items. Items have qualities and ID's.
I have come to this solution:
Each character table can have a textbox that holds json data that is key value pairs of item slots [0-49] and item id's, since every item has a unique id.
However I am not sure this is the most elegant solution.
And then this problem, that should be solved the same way? [I think]
Each player has access to 50 skills. Each skill can grow 100 levels.
I wouldn't encode JSON data in a database -- you want a separate table to link things together.
So, maybe player_item_map which would have fields item_id, player_id (and maybe an internal id if you ever need to refer to the map itself). Then you link arbitrary numbers of items to users (you can use constraints in item ownership is unique, and enforce the item limit somewhere else). Alternatively prepopulate an inventory table with the slots, using the fields player_id, slot, item_id (can be null). Then you don't need to insert or worry about missing rows.
You can do similar for skills, but had a skill_level field as well.

In handsontable - convert number to date by function and load refresh more data

I am using handsontable for creating a CRUD (create read update delete) interface for my databases which now require these two things.
Able to create/update/delete rows/cells which are changed (and not the whole data set)
Able to load more data from database (as new records are added) or refresh changed rows
-
Part 1 (Solved) - http://jsfiddle.net/p7KwM/
So I added a field name modified as timestamp (INT) to database so that data can be refreshed at user side by checking this modified field, and it is INT so that TZ value can be added as per user. See here http://demo.mgvz.com/.twilio/loader.pl and now I am stuck at that I want to modify this INT to add TZ value and to covert it to date-time format within handsontable. (Can't do it server side) If it can be modified by way of function within handsontable, else the only option is to modify it before giving it to handsontable.
Part 2 (any help appreciated)
Second, I have to add rows which are created on server and update rows which are changed on server, without effecting other unchanged rows, (a user might be editing other rows), and to maintain sorting while adding the rows.
Can anyone guide me about it.
Thanks
To update data, have a look at setDataAtCell method:
setDataAtCell (row: Number, col: Number, value: Mixed, source: String (Optional))
To add new rows created on server, have a look at the alter method:
alter ('insert_row', index: Number, amount: Number (Optional), source: String (Optional))
You can find more detailed description of those 2 methods here
So to update something that was changed on server:
$container.handsontable('setDataAtCell', rowIndex, colNumber, "New Value");
And to add new:
var rowIndex = 2; //You will need to determine this to maintain sorting, or set to null to add as last row.
var numberOfRows 1; //Only adding one row at a time
$container.handsontable('alter', 'insert_row', rowIndex, colNumber);
//After row is added you can update the values of each column using setDataAtCell as per above
$container.handsontable('setDataAtCell', rowIndex, 1, "FirstName");
$container.handsontable('setDataAtCell', rowIndex, 2, "LastName");
//One line for each column or have a look at the setDataAtCell method for alternative option
You may enocunter issues if the user is for example editing values that need updating, or what happens if you add a row while user is editing?
I hope any of this helps

How to count the number of rows in a jqGrid?

How do I count the number of rows in a jqGrid?
To clarify, there is not much data involved so the grid is pulling all of its data back from the server in a single query, instead of using pagination.
jQuery("#myGrid").jqGrid('getGridParam', 'records');
Update
Note there are two parameters to determine record count:
records
integer
Readonly property. Gives the number of records returned as a result of a query to the server.
reccount
integer
Readonly property. Determines the exact number of rows in the grid. Do not confuse this with records parameter. Although in many cases they may be equal, there are cases where they are not. For example, if you define rowNum to be 15, but the request to the server returns 20 records, the records parameter will be 20, but the reccount parameter will be 15 (the grid you will have 15 records and not 20).
$("#grid").getGridParam("reccount");
Readonly property. Returns integer. Determines the exact number of rows in the grid. (And not the number of records fetched).
More information here.
Here is the code I have so far. It seems like there should be a better way:
jQuery("#myGrid").getDataIDs().length;
How about this?
jQuery("#myGrid tr").length;
Actually, you can take that a step further with the optional context parameter.
jQuery("tr", "#myGrid").length;
Either one will search for every "tr" inside of "#myGrid". However, from my own testing, specifying the context parameter is usually faster.
jQuery("#myGrid").jqGrid('getGridParam', 'records');
You could try:
jQuery("#GridId").jqGrid('getDataIDs');

Categories