This commit is contained in:
Rizky 2024-03-07 20:54:39 +07:00
parent f025013315
commit 43c397c743
10 changed files with 157 additions and 78 deletions

View File

@ -5,6 +5,7 @@ import { apiContext } from "../../../pkgs/core/server/api/api-ctx";
import { g } from "utils/global";
import { baseTypings } from "../../web/src/utils/script/types/base";
import { dir } from "dir";
import { prismaExtendType } from "../../web/src/utils/script/prisma-extend";
export const _ = {
url: "/code/:site_id/:action",
@ -52,24 +53,24 @@ import {
} from "react";
import * as prisma from "./prisma";
${iftext(
apiPath,
`\
apiPath,
`\
import "./api-types";
import type * as SRVAPI from "${apiPath}";
`
)}
)}
declare global {
const db: prisma.PrismaClient;
const db: prisma.PrismaClient & ${prismaExtendType};
${baseTypings}
${iftext(
apiPath,
`\
apiPath,
`\
type Api = typeof SRVAPI;
type ApiName = keyof Api;
const api: { [k in ApiName]: Awaited<Api[k]["handler"]>["_"]["api"] };
`
)}
)}
}
`

File diff suppressed because one or more lines are too long

View File

@ -8,6 +8,7 @@ import { SyncConnection } from "../type";
import { dirAsync } from "fs-jetpack";
import path from "path";
import { gunzipAsync } from "../entity/zlib";
import { prismaExtendType } from "../../../../web/src/utils/script/prisma-extend";
const decoder = new TextDecoder();
const code_startup = {
@ -80,7 +81,7 @@ export const code_action: SAction["code"]["action"] = async function (
) {
return { type: "check-typings", hash: true };
}
} catch (e) {}
} catch (e) { }
return { type: "check-typings", hash: false };
}
case "push-typings": {
@ -104,14 +105,13 @@ export const code_action: SAction["code"]["action"] = async function (
);
await Bun.write(
Bun.file(path.join(dir, "global.d.ts")),
`\
`//@ts-ignore
import type * as SRVAPI from "gen/srv/api/srv";
import { Server, WebSocketHandler } from "bun";
import prisma from "./prisma";
import prisma from "./prisma";
declare global {
const db: prisma.PrismaClient;
const db: prisma.PrismaClient & ${prismaExtendType};
type Api = typeof SRVAPI;
type ApiName = keyof Api;

View File

@ -7,6 +7,44 @@ export const dbProxy = (dburl: string) => {
{},
{
get(_, table: string) {
if (table === "_schema") {
return {
tables: async () => {
return fetchSendDb(
{
name,
action: "schema_tables",
table: "",
params: [],
},
dburl
);
},
columns: async (table: string) => {
return fetchSendDb(
{
name,
action: "schema_columns",
table,
params: [],
},
dburl
);
},
rels: async (table: string) => {
return fetchSendDb(
{
name,
action: "schema_rels",
table,
params: [],
},
dburl
);
},
};
}
if (table.startsWith("$")) {
return (...params: any[]) => {
return fetchSendDb(

View File

@ -19,7 +19,7 @@ export const EdScriptSnippet: FC<{}> = ({}) => {
onClick={() => {
p.script.do_edit(
`\
<div {...props}>
<div {...props} className={cx(props.className, "")}>
<Local
name="local"
value={
@ -59,7 +59,7 @@ effect={async (local) => {
onClick={() => {
p.script.do_edit(
`\
<div {...props}>
<div {...props} className={cx(props.className, "")}>
{[].map((item, idx) => (
<Fragment key={idx}>
<PassProp item={item} children={children} />
@ -78,7 +78,7 @@ effect={async (local) => {
onClick={() => {
p.script.do_edit(
`\
<>{true && <div {...props}>{children}</div>}</>
<>{true && <div {...props} className={cx(props.className, "")}>{children}</div>}</>
`,
true
);
@ -96,10 +96,10 @@ effect={async (local) => {
/**if condition */
true ? (
/** then */
<div {...props}>{children}</div>
<div {...props} className={cx(props.className, "")}>{children}</div>
) : (
/** else */
<div {...props}>ELSE CONDITION</div>
<div {...props} className={cx(props.className, "")}>ELSE CONDITION</div>
)
}
</>
@ -107,7 +107,7 @@ true ? (
true
);
}}
>
>
&lt;If Else /&gt;
</Button>
<Button

View File

@ -3,6 +3,7 @@ import { w } from "../../../../../utils/types/general";
import { PG } from "../../../logic/global";
import { extractProp } from "./types/prop";
import { baseTypings } from "../../../../../utils/script/types/base";
import { prismaExtendType } from "../../../../../utils/script/prisma-extend";
export type MonacoEditor = Parameters<OnMount>[0];
type Monaco = Parameters<OnMount>[1];
@ -47,9 +48,9 @@ declare module "ts:runtime/library" {
`\
declare module "ts:prisma" {
${prisma["prisma.d.ts"].replace(
`import * as runtime from './runtime/library.js';`,
`import * as runtime from 'ts:runtime/library';`
)}
`import * as runtime from './runtime/library.js';`,
`import * as runtime from 'ts:runtime/library';`
)}
}`,
`ts:prisma.d.ts`
);
@ -105,27 +106,27 @@ import {
import prisma from 'ts:prisma';
${iftext(
apiTypes,
`\
apiTypes,
`\
import "./api"
import type * as SRVAPI from "${apiPath}";`
)}
)}
declare global {;
const db: prisma.PrismaClient;
const db: prisma.PrismaClient & ${prismaExtendType};
${baseTypings}
${propText.join("\n")}
${iftext(
apiTypes,
`
apiTypes,
`
type Api = typeof SRVAPI;
type ApiName = keyof Api;
const api: { [k in ApiName]: Awaited<Api[k]["handler"]>["_"]["api"] };
`
)}
)}
}
`,

View File

@ -0,0 +1,34 @@
export const prismaExtendType = `\
{
_schema: {
tables: () => Promise<string[]>;
columns: (table: string) => Promise<
Record<
string,
{
is_pk: boolean;
type: string;
optional: boolean;
db_type: string;
default?: any
}
>
>;
rels: (table: string) => Promise<{
type: 'has-many' | 'has-one';
to: {
table: string,
fields: string[]
};
from: {
table: string,
fields: string[]
}
}>;
}
}`
const rel_types = `\
{
}`

View File

@ -2,6 +2,7 @@ import type { OnMount } from "@monaco-editor/react";
import { w } from "../types/general";
import { baseTypings } from "./types/base";
import { extractProp } from "./types/prop";
import { prismaExtendType } from "./prisma-extend";
export type MonacoEditor = Parameters<OnMount>[0];
type Monaco = Parameters<OnMount>[1];
@ -51,14 +52,14 @@ declare module "ts:runtime/library" {
`\
declare module "ts:prisma" {
${prisma["prisma.d.ts"]
.replace(
`import * as runtime from './runtime/library.js';`,
`import * as runtime from 'ts:runtime/library';`
)
.replace(
`import * as runtime from './runtime/library';`,
`import * as runtime from 'ts:runtime/library';`
)}
.replace(
`import * as runtime from './runtime/library.js';`,
`import * as runtime from 'ts:runtime/library';`
)
.replace(
`import * as runtime from './runtime/library';`,
`import * as runtime from 'ts:runtime/library';`
)}
}`,
`ts:prisma.d.ts`
);
@ -113,27 +114,27 @@ import {
import prisma from 'ts:prisma';
${iftext(
apiTypes,
`\
apiTypes,
`\
import "./api"
import type * as SRVAPI from "${apiPath}";`
)}
)}
declare global {
const db: prisma.PrismaClient;
const db: prisma.PrismaClient & ${prismaExtendType};
${baseTypings}
${propText.join("\n")}
${iftext(
apiTypes,
`
apiTypes,
`
type Api = typeof SRVAPI;
type ApiName = keyof Api;
const api: { [k in ApiName]: Awaited<Api[k]["handler"]>["_"]["api"] };
`
)}
)}
}
`,

View File

@ -57,13 +57,17 @@ export const serveStatic = {
watch(dir.path(`app/static`), async (_, filename) => {
if (filename) {
const path = join("static", filename);
const file = Bun.file(dir.path(`app/${path}`));
if (await file.exists()) {
cache.static[`/${filename}`] = {
type: mime.getType(path) || "application/octet-stream",
compression: g.mode === "prod" ? "br" : "",
content: await file.arrayBuffer(),
};
try {
const file = Bun.file(dir.path(`app/${path}`));
if (await file.exists()) {
cache.static[`/${filename}`] = {
type: mime.getType(path) || "application/octet-stream",
compression: g.mode === "prod" ? "br" : "",
content: await file.arrayBuffer(),
};
}
} catch (e: any) {
cache.static = {}
}
}
});
@ -77,7 +81,7 @@ export const serveStatic = {
if (file) {
return new Response(file.content, {
headers: {
...CORS_HEADERS,
...CORS_HEADERS,
...{ "content-type": file.type },
...(file.compression ? { "content-encoding": file.compression } : {}),
},

View File

@ -33,7 +33,7 @@ export const parcelBuild = async () => {
let decoded = false;
(async () => {
if (parcel.stdout) {
for await (const chunk of parcel.stdout) {
for await (const chunk of parcel.stdout as any) {
if (!decoded && decoder.decode(chunk).includes("✨")) {
resolve();
decoded = true;
@ -45,7 +45,7 @@ export const parcelBuild = async () => {
(async () => {
if (parcel.stderr) {
for await (const chunk of parcel.stderr) {
for await (const chunk of parcel.stderr as any) {
if (output) process.stderr.write(chunk);
}
}