Skip to content

Commit

Permalink
transform data before insert to supabase
Browse files Browse the repository at this point in the history
  • Loading branch information
chanwit-y committed Nov 5, 2024
1 parent 34ba1e0 commit 55b0e60
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 30 deletions.
11 changes: 11 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/openai_res.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"content": "- แปลเป็นภาษาไทย: บรรพบุรุษ, สิ่งที่มาก่อน\n- คำอ่านในภาษาอังกฤษ: แอน-ติ-ซี-เดนท์\n- ประเภทของคำ: คำนาม (Noun)\n- ตัวอย่างประโยคในภาษาอังกฤษ: In grammar, the noun \"dog\" is the antecedent of the pronoun \"it\" in the sentence \"The dog chased its tail.\"",
"refusal": null
},
"logprobs": null,
"logprobs": null,
"finish_reason": "stop"
}
],
Expand Down
2 changes: 1 addition & 1 deletion libs/api/openai.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { injectable } from "inversify";
import OpenAI from "openai";
import { Env } from "../config/index.ts";
import { Env } from "../utils/config/index.ts";

const openai = new OpenAI({
apiKey: Env.openaiApiKey,
Expand Down
2 changes: 1 addition & 1 deletion libs/mod/auth/repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AuthTokenResponsePassword, SupabaseClient, UserResponse } from "@supabase/supabase-js";

import { injectable } from "inversify";
import { supabase } from "../../db/index.ts";
import { supabase } from "../../utils/db/index.ts";

export interface IUserRepository {
// auth(): Promise<any>;
Expand Down
4 changes: 2 additions & 2 deletions libs/mod/auth/routes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IUserService } from "./service.ts";
import { Hono } from "hono";
import { setCookie } from "hono/cookie";
import { zValidator } from "../../middleware/zodValidator.middleware.ts";
import { zValidator } from "../../utils/middleware/zodValidator.middleware.ts";
import { z } from "zod";
import { container, Instances } from "../../config/container.ts";
import { container, Instances } from "../../utils/config/container.ts";

const authRoutes = new Hono().post(
"/sign-in",
Expand Down
19 changes: 11 additions & 8 deletions libs/mod/vocabulary/repository.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import type { SupabaseClient } from "@supabase/supabase-js";
import { injectable } from "inversify";
import { Env } from "../../config/index.ts";
import { supabase } from "../../db/index.ts";

import "reflect-metadata";
import { supabase } from "../../utils/db/index.ts";
import { Env } from "../../utils/config/index.ts";

type TVocabulary = {
word: string;
content: string;
remark: string;
thai: string;
english: string;
type: string;
example: string;
};

export interface IVocabularyRepository {
Expand Down Expand Up @@ -46,12 +50,11 @@ export class VocabularyRepository implements IVocabularyRepository {
return data;
}
public async insert(v: TVocabulary) {
// const auth = await this._db.auth.signInWithPassword({
// email: Env.supabaseUser!,
// password: Env.supabasePass!,
// });

// console.log("auth", auth);
// TODO: Fix this
await this._db.auth.signInWithPassword({
email: Env.supabaseUser!,
password: Env.supabasePass!,
});
const { data, error } = await this._db.from(TableName).insert([{ ...v }]);
if (error) {
console.error(error);
Expand Down
17 changes: 12 additions & 5 deletions libs/mod/vocabulary/service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { transform } from './../../utils/common/transform.ts';
import { injectable, inject } from "inversify";
import type { IVocabularyRepository, VocabularyRepository } from "./repository.ts";
import type { IVocabularyRepository } from "./repository.ts";
import { OpenAIAPI } from "../../api/openai.api.ts";

import "reflect-metadata";

export interface IVocabularyService {
insert(word: string): Promise<any>;
// TODO: move to auth service file
// auth(): Promise<any>;
// getUser(token: string): Promise<any>;
}

@injectable()
Expand Down Expand Up @@ -42,10 +40,19 @@ export class VocabularyService implements IVocabularyService {
console.log(`call openai: ${word}`);
const res = await this._openai.translate(word);
const content = res.choices[0].message.content ?? "";
const {thai, english, example, type, remark} = transform(content);
console.log("==============")
console.log(content)
// console.log(data);
console.log("==============")
vocabulary = await this._repo.insert({
word,
content,
remark: "auto",
remark,
thai,
english,
type,
example,
});
}
return vocabulary;
Expand Down
32 changes: 32 additions & 0 deletions libs/utils/common/transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const transform = (content: string) => {
return content.split("\n").reduce(
(p, c) => {
// console.log("p", p);
// console.log("c", c.split(":"));
const [k, v] = c.split(":");
return { ...p, [key(k)]: v.trim() };
},
{
thai: "",
english: "",
type: "",
example: "",
remark: "",
}
);
};

const key = (k: string) => {
switch (k.replace("-", "").trim()) {
case "แปลเป็นภาษาไทย":
return "thai";
case "คำอ่านในภาษาอังกฤษ":
return "english";
case "ประเภทของคำ":
return "type";
case "ตัวอย่างประโยคในภาษาอังกฤษ":
return "example";
default:
return "remark";
}
};
17 changes: 5 additions & 12 deletions libs/utils/config/container.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { IUserRepository } from "./../mod/auth/repository.ts";
import { Container } from "inversify";
import {
VocabularyRepository,
type IVocabularyRepository,
} from "../mod/vocabulary/repository.ts";
import { OpenAIAPI } from "../api/openai.api.ts";
import {
VocabularyService,
type IVocabularyService,
} from "../mod/vocabulary/service.ts";
import { UserRepository } from "../mod/auth/repository.ts";
import { UserService, type IUserService } from "../mod/auth/service.ts";
import { VocabularyRepository, type IVocabularyRepository } from "../../mod/vocabulary/repository.ts";
import { OpenAIAPI } from "../../api/openai.api.ts";
import { VocabularyService, type IVocabularyService } from "../../mod/vocabulary/service.ts";
import { UserRepository, type IUserRepository } from "../../mod/auth/repository.ts";
import { UserService, type IUserService } from "../../mod/auth/service.ts";

enum Instances {
OpenAIAPI = "OpenAIAPI",
Expand Down
16 changes: 16 additions & 0 deletions test/transform_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { assertEquals } from "jsr:@std/assert";
import { transform } from "../libs/utils/common/transform.ts";

Deno.test("transform", () => {
const input = `- แปลเป็นภาษาไทย: บรรพบุรุษ, สิ่งที่มาก่อน\n- คำอ่านในภาษาอังกฤษ: แอน-ติ-ซี-เดนท์\n- ประเภทของคำ: คำนาม (Noun)\n- ตัวอย่างประโยคในภาษาอังกฤษ: In grammar, the noun "dog" is the antecedent of the pronoun "it" in the sentence "The dog chased its tail."`;
const result = transform(input);
console.log("result", result);
assertEquals(result, {
thai: "บรรพบุรุษ, สิ่งที่มาก่อน",
english: "แอน-ติ-ซี-เดนท์",
type: "คำนาม (Noun)",
example:
'In grammar, the noun "dog" is the antecedent of the pronoun "it" in the sentence "The dog chased its tail."',
remark: "",
});
});

0 comments on commit 55b0e60

Please sign in to comment.