Skip to content

Commit

Permalink
account sort key
Browse files Browse the repository at this point in the history
  • Loading branch information
if1live committed Jun 28, 2024
1 parent 71864ae commit eeac3c0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
31 changes: 24 additions & 7 deletions packages/api/etc/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import { assert } from "@toss/assert";
import {
CamelCasePlugin,
Kysely,
Expand Down Expand Up @@ -40,7 +41,7 @@ const presetSchema = z.object({
),
});

const dataPath = "./etc/";
const dataPath = "d:/dev/yuuka/personal-financial-statements/fixtures";

const readConfig = async () => {
const fp = path.resolve(dataPath, "config.json");
Expand Down Expand Up @@ -90,20 +91,26 @@ const execute_schema = async (db: Kysely<MyDatabase>) => {

const query_account = async (db: Kysely<MyDatabase>) => {
// 복사해서 바로 돌릴수 있는 쿼리를 기대
for (const item of accounts) {
for (let i = 0; i < accounts.length; i++) {
const item = accounts[i];
assert(item);

const sortKey = (i + 1) * 10;
const sql = `
insert into "yuuka"."account" ("user_id", "name", "description")
values ('${userId}', '${item.name}', '${item.description}');`.trimStart();
insert into "yuuka"."account" ("user_id", "name", "description", "sort_key")
values ('${userId}', '${item.name}', '${item.description}', ${sortKey});`.trimStart();
console.log(sql);
}
};

const execute_account = async (db: Kysely<MyDatabase>) => {
const items = accounts.map((item): AccountTable.NewRow => {
const items = accounts.map((item, idx): AccountTable.NewRow => {
const sortKey = (idx + 1) * 10;
return {
userId,
name: item.name,
description: item.description,
sortKey,
};
});

Expand Down Expand Up @@ -180,5 +187,15 @@ const main_sqlite = async () => {
await fs.writeFile(filename, Buffer.from(bytes));
};

// await main_pg();
await main_sqlite();
const target = process.argv[process.argv.length - 1];
switch (target) {
case "pg":
await main_pg();
break;
case "sqlite":
await main_sqlite();
break;
default:
console.log("Usage: prepare.ts pg|sqlite");
break;
}
1 change: 1 addition & 0 deletions packages/api/src/ledgers/Account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Account {
name: string;
description: string;
sortKey: number;
}
2 changes: 2 additions & 0 deletions packages/api/src/repositories/AccountRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ export const loadAll = async (
.selectFrom(AccountTable.name)
.selectAll()
.where("userId", "=", userId)
.orderBy("sortKey")
.execute();

const results = rows.map((row): Account => {
return {
name: row.name,
description: row.description,
sortKey: row.sortKey,
};
});

Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/tables/AccountTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Table {
userId: string;
name: string;
description: string;
sortKey: number;
}

// TODO: 타입 유도?
Expand All @@ -35,6 +36,7 @@ export const defineSchema_sqlite = <T>(db: Kysely<T>) => {
.addColumn("userId", "text")
.addColumn("name", "text")
.addColumn("description", "text")
.addColumn("sortKey", "integer")
.addPrimaryKeyConstraint("primary", [...primaryKeyFields])
.addUniqueConstraint("userId_name", ["userId", "name"]);
};
Expand All @@ -47,6 +49,7 @@ export const defineSchema_pg = <T>(db: Kysely<T>) => {
.addColumn("userId", "text")
.addColumn("name", "text")
.addColumn("description", "text")
.addColumn("sortKey", "integer")
.addPrimaryKeyConstraint(`${prefix}_primary`, [...primaryKeyFields])
.addUniqueConstraint(`${prefix}_userId_name`, ["userId", "name"]);
};
5 changes: 4 additions & 1 deletion packages/ui/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ const synchronize = async (props: {
};

const load = async () => {
const accounts = await db.accounts.toArray();
const accounts_naive = await db.accounts.toArray();
const accounts = accounts_naive.sort((a, b) => a.sortKey - b.sortKey);

const presets = await db.presets.toArray();

return {
accounts,
presets,
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import react from "@vitejs/plugin-react";
import { VitePWA } from "vite-plugin-pwa";
import { visualizer } from "rollup-plugin-visualizer";
import { defineConfig } from "vite";
import { VitePWA } from "vite-plugin-pwa";

const baseUrl_ghpage = "/yuuka/";
const baseUrl_vercel = undefined;
Expand All @@ -11,7 +11,8 @@ const baseUrl = process.env.VERCEL ? baseUrl_vercel : baseUrl_ghpage;
export default defineConfig({
plugins: [
react(),
visualizer(),
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
visualizer() as any,
VitePWA({
includeAssets: ["favicon.ico", "apple-touch-icon.png", "mask-icon.svg"],
manifest: {
Expand Down

0 comments on commit eeac3c0

Please sign in to comment.