Skip to content

Commit

Permalink
fix: bigserial id cast to string (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Nov 11, 2021
1 parent 504809e commit 7c6b9b4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 51 deletions.
30 changes: 15 additions & 15 deletions packages/db/db-client-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type UpsertUserOutput = {
export type User = definitions['user']

export type UserOutput = {
_id: definitions['user']['id'],
_id: string,
name: definitions['user']['name'],
email: definitions['user']['email'],
issuer: definitions['user']['issuer'],
Expand All @@ -36,7 +36,7 @@ export interface CreateAuthKeyInput {
}

export type CreateAuthKeyOutput = {
_id: definitions['auth_key']['id']
_id: string
}

export type AuthKey = {
Expand All @@ -53,7 +53,7 @@ export type AuthKeyItem = definitions['auth_key'] & {
}

export type AuthKeyItemOutput = {
_id: definitions['auth_key']['id']
_id: string
name: definitions['auth_key']['name']
secret: definitions['auth_key']['secret']
created: definitions['auth_key']['inserted_at']
Expand All @@ -67,20 +67,20 @@ export type PinsUpsertInput = {
}

export type PinItem = {
_id: definitions['pin']['id']
_id: string
status: definitions['pin']['status']
created: definitions['pin']['inserted_at']
updated: definitions['pin']['updated_at']
location: {
_id: definitions['pin_location']['id']
_id: string
peerId: definitions['pin_location']['peer_id']
peerName: definitions['pin_location']['peer_name']
region: definitions['pin_location']['region']
}
}

export type PinItemOutput = {
_id?: definitions['pin']['id']
_id?: string
status: definitions['pin']['status']
created?: definitions['pin']['inserted_at']
updated: definitions['pin']['updated_at']
Expand All @@ -90,20 +90,20 @@ export type PinItemOutput = {
}

export type PinRequestItemOutput = {
_id: definitions['pin_request']['id']
_id: string
cid: definitions['pin_request']['content_cid']
created: definitions['pin_request']['inserted_at']
}

export type PinSyncRequestItem = {
_id: definitions['pin_sync_request']['id']
_id: string
pin: {
_id: definitions['pin']['id']
_id: string
status: definitions['pin']['status']
contentCid: definitions['pin']['content_cid']
created: definitions['pin']['inserted_at']
location: {
_id?: definitions['pin_location']['id']
_id?: string
peerId: definitions['pin_location']['peer_id']
peerName?: definitions['pin_location']['peer_name']
region?: definitions['pin_location']['region']
Expand All @@ -118,7 +118,7 @@ export type PinSyncRequestOutput = {

// Backup
export type BackupOutput = {
_id: definitions['backup']['id']
_id: string
created: definitions['backup']['inserted_at']
url: definitions['backup']['url']
uploadId: definitions['backup']['upload_id']
Expand Down Expand Up @@ -147,7 +147,7 @@ export type ContentItem = {
status: definitions['pin']['status']
updated: definitions['pin']['updated_at']
location: {
_id: definitions['pin_location']['id']
_id: string
peerId: definitions['pin_location']['peer_id']
peerName: definitions['pin_location']['peer_name']
region: definitions['pin_location']['region']
Expand Down Expand Up @@ -187,12 +187,12 @@ export interface CreateUploadInput {
}

export type CreateUploadOutput = {
_id: definitions['upload']['id'],
_id: string
cid: definitions['content']['cid']
}

export type UploadItem = {
id: definitions['upload']['id']
id: string
type: definitions['upload']['type']
name?: definitions['upload']['name']
created?: definitions['upload']['inserted_at']
Expand All @@ -214,7 +214,7 @@ export type UploadItem = {
}

export type UploadItemOutput = {
_id: definitions['upload']['id']
_id: string
type: definitions['upload']['type']
name?: definitions['upload']['name']
created: definitions['upload']['inserted_at']
Expand Down
51 changes: 27 additions & 24 deletions packages/db/postgres/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './metrics.js'

const uploadQuery = `
_id:id,
_id:id::text,
type,
name,
created:inserted_at,
Expand Down Expand Up @@ -76,7 +76,7 @@ export class PostgresClient {
const { data, error } = await this._client
.from('user')
.select(`
_id:id,
_id:id::text,
issuer,
name,
email,
Expand Down Expand Up @@ -328,19 +328,19 @@ export class PostgresClient {
/** @type {{ data: Array<definitions['backup']>, error: Error }} */
const { data: backups, error } = await this._client
.from('backup')
.select('*')
.select(`
_id:id::text,
created:inserted_at,
uploadId:upload_id::text,
url
`)
.match({ upload_id: uploadId })

if (error) {
throw new DBError(error)
}

return backups.map(b => ({
_id: b.id,
created: b.inserted_at,
uploadId: b.upload_id,
url: b.url
}))
return backups
}

/**
Expand Down Expand Up @@ -399,11 +399,11 @@ export class PostgresClient {
const { data: pins, error } = await this._client
.from('pin')
.select(`
_id:id,
_id:id::text,
status,
created:inserted_at,
updated:updated_at,
location:pin_location(id, peerId:peer_id, peerName:peer_name, region)
location:pin_location(id::text, peerId:peer_id, peerName:peer_name, region)
`)
.match({ content_cid: cid })

Expand All @@ -426,7 +426,7 @@ export class PostgresClient {
const { data: pinReqs, error } = await this._client
.from('pin_request')
.select(`
_id:id,
_id:id::text,
cid:content_cid,
created:inserted_at
`)
Expand Down Expand Up @@ -491,7 +491,7 @@ export class PostgresClient {
let query = this._client
.from('pin_sync_request')
.select(`
_id:id,
_id:id::text,
pin:pin(_id:id, status, contentCid:content_cid, created:inserted_at, location:pin_location(_id:id, peerId:peer_id, peerName:peer_name, region))
`)
.order(
Expand Down Expand Up @@ -586,22 +586,25 @@ export class PostgresClient {
* @return {Promise<import('../db-client-types').CreateAuthKeyOutput>}
*/
async createKey ({ name, secret, user }) {
const now = new Date().toISOString()

/** @type {{ data: definitions['auth_key'], error: Error }} */
const { data, error } = await this._client
.from('auth_key')
.insert({
name: name,
secret: secret,
user_id: user
})
.single()
const { data, error } = await this._client.rpc('create_key', {
data: {
name,
secret,
user_id: user,
inserted_at: now,
updated_at: now
}
}).single()

if (error) {
throw new DBError(error)
}

return {
_id: data.id
_id: data
}
}

Expand All @@ -617,9 +620,9 @@ export class PostgresClient {
const { data, error } = await this._client
.from('user')
.select(`
_id:id,
_id:id::text,
issuer,
keys:auth_key_user_id_fkey(_id:id, name,secret)
keys:auth_key_user_id_fkey(_id:id::text, name,secret)
`)
.match({
issuer
Expand Down
45 changes: 33 additions & 12 deletions packages/db/postgres/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,28 @@ CREATE OR REPLACE FUNCTION json_arr_to_json_element_array(_json json)
RETURNS json[] LANGUAGE sql IMMUTABLE PARALLEL SAFE AS
'SELECT ARRAY(SELECT * FROM json_array_elements(_json))';

CREATE OR REPLACE FUNCTION create_upload(data json) RETURNS BIGINT
CREATE OR REPLACE FUNCTION create_key(data json) RETURNS TEXT
LANGUAGE plpgsql
volatile
PARALLEL UNSAFE
AS
$$
DECLARE
inserted_key_id BIGINT;
BEGIN
insert into auth_key (name, secret, user_id, inserted_at, updated_at)
VALUES (data ->> 'name',
data ->> 'secret',
(data ->> 'user_id')::BIGINT,
(data ->> 'inserted_at')::timestamptz,
(data ->> 'updated_at')::timestamptz)
returning id into inserted_key_id;

return (inserted_key_id)::TEXT;
END
$$;

CREATE OR REPLACE FUNCTION create_upload(data json) RETURNS TEXT
LANGUAGE plpgsql
volatile
PARALLEL UNSAFE
Expand Down Expand Up @@ -113,11 +134,11 @@ BEGIN
ON CONFLICT ( url ) DO NOTHING;
end loop;

return inserted_upload_id;
return (inserted_upload_id)::TEXT;
END
$$;

CREATE OR REPLACE FUNCTION upsert_pin(data json) RETURNS BIGINT
CREATE OR REPLACE FUNCTION upsert_pin(data json) RETURNS TEXT
LANGUAGE plpgsql
volatile
PARALLEL UNSAFE
Expand Down Expand Up @@ -150,11 +171,11 @@ BEGIN
"updated_at" = NOW()
returning id into pin_result_id;

return pin_location_result_id;
return (pin_location_result_id)::TEXT;
END
$$;

CREATE OR REPLACE FUNCTION user_used_storage(query_user_id BIGINT) RETURNS BIGINT
CREATE OR REPLACE FUNCTION user_used_storage(query_user_id BIGINT) RETURNS TEXT
LANGUAGE plpgsql
AS
$$
Expand All @@ -164,14 +185,14 @@ BEGIN
from upload u
join content c on c.cid = u.content_cid
where u.user_id = query_user_id and u.deleted_at is null
);
)::TEXT;
END
$$;

CREATE OR REPLACE FUNCTION user_keys_list(query_user_id BIGINT)
RETURNS TABLE
(
"id" bigint,
"id" text,
"name" text,
"secret" text,
"created" timestamptz,
Expand All @@ -180,7 +201,7 @@ CREATE OR REPLACE FUNCTION user_keys_list(query_user_id BIGINT)
LANGUAGE sql
AS
$$
select ak.id as id,
select (ak.id)::TEXT as id,
ak.name as name,
ak.secret as secret,
ak.inserted_at as created,
Expand All @@ -191,19 +212,19 @@ where ak.user_id = query_user_id and u.deleted_at is null and ak.deleted_at is n
group by ak.id
$$;

CREATE OR REPLACE FUNCTION content_dag_size_total() RETURNS BIGINT
CREATE OR REPLACE FUNCTION content_dag_size_total() RETURNS TEXT
LANGUAGE plpgsql
AS
$$
BEGIN
return(
select sum(c.dag_size)
from content c
);
)::TEXT;
END
$$;

CREATE OR REPLACE FUNCTION pin_dag_size_total() RETURNS BIGINT
CREATE OR REPLACE FUNCTION pin_dag_size_total() RETURNS TEXT
LANGUAGE plpgsql
AS
$$
Expand All @@ -212,7 +233,7 @@ BEGIN
select sum(c.dag_size)
from pin p
join content c on c.cid = p.content_cid
);
)::TEXT;
END
$$;

Expand Down

0 comments on commit 7c6b9b4

Please sign in to comment.