Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 11 additions & 41 deletions packages/server-core/src/seeder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FeathersService } from '@feathersjs/feathers/lib'
import appRootPath from 'app-root-path'
import fs from 'fs'
import path from 'path'
Expand All @@ -7,49 +8,18 @@ import config from './appconfig'
import { copyDefaultProject, uploadLocalProjectToProvider } from './projects/project/project.class'
import seederConfig from './seeder-config'

const settingsServiceNames = [
'authentication-setting',
'aws-setting',
'chargebee-setting',
'coil-setting',
'client-setting',
'email-setting',
'instance-server-setting',
'redis-setting',
'server-setting',
'task-server-setting'
]

export async function seeder(app: Application, forceRefresh: boolean, prepareDb: boolean) {
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 (settingsServiceNames.indexOf(config.path) > -1) {
const result = await service.find()
isSeeded = result.total > 0
} else {
const searchTemplate = {}
if (!forceRefresh && !prepareDb) return

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)
}
}
}
for (const config of seederConfig) {
if (!config.path || !config.templates) return

const templates = config.templates

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

await service.create(templates)
}

if (forceRefresh) {
// for local dev clear the storage provider
Expand Down
19 changes: 17 additions & 2 deletions packages/server-core/src/social/location/location.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class Location<T = LocationDataType> extends Service<T> {
* @param params
* @returns new location object
*/
async create(data: any, params?: UserParams): Promise<T> {
async createSingle(data: any, params?: UserParams): Promise<T> {
const t = await this.app.get('sequelizeClient').transaction()

try {
Expand Down Expand Up @@ -294,13 +294,28 @@ export class Location<T = LocationDataType> extends Service<T> {
} catch (err) {
logger.error(err)
await t.rollback()
if (err.errors[0].message === 'slugifiedName must be unique') {
if (err && err.errors[0].message === 'slugifiedName must be unique') {
throw new Error('Name is in use.')
}
throw err
}
}

/**
* A function which is used to create new location
*
* @param data of location
* @param params
* @returns new location object
*/
async create(data: any | any[], params?: UserParams): Promise<T | Awaited<T>[]> {
if (Array.isArray(data)) {
return Promise.all(data.map((d) => this.createSingle(d, params)))
}

return this.createSingle(data, params)
}

/**
* A function which is used to update location
*
Expand Down