feat: 🤡 add demo mock for db

This commit is contained in:
Julien Oculi 2024-06-19 16:10:22 +02:00
parent 80a2eed2ee
commit c766a00980
3 changed files with 124 additions and 0 deletions

14
src/db/mock/groups.json Normal file
View file

@ -0,0 +1,14 @@
[
{
"type": "group",
"name": "user.admin"
},
{
"type": "group",
"name": "user.member"
},
{
"type": "group",
"name": "user.devops"
}
]

68
src/db/mock/users.json Normal file
View file

@ -0,0 +1,68 @@
[
{
"type": "user",
"firstname": "Jean-Baptiste",
"lastname": "Bonnemaison",
"mail": "jean-baptiste.bonnemaison@u-bordeaux.fr",
"groups": [
"user.admin"
]
},
{
"type": "user",
"firstname": "Pierre",
"lastname": "Grange-Praderas",
"mail": "pierre.grange-praderas@u-bordeaux.fr",
"groups": [
"user.admin",
"user.devops"
]
},
{
"type": "user",
"firstname": "Eléonore",
"lastname": "Fraisse",
"mail": "eleonore.fraisse@icloud.com",
"groups": [
"user.member"
]
},
{
"type": "user",
"firstname": "pgp",
"lastname": "pgp",
"mail": "pgp@cplusmoins.org",
"groups": [
"user.admin"
]
},
{
"type": "user",
"firstname": "Julien",
"lastname": "Oculi",
"mail": "oculi.julien.t@gmail.com",
"groups": [
"user.admin",
"user.devops"
]
},
{
"type": "user",
"firstname": "John",
"lastname": "Doe",
"mail": "test_coha@yopmail.com",
"groups": [
"user.admin",
"user.devops"
]
},
{
"type": "user",
"firstname": "Ulrich",
"lastname": "Moniteur",
"mail": "moniteurulrich@gmail.com",
"groups": [
"user.member"
]
}
]

42
src/db/mod.ts Normal file
View file

@ -0,0 +1,42 @@
import { Db, Group } from '@cohabit/ressources_manager/mod.ts'
// Import Datas
import groups from './mock/groups.json' with { type: 'json' }
import users from './mock/users.json' with { type: 'json' }
import { User } from '@cohabit/ressources_manager/src/models/mod.ts'
import { MailAddress } from '@cohabit/ressources_manager/types.ts'
import { existsSync } from '$std/fs/exists.ts'
const dbPath = './_fresh/db.sqlite'
export const db = await Db.init(dbPath)
//! Temp for demo only
if (!sessionStorage.getItem('db-loaded') && !existsSync(dbPath)) {
console.log('=============LOAD DB=============')
// Load groups to DB
for (const { name } of groups) {
const group = Group.load({ name })
db.ressource.group.set([group])
}
// Load users to DB
for (const { lastname, firstname, mail, groups: groupNames } of users) {
// Get groups of user
const groups = await db.ressource.group.listRef((group) =>
groupNames.includes(group.name)
)
const user = User.load({
firstname,
lastname,
mail: mail as MailAddress,
groups,
})
db.ressource.user.set([user])
}
sessionStorage.setItem('db-loaded', 'true')
}