Skip to content

Commit

Permalink
Merge pull request #3395 from specklesystems/fabians/quick-js-to-ts-1
Browse files Browse the repository at this point in the history
chore(server): easy js to ts migrations #1
  • Loading branch information
fabis94 authored Oct 25, 2024
2 parents b590fdd + 64dbc69 commit d4241b0
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/** @type {import('yargs').CommandModule} */
const command = {
import { noop } from 'lodash'
import { CommandModule } from 'yargs'

const command: CommandModule = {
command: 'migrate',
describe: 'Migration specific commands',
builder(yargs) {
return yargs.commandDir('migrate', { extensions: ['js', 'ts'] }).demandCommand()
}
},
handler: noop
}

module.exports = command
export = command
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
const knex = require('@/db/knex')
const { appRoot } = require('@/bootstrap')
const fs = require('fs/promises')
const { logger } = require('@/logging/logging')
import knex from '@/db/knex'
import { appRoot } from '@/bootstrap'
import fs from 'fs/promises'
import { logger } from '@/logging/logging'
import { CommandModule } from 'yargs'

/** @type {import('yargs').CommandModule} */
const command = {
const command: CommandModule<unknown, { name: string; module: string }> = {
command: 'create <name> [module]',
describe: 'Create a new migration',
builder(yargs) {
return yargs
.positional('name', {
describe: 'Migration name',
type: 'string'
})
.positional('module', {
describe: 'The server module into which this migration should be generated',
type: 'string',
default: 'core'
})
builder: {
name: {
describe: 'Migration name',
type: 'string'
},
module: {
describe: 'The server module into which this migration should be generated',
type: 'string',
default: 'core'
}
},
async handler(argv) {
const name = argv.name
Expand All @@ -40,4 +40,4 @@ const command = {
}
}

module.exports = command
export = command
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const knex = require('@/db/knex')
const { logger } = require('@/logging/logging')
import knex from '@/db/knex'
import { logger } from '@/logging/logging'
import { CommandModule } from 'yargs'

/** @type {import('yargs').CommandModule} */
const command = {
const command: CommandModule<unknown, { times: number }> = {
command: 'down [times]',
describe: 'Undo last migration',
builder(yargs) {
Expand All @@ -29,4 +29,4 @@ const command = {
}
}

module.exports = command
export = command
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const knex = require('@/db/knex')
const { logger } = require('@/logging/logging')
import knex from '@/db/knex'
import { logger } from '@/logging/logging'
import { CommandModule } from 'yargs'

/** @type {import('yargs').CommandModule} */
const command = {
const command: CommandModule = {
command: 'latest',
describe: 'Run all migrations that have not yet been run',
async handler() {
Expand All @@ -12,4 +12,4 @@ const command = {
}
}

module.exports = command
export = command
15 changes: 0 additions & 15 deletions packages/server/modules/cli/commands/db/migrate/rollback.js

This file was deleted.

15 changes: 15 additions & 0 deletions packages/server/modules/cli/commands/db/migrate/rollback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import knex from '@/db/knex'
import { logger } from '@/logging/logging'
import { CommandModule } from 'yargs'

const command: CommandModule = {
command: 'rollback',
describe: 'Roll back all migrations',
async handler() {
logger.info('Rolling back migrations...')
await knex.migrate.rollback(undefined, true)
logger.info('Completed rolling back migrations')
}
}

export = command
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const knex = require('@/db/knex')
const { logger } = require('@/logging/logging')
import knex from '@/db/knex'
import { logger } from '@/logging/logging'
import { CommandModule } from 'yargs'

/** @type {import('yargs').CommandModule} */
const command = {
const command: CommandModule = {
command: 'up',
describe: 'Run next migration that has not yet been run',
async handler() {
Expand All @@ -12,4 +12,4 @@ const command = {
}
}

module.exports = command
export = command
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/** @type {import('yargs').CommandModule} */
const command = {
import { noop } from 'lodash'
import { CommandModule } from 'yargs'

const command: CommandModule = {
command: 'seed',
describe: 'Seed your local DB with fake data',
builder(yargs) {
return yargs.commandDir('seed', { extensions: ['js', 'ts'] }).demandCommand()
}
},
handler: noop
}

module.exports = command
export = command
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const { logger } = require('@/logging/logging')
const { Users, ServerAcl } = require('@/modules/core/dbSchema')
const { Roles } = require('@/modules/core/helpers/mainConstants')
const { faker } = require('@faker-js/faker')
const { range } = require('lodash')
const { UniqueEnforcer } = require('enforce-unique')
import { logger } from '@/logging/logging'
import { Users, ServerAcl } from '@/modules/core/dbSchema'
import { Roles } from '@/modules/core/helpers/mainConstants'
import { faker } from '@faker-js/faker'
import { range } from 'lodash'
import { UniqueEnforcer } from 'enforce-unique'
import { CommandModule } from 'yargs'
import { UserRecord } from '@/modules/core/helpers/types'

const RETRY_COUNT = 3
const UNIQUE_MAX_TIME = 500
Expand Down Expand Up @@ -36,23 +38,31 @@ function createFakeUser() {
}
}

function generateUsers(count) {
function generateUsers(count: number) {
const users = []
for (let i = 0; i < count; i++) {
users.push(createFakeUser())
}
return users
}

function insertUsers(users) {
return Users.knex().returning(Users.col.id).insert(users)
function insertUsers(users: Partial<UserRecord>[]): Promise<Array<{ id: string }>> {
return Users.knex<UserRecord>().returning(Users.col.id).insert(users)
}

async function* batchedOperationGenerator({
batchInsertGenerator,
itemCount,
batchSize,
retryCount = 3
}: {
batchInsertGenerator: (
insertCount: number,
currentItemCount: number
) => Promise<string[]>
itemCount: number
batchSize: number
retryCount?: number
}) {
logger.info('Starting batched operation...')
let currentItemCount = 0
Expand Down Expand Up @@ -80,6 +90,7 @@ async function* batchedOperationGenerator({
})
batchPromise = batchPromise.catch((e) => {
logger.error(e, 'Operation failed all retries')
return []
})

currentItemCount = newItemCount
Expand All @@ -88,21 +99,19 @@ async function* batchedOperationGenerator({
}
}

/** @type {import('yargs').CommandModule} */
const command = {
const command: CommandModule<unknown, { count: number; batchsize: number }> = {
command: 'users <count> [batchsize]',
describe: 'Fill the `users` table with a ton of fake users',
builder(yargs) {
return yargs
.positional('count', {
describe: 'User count',
type: 'number'
})
.positional('batchsize', {
describe: 'Max amount of inserts to process at once',
type: 'number',
default: '500'
})
builder: {
count: {
describe: 'User count',
type: 'number'
},
batchsize: {
describe: 'Max amount of inserts to process at once',
type: 'number',
default: '500'
}
},
async handler(argv) {
const count = argv.count
Expand Down Expand Up @@ -131,4 +140,4 @@ const command = {
}
}

module.exports = command
export = command
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const path = require('path')
const yargs = require('yargs')
require('../../bootstrap')
const { logger } = require('@/logging/logging')
/* eslint-disable no-restricted-imports */
import path from 'path'
import yargs from 'yargs'
import '../../bootstrap'
import { logger } from '@/logging/logging'

const execution = yargs
.scriptName('yarn cli')
Expand All @@ -25,5 +26,6 @@ const execution = yargs

const promise = Promise.resolve(execution)
promise.then(() => {
yargs.exit(0)
// weird TS typing issue
yargs.exit(0, undefined as unknown as Error)
})
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const getIpFromRequest = (req) => {
let ip
import { Optional } from '@speckle/shared'
import type express from 'express'
import type http from 'http'

export const getIpFromRequest = (req: express.Request | http.IncomingMessage) => {
let ip: Optional<string> = undefined
try {
ip =
req.headers['cf-connecting-ip'] ||
ip = (req.headers['cf-connecting-ip'] ||
req.headers['true-client-ip'] ||
req.headers['x-real-ip'] ||
req.headers['x-forwarded-for'] ||
req.headers['x-original-forwarded-for'] ||
req.ip ||
('ip' in req ? req.ip : undefined) ||
req.connection.remoteAddress ||
''
'') as string
} catch {
ip = ''
}
Expand All @@ -19,5 +22,3 @@ const getIpFromRequest = (req) => {
if (ip.startsWith(ipPrefix) || ip === '') return null
return ip
}

module.exports = { getIpFromRequest }
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"lint:ci": "yarn lint:tsc",
"lint:tsc": "tsc --noEmit",
"lint:eslint": "eslint .",
"cli": "cross-env LOG_LEVEL=debug LOG_PRETTY=true NODE_ENV=development ts-node ./modules/cli/index.js",
"cli": "cross-env LOG_LEVEL=debug LOG_PRETTY=true NODE_ENV=development ts-node ./modules/cli/index.ts",
"cli:download:commit": "cross-env LOG_PRETTY=true LOG_LEVEL=debug yarn cli download commit",
"migrate": "yarn cli db migrate",
"migrate:test": "cross-env NODE_ENV=test ts-node ./modules/cli/index.js db migrate",
Expand Down

0 comments on commit d4241b0

Please sign in to comment.