add lowdb
This commit is contained in:
101
src/database/database.schema.ts
Normal file
101
src/database/database.schema.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
// database.schema.ts
|
||||
|
||||
export interface Customer {
|
||||
id: number;
|
||||
firstNameEn: string;
|
||||
lastNameEn: string;
|
||||
// firstNameJp: string;
|
||||
// lastNameJp: string;
|
||||
// firstNameJpKana: string;
|
||||
// lastNameJpKana: string;
|
||||
email: string;
|
||||
// email2?: string;
|
||||
mobile: string;
|
||||
// originAdd1: string;
|
||||
// originAdd2: string;
|
||||
// originAdd3: string;
|
||||
// originCity: string;
|
||||
// originState: string;
|
||||
// originPostcode: string;
|
||||
// originCountry: string;
|
||||
// localAdd1: string;
|
||||
// localAdd2: string;
|
||||
// localAdd3: string;
|
||||
// localCity: string;
|
||||
// localState: string;
|
||||
// localPostcode: string;
|
||||
// localCountry: string;
|
||||
// preferLang: string;
|
||||
// dob: string; // ISO string
|
||||
// gender: string;
|
||||
// martial: string;
|
||||
// occupation: string;
|
||||
// ecName: string;
|
||||
// ecRelation: string;
|
||||
// ecEmail: string;
|
||||
// ecMobile: string;
|
||||
// initialOutReach?: string;
|
||||
// initialOutReachRemark?: string;
|
||||
// consentGiven?: string;
|
||||
// contentDate?: string;
|
||||
// status: string;
|
||||
// remarks: string;
|
||||
// potcustId?: string;
|
||||
// isDeleted: boolean;
|
||||
}
|
||||
|
||||
export interface User {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
password: string;
|
||||
isDeleted: boolean;
|
||||
}
|
||||
|
||||
export interface CustomerDependant {
|
||||
id: number;
|
||||
custId: string;
|
||||
// deptSeq: number;
|
||||
firstNameEn: string;
|
||||
lastNameEn: string;
|
||||
// firstNameJp: string;
|
||||
// lastNameJp: string;
|
||||
// firstNameJpKana: string;
|
||||
// lastNameJpKana: string;
|
||||
email: string;
|
||||
email2?: string;
|
||||
// mobile: string;
|
||||
// originAdd1: string;
|
||||
// originAdd2: string;
|
||||
// originAdd3: string;
|
||||
// originCity: string;
|
||||
// originState: string;
|
||||
// originPostcode: string;
|
||||
// originCountry: string;
|
||||
// localAdd1: string;
|
||||
// localAdd2: string;
|
||||
// localAdd3: string;
|
||||
// localCity: string;
|
||||
// localState: string;
|
||||
// localPostcode: string;
|
||||
// localCountry: string;
|
||||
// preferLang: string;
|
||||
// dob: string;
|
||||
// gender: string;
|
||||
// martial: string;
|
||||
// occupation: string;
|
||||
// ecName: string;
|
||||
// ecRelation: string;
|
||||
// ecEmail: string;
|
||||
// ecMobile: string;
|
||||
// isDeleted: boolean;
|
||||
}
|
||||
|
||||
// Lowdb root schema
|
||||
export interface DBSchema {
|
||||
customers: Customer[];
|
||||
users: User[];
|
||||
customerDependants: CustomerDependant[];
|
||||
}
|
||||
33
src/database/database.ts
Normal file
33
src/database/database.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Low } from "lowdb";
|
||||
import { JSONFile } from "lowdb/node";
|
||||
import { DBSchema } from "./database.schema";
|
||||
import { defaultAdminUser } from "./defaultAdminUser";
|
||||
import path from "path";
|
||||
|
||||
// File path for db.json
|
||||
const file = path.resolve(process.cwd(), "src/database/db.json");
|
||||
const adapter = new JSONFile<DBSchema>(file);
|
||||
const db = new Low<DBSchema>(adapter, {
|
||||
customers: [],
|
||||
users: [],
|
||||
customerDependants: [],
|
||||
});
|
||||
|
||||
|
||||
// Helper to generate next id for a collection
|
||||
export function getNextId<T extends { id: number }>(items: T[]): number {
|
||||
if (!items || items.length === 0) return 1;
|
||||
return Math.max(...items.map((item) => item.id)) + 1;
|
||||
}
|
||||
|
||||
// Initialize DB and ensure default admin user exists
|
||||
export async function initDB() {
|
||||
await db.read();
|
||||
db.data ||= { customers: [], users: [], customerDependants: [] };
|
||||
if (db.data.users.length === 0) {
|
||||
db.data.users.push(defaultAdminUser);
|
||||
await db.write();
|
||||
}
|
||||
}
|
||||
|
||||
export { db };
|
||||
15
src/database/db.json
Normal file
15
src/database/db.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"customers": [],
|
||||
"users": [
|
||||
{
|
||||
"id": 1,
|
||||
"username": "admin",
|
||||
"email": "admin@example.com",
|
||||
"firstName": "Admin",
|
||||
"lastName": "User",
|
||||
"password": "admin123",
|
||||
"isDeleted": false
|
||||
}
|
||||
],
|
||||
"customerDependants": []
|
||||
}
|
||||
11
src/database/defaultAdminUser.ts
Normal file
11
src/database/defaultAdminUser.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { User } from "./database.schema";
|
||||
|
||||
export const defaultAdminUser: User = {
|
||||
id: 1,
|
||||
username: "admin",
|
||||
email: "admin@example.com",
|
||||
firstName: "Admin",
|
||||
lastName: "User",
|
||||
password: "admin123", // You should hash this in production
|
||||
isDeleted: false,
|
||||
};
|
||||
Reference in New Issue
Block a user