Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
postgres db setup
Browse files Browse the repository at this point in the history
  • Loading branch information
wslyvh committed Mar 25, 2024
1 parent b1cc42c commit 1927cc7
Show file tree
Hide file tree
Showing 24 changed files with 9,112 additions and 149 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# ignore mongoexport old db file
db.json
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"lint": "eslint --fix",
"prettier": "prettier './src' --write",
"db:generate": "prisma db push --force-reset --accept-data-loss && prisma db seed",
"db:generate:account": "prisma db push --force-reset --accept-data-loss --schema src/db/account.prisma && ts-node ./src/db/account-seed",
"db:studio": "prisma studio",
"db:studio:account": "prisma studio --schema src/db/account.prisma",
"swagger:generate": "ts-node ./src/swagger/generate",
"postinstall": "tsc && yarn swagger:generate && yarn db:generate"
},
Expand Down Expand Up @@ -55,7 +57,7 @@
"dependencies": {
"@ethersproject/providers": "^5.7.1",
"@octokit/rest": "^19.0.7",
"@prisma/client": "4.11.0",
"@prisma/client": "^5.11.0",
"cors": "^2.8.5",
"cross-fetch": "^3.1.5",
"dayjs": "^1.11.7",
Expand All @@ -66,10 +68,9 @@
"handlebars": "^4.7.7",
"helmet": "^6.0.1",
"markdown-it": "^13.0.1",
"prisma": "^4.11.0",
"puppeteer": "18.2.1",
"slugify": "^1.6.5",
"sqlite3": "^5.1.4",
"sqlite3": "^5.1.7",
"swagger-ui-express": "^4.6.0"
},
"devDependencies": {
Expand Down Expand Up @@ -97,6 +98,7 @@
"lint-staged": "^13.1.0",
"nodemon": "^2.0.20",
"prettier": "^2.8.3",
"prisma": "^5.11.0",
"supertest": "^6.3.3",
"swagger-autogen": "^2.22.0",
"ts-jest": "^29.0.5",
Expand Down
75 changes: 75 additions & 0 deletions src/db/account-seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { PrismaClient } from '../db/clients/account'
import { FileHandle, open } from 'node:fs/promises'
import path from 'path'

const client = new PrismaClient()
const testLimit = 0

async function main() {
let file: FileHandle | undefined
try {
file = await open(path.join(__dirname, '../../', 'db.json'))
} catch (e) {
console.log('Error opening file')
console.error(e)
}
if (!file) return

let connected = false
while (!connected) {
console.log('Connecting to db..')
try {
await client.$connect()
connected = true
} catch (e) {
console.log('Error connecting to db. Retrying in 5 secs..')
await new Promise((resolve) => setTimeout(resolve, 5000))
}
}

let i = 0
let accounts = []
console.log('Importing accounts from db.json file..')
for await (const line of file.readLines()) {
i++
const accountImport = JSON.parse(line)
const interested = accountImport.appState.sessions.filter((s: any) => s.level === 'interested').map((i: any) => i.id)
const attending = accountImport.appState.sessions.filter((s: any) => s.level === 'attending').map((i: any) => i.id)

accounts.push({
username: accountImport.username,
email: accountImport.email,
addresses: accountImport.addresses ?? [],
disabled: accountImport.disabled ?? false,

favorite_speakers: accountImport.appState.speakers,
interested_sessions: [],
attending_sessions: [],
publicSchedule: accountImport.appState.publicSchedule,
notifications: false,
appState_bogota: JSON.stringify({ interested, attending }),

createdAt: new Date(accountImport.createdAt['$date']),
updatedAt: accountImport.updatedAt ? new Date(accountImport.updatedAt['$date']) : new Date(),
})

if (testLimit > 0 && i >= testLimit) break
}

console.log(`Importing ${i} accounts..`)
const result = await client.account.createMany({
data: accounts,
})
console.log(`Imported ${result.count} accounts.`)
console.log('Done!')
}

main()
.then(async () => {
await client.$disconnect()
})
.catch(async (e) => {
console.error(e)
await client.$disconnect()
process.exit(1)
})
35 changes: 35 additions & 0 deletions src/db/account.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
datasource db {
provider = "postgres"
url = env("DB_CONNECTION_STRING")
}

generator client {
provider = "prisma-client-js"
output = "./clients/account"
}

model VerificationToken {
id String @default(cuid()) @id
identifier String
nonce Int
issued DateTime @default(now())
expires DateTime
}

model Account {
id String @default(cuid()) @id
username String?
email String?
addresses String[]
disabled Boolean @default(false)
favorite_speakers String[]
interested_sessions String[]
attending_sessions String[]
publicSchedule Boolean @default(false)
notifications Boolean @default(false)
appState_bogota String?
createdAt DateTime @default(now())
updatedAt DateTime?
}
1 change: 1 addition & 0 deletions src/db/clients/account/default.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './index'
1 change: 1 addition & 0 deletions src/db/clients/account/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { ...require('.') }
1 change: 1 addition & 0 deletions src/db/clients/account/edge.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './default'
203 changes: 203 additions & 0 deletions src/db/clients/account/edge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@

Object.defineProperty(exports, "__esModule", { value: true });

const {
PrismaClientKnownRequestError,
PrismaClientUnknownRequestError,
PrismaClientRustPanicError,
PrismaClientInitializationError,
PrismaClientValidationError,
NotFoundError,
getPrismaClient,
sqltag,
empty,
join,
raw,
Decimal,
Debug,
objectEnumValues,
makeStrictEnum,
Extensions,
warnOnce,
defineDmmfProperty,
Public,
getRuntime
} = require('./runtime/edge.js')


const Prisma = {}

exports.Prisma = Prisma
exports.$Enums = {}

/**
* Prisma Client JS version: 5.11.0
* Query Engine version: efd2449663b3d73d637ea1fd226bafbcf45b3102
*/
Prisma.prismaVersion = {
client: "5.11.0",
engine: "efd2449663b3d73d637ea1fd226bafbcf45b3102"
}

Prisma.PrismaClientKnownRequestError = PrismaClientKnownRequestError;
Prisma.PrismaClientUnknownRequestError = PrismaClientUnknownRequestError
Prisma.PrismaClientRustPanicError = PrismaClientRustPanicError
Prisma.PrismaClientInitializationError = PrismaClientInitializationError
Prisma.PrismaClientValidationError = PrismaClientValidationError
Prisma.NotFoundError = NotFoundError
Prisma.Decimal = Decimal

/**
* Re-export of sql-template-tag
*/
Prisma.sql = sqltag
Prisma.empty = empty
Prisma.join = join
Prisma.raw = raw
Prisma.validator = Public.validator

/**
* Extensions
*/
Prisma.getExtensionContext = Extensions.getExtensionContext
Prisma.defineExtension = Extensions.defineExtension

/**
* Shorthand utilities for JSON filtering
*/
Prisma.DbNull = objectEnumValues.instances.DbNull
Prisma.JsonNull = objectEnumValues.instances.JsonNull
Prisma.AnyNull = objectEnumValues.instances.AnyNull

Prisma.NullTypes = {
DbNull: objectEnumValues.classes.DbNull,
JsonNull: objectEnumValues.classes.JsonNull,
AnyNull: objectEnumValues.classes.AnyNull
}



/**
* Enums
*/
exports.Prisma.TransactionIsolationLevel = makeStrictEnum({
ReadUncommitted: 'ReadUncommitted',
ReadCommitted: 'ReadCommitted',
RepeatableRead: 'RepeatableRead',
Serializable: 'Serializable'
});

exports.Prisma.VerificationTokenScalarFieldEnum = {
id: 'id',
identifier: 'identifier',
nonce: 'nonce',
issued: 'issued',
expires: 'expires'
};

exports.Prisma.AccountScalarFieldEnum = {
id: 'id',
username: 'username',
email: 'email',
addresses: 'addresses',
disabled: 'disabled',
favorite_speakers: 'favorite_speakers',
interested_sessions: 'interested_sessions',
attending_sessions: 'attending_sessions',
publicSchedule: 'publicSchedule',
notifications: 'notifications',
appState_bogota: 'appState_bogota',
createdAt: 'createdAt',
updatedAt: 'updatedAt'
};

exports.Prisma.SortOrder = {
asc: 'asc',
desc: 'desc'
};

exports.Prisma.QueryMode = {
default: 'default',
insensitive: 'insensitive'
};

exports.Prisma.NullsOrder = {
first: 'first',
last: 'last'
};


exports.Prisma.ModelName = {
VerificationToken: 'VerificationToken',
Account: 'Account'
};
/**
* Create the Client
*/
const config = {
"generator": {
"name": "client",
"provider": {
"fromEnvVar": null,
"value": "prisma-client-js"
},
"output": {
"value": "/home/w/Code/devcon-api/src/db/clients/account",
"fromEnvVar": null
},
"config": {
"engineType": "library"
},
"binaryTargets": [
{
"fromEnvVar": null,
"value": "debian-openssl-3.0.x",
"native": true
}
],
"previewFeatures": [],
"isCustomOutput": true
},
"relativeEnvPaths": {
"rootEnvPath": null,
"schemaEnvPath": "../../../../.env"
},
"relativePath": "../..",
"clientVersion": "5.11.0",
"engineVersion": "efd2449663b3d73d637ea1fd226bafbcf45b3102",
"datasourceNames": [
"db"
],
"activeProvider": "postgresql",
"inlineDatasources": {
"db": {
"url": {
"fromEnvVar": "DB_CONNECTION_STRING",
"value": null
}
}
},
"inlineSchema": "datasource db {\n provider = \"postgres\"\n url = env(\"DB_CONNECTION_STRING\")\n}\n\ngenerator client {\n provider = \"prisma-client-js\"\n output = \"./clients/account\"\n}\n\nmodel VerificationToken {\n id String @default(cuid()) @id\n identifier String\n nonce Int\n issued DateTime @default(now())\n expires DateTime\n}\n\nmodel Account {\n id String @default(cuid()) @id\n username String?\n email String?\n addresses String[]\n disabled Boolean @default(false)\n \n favorite_speakers String[]\n interested_sessions String[]\n attending_sessions String[]\n publicSchedule Boolean @default(false)\n notifications Boolean @default(false)\n appState_bogota String?\n\n createdAt DateTime @default(now())\n updatedAt DateTime?\n}\n",
"inlineSchemaHash": "f9a95bb20669ea54b0f8ea74f6b4e960b17382b534f52d8d04a619060eb53a42",
"copyEngine": true
}
config.dirname = '/'

config.runtimeDataModel = JSON.parse("{\"models\":{\"VerificationToken\":{\"dbName\":null,\"fields\":[{\"name\":\"id\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":true,\"isUnique\":false,\"isId\":true,\"isReadOnly\":false,\"hasDefaultValue\":true,\"type\":\"String\",\"default\":{\"name\":\"cuid\",\"args\":[]},\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"identifier\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"String\",\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"nonce\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"Int\",\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"issued\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":true,\"type\":\"DateTime\",\"default\":{\"name\":\"now\",\"args\":[]},\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"expires\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"DateTime\",\"isGenerated\":false,\"isUpdatedAt\":false}],\"primaryKey\":null,\"uniqueFields\":[],\"uniqueIndexes\":[],\"isGenerated\":false},\"Account\":{\"dbName\":null,\"fields\":[{\"name\":\"id\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":true,\"isUnique\":false,\"isId\":true,\"isReadOnly\":false,\"hasDefaultValue\":true,\"type\":\"String\",\"default\":{\"name\":\"cuid\",\"args\":[]},\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"username\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":false,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"String\",\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"email\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":false,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"String\",\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"addresses\",\"kind\":\"scalar\",\"isList\":true,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"String\",\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"disabled\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":true,\"type\":\"Boolean\",\"default\":false,\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"favorite_speakers\",\"kind\":\"scalar\",\"isList\":true,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"String\",\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"interested_sessions\",\"kind\":\"scalar\",\"isList\":true,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"String\",\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"attending_sessions\",\"kind\":\"scalar\",\"isList\":true,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"String\",\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"publicSchedule\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":true,\"type\":\"Boolean\",\"default\":false,\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"notifications\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":true,\"type\":\"Boolean\",\"default\":false,\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"appState_bogota\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":false,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"String\",\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"createdAt\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":true,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":true,\"type\":\"DateTime\",\"default\":{\"name\":\"now\",\"args\":[]},\"isGenerated\":false,\"isUpdatedAt\":false},{\"name\":\"updatedAt\",\"kind\":\"scalar\",\"isList\":false,\"isRequired\":false,\"isUnique\":false,\"isId\":false,\"isReadOnly\":false,\"hasDefaultValue\":false,\"type\":\"DateTime\",\"isGenerated\":false,\"isUpdatedAt\":false}],\"primaryKey\":null,\"uniqueFields\":[],\"uniqueIndexes\":[],\"isGenerated\":false}},\"enums\":{},\"types\":{}}")
defineDmmfProperty(exports.Prisma, config.runtimeDataModel)
config.engineWasm = undefined

config.injectableEdgeEnv = () => ({
parsed: {
DB_CONNECTION_STRING: typeof globalThis !== 'undefined' && globalThis['DB_CONNECTION_STRING'] || typeof process !== 'undefined' && process.env && process.env.DB_CONNECTION_STRING || undefined
}
})

if (typeof globalThis !== 'undefined' && globalThis['DEBUG'] || typeof process !== 'undefined' && process.env && process.env.DEBUG || undefined) {
Debug.enable(typeof globalThis !== 'undefined' && globalThis['DEBUG'] || typeof process !== 'undefined' && process.env && process.env.DEBUG || undefined)
}

const PrismaClient = getPrismaClient(config)
exports.PrismaClient = PrismaClient
Object.assign(exports, Prisma)

Loading

0 comments on commit 1927cc7

Please sign in to comment.