@voscarmv/aimessagestore 1.0.3
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/LICENSE +674 -0
- package/README.md +36 -0
- package/dist/db.d.ts +2 -0
- package/dist/db.d.ts.map +1 -0
- package/dist/db.js +64 -0
- package/dist/db.js.map +1 -0
- package/dist/drizzle/0000_lucky_harpoon.sql +8 -0
- package/dist/drizzle/meta/0000_snapshot.json +81 -0
- package/dist/drizzle/meta/_journal.json +13 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/s.d.ts +17 -0
- package/dist/s.d.ts.map +1 -0
- package/dist/s.js +36 -0
- package/dist/s.js.map +1 -0
- package/dist/schema.d.ts +112 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +10 -0
- package/dist/schema.js.map +1 -0
- package/dist/server.d.ts +17 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +93 -0
- package/dist/server.js.map +1 -0
- package/drizzle.config.ts +11 -0
- package/install.sh +25 -0
- package/package.json +59 -0
- package/src/index.ts +3 -0
- package/src/schema.ts +10 -0
- package/src/server.ts +121 -0
- package/tsconfig.json +45 -0
package/src/schema.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { boolean, bigint, timestamp, text, pgTable, varchar } from "drizzle-orm/pg-core";
|
|
2
|
+
|
|
3
|
+
export const messages = pgTable("messages", {
|
|
4
|
+
id: bigint({ mode: 'number' }).primaryKey().generatedAlwaysAsIdentity(),
|
|
5
|
+
user_id: varchar({ length: 255 }).notNull(),
|
|
6
|
+
message: text().notNull(),
|
|
7
|
+
queued: boolean().notNull(),
|
|
8
|
+
created_at: timestamp('created_at').notNull().defaultNow(),
|
|
9
|
+
updated_at: timestamp('updated_at').notNull().defaultNow()
|
|
10
|
+
});
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import express, { type Request, type Response, type Express } from "express";
|
|
2
|
+
import { type CorsOptions } from "cors";
|
|
3
|
+
import { drizzle, NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
4
|
+
import { messages } from './schema.js';
|
|
5
|
+
import { migrate } from 'drizzle-orm/node-postgres/migrator';
|
|
6
|
+
import cors from "cors";
|
|
7
|
+
import helmet from "helmet";
|
|
8
|
+
import morgan from "morgan";
|
|
9
|
+
import { and, asc, eq, sql } from "drizzle-orm";
|
|
10
|
+
|
|
11
|
+
export interface StoreBackend {
|
|
12
|
+
migrate: () => void;
|
|
13
|
+
listen: () => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type MessageStoreBackendParams = {
|
|
17
|
+
dbUrl: string,
|
|
18
|
+
port: number,
|
|
19
|
+
corsOpts?: CorsOptions,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export class AiMessageStoreBackend implements StoreBackend {
|
|
23
|
+
#db: NodePgDatabase<Record<string, never>>;
|
|
24
|
+
#app: Express;
|
|
25
|
+
#port: number;
|
|
26
|
+
constructor(params: MessageStoreBackendParams) {
|
|
27
|
+
this.#db = drizzle(params.dbUrl);
|
|
28
|
+
this.#app = express();
|
|
29
|
+
if (params.corsOpts) {
|
|
30
|
+
this.#app.use(cors(params.corsOpts));
|
|
31
|
+
} else {
|
|
32
|
+
this.#app.use(cors());
|
|
33
|
+
}
|
|
34
|
+
this.#app.use(helmet());
|
|
35
|
+
this.#app.use(morgan("combined"));
|
|
36
|
+
this.#app.use(express.json());
|
|
37
|
+
this.#port = params.port;
|
|
38
|
+
type CreateMessageBody = {
|
|
39
|
+
user_id: string;
|
|
40
|
+
queued: boolean;
|
|
41
|
+
msgs: string[];
|
|
42
|
+
};
|
|
43
|
+
this.#app.post(
|
|
44
|
+
"/messages",
|
|
45
|
+
async (req: Request<{}, {}, CreateMessageBody>, res: Response) => {
|
|
46
|
+
try {
|
|
47
|
+
const { user_id, queued, msgs } = req.body;
|
|
48
|
+
const response: { message: string }[] = await this.#db
|
|
49
|
+
.insert(messages)
|
|
50
|
+
.values(msgs.map((msg) => (
|
|
51
|
+
{
|
|
52
|
+
user_id,
|
|
53
|
+
queued,
|
|
54
|
+
message: msg
|
|
55
|
+
}
|
|
56
|
+
)))
|
|
57
|
+
.returning({ message: messages.message });
|
|
58
|
+
res.json(response.map(item => item.message));
|
|
59
|
+
} catch (err) {
|
|
60
|
+
res.status(500).json({ error: String(err) });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
this.#app.get(
|
|
65
|
+
"/messages/:user_id",
|
|
66
|
+
async (req: Request<{ user_id: string }>, res: Response) => {
|
|
67
|
+
try {
|
|
68
|
+
const response: { message: string }[] = await this.#db
|
|
69
|
+
.select({ message: messages.message })
|
|
70
|
+
.from(messages)
|
|
71
|
+
.where(eq(messages.user_id, req.params.user_id))
|
|
72
|
+
.orderBy(asc(messages.updated_at), asc(messages.id));
|
|
73
|
+
res.json(response.map(item => item.message));
|
|
74
|
+
} catch (err) {
|
|
75
|
+
res.status(500).json({ error: String(err) });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
this.#app.get(
|
|
80
|
+
"/messages/:user_id/queued",
|
|
81
|
+
async (req: Request<{ user_id: string }>, res: Response) => {
|
|
82
|
+
try {
|
|
83
|
+
const response: { message: string }[] = await this.#db
|
|
84
|
+
.select({ message: messages.message })
|
|
85
|
+
.from(messages)
|
|
86
|
+
.where(
|
|
87
|
+
and(
|
|
88
|
+
eq(messages.queued, true),
|
|
89
|
+
eq(messages.user_id, req.params.user_id)
|
|
90
|
+
));
|
|
91
|
+
res.json(response.map(item => item.message));
|
|
92
|
+
} catch (err) {
|
|
93
|
+
res.status(500).json({ error: String(err) });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
this.#app.put(
|
|
98
|
+
"/messages/:user_id/unqueue",
|
|
99
|
+
async (req: Request<{ user_id: string }>, res: Response) => {
|
|
100
|
+
try {
|
|
101
|
+
const response: { message: string }[] = await this.#db
|
|
102
|
+
.update(messages)
|
|
103
|
+
.set({ queued: false, updated_at: sql`now()` })
|
|
104
|
+
.where(eq(messages.user_id, req.params.user_id))
|
|
105
|
+
.returning({ message: messages.message });
|
|
106
|
+
res.json(response.map(item => item.message));
|
|
107
|
+
} catch (err) {
|
|
108
|
+
res.status(500).json({ error: String(err) });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
async migrate() {
|
|
114
|
+
await migrate(this.#db, { migrationsFolder: './dist/drizzle' });
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
listen() {
|
|
118
|
+
this.#app.listen(this.#port);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
// File Layout
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
|
|
8
|
+
// Environment Settings
|
|
9
|
+
// See also https://aka.ms/tsconfig/module
|
|
10
|
+
"module": "nodenext",
|
|
11
|
+
"target": "esnext",
|
|
12
|
+
"types": [],
|
|
13
|
+
// For nodejs:
|
|
14
|
+
// "lib": ["esnext"],
|
|
15
|
+
// "types": ["node"],
|
|
16
|
+
// and npm install -D @types/node
|
|
17
|
+
|
|
18
|
+
// Other Outputs
|
|
19
|
+
"sourceMap": true,
|
|
20
|
+
"declaration": true,
|
|
21
|
+
"declarationMap": true,
|
|
22
|
+
|
|
23
|
+
// Stricter Typechecking Options
|
|
24
|
+
"noUncheckedIndexedAccess": true,
|
|
25
|
+
"exactOptionalPropertyTypes": true,
|
|
26
|
+
|
|
27
|
+
// Style Options
|
|
28
|
+
// "noImplicitReturns": true,
|
|
29
|
+
// "noImplicitOverride": true,
|
|
30
|
+
// "noUnusedLocals": true,
|
|
31
|
+
// "noUnusedParameters": true,
|
|
32
|
+
// "noFallthroughCasesInSwitch": true,
|
|
33
|
+
// "noPropertyAccessFromIndexSignature": true,
|
|
34
|
+
|
|
35
|
+
// Recommended Options
|
|
36
|
+
"strict": true,
|
|
37
|
+
"jsx": "react-jsx",
|
|
38
|
+
"verbatimModuleSyntax": true,
|
|
39
|
+
"isolatedModules": true,
|
|
40
|
+
"noUncheckedSideEffectImports": true,
|
|
41
|
+
"moduleDetection": "force",
|
|
42
|
+
"skipLibCheck": true,
|
|
43
|
+
},
|
|
44
|
+
"include": ["src"]
|
|
45
|
+
}
|