FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
opencode/packages/effect-drizzle-sqlite/examples/basic.ts at dev · argszero/opencode · GitHub
argszero
/
opencode
Public
forked from
anomalyco/opencode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
opencode
/
packages
/
effect-drizzle-sqlite
/
examples
/
basic.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
92 lines (77 loc) · 3.28 KB
Breadcrumbs
opencode
/
packages
/
effect-drizzle-sqlite
/
examples
/
basic.ts
Copy path
File metadata and controls
92 lines (77 loc) · 3.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import
{
SqliteClient
}
from
"@effect/sql-sqlite-bun"
import
{
eq
}
from
"drizzle-orm"
import
{
integer
,
sqliteTable
,
text
}
from
"drizzle-orm/sqlite-core"
import
*
as
Context
from
"effect/Context"
import
*
as
Effect
from
"effect/Effect"
import
*
as
Layer
from
"effect/Layer"
import
*
as
Schema
from
"effect/Schema"
import
{
EffectDrizzleSqlite
}
from
"../src"
const
users
=
sqliteTable
(
"users"
,
{
id
:
integer
(
)
.
primaryKey
(
{
autoIncrement
:
true
}
)
,
name
:
text
(
)
.
notNull
(
)
,
}
)
type
User
=
typeof
users
.
$inferSelect
const
makeDatabase
=
EffectDrizzleSqlite
.
makeWithDefaults
(
)
type
DatabaseShape
=
Effect
.
Success
<
typeof
makeDatabase
>
const
sqliteLayer
=
SqliteClient
.
layer
(
{
filename
:
":memory:"
,
disableWAL
:
true
}
)
class
Database
extends
Context
.
Service
<
Database
,
DatabaseShape
>
(
)
(
"@opencode/example/Database"
)
{
static
layer
=
Layer
.
effect
(
Database
,
makeDatabase
)
.
pipe
(
Layer
.
provide
(
sqliteLayer
)
)
}
class
UserStoreError
extends
Schema
.
TaggedErrorClass
<
UserStoreError
>
(
)
(
"UserStoreError"
,
{
message
:
Schema
.
String
,
cause
:
Schema
.
optional
(
Schema
.
Defect
(
)
)
,
}
)
{
}
const
mapStoreError
=
(
message
:
string
)
=>
(
cause
:
unknown
)
=>
new
UserStoreError
(
{
message
,
cause
}
)
interface
UserStoreShape
{
migrate
(
)
:
Effect
.
Effect
<
void
,
UserStoreError
>
create
(
name
:
string
)
:
Effect
.
Effect
<
void
,
UserStoreError
>
rename
(
from
:
string
,
to
:
string
)
:
Effect
.
Effect
<
void
,
UserStoreError
>
list
(
)
:
Effect
.
Effect
<
User
[
]
,
UserStoreError
>
}
class
UserStore
extends
Context
.
Service
<
UserStore
,
UserStoreShape
>
(
)
(
"@opencode/example/UserStore"
)
{
static
layer
=
Layer
.
effect
(
UserStore
,
Effect
.
gen
(
function
*
(
)
{
const
db
=
yield
*
Database
return
UserStore
.
of
(
{
migrate
:
Effect
.
fn
(
"UserStore.migrate"
)
(
function
*
(
)
{
yield
*
EffectDrizzleSqlite
.
migrate
(
db
,
{
migrationsFolder
:
`
${
import
.
meta
.
dirname
}
/migrations`
}
)
.
pipe
(
Effect
.
mapError
(
(
cause
)
=>
new
UserStoreError
(
{
message
:
"Failed to migrate users"
,
cause
}
)
)
,
)
}
)
,
create
:
Effect
.
fn
(
"UserStore.create"
)
(
function
*
(
name
:
string
)
{
yield
*
db
.
insert
(
users
)
.
values
(
{
name
}
)
.
pipe
(
Effect
.
asVoid
,
Effect
.
mapError
(
mapStoreError
(
"Failed to create user"
)
)
)
}
)
,
rename
:
Effect
.
fn
(
"UserStore.rename"
)
(
function
*
(
from
:
string
,
to
:
string
)
{
yield
*
db
.
transaction
(
Effect
.
fnUntraced
(
function
*
(
tx
)
{
yield
*
tx
.
insert
(
users
)
.
values
(
{
name
:
from
}
)
yield
*
tx
.
update
(
users
)
.
set
(
{
name
:
to
}
)
.
where
(
eq
(
users
.
name
,
from
)
)
}
)
,
{
behavior
:
"immediate"
}
,
)
.
pipe
(
Effect
.
asVoid
,
Effect
.
mapError
(
mapStoreError
(
"Failed to rename user"
)
)
)
}
)
,
list
:
Effect
.
fn
(
"UserStore.list"
)
(
function
*
(
)
{
return
yield
*
db
.
select
(
)
.
from
(
users
)
.
pipe
(
Effect
.
mapError
(
mapStoreError
(
"Failed to list users"
)
)
)
}
)
,
}
)
}
)
,
)
.
pipe
(
Layer
.
provide
(
Database
.
layer
)
)
}
const
program
=
Effect
.
gen
(
function
*
(
)
{
const
userStore
=
yield
*
UserStore
yield
*
userStore
.
migrate
(
)
yield
*
userStore
.
create
(
"Ada"
)
yield
*
userStore
.
rename
(
"Grace"
,
"Grace Hopper"
)
return
yield
*
userStore
.
list
(
)
}
)
const
rows
=
await
Effect
.
runPromise
(
program
.
pipe
(
Effect
.
provide
(
UserStore
.
layer
)
)
)
console
.
log
(
rows
)
Back
|
FazBrowse Home
|
New Git URL