-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the first implementation of the Pongo implementation
- Loading branch information
1 parent
a2ae083
commit d3ed45a
Showing
14 changed files
with
350 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './main'; | ||
export * from './postgres'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { getDbClient } from './dbClient'; | ||
import type { PongoClient, PongoDb } from './typing'; | ||
|
||
export const pongoClient = (connectionString: string): PongoClient => { | ||
const dbClient = getDbClient(connectionString); | ||
|
||
return { | ||
connect: () => dbClient.connect(), | ||
close: () => dbClient.close(), | ||
db: (dbName?: string): PongoDb => | ||
dbName ? getDbClient(connectionString, dbName) : dbClient, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { postgresClient } from '../postgres'; | ||
import type { PongoCollection } from './typing'; | ||
|
||
export interface DbClient { | ||
connect(): Promise<void>; | ||
close(): Promise<void>; | ||
collection: <T>(name: string) => PongoCollection<T>; | ||
} | ||
|
||
export const getDbClient = ( | ||
connectionString: string, | ||
database?: string, | ||
): DbClient => { | ||
// This is the place where in the future could come resolution of other database types | ||
return postgresClient(connectionString, database); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './client'; | ||
export * from './dbClient'; | ||
export * from './typing'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import type { Pool } from 'pg'; | ||
import { v4 as uuid } from 'uuid'; | ||
import { | ||
type DbClient, | ||
type PongoCollection, | ||
type PongoDeleteResult, | ||
type PongoFilter, | ||
type PongoInsertResult, | ||
type PongoUpdate, | ||
type PongoUpdateResult, | ||
} from '../main'; | ||
import { constructFilterQuery } from './filter'; | ||
import { getPool } from './pool'; | ||
import { constructUpdateQuery } from './update'; | ||
import { sql } from './execute'; | ||
|
||
export const postgresClient = ( | ||
connectionString: string, | ||
database?: string, | ||
): DbClient => { | ||
const pool = getPool({ connectionString, database }); | ||
|
||
return { | ||
connect: () => Promise.resolve(), | ||
close: () => Promise.resolve(), | ||
collection: <T>(name: string) => postgresCollection<T>(name, pool), | ||
}; | ||
}; | ||
|
||
export const postgresCollection = <T>( | ||
collectionName: string, | ||
pool: Pool, | ||
): PongoCollection<T> => { | ||
const createCollection = async (): Promise<void> => { | ||
await sql( | ||
pool, | ||
'CREATE TABLE IF NOT EXISTS %I (id UUID PRIMARY KEY, data JSONB)', | ||
collectionName, | ||
); | ||
}; | ||
|
||
return { | ||
createCollection, | ||
insertOne: async (document: T): Promise<PongoInsertResult> => { | ||
await createCollection(); | ||
|
||
const id = uuid(); | ||
|
||
const result = await sql( | ||
pool, | ||
'INSERT INTO %I (id, data) VALUES (%L, %L)', | ||
collectionName, | ||
id, | ||
JSON.stringify({ ...document, _id: id }), | ||
); | ||
|
||
return result.rowCount | ||
? { insertedId: id, insertedCount: result.rowCount } | ||
: { insertedId: null, insertedCount: null }; | ||
}, | ||
updateOne: async ( | ||
filter: PongoFilter<T>, | ||
update: PongoUpdate<T>, | ||
): Promise<PongoUpdateResult> => { | ||
const filterQuery = constructFilterQuery(filter); | ||
const updateQuery = constructUpdateQuery(update); | ||
|
||
const result = await sql( | ||
pool, | ||
'UPDATE %I SET data = %s WHERE %s', | ||
collectionName, | ||
updateQuery, | ||
filterQuery, | ||
); | ||
return { modifiedCount: result.rowCount }; | ||
}, | ||
deleteOne: async (filter: PongoFilter<T>): Promise<PongoDeleteResult> => { | ||
const filterQuery = constructFilterQuery(filter); | ||
const result = await sql( | ||
pool, | ||
'DELETE FROM %I WHERE %s', | ||
collectionName, | ||
filterQuery, | ||
); | ||
return { deletedCount: result.rowCount }; | ||
}, | ||
findOne: async (filter: PongoFilter<T>): Promise<T | null> => { | ||
const filterQuery = constructFilterQuery(filter); | ||
const result = await sql( | ||
pool, | ||
'SELECT data FROM %I WHERE %s LIMIT 1', | ||
collectionName, | ||
filterQuery, | ||
); | ||
return (result.rows[0]?.data ?? null) as T | null; | ||
}, | ||
find: async (filter: PongoFilter<T>): Promise<T[]> => { | ||
const filterQuery = constructFilterQuery(filter); | ||
const result = await sql( | ||
pool, | ||
'SELECT data FROM %I WHERE %s LIMIT 1', | ||
collectionName, | ||
filterQuery, | ||
); | ||
|
||
return result.rows.map((row) => row.data as T); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { QueryResultRow, Pool, QueryResult, PoolClient } from 'pg'; | ||
import format from 'pg-format'; | ||
|
||
export const sql = async <Result extends QueryResultRow = QueryResultRow>( | ||
pool: Pool, | ||
sqlText: string, | ||
...params: unknown[] | ||
): Promise<QueryResult<Result>> => { | ||
const client = await pool.connect(); | ||
try { | ||
const query = format(sqlText, ...params); | ||
return await client.query<Result>(query); | ||
} finally { | ||
client.release(); | ||
} | ||
}; | ||
|
||
export const execute = async <Result = void>( | ||
pool: Pool, | ||
handle: (client: PoolClient) => Promise<Result>, | ||
) => { | ||
const client = await pool.connect(); | ||
try { | ||
return await handle(client); | ||
} finally { | ||
client.release(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import format from 'pg-format'; | ||
import type { PongoFilter } from '../../main'; | ||
|
||
export const constructFilterQuery = <T>(filter: PongoFilter<T>): string => { | ||
const filters = Object.entries(filter).map(([key, value]) => { | ||
if (typeof value === 'object' && !Array.isArray(value)) { | ||
return constructComplexFilterQuery(key, value as Record<string, unknown>); | ||
} else { | ||
return format('data->>%I = %L', key, value); | ||
} | ||
}); | ||
return filters.join(' AND '); | ||
}; | ||
|
||
export const constructComplexFilterQuery = ( | ||
key: string, | ||
value: Record<string, unknown>, | ||
): string => { | ||
const subFilters = Object.entries(value).map(([operator, val]) => { | ||
switch (operator) { | ||
case '$eq': | ||
return format('data->>%I = %L', key, val); | ||
case '$gt': | ||
return format('data->>%I > %L', key, val); | ||
case '$gte': | ||
return format('data->>%I >= %L', key, val); | ||
case '$lt': | ||
return format('data->>%I < %L', key, val); | ||
case '$lte': | ||
return format('data->>%I <= %L', key, val); | ||
case '$ne': | ||
return format('data->>%I != %L', key, val); | ||
case '$in': | ||
return format( | ||
'data->>%I IN (%s)', | ||
key, | ||
(val as unknown[]).map((v) => format('%L', v)).join(', '), | ||
); | ||
case '$nin': | ||
return format( | ||
'data->>%I NOT IN (%s)', | ||
key, | ||
(val as unknown[]).map((v) => format('%L', v)).join(', '), | ||
); | ||
default: | ||
throw new Error(`Unsupported operator: ${operator}`); | ||
} | ||
}); | ||
return subFilters.join(' AND '); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './client'; | ||
export * from './pool'; |
Oops, something went wrong.