Added create_user and also making users table schema changes

This commit is contained in:
Khalim Conn-Kowlessar 2023-07-10 19:14:06 +01:00
parent ec8da0e40b
commit a1a578c97f
8 changed files with 217 additions and 6 deletions

40
src/app/db/create_user.ts Normal file
View file

@ -0,0 +1,40 @@
import dotenv from "dotenv";
import { user } from "./schema/users";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
dotenv.config({ path: ".env.local" });
// Get email from command line arguments
const email = process.argv[2];
const name = process.argv[3];
// Check if email is provided
if (!email) {
console.error("You must provide an email address");
process.exit(1);
}
// Connect to the database
const pool = new Pool({
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
const db = drizzle(pool);
async function createUser(email: string, name: string) {
try {
await db.insert(user).values({ email: email, firstName: name });
console.log(`User with email ${email} created successfully`);
process.exit(0);
} catch (err) {
console.error("Failed to create user:", err);
process.exit(1);
}
}
createUser(email, name);

View file

@ -0,0 +1,7 @@
ALTER TABLE "user" ALTER COLUMN "email" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "user" ALTER COLUMN "created_at" SET DATA TYPE timestamp (6) with time zone;--> statement-breakpoint
ALTER TABLE "user" ALTER COLUMN "created_at" SET DEFAULT now();--> statement-breakpoint
ALTER TABLE "user" ALTER COLUMN "created_at" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "user" ALTER COLUMN "updated_at" SET DATA TYPE timestamp (6) with time zone;--> statement-breakpoint
ALTER TABLE "user" ALTER COLUMN "updated_at" SET DEFAULT now();--> statement-breakpoint
ALTER TABLE "user" ALTER COLUMN "updated_at" SET NOT NULL;

View file

@ -0,0 +1 @@
ALTER TABLE "user" RENAME COLUMN "name" TO "firstName";

View file

@ -0,0 +1,68 @@
{
"version": "5",
"dialect": "pg",
"id": "1f801509-86d5-48dc-b038-b2319fff0c13",
"prevId": "9cd7955c-6b58-415a-baab-bab2d5e5ff12",
"tables": {
"user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false
},
"email": {
"name": "email",
"type": "text",
"primaryKey": false,
"notNull": true
},
"oauth_id": {
"name": "oauth_id",
"type": "text",
"primaryKey": false,
"notNull": false
},
"oauth_provider": {
"name": "oauth_provider",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp (6) with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp (6) with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}

View file

@ -0,0 +1,70 @@
{
"version": "5",
"dialect": "pg",
"id": "4347e9a7-f388-4bac-8860-a482db2a5c8b",
"prevId": "1f801509-86d5-48dc-b038-b2319fff0c13",
"tables": {
"user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"firstName": {
"name": "firstName",
"type": "text",
"primaryKey": false,
"notNull": false
},
"email": {
"name": "email",
"type": "text",
"primaryKey": false,
"notNull": true
},
"oauth_id": {
"name": "oauth_id",
"type": "text",
"primaryKey": false,
"notNull": false
},
"oauth_provider": {
"name": "oauth_provider",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp (6) with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp (6) with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {
"\"user\".\"name\"": "\"user\".\"firstName\""
}
}
}

View file

@ -8,6 +8,20 @@
"when": 1689006611034,
"tag": "0000_magenta_clea",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1689011877245,
"tag": "0001_fine_toad_men",
"breakpoints": true
},
{
"idx": 2,
"version": "5",
"when": 1689012318015,
"tag": "0002_hard_human_torch",
"breakpoints": true
}
]
}

View file

@ -2,11 +2,22 @@ import { serial, text, timestamp, pgTable } from "drizzle-orm/pg-core";
export const user = pgTable("user", {
id: serial("id").primaryKey(),
name: text("name"),
email: text("email"),
firstName: text("firstName"),
// At the moment, Drizzle doesn't support unique constraints
email: text("email").notNull(),
oauthId: text("oauth_id"),
oauthProvider: text("oauth_provider").$type<"google">(),
// role: text("role").$type<"admin" | "write" | "read">(),
createdAt: timestamp("created_at"),
updatedAt: timestamp("updated_at"),
createdAt: timestamp("created_at", {
precision: 6,
withTimezone: true,
})
.defaultNow()
.notNull(),
updatedAt: timestamp("updated_at", {
precision: 6,
withTimezone: true,
})
.defaultNow()
.notNull(),
});

View file

@ -1,8 +1,8 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"target": "es5",
// "target": "ESNext",
// "target": "es5",
"target": "ESNext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,