Skip to content

Commit

Permalink
add json
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Jan 22, 2024
1 parent 06be769 commit 186423c
Show file tree
Hide file tree
Showing 34 changed files with 757 additions and 451 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ prostgles({
user: process.env.PG_USER,
password: process.env.PG_PASS
},
onReady: async (dbo) => {
onReady: async ({ dbo }) => {

const posts = await dbo.posts.find(
{ title: { $ilike: "%car%" } },
Expand Down
6 changes: 3 additions & 3 deletions lib/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export default class AuthHandler {
res,
next,
getUser: () => this.getUser({ httpReq: req }) as any,
dbo: this.dbo,
dbo: this.dbo as DBOFullyTyped,
db: this.db,
})
})
Expand Down Expand Up @@ -449,7 +449,7 @@ export default class AuthHandler {
return;
}

onGetRequestOK?.(req, res, { getUser: () => getUser(clientReq), dbo: this.dbo, db: this.db })
onGetRequestOK?.(req, res, { getUser: () => getUser(clientReq), dbo: this.dbo as DBOFullyTyped, db: this.db })

} catch (error) {
console.error(error);
Expand Down Expand Up @@ -505,7 +505,7 @@ export default class AuthHandler {
const { responseThrottle = 500 } = this.opts;

return this.throttledFunc(async () => {
const result = await this.opts?.login?.(params, this.dbo, this.db, client);
const result = await this.opts?.login?.(params, this.dbo as DBOFullyTyped, this.db, client);
const err = {
msg: "Bad login result type. \nExpecting: undefined | null | { sid: string; expires: number } but got: " + JSON.stringify(result)
}
Expand Down
35 changes: 25 additions & 10 deletions lib/DBSchemaBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DBSchema, isObject, JSONB, TableHandler, ViewHandler } from "prostgles-types";
import { DbJoinMaker, DBSchema, isObject, JSONB, SQLHandler, TableHandler, ViewHandler } from "prostgles-types";
import prostgles from ".";
import { Auth } from "./AuthHandler";
import { DBHandlerServer, DboBuilder, escapeTSNames, postgresToTsType } from "./DboBuilder/DboBuilder";
import { DboBuilder, escapeTSNames, postgresToTsType } from "./DboBuilder/DboBuilder";
import { PublishAllOrNothing, PublishParams, PublishTableRule, PublishViewRule, } from "./PublishParser/PublishParser";
import { getJSONBSchemaTSTypes } from "./JSONBValidation/validation";
import { TableSchemaColumn } from "./DboBuilder/DboBuilderTypes";
import { DBHandlerServer, TableSchemaColumn, TX } from "./DboBuilder/DboBuilderTypes";


export const getDBSchema = (dboBuilder: DboBuilder): string => {
Expand Down Expand Up @@ -41,6 +41,10 @@ export const getDBSchema = (dboBuilder: DboBuilder): string => {
type = types.join(" | ");
}
}
/**
* Columns that are nullable or have default values can be ommitted from an insert
* Non nullable columns with default values cannot containt null values in an insert so they must contain a valid value or be omitted
*/
return `${escapeTSNames(c.name)}${c.is_nullable || c.has_default? "?" : ""}: ${type}`
}
tables.push(`${escapeTSNames(tov.name)}: {
Expand All @@ -61,16 +65,23 @@ export type DBSchemaGenerated = {
`;
}

type DBTableHandlersFromSchema<Schema = void> = Schema extends DBSchema? {
export type DBTableHandlersFromSchema<Schema = void> = Schema extends DBSchema? {
[tov_name in keyof Schema]: Schema[tov_name]["is_view"] extends true?
ViewHandler<Schema[tov_name]["columns"]> :
TableHandler<Schema[tov_name]["columns"]>
} : Record<string, TableHandler>;
} : Record<string, Partial<TableHandler>>;

export type DBOFullyTyped<Schema = void> = Schema extends DBSchema? (
DBTableHandlersFromSchema<Schema> & Pick<DBHandlerServer<DBTableHandlersFromSchema<Schema>>, "tx" | "sql">
) :
DBHandlerServer;
export type DBHandlerServerExtra<TH = Record<string, Partial<TableHandler>>, WithTransactions = true> = {
sql: SQLHandler;
} & Partial<DbJoinMaker> & (
WithTransactions extends true? { tx: TX<TH> } :
Record<string, never>
);
// export type DBOFullyTyped<Schema = void> = Schema extends DBSchema? (
// DBTableHandlersFromSchema<Schema> & DBHandlerServerExtra<DBTableHandlersFromSchema<Schema>>
// ) :
// DBHandlerServer;
export type DBOFullyTyped<Schema = void> = DBTableHandlersFromSchema<Schema> & DBHandlerServerExtra<DBTableHandlersFromSchema<Schema>>

export type PublishFullyTyped<Schema = void> = Schema extends DBSchema? (
| PublishAllOrNothing
Expand Down Expand Up @@ -113,8 +124,12 @@ export type PublishFullyTyped<Schema = void> = Schema extends DBSchema? (

return "*" as const
},
transactions: true,
onReady: ({ dbo }) => {
dbo.tdwa?.find!()
dbo.tdwa?.find!();
dbo.tx?.(t => {
t.dwa?.find!();
})
}
});

Expand Down
1 change: 1 addition & 0 deletions lib/DboBuilder/DboBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export class DboBuilder {
this.dbo.leftJoinOne ??= {};

const joinHandlers = getJoinHandlers(table);
//@ts-ignore
this.dbo.leftJoin[table] = joinHandlers.leftJoin;
this.dbo.innerJoin[table] = joinHandlers.innerJoin;
this.dbo.leftJoinOne[table] = joinHandlers.leftJoinOne;
Expand Down
18 changes: 17 additions & 1 deletion lib/DboBuilder/DboBuilderTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,28 @@ export type TX<TH = TableHandlers> = {
}

export type TableHandlers = {
[key: string]: Partial<TableHandler> | TableHandler;
[key: string]: Partial<TableHandler>;
}
export type DbTxTableHandlers = {
[key: string]: Omit<Partial<TableHandler>, "dbTx"> | Omit<TableHandler, "dbTx">;
}


export type DBHandlerServerExtra<TH = TableHandlers, WithTransactions = true> = {
sql: SQLHandler;
} & (
WithTransactions extends true? { tx: TX<TH> } :
Record<string, never>
);

// export type DBHandlerServer<TH = TableHandlers> =
// TH &
// Partial<DbJoinMaker> & {
// sql?: SQLHandler
// } & {
// tx?: TX<TH>
// }

export type DBHandlerServer<TH = TableHandlers> =
TH &
Partial<DbJoinMaker> & {
Expand Down
2 changes: 1 addition & 1 deletion lib/DboBuilder/QueryBuilder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const asNameAlias = (field: string, tableAlias?: string) => {
}

export const parseFunctionObject = (funcData: any): { funcName: string; args: any[] } => {
const makeErr = (msg: string) => `Function not specified correctly. Expecting { $funcName: ["columnName",...] } object but got: ${JSON.stringify(funcData)} \n ${msg}`
const makeErr = (msg: string) => `Function not specified correctly. Expecting { $funcName: ["columnName" | <value>, ...args] } object but got: ${JSON.stringify(funcData)} \n ${msg}`
if(!isObject(funcData)) throw makeErr("");
const keys = getKeys(funcData);
if(keys.length !== 1) throw makeErr("");
Expand Down
Loading

0 comments on commit 186423c

Please sign in to comment.