Skip to content

Commit

Permalink
fix(server): let the db handle UUIDs, mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
jrea committed Sep 30, 2024
1 parent 6e221f2 commit b86966e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
15 changes: 14 additions & 1 deletion packages/server/src/api/routes/tenants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Config } from '../../../utils/Config';
import urlMatches from '../../utils/routes/urlMatches';
import { Routes } from '../../types';
import auth from '../../utils/auth';
import { isUUID } from '../../../db/isUUID';
import Logger from '../../../utils/Logger';

import { GET } from './GET';
Expand All @@ -11,6 +10,17 @@ import { DELETE } from './[tenantId]/DELETE';
import { PUT } from './[tenantId]/PUT';
import { POST } from './POST';

export function isUUID(value: string | null | undefined) {
if (!value) {
return false;
}
// is any UUID
const regex =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5|7][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;

return regex.test(value);
}

const key = 'TENANTS';

export default async function route(request: Request, config: Config) {
Expand All @@ -32,6 +42,9 @@ export default async function route(request: Request, config: Config) {
if (isUUID(possibleTenantId)) {
return await TENANT_GET(config, { request }, info);
}
if (possibleTenantId) {
return new Response(null, { status: 404 });
}
return await GET(config, session, { request }, info);
case 'POST':
return await POST(config, { request }, info);
Expand Down
11 changes: 3 additions & 8 deletions packages/server/src/db/DBManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Logger from '../utils/Logger';
import { ServerConfig } from '../types';

import NileDatabase from './NileInstance';
import { isUUID } from './isUUID';

export default class DBManager {
connections: Map<string, NileDatabase>;
Expand All @@ -16,10 +15,10 @@ export default class DBManager {
tenantId?: string | undefined | null,
userId?: string | undefined | null
) {
if (isUUID(tenantId) && isUUID(userId)) {
if (tenantId && userId) {
return `${tenantId}:${userId}`;
}
if (isUUID(tenantId)) {
if (tenantId) {
return `${tenantId}`;
}
return 'base';
Expand All @@ -44,11 +43,7 @@ export default class DBManager {

getConnection = (config: ServerConfig): Pool => {
const { info } = Logger(config, '[DBManager]');
let id = this.makeId(config.tenantId, config.userId);
if (id === 'base' && (config.tenantId || config.userId)) {
info('tenantId or useId unable to be validated, creating new connection');
id = crypto.randomUUID();
}
const id = this.makeId(config.tenantId, config.userId);

const existing = this.connections.get(id);
info('# of instances:', this.connections.size);
Expand Down
5 changes: 2 additions & 3 deletions packages/server/src/db/NileInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { AfterCreate } from '../types';
import Logger from '../utils/Logger';

import { createProxyForPool } from './PoolProxy';
import { isUUID } from './isUUID';

class NileDatabase {
pool: Pool;
Expand Down Expand Up @@ -96,9 +95,9 @@ function makeAfterCreate(config: Config): AfterCreate {
done(error, conn);
});

if (isUUID(config.tenantId)) {
if (config.tenantId) {
const query = [`SET nile.tenant_id = '${config.tenantId}'`];
if (isUUID(config.userId)) {
if (config.userId) {
if (!config.tenantId) {
warn('A user id cannot be set in context without a tenant id');
}
Expand Down
10 changes: 0 additions & 10 deletions packages/server/src/db/isUUID.ts
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
export function isUUID(value: string | null | undefined) {
if (!value) {
return false;
}
// is any UUID
const regex =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5|7][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;

return regex.test(value);
}

0 comments on commit b86966e

Please sign in to comment.