Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify GET package endpoint to follow creates.io API specification + add DB #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

melnikga
Copy link
Collaborator

@melnikga melnikga commented Dec 19, 2024

The database was initialized, endpoints for retrieving packages were added, and the POST endpoint was enhanced:
photo_2024-12-19_22-22-32

Request examples:

  • GET for all packages: localhost:3000/api/v1/upload

  • GET for a single package: localhost:3000/api/v1/packages/package_name/major.minor.patch

  • GET for package download: localhost:3000/api/v1/packages/package_name/major.minor.patch/download

  • POST for manual package upload: localhost:3000/api/v1/upload

Screenshot 2024-12-19 at 22 53 35
Screenshot 2024-12-19 at 22 53 50

I will continue working on this PR, as it's still necessary to implement the GET endpoint for retrieving the number of downloads:

  • GET endpoint for retrieving the number of downloads: localhost:3000/api/v1/packages/package_name/major.minor.patch/downloads
  • GET for latest package download: localhost:3000/api/v1/packages/package_name/latest/download

Copy link
Collaborator

@PeerZetZzZzZ PeerZetZzZzZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments left, pls fix

@@ -0,0 +1,33 @@
import { Migration } from '@mikro-orm/migrations';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it took some work, but I recommend to remove these SQLs.
We can use automatic schema generation for entities: npx mikro-orm schema:update --run

Having migration SQLs we have to maintain 2 things: migration SQLs + entities. It's easier to maintain only entities.

src/model/package.entity.ts Show resolved Hide resolved
@Entity()
export class Package {
@PrimaryKey()
id!: number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

src/packages/packages.controller.ts Show resolved Hide resolved
return {
package: name,
version: version,
totalDownloads: downloads.length,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we have downloads - let's remove this property (it's just downloads array length)

Comment on lines 12 to 22
@Property()
major!: number;

@Property()
minor!: number;

@Property()
patch!: number;

@Property({ unique: true })
sortableVersion!: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach will not work. Look on this:
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I have another thought on this: let's remove all of sorting on top of DB level. If needed we will add later. We don't need it anyway now, since we don't support latest. In every case we return to the users all versions, also we don't expect this "versions" load to be very big now. So pls, remove major, minor, patch from DB. Leave only 1 field: version string (composite primary key with package_name)



@Get(':name/latest/download')
async downloadLatestPackage(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this endpoint.

Our CLI tool needs information about version it downloads, because it must also update .toml file. So if client has version - can use our version specific download endpoint, not the "latest". I thought it can be useful, but no - we don't need it.

async getPackage(@Param('name') name: string, @Param('version') version: string) {
const [major, minor, patch] = version.split('.').map(Number);

const pkg = await this.em.findOne(Package, { name }, { populate: ['versions'] });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should be able to fetch package by name and version here already in 1 DB query

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have package name and version. We can do a fetch for specific package and version, not fetch whole versions. Pls fix it to avoid getting all versions. If you change primary keys to composite as I suggested above it will be index.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls remove it for now from this merge request (you can keep a code).
We just don't do upload in milesotne 1 and milestone 2

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls remove this service from PR (keep the code for later).
We will create first packages manually, later we will add "upload" support :)

Copy link
Collaborator

@PeerZetZzZzZ PeerZetZzZzZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added 1 more comment

Comment on lines 12 to 22
@Property()
major!: number;

@Property()
minor!: number;

@Property()
patch!: number;

@Property({ unique: true })
sortableVersion!: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I have another thought on this: let's remove all of sorting on top of DB level. If needed we will add later. We don't need it anyway now, since we don't support latest. In every case we return to the users all versions, also we don't expect this "versions" load to be very big now. So pls, remove major, minor, patch from DB. Leave only 1 field: version string (composite primary key with package_name)

Comment on lines 6 to 7
@PrimaryKey()
id!: number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make primary key as composite key <package, version> - it will be simpler and better index.
Is probably will work if you put @PrimaryKey() on id! and version! properties.

Comment on lines 15 to 16
@Property({ columnType: 'text' })
packageBlob!: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

@Property({ type: 'blob' }) // Maps to BLOB in the database
data!: Buffer;

then we will use blob data. Here we store .tar.gz. It should be also easy to download file

Copy link
Collaborator

@PeerZetZzZzZ PeerZetZzZzZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review 2

async getPackage(@Param('name') name: string, @Param('version') version: string) {
const [major, minor, patch] = version.split('.').map(Number);

const pkg = await this.em.findOne(Package, { name }, { populate: ['versions'] });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have package name and version. We can do a fetch for specific package and version, not fetch whole versions. Pls fix it to avoid getting all versions. If you change primary keys to composite as I suggested above it will be index.

download.version = ver;
await this.em.persistAndFlush(download);

const buffer = Buffer.from(ver.packageBlob, 'base64');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably you will not have to do this if you change version entity to use blob (it will use Buffer)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file pls for now from PR. We don't support it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants