Skip to content

Commit

Permalink
revert seeder (#7833)
Browse files Browse the repository at this point in the history
  • Loading branch information
HexaField committed Mar 29, 2023
1 parent 8f73841 commit 62de3bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 78 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
"postinstall": "patch-package",
"precommit": "no-master-commits -b master",
"prepare-database": "cross-env APP_ENV=production PREPARE_DATABASE=true EXIT_ON_DB_INIT=true ts-node --swc packages/server/src/index.ts",
"pretest": "cross-env TEST=true npm run dev-reinit",
"publish": "lerna publish from-package --yes --registry https://registry.npmjs.org",
"publish-npm": "lerna publish from-package --yes --no-verify-access --ignore-scripts --registry https://registry.npmjs.org",
"publish-github": "lerna publish from-package --yes --no-verify-access --ignore-scripts --registry https://npm.pkg.github.com",
Expand Down
105 changes: 28 additions & 77 deletions packages/server-core/src/seeder.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,42 @@
import { FeathersService } from '@feathersjs/feathers/lib'
import appRootPath from 'app-root-path'
import fs from 'fs'
import { isEqual } from 'lodash'
import path from 'path'
import { Model, ModelStatic, Op } from 'sequelize'

import { ServicesSeedConfig } from '@etherealengine/common/src/interfaces/ServicesSeedConfig'

import { Application } from '../declarations'
import config from './appconfig'
import { copyDefaultProject, uploadLocalProjectToProvider } from './projects/project/project.class'
import seederConfig from './seeder-config'

async function insertOneByOne(app: Application, config: ServicesSeedConfig) {
const service = app.service(config.path as any) as FeathersService
const templates = config.templates as any[]

const Model = (service as any).Model as ModelStatic<Model>

const templateInsertionPromises = templates.map(
(template) =>
new Promise(async (resolve) => {
const searchTemplate = {}

const uniqueField = Object.values(Model.rawAttributes).find((value: any) => value.unique) as any

if (uniqueField) {
searchTemplate[uniqueField.fieldName] = template[uniqueField.fieldName]
} else {
for (const key of Object.keys(template)) {
if (typeof template[key] !== 'object' && template[key]) {
searchTemplate[key] = template[key]
}
}
}

const result = await service.find({
query: searchTemplate
})
const isSeeded = result.total > 0

let insertionResult: any
if (!isSeeded) insertionResult = await service.create(template)

resolve(insertionResult)
})
)

return Promise.all(templateInsertionPromises)
}

export async function seeder(app: Application, forceRefresh: boolean, prepareDb: boolean) {
if (!forceRefresh && !prepareDb) return

const insertionPromises = seederConfig
.filter((config) => config.path && config.templates)
.map(
(config) =>
new Promise(async (resolve) => {
if (config.insertSingle) {
resolve(await insertOneByOne(app, config))
return
if (forceRefresh || prepareDb)
for (let config of seederConfig) {
if (config.path) {
const templates = config.templates
const service = app.service(config.path as any)
if (templates)
for (let template of templates) {
let isSeeded
if (config.path.endsWith('-setting')) {
const result = await service.find()
isSeeded = result.total > 0
} else {
const searchTemplate = {}

const sequelizeModel = service.Model
const uniqueField = Object.values(sequelizeModel.rawAttributes).find((value: any) => value.unique) as any
if (uniqueField) searchTemplate[uniqueField.fieldName] = template[uniqueField.fieldName]
else
for (let key of Object.keys(template))
if (typeof template[key] !== 'object') searchTemplate[key] = template[key]
const result = await service.find({
query: searchTemplate
})
isSeeded = result.total > 0
}
if (!isSeeded) await service.create(template)
}

const templates = config.templates as any[]

const Model = app.service(config.path as any).Model as ModelStatic<Model>

const rows = await Model.findAll({
where: {
[Op.or]: templates
},
attributes: Object.keys(templates.at(0)),
raw: true
})

const templatesToBeInserted = templates.filter(
(template) => rows.findIndex((row) => isEqual(row, template)) === -1
)

const service = app.service(config.path as any) as FeathersService

resolve(await service.create(templatesToBeInserted))
})
)

await Promise.all(insertionPromises)
}
}

if (forceRefresh) {
// for local dev clear the storage provider
Expand Down

0 comments on commit 62de3bd

Please sign in to comment.