forked from logto-io/logto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(cli): added user seeding in local seeder * feat(cli): seed users code * feat(cli): seed testing env
- Loading branch information
1 parent
8f9d73e
commit 8aa017b
Showing
5 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* eslint-disable eslint-comments/disable-enable-pair */ | ||
/* eslint-disable @silverhand/fp/no-mutating-methods */ | ||
|
||
import { Users, UsersRoles } from '@logto/schemas'; | ||
import { sql, type DatabaseTransactionConnection } from '@silverhand/slonik'; | ||
|
||
import { type UserSeeder } from './ogcio-seeder.js'; | ||
import { createOrUpdateItem } from './queries.js'; | ||
|
||
export const seedUsers = async (params: { | ||
transaction: DatabaseTransactionConnection; | ||
tenantId: string; | ||
usersToSeed: UserSeeder[]; | ||
}) => { | ||
if (params.usersToSeed.length === 0) { | ||
return {}; | ||
} | ||
|
||
const queries: Array<Promise<void>> = []; | ||
|
||
for (const user of params.usersToSeed) { | ||
queries.push(createUser({ ...params, userToSeed: user })); | ||
} | ||
|
||
await Promise.all(queries); | ||
|
||
return params.usersToSeed; | ||
}; | ||
|
||
const createUser = async (params: { | ||
transaction: DatabaseTransactionConnection; | ||
tenantId: string; | ||
userToSeed: UserSeeder; | ||
}): Promise<void> => { | ||
await createOrUpdateItem({ | ||
transaction: params.transaction, | ||
tenantId: params.tenantId, | ||
toLogFieldName: 'id', | ||
whereClauses: [sql`tenant_id = ${params.tenantId}`, sql`id = ${params.userToSeed.id}`], | ||
toInsert: { | ||
id: params.userToSeed.id, | ||
username: params.userToSeed.username, | ||
primary_email: params.userToSeed.primary_email, | ||
primary_phone: params.userToSeed.primary_phone ?? undefined, | ||
name: params.userToSeed.name, | ||
application_id: params.userToSeed.application_id, | ||
}, | ||
tableName: Users.table, | ||
}); | ||
|
||
const assignRoleQueries = []; | ||
for (const roleId of params.userToSeed.resource_role_ids) { | ||
assignRoleQueries.push( | ||
createOrUpdateItem({ | ||
transaction: params.transaction, | ||
tenantId: params.tenantId, | ||
toLogFieldName: 'role_id', | ||
whereClauses: [ | ||
sql`tenant_id = ${params.tenantId}`, | ||
sql`user_id = ${params.userToSeed.id}`, | ||
sql`role_id = ${roleId}`, | ||
], | ||
toInsert: { | ||
user_id: params.userToSeed.id, | ||
role_id: roleId, | ||
}, | ||
tableName: UsersRoles.table, | ||
}) | ||
); | ||
} | ||
|
||
await Promise.all(assignRoleQueries); | ||
}; |