Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
niktek committed Nov 30, 2023
1 parent eaf562b commit c7ab62c
Show file tree
Hide file tree
Showing 21 changed files with 219,073 additions and 175,867 deletions.
35 changes: 0 additions & 35 deletions CHANGELOG.md

This file was deleted.

Binary file added bun.lockb
Binary file not shown.
File renamed without changes.
File renamed without changes.
50 changes: 50 additions & 0 deletions data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export type Downloads = {
day: string;
downloads: number;
}

export type Package = {
name: string;
scope: string;
version: string;
description: string;
keywords: string[];
createdAt: Date;
updatedAt: Date;
isArchived: boolean;
isLocked: boolean;
isTemplate: boolean;
quality: number;
popularity: number;
maintenance: number;
downloads?: Downloads[];
stargazers?: string[];
maintainers?: User[];
};

export type User = {
username: string;
email: string;
}

export class PackageRegistry {
[key: string]: Package;
constructor() {
this['test'] = {
name: 'test',
scope: '',
version: '0.0.1',
description: 'This is the description',
keywords: ['keyword'],
quality: 0,
popularity: 0,
maintenance: 0,
downloads: [],
createdAt: new Date(),
updatedAt: new Date(),
isArchived: false,
isLocked: false,
isTemplate: false
};
}
}
116 changes: 116 additions & 0 deletions data/packages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import type { Package, User } from '..';
import { pool, quickQuery } from '../src/data/db';

export async function getAllPackages() {
return await quickQuery('SELECT * FROM packages', null, null);
}

export async function bulkAddKeywords(keywords: string[]) {
const values = keywords.map((keyword) => `('${keyword}')`).join(',');
const text = `INSERT INTO keywords (keyword) VALUES ${values} ON CONFLICT DO NOTHING`;
return await quickQuery(text, null, null);
}

export async function bulkAddUsers(users: User[]) {
const values = users
.map((user) => `('${user.username}', '${user.email}')`)
.join(',');
const text = `INSERT INTO users (username, email) VALUES ${values} ON CONFLICT DO NOTHING`;
return await quickQuery(text, null, null);
}

async function bulkInsertPackages(packages: Package[]): Promise<void> {
const client = await pool.connect();
try {
await client.query('BEGIN');

// Insert packages
const packageInsertText =
'INSERT INTO packages(name, scope, version, ..., keywords, maintainers) VALUES ($1, $2, $3, ..., $n, $n+1) RETURNING package_id';
for (let pkg of packages) {
const res = await client.query(packageInsertText, [
pkg.name,
pkg.scope,
pkg.version,
pkg.description,
pkg.createdAt,
pkg.updatedAt,
pkg.isArchived,
pkg.isLocked,
pkg.isTemplate,
pkg.quality,
pkg.popularity,
pkg.maintenance,
]);
const packageId = res.rows[0].package_id;

// Handle Keywords
for (let keyword of pkg.keywords) {
// Insert unique keyword
const keywordInsertText =
'INSERT INTO keywords(keyword) VALUES ($1) ON CONFLICT (keyword) DO NOTHING RETURNING keyword_id';
const keywordRes = await client.query(keywordInsertText, [
keyword,
]);

// Get the keyword_id; if new keyword inserted, use returned id; else, fetch from keywords table
const keywordId =
keywordRes.rows.length > 0
? keywordRes.rows[0].keyword_id
: (
await client.query(
'SELECT keyword_id FROM keywords WHERE keyword = $1',
[keyword]
)
).rows[0].keyword_id;

// Insert package-keyword association
const pkgKeywordInsertText =
'INSERT INTO package_keywords(package_id, keyword_id) VALUES ($1, $2)';
await client.query(pkgKeywordInsertText, [
packageId,
keywordId,
]);
}

// Handle Maintainers
if (pkg?.maintainers != undefined) {
for (let maintainer of pkg.maintainers) {
// Insert unique user/maintainer
const userInsertText =
'INSERT INTO users(username, email) VALUES ($1, $2) ON CONFLICT (username) DO NOTHING RETURNING user_id';
const userRes = await client.query(userInsertText, [
maintainer.username,
maintainer.email,
]);

// Get the user_id; if new user inserted, use returned id; else, fetch from users table
const userId =
userRes.rows.length > 0
? userRes.rows[0].user_id
: (
await client.query(
'SELECT user_id FROM users WHERE username = $1',
[maintainer.username]
)
).rows[0].user_id;

// Insert package-maintainer association
const pkgMaintainerInsertText =
'INSERT INTO package_maintainers(package_id, user_id) VALUES ($1, $2)';
await client.query(pkgMaintainerInsertText, [
packageId,
userId,
]);
}
}
}

await client.query('COMMIT');
} catch (e) {
await client.query('ROLLBACK');
throw e;
} finally {
client.release();
}
}
43 changes: 42 additions & 1 deletion data/packages_crud.pgsql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,45 @@ VALUES
('svelte'),
('skeleton')
ON CONFLICT DO NOTHING
-- bulk insert https://stackoverflow.com/questions/37300997/multi-row-insert-with-pg-promise
-- bulk insert https://stackoverflow.com/questions/37300997/multi-row-insert-with-pg-promise

-- upsert an entity with m2m
--- test tables with m2m
create table test1(test1_id bigserial not null primary key, uq_value1 text ,constraint unique_uq_val1 unique (uq_value1));

create table test2(test2_id bigserial not null primary key, uq_value2 text ,constraint unique_uq_val2 unique (uq_value2));


create table test1_test2 (test1_id bigint not null, test2_id bigint not null,
primary key (test1_id, test2_id),
constraint fk_test1 foreign key (test1_id) references test1(test1_id ),
constraint fk_test2 foreign key (test2_id) references test2(test2_id )
);
------------------------------------
-- insert value into the first table , or do a dummy update if it's already there
with
ins1 as
(insert into test1(uq_value1) values('foo') on conflict on constraint unique_uq_val1 do update
set uq_value1=test1.uq_value1
returning test1_id
)
,
-- insert value into the second table , or do a dummy update if it's already there
ins2 as
(insert into test2(uq_value2) values('bar') on conflict on constraint unique_uq_val2 do update
set uq_value2=test2.uq_value2
returning test2_id
)
,
-- select PK of inserted records (since there is exactly one record in each insert,
-- cross join is used
sel_1 as
(
select test1_id,test2_id
from ins1
cross join ins2
)
-- finally insert into link tables if such a record doesn't exist :
insert into test1_test2(test1_id, test2_id)
select * from sel_1 a
where not exists( select null from test1_test2 b where (b.test1_id, b.test2_id) = (a.test1_id, a.test2_id))
65 changes: 0 additions & 65 deletions data/schema.pgsql

This file was deleted.

Loading

0 comments on commit c7ab62c

Please sign in to comment.