initial
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
sqlite.db
|
||||
10
Taskfile.yml
Normal file
10
Taskfile.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
version: "3"
|
||||
|
||||
tasks:
|
||||
generate:
|
||||
cmds:
|
||||
- pnpm drizzle-kit generate:sqlite
|
||||
|
||||
studio:
|
||||
cmds:
|
||||
- pnpm drizzle-kit studio
|
||||
11
drizzle.config.ts
Normal file
11
drizzle.config.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { defineConfig } from "drizzle-kit";
|
||||
export default defineConfig({
|
||||
schema: "./src/schema.ts",
|
||||
out: "./drizzle",
|
||||
driver: "better-sqlite",
|
||||
dbCredentials: {
|
||||
url: "sqlite.db",
|
||||
},
|
||||
verbose: true,
|
||||
strict: true,
|
||||
});
|
||||
45
drizzle/0000_aspiring_paibok.sql
Normal file
45
drizzle/0000_aspiring_paibok.sql
Normal file
@@ -0,0 +1,45 @@
|
||||
CREATE TABLE `groups` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`name` text
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE `opinions` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`name` text,
|
||||
`type` text
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE `provinces` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`name` text NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE `users` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`name` text,
|
||||
`phone` text NOT NULL,
|
||||
`email` text,
|
||||
`job` text,
|
||||
`education` text,
|
||||
`vision` text,
|
||||
`reason` text,
|
||||
`group_id` integer,
|
||||
FOREIGN KEY (`group_id`) REFERENCES `groups`(`id`) ON UPDATE no action ON DELETE no action
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE `user_opinions` (
|
||||
`user_id` integer,
|
||||
`opinion_id` integer,
|
||||
`choice` text DEFAULT 'ignore',
|
||||
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
|
||||
FOREIGN KEY (`opinion_id`) REFERENCES `opinions`(`id`) ON UPDATE no action ON DELETE no action
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE `zones` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`name` text NOT NULL,
|
||||
`province_id` integer NOT NULL,
|
||||
FOREIGN KEY (`province_id`) REFERENCES `provinces`(`id`) ON UPDATE no action ON DELETE no action
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX `users_phone_unique` ON `users` (`phone`);
|
||||
286
drizzle/meta/0000_snapshot.json
Normal file
286
drizzle/meta/0000_snapshot.json
Normal file
@@ -0,0 +1,286 @@
|
||||
{
|
||||
"version": "5",
|
||||
"dialect": "sqlite",
|
||||
"id": "71187b23-6b9a-4f78-9f3c-0af6fb035f1b",
|
||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||
"tables": {
|
||||
"groups": {
|
||||
"name": "groups",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "integer",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": true
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {}
|
||||
},
|
||||
"opinions": {
|
||||
"name": "opinions",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "integer",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": true
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"type": {
|
||||
"name": "type",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {}
|
||||
},
|
||||
"provinces": {
|
||||
"name": "provinces",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "integer",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": true
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {}
|
||||
},
|
||||
"users": {
|
||||
"name": "users",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "integer",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": true
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"phone": {
|
||||
"name": "phone",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"email": {
|
||||
"name": "email",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"job": {
|
||||
"name": "job",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"education": {
|
||||
"name": "education",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"vision": {
|
||||
"name": "vision",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"reason": {
|
||||
"name": "reason",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"group_id": {
|
||||
"name": "group_id",
|
||||
"type": "integer",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {
|
||||
"users_phone_unique": {
|
||||
"name": "users_phone_unique",
|
||||
"columns": [
|
||||
"phone"
|
||||
],
|
||||
"isUnique": true
|
||||
}
|
||||
},
|
||||
"foreignKeys": {
|
||||
"users_group_id_groups_id_fk": {
|
||||
"name": "users_group_id_groups_id_fk",
|
||||
"tableFrom": "users",
|
||||
"tableTo": "groups",
|
||||
"columnsFrom": [
|
||||
"group_id"
|
||||
],
|
||||
"columnsTo": [
|
||||
"id"
|
||||
],
|
||||
"onDelete": "no action",
|
||||
"onUpdate": "no action"
|
||||
}
|
||||
},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {}
|
||||
},
|
||||
"user_opinions": {
|
||||
"name": "user_opinions",
|
||||
"columns": {
|
||||
"user_id": {
|
||||
"name": "user_id",
|
||||
"type": "integer",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"opinion_id": {
|
||||
"name": "opinion_id",
|
||||
"type": "integer",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false
|
||||
},
|
||||
"choice": {
|
||||
"name": "choice",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": false,
|
||||
"autoincrement": false,
|
||||
"default": "'ignore'"
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {
|
||||
"user_opinions_user_id_users_id_fk": {
|
||||
"name": "user_opinions_user_id_users_id_fk",
|
||||
"tableFrom": "user_opinions",
|
||||
"tableTo": "users",
|
||||
"columnsFrom": [
|
||||
"user_id"
|
||||
],
|
||||
"columnsTo": [
|
||||
"id"
|
||||
],
|
||||
"onDelete": "no action",
|
||||
"onUpdate": "no action"
|
||||
},
|
||||
"user_opinions_opinion_id_opinions_id_fk": {
|
||||
"name": "user_opinions_opinion_id_opinions_id_fk",
|
||||
"tableFrom": "user_opinions",
|
||||
"tableTo": "opinions",
|
||||
"columnsFrom": [
|
||||
"opinion_id"
|
||||
],
|
||||
"columnsTo": [
|
||||
"id"
|
||||
],
|
||||
"onDelete": "no action",
|
||||
"onUpdate": "no action"
|
||||
}
|
||||
},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {}
|
||||
},
|
||||
"zones": {
|
||||
"name": "zones",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "integer",
|
||||
"primaryKey": true,
|
||||
"notNull": true,
|
||||
"autoincrement": true
|
||||
},
|
||||
"name": {
|
||||
"name": "name",
|
||||
"type": "text",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
},
|
||||
"province_id": {
|
||||
"name": "province_id",
|
||||
"type": "integer",
|
||||
"primaryKey": false,
|
||||
"notNull": true,
|
||||
"autoincrement": false
|
||||
}
|
||||
},
|
||||
"indexes": {},
|
||||
"foreignKeys": {
|
||||
"zones_province_id_provinces_id_fk": {
|
||||
"name": "zones_province_id_provinces_id_fk",
|
||||
"tableFrom": "zones",
|
||||
"tableTo": "provinces",
|
||||
"columnsFrom": [
|
||||
"province_id"
|
||||
],
|
||||
"columnsTo": [
|
||||
"id"
|
||||
],
|
||||
"onDelete": "no action",
|
||||
"onUpdate": "no action"
|
||||
}
|
||||
},
|
||||
"compositePrimaryKeys": {},
|
||||
"uniqueConstraints": {}
|
||||
}
|
||||
},
|
||||
"enums": {},
|
||||
"_meta": {
|
||||
"schemas": {},
|
||||
"tables": {},
|
||||
"columns": {}
|
||||
}
|
||||
}
|
||||
13
drizzle/meta/_journal.json
Normal file
13
drizzle/meta/_journal.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"version": "5",
|
||||
"dialect": "sqlite",
|
||||
"entries": [
|
||||
{
|
||||
"idx": 0,
|
||||
"version": "5",
|
||||
"when": 1713465576653,
|
||||
"tag": "0000_aspiring_paibok",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
27
package.json
Normal file
27
package.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "sorvor",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"dev": "nodemon --exec ts-node --swc src/app.ts"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@trpc/client": "11.0.0-rc.340",
|
||||
"@trpc/server": "11.0.0-rc.340",
|
||||
"better-sqlite3": "^9.5.0",
|
||||
"drizzle-orm": "^0.30.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@swc/core": "^1.4.16",
|
||||
"@types/better-sqlite3": "^7.6.9",
|
||||
"drizzle-kit": "^0.20.14",
|
||||
"nodemon": "^3.1.0",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^5.4.5"
|
||||
}
|
||||
}
|
||||
1627
pnpm-lock.yaml
generated
Normal file
1627
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
27
src/app.ts
Normal file
27
src/app.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import Database from "better-sqlite3";
|
||||
import { user } from "./schema.ts";
|
||||
|
||||
const sqlite = new Database("sqlite.db");
|
||||
const db = drizzle(sqlite);
|
||||
migrate(db, { migrationsFolder: "drizzle" });
|
||||
|
||||
async function main() {
|
||||
await db.insert(user).values({
|
||||
job: "Software Engineer",
|
||||
phone: "1234567890",
|
||||
group: 1,
|
||||
name: "John Doe",
|
||||
education: "Bachelor",
|
||||
});
|
||||
console.log("User inserted");
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
await main();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
})();
|
||||
88
src/schema.ts
Normal file
88
src/schema.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import {
|
||||
sqliteTable,
|
||||
text,
|
||||
integer,
|
||||
uniqueIndex,
|
||||
} from "drizzle-orm/sqlite-core";
|
||||
import { relations } from "drizzle-orm";
|
||||
|
||||
//----------------User
|
||||
export const user = sqliteTable("users", {
|
||||
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
||||
name: text("name").notNull(),
|
||||
phone: text("phone").unique().notNull(),
|
||||
email: text("email"),
|
||||
job: text("job").notNull(),
|
||||
education: text("education").notNull(),
|
||||
vision: text("vision"),
|
||||
reason: text("reason"),
|
||||
group: integer("group_id")
|
||||
.references(() => group.id)
|
||||
.notNull(),
|
||||
});
|
||||
|
||||
export const userRelation = relations(user, ({ many, one }) => ({
|
||||
opinions: many(userOpinion),
|
||||
group: one(group, {
|
||||
fields: [user.group],
|
||||
references: [group.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
//----------------Group
|
||||
export const group = sqliteTable("groups", {
|
||||
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
||||
name: text("name"),
|
||||
});
|
||||
|
||||
export const groupRelation = relations(group, ({ many }) => ({
|
||||
users: many(user),
|
||||
}));
|
||||
|
||||
//----------------Opinion
|
||||
export const opinion = sqliteTable("opinions", {
|
||||
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
||||
name: text("name"),
|
||||
type: text("type", { enum: ["3Choice", "4Choice"] }),
|
||||
});
|
||||
|
||||
//----------------UserOpinion
|
||||
export const userOpinion = sqliteTable("user_opinions", {
|
||||
userId: integer("user_id").references(() => user.id),
|
||||
opinionId: integer("opinion_id").references(() => opinion.id),
|
||||
choice: text("choice", {
|
||||
enum: ["agree", "disagree", "deciding", "ignore"],
|
||||
}).default("ignore"),
|
||||
});
|
||||
|
||||
export const userOpinionRelation = relations(userOpinion, ({ one }) => ({
|
||||
user: one(user, {
|
||||
fields: [userOpinion.userId],
|
||||
references: [user.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
//----------------Zone
|
||||
export const zone = sqliteTable("zones", {
|
||||
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
||||
name: text("name").notNull(),
|
||||
province: integer("province_id")
|
||||
.notNull()
|
||||
.references(() => province.id),
|
||||
});
|
||||
export const zoneRelation = relations(zone, ({ one }) => ({
|
||||
province: one(province, {
|
||||
fields: [zone.province],
|
||||
references: [province.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
//----------------Province
|
||||
export const province = sqliteTable("provinces", {
|
||||
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
||||
name: text("name").notNull(),
|
||||
});
|
||||
|
||||
export const provinceRelation = relations(province, ({ many }) => ({
|
||||
province: many(zone),
|
||||
}));
|
||||
Reference in New Issue
Block a user