Skip to content

Commit

Permalink
bug fixes, added css utils
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-schabel committed Nov 29, 2023
1 parent d23f486 commit c1450ca
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
11 changes: 9 additions & 2 deletions htmlody/css-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function lightenColor(color: { r: number; g: number; b: number }, factor: number
}

export const generateVariablesForColor = <Color extends ColorType>(color: Color) => {
let cssVariables: string[] = [];
const cssVariables: string[] = [];

const shades = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900] as const;

Expand Down Expand Up @@ -640,6 +640,13 @@ export const CSS_MAP = {
"h-px": "height: 1px;",
"w-0": "width: 0;",
"h-0": "height: 0;",
"100vh": "height: 100vh;",
"min-h-screen": "min-height: 100vh;",
"min-w-screen": "min-width: 100vw;",
"min-h-0": "min-height: 0;",
"min-w-0": "min-width: 0;",
"min-h-full": "min-height: 100%;",
"min-w-full": "min-width: 100%;",

// Display
block: "display: block;",
Expand Down Expand Up @@ -692,6 +699,7 @@ export const CSS_MAP = {
"flex-row-reverse": "flex-direction: row-reverse;",
"flex-col": "flex-direction: column;",
"flex-col-reverse": "flex-direction: column-reverse;",
"flex-grow": "flex-grow: 1;",

"items-start": "align-items: flex-start;",
"items-center": "align-items: center;",
Expand Down Expand Up @@ -880,7 +888,6 @@ export const CSS_MAP = {
"max-h-6xl": "max-height: 72rem;",
"max-h-7xl": "max-height: 80rem;",
"max-h-full": "max-height: 100%;",
"100vh": "height: 100vh;",

// to support the return type of uClass function`
"u-class": "",
Expand Down
4 changes: 2 additions & 2 deletions htmlody/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ export type {
Attributes,
ClassRecord,
ExtensionRec,
JsonHtmlNodeTree as JsonHtmlNodeTree,
JsonHtmlNodeTree,
JsonTagElNode,
} from "./htmlody-types";
export { children } from "./htmlody-utils";

export { jsonToHtml, htmlodyBuilder } from "./json-to-html-engine";
export { htmlodyBuilder, jsonToHtml } from "./json-to-html-engine";

export { classRecordPlugin, markdownPlugin } from "./htmlody-plugins";
export type {
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,5 @@
},
"bin": {
"bnkit": "sh ./scripts/cli.ts"
},
"dependencies": {
"bnkit": "latest"
}
}
}
20 changes: 15 additions & 5 deletions server/server-factory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import Bun from "bun";
import { serverRequestHandler } from "./incoming-request-handler";
import { middlewareFactory } from "./middleware-manager";
import { InferMiddlewareDataMap, MiddlewareConfigMap } from "./middleware-types";
import {
InferMiddlewareDataMap,
MiddlewareConfigMap,
} from "./middleware-types";
import { RouteHandler, Routes } from "./routes";

export const serverFactory = <
MiddlewareFactory extends ReturnType<typeof middlewareFactory>,
MiddlewareConfig extends MiddlewareConfigMap = Parameters<typeof middlewareFactory>[0],
MiddlewareDataMap extends InferMiddlewareDataMap<MiddlewareConfig> = InferMiddlewareDataMap<MiddlewareConfig>,
MiddlewareConfig extends MiddlewareConfigMap = Parameters<
typeof middlewareFactory
>[0],
MiddlewareDataMap extends InferMiddlewareDataMap<MiddlewareConfig> = InferMiddlewareDataMap<MiddlewareConfig>
>({
middleware,
routes,
Expand All @@ -17,12 +22,17 @@ export const serverFactory = <
}: {
middleware?: MiddlewareFactory;
routes: Routes<MiddlewareConfig>;
fetchHandler?: typeof serverRequestHandler<MiddlewareFactory, MiddlewareConfig, MiddlewareDataMap>;
fetchHandler?: typeof serverRequestHandler<
MiddlewareFactory,
MiddlewareConfig,
MiddlewareDataMap
>;
optionsHandler?: RouteHandler<MiddlewareDataMap>;
serve?: typeof Bun.serve;
}) => {
const start = (port: number = 3000) => {
const start = (port = 3000) => {
if (Bun?.env.NODE_ENV === "development") {
console;
console.log("Starting server on port: ", port);
}
return serve({
Expand Down
37 changes: 25 additions & 12 deletions sqlite/sqlite-utils/crud-fn-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Database from "bun:sqlite";
import { SchemaMap, SQLInfer } from "../sqlite-factory";
import { SQLInfer, SchemaMap } from "../sqlite-factory";
import {
deleteQueryString,
insertQueryString,
Expand Down Expand Up @@ -27,7 +27,6 @@ export function createItem<S extends SchemaMap>({
}: Omit<ParamsWithId, "id"> & {
item: Partial<DBItem<S>>;
returnInsertedItem?: boolean;
// key of schema for return lookup, defaults to "id"
keyForInsertLookup?: keyof SQLInfer<S> extends string ? keyof SQLInfer<S> : never;
}): SQLInfer<S> | null {
const query = insertQueryString(tableName, item);
Expand All @@ -45,14 +44,22 @@ export function createItem<S extends SchemaMap>({
// Perform the insert operation
db.query(query).run(...valuesArray);
} catch (e) {
if (debug) {
throw {
info: "CreateItem: Error during database insert operation",
message: e.message,
query,
valuesArray,
};
}

throw e;
}

const lookupKey = keyForInsertLookup ? keyForInsertLookup : "id";
const lookupValue = item[lookupKey];

if (lookupValue && lookupKey && returnInsertedItem) {
// Fetch the last inserted item using the lookupValue
const selectQuery = `SELECT * FROM ${tableName} WHERE ${lookupKey} = ?;`;
try {
const insertedItem = db.prepare(selectQuery).get(lookupValue) as SQLInfer<S>;
Expand All @@ -61,17 +68,26 @@ export function createItem<S extends SchemaMap>({

return insertedItem as SQLInfer<S>;
} catch (e) {
if (debug) {
throw {
info: "CreateItem: Error during database select operation",
message: e.message,
selectQuery,
lookupValue,
};
}
throw e;
}
}

if ((!lookupValue || !lookupKey) && returnInsertedItem) {
throw new Error(
`returnInsertedItem is true but no lookupKey or lookupValue was provided \n
const errorMsg = `returnInsertedItem is true but no lookupKey or lookupValue was provided \n
lookupKey: ${lookupKey} \n
lookupValue: ${lookupValue} \n`,
);
lookupValue: ${lookupValue} \n`;
console.error(errorMsg);
throw new Error(errorMsg);
}

return null;
}

Expand Down Expand Up @@ -111,10 +127,7 @@ interface WhereClauseResult {
}

// Function to create a WHERE clause and parameters for a SQL query
export function createWhereClause<T extends Record<string, any>>(
where: Where<T>,
debug: boolean = false,
): WhereClauseResult {
export function createWhereClause<T extends Record<string, any>>(where: Where<T>, debug = false): WhereClauseResult {
const keys = Object.keys(where) as Array<keyof T>;
const whereClause = keys.map((key) => `${String(key)} = ?`).join(" AND ");
const parameters = keys.map((key) => where[key]);
Expand Down Expand Up @@ -185,6 +198,6 @@ export function updateItem<Schema extends SchemaMap>({

export function deleteItemById({ db, debug, id, tableName }: ParamsWithId) {
const query = deleteQueryString(tableName);
if (debug) console.info(`deleteQueryString: `, query);
if (debug) console.info("deleteQueryString: ", query);
db.query(query).run({ $id: id });
}

0 comments on commit c1450ca

Please sign in to comment.