@dnax/core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/ai/gemini.ts +97 -0
- package/ai/index.ts +2 -0
- package/ai/mistral.ts +63 -0
- package/app/ctrl.ts +26 -0
- package/app/hono.ts +398 -0
- package/app/index.ts +72 -0
- package/config/index.ts +68 -0
- package/define/index.ts +35 -0
- package/driver/index.ts +19 -0
- package/driver/mongo/@types.ts +44 -0
- package/driver/mongo/connect.ts +26 -0
- package/driver/mongo/index.ts +4 -0
- package/driver/mongo/rest.ts +1214 -0
- package/driver/mongo/utils.ts +361 -0
- package/index.ts +11 -0
- package/lib/asyncLocalStorage.ts +47 -0
- package/lib/collection.ts +191 -0
- package/lib/endpoint.ts +36 -0
- package/lib/index.ts +26 -0
- package/lib/media.ts +74 -0
- package/lib/schema.ts +112 -0
- package/lib/service.ts +43 -0
- package/lib/socket.ts +51 -0
- package/lib/studio.ts +12 -0
- package/lib/tenant.ts +9 -0
- package/package.json +38 -0
- package/tsconfig.json +27 -0
- package/types/index.ts +377 -0
- package/utils/index.ts +251 -0
package/config/index.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { cors } from "hono/cors";
|
|
2
|
+
import { Config } from "./../types/index";
|
|
3
|
+
import type { Config } from "../types";
|
|
4
|
+
import { Glob } from "bun";
|
|
5
|
+
import path from "path";
|
|
6
|
+
const ROOT_PATH = process.cwd();
|
|
7
|
+
const Cfg: Config = {
|
|
8
|
+
cwd: process.cwd(),
|
|
9
|
+
server: {
|
|
10
|
+
port: 4000,
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
tenants: [
|
|
14
|
+
{
|
|
15
|
+
id: "0001",
|
|
16
|
+
dir: "./default",
|
|
17
|
+
database: {
|
|
18
|
+
driver: "mongodb",
|
|
19
|
+
uri: "mongodb://localhost:27017",
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function setCfg(config: Config) {
|
|
26
|
+
Cfg.server = config.server;
|
|
27
|
+
Cfg.tenants = config.tenants;
|
|
28
|
+
|
|
29
|
+
Cfg.studio = {
|
|
30
|
+
enabled: config.studio?.enabled ?? false,
|
|
31
|
+
enableIps: config.studio?.enableIps ?? false,
|
|
32
|
+
whiteListIps: config.studio?.whiteListIps ?? [],
|
|
33
|
+
secretKey: config?.studio?.secretKey ?? "",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
if (!Cfg.studio?.secretKey && Cfg?.studio?.enabled) {
|
|
37
|
+
console.error("Please provide a studio secret key");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!Cfg?.server.port) {
|
|
41
|
+
Cfg.server.port = Number(process.env.PORT) || 4000;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
Cfg.tenants.map((t) => {
|
|
45
|
+
if (t?.enabled == undefined && t.enabled == null) {
|
|
46
|
+
t.enabled = true;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
//console.log(Cfg.tenants);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function loadCfg() {
|
|
54
|
+
let configPath = process.cwd() + `/config/app.ts`;
|
|
55
|
+
await import(path.join(configPath))
|
|
56
|
+
.then((inject: { default: Config }) => {
|
|
57
|
+
// Cfg.tenants = inject.default?.tenants || [];
|
|
58
|
+
// Cfg.server = inject.default?.server || [];
|
|
59
|
+
///console.log(inject.default);
|
|
60
|
+
setCfg(inject.default);
|
|
61
|
+
//console.log(Cfg);
|
|
62
|
+
})
|
|
63
|
+
.catch((err) => {
|
|
64
|
+
console.error("/config/app.ts not set ");
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { Cfg, setCfg, loadCfg };
|
package/define/index.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Collection, Config, Endpoint, Service, Socket } from "../types";
|
|
2
|
+
import { Cfg } from "../config/";
|
|
3
|
+
import { deepMerge, freeze } from "../utils";
|
|
4
|
+
import { config } from "valibot";
|
|
5
|
+
|
|
6
|
+
function Config(config: Config) {
|
|
7
|
+
return config;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function Collection(col: Collection) {
|
|
11
|
+
return col;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function Endpoint(config: Endpoint) {
|
|
15
|
+
return config;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function Service(config: Service) {
|
|
19
|
+
return config;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function WebSocket(config: Socket) {
|
|
23
|
+
return config;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const define = {
|
|
27
|
+
Service,
|
|
28
|
+
Config,
|
|
29
|
+
Collection,
|
|
30
|
+
Endpoint,
|
|
31
|
+
App: Config,
|
|
32
|
+
WebSocket,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export default define;
|
package/driver/index.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Cfg } from "../config";
|
|
2
|
+
import { connectToMongo } from "./mongo/connect";
|
|
3
|
+
async function connectTenantsDatabase() {
|
|
4
|
+
return new Promise(async (resolve, reject) => {
|
|
5
|
+
if (Cfg.tenants) {
|
|
6
|
+
let i = 0;
|
|
7
|
+
for await (let t of Cfg.tenants) {
|
|
8
|
+
if (t?.database?.driver == "mongodb") {
|
|
9
|
+
let tenant_ = await connectToMongo(t);
|
|
10
|
+
|
|
11
|
+
i++;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
resolve(true);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { connectTenantsDatabase };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type Lookup = {
|
|
2
|
+
from?: string;
|
|
3
|
+
localField?: string;
|
|
4
|
+
foreignField?: string;
|
|
5
|
+
as?: string;
|
|
6
|
+
pipeline?: Lookup[];
|
|
7
|
+
unwind?: Boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type findParam = {
|
|
11
|
+
$match?: object;
|
|
12
|
+
$sort?: object;
|
|
13
|
+
$skip?: number;
|
|
14
|
+
$limit?: number;
|
|
15
|
+
$include?: Array<string | Lookup>;
|
|
16
|
+
$project?: object;
|
|
17
|
+
$matchInclude?: object;
|
|
18
|
+
$sample?: {
|
|
19
|
+
size: number;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type findOneParam = {
|
|
24
|
+
$match: object;
|
|
25
|
+
$include: Array<object>;
|
|
26
|
+
$matchInclude: object;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type updateParams = {
|
|
30
|
+
$set?: object;
|
|
31
|
+
$pull?: object;
|
|
32
|
+
$push?: object;
|
|
33
|
+
$inc?: object;
|
|
34
|
+
$addToSet?: object;
|
|
35
|
+
$pop?: object;
|
|
36
|
+
$pullAll?: object;
|
|
37
|
+
$each?: object;
|
|
38
|
+
$currentDate?: object;
|
|
39
|
+
$mul?: object;
|
|
40
|
+
$unset?: object;
|
|
41
|
+
$setOnInsert?: object;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type queryParam = object;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Tenant } from "../../types";
|
|
2
|
+
|
|
3
|
+
import { MongoClient } from "mongodb";
|
|
4
|
+
import { consola } from "consola";
|
|
5
|
+
async function connectToMongo(t: Tenant) {
|
|
6
|
+
return new Promise(async (resolve, reject) => {
|
|
7
|
+
let client = new MongoClient(
|
|
8
|
+
t?.database?.uri || "mongodb://localhost:27017"
|
|
9
|
+
);
|
|
10
|
+
await client
|
|
11
|
+
.connect()
|
|
12
|
+
.then((e) => {
|
|
13
|
+
consola.log(`[${t.id}] : Connected to db ✅`.green);
|
|
14
|
+
t.database.isConnected = true;
|
|
15
|
+
t.database.client = client;
|
|
16
|
+
t.database.db = client.db();
|
|
17
|
+
resolve(t);
|
|
18
|
+
})
|
|
19
|
+
.catch((error) => {
|
|
20
|
+
console.error(error?.message);
|
|
21
|
+
t.database.isConnected = false;
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { connectToMongo };
|