mirror of
https://github.com/Effect-TS/effect.git
synced 2026-11-25 02:06:20 +08:00
add Cluster modules (#433)
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-present The Contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,7 @@
|
||||
# `@effect/sql-sqlite-react-native`
|
||||
|
||||
An `@effect/sql` implementation using the `react-native-quick-sqlite` library.
|
||||
|
||||
## Documentation
|
||||
|
||||
- **API Reference**: [View the full documentation](https://effect-ts.github.io/effect/docs/sql-sqlite-react-native).
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"$schema": "../../node_modules/@effect/docgen/schema.json",
|
||||
"srcLink": "https://github.com/Effect-TS/effect/tree/main/packages/sql/sqlite-react-native/src/",
|
||||
"exclude": ["src/internal/**/*.ts"],
|
||||
"examplesCompilerOptions": {
|
||||
"noEmit": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "Bundler",
|
||||
"module": "ES2022",
|
||||
"target": "ES2022",
|
||||
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
||||
"rewriteRelativeImportExtensions": true,
|
||||
"allowImportingTsExtensions": true,
|
||||
"paths": {
|
||||
"effect": ["../../../effect/src/index.js"],
|
||||
"effect/*": ["../../../effect/src/*.js"]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"name": "@effect/sql-sqlite-react-native",
|
||||
"version": "0.47.1",
|
||||
"type": "module",
|
||||
"license": "MIT",
|
||||
"description": "A SQLite toolkit for Effect",
|
||||
"homepage": "https://effect.website",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/Effect-TS/effect.git",
|
||||
"directory": "packages/sql/sqlite-react-native"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/Effect-TS/effect/issues"
|
||||
},
|
||||
"tags": [
|
||||
"typescript",
|
||||
"sql",
|
||||
"database"
|
||||
],
|
||||
"keywords": [
|
||||
"typescript",
|
||||
"sql",
|
||||
"database"
|
||||
],
|
||||
"sideEffects": [],
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": "./src/index.ts",
|
||||
"./*": "./src/*.ts",
|
||||
"./internal/*": null,
|
||||
"./*/index": null
|
||||
},
|
||||
"files": [
|
||||
"src/**/*.ts",
|
||||
"dist/**/*.js",
|
||||
"dist/**/*.js.map",
|
||||
"dist/**/*.d.ts",
|
||||
"dist/**/*.d.ts.map"
|
||||
],
|
||||
"publishConfig": {
|
||||
"provenance": true,
|
||||
"exports": {
|
||||
"./package.json": "./package.json",
|
||||
".": "./dist/index.js",
|
||||
"./*": "./dist/*.js",
|
||||
"./internal/*": null,
|
||||
"./*/index": null
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"codegen": "build-utils prepare-v4",
|
||||
"build": "tsc -b tsconfig.build.json && babel dist --plugins annotate-pure-calls --out-dir dist --source-maps",
|
||||
"check": "tsc -b tsconfig.json",
|
||||
"test": "vitest",
|
||||
"coverage": "vitest --coverage"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@op-engineering/op-sqlite": "15.0.1",
|
||||
"effect": "workspace:^"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@op-engineering/op-sqlite": "7.1.0",
|
||||
"effect": "workspace:^"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
import * as Sqlite from "@op-engineering/op-sqlite"
|
||||
import * as Config from "effect/config/Config"
|
||||
import * as Effect from "effect/Effect"
|
||||
import * as Fiber from "effect/Fiber"
|
||||
import { constFalse, identity } from "effect/Function"
|
||||
import * as Layer from "effect/Layer"
|
||||
import * as Scope from "effect/Scope"
|
||||
import * as ServiceMap from "effect/ServiceMap"
|
||||
import * as Stream from "effect/stream/Stream"
|
||||
import * as Reactivity from "effect/unstable/reactivity/Reactivity"
|
||||
import * as Client from "effect/unstable/sql/SqlClient"
|
||||
import type { Connection } from "effect/unstable/sql/SqlConnection"
|
||||
import { SqlError } from "effect/unstable/sql/SqlError"
|
||||
import * as Statement from "effect/unstable/sql/Statement"
|
||||
|
||||
const ATTR_DB_SYSTEM_NAME = "db.system.name"
|
||||
|
||||
/**
|
||||
* @category type ids
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export const TypeId: TypeId = "~@effect/sql-sqlite-react-native/SqliteClient"
|
||||
|
||||
/**
|
||||
* @category type ids
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export type TypeId = "~@effect/sql-sqlite-react-native/SqliteClient"
|
||||
|
||||
/**
|
||||
* @category models
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export interface SqliteClient extends Client.SqlClient {
|
||||
readonly [TypeId]: TypeId
|
||||
readonly config: SqliteClientConfig
|
||||
|
||||
/** Not supported in sqlite */
|
||||
readonly updateValues: never
|
||||
}
|
||||
|
||||
/**
|
||||
* @category tags
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export const SqliteClient = ServiceMap.Key<SqliteClient>("@effect/sql-sqlite-react-native/SqliteClient")
|
||||
|
||||
/**
|
||||
* @category models
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export interface SqliteClientConfig {
|
||||
readonly filename: string
|
||||
readonly location?: string | undefined
|
||||
readonly encryptionKey?: string | undefined
|
||||
readonly spanAttributes?: Record<string, unknown> | undefined
|
||||
readonly transformResultNames?: ((str: string) => string) | undefined
|
||||
readonly transformQueryNames?: ((str: string) => string) | undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* @category fiber refs
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export const AsyncQuery = ServiceMap.Reference<boolean>(
|
||||
"@effect/sql-sqlite-react-native/Client/asyncQuery",
|
||||
{ defaultValue: constFalse }
|
||||
)
|
||||
|
||||
/**
|
||||
* @category fiber refs
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export const withAsyncQuery = <R, E, A>(effect: Effect.Effect<A, E, R>) =>
|
||||
Effect.provideService(effect, AsyncQuery, true)
|
||||
|
||||
interface SqliteConnection extends Connection {}
|
||||
|
||||
/**
|
||||
* @category constructor
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export const make = (
|
||||
options: SqliteClientConfig
|
||||
): Effect.Effect<SqliteClient, never, Scope.Scope | Reactivity.Reactivity> =>
|
||||
Effect.gen(function*() {
|
||||
const clientOptions: Parameters<typeof Sqlite.open>[0] = {
|
||||
name: options.filename
|
||||
}
|
||||
if (options.location) {
|
||||
clientOptions.location = options.location
|
||||
}
|
||||
if (options.encryptionKey) {
|
||||
clientOptions.encryptionKey = options.encryptionKey
|
||||
}
|
||||
|
||||
const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
|
||||
const transformRows = options.transformResultNames ?
|
||||
Statement.defaultTransforms(options.transformResultNames).array :
|
||||
undefined
|
||||
|
||||
const makeConnection = Effect.gen(function*() {
|
||||
const db = Sqlite.open(clientOptions)
|
||||
yield* Effect.addFinalizer(() => Effect.sync(() => db.close()))
|
||||
|
||||
const run = (
|
||||
sql: string,
|
||||
params: ReadonlyArray<Statement.Primitive> = [],
|
||||
values = false
|
||||
) =>
|
||||
Effect.withFiber<Array<any>, SqlError>((fiber) => {
|
||||
if (fiber.getRef(AsyncQuery)) {
|
||||
return Effect.map(
|
||||
Effect.tryPromise({
|
||||
try: () => db.execute(sql, params as Array<any>),
|
||||
catch: (cause) => new SqlError({ cause, message: "Failed to execute statement (async)" })
|
||||
}),
|
||||
(result) => values ? result.rawRows ?? [] : result.rows
|
||||
)
|
||||
}
|
||||
return Effect.try({
|
||||
try: () => {
|
||||
const result = db.executeSync(sql, params as Array<any>)
|
||||
return values ? result.rawRows ?? [] : result.rows
|
||||
},
|
||||
catch: (cause) => new SqlError({ cause, message: "Failed to execute statement" })
|
||||
})
|
||||
})
|
||||
|
||||
return identity<SqliteConnection>({
|
||||
execute(sql, params, transformRows) {
|
||||
return transformRows
|
||||
? Effect.map(run(sql, params), transformRows)
|
||||
: run(sql, params)
|
||||
},
|
||||
executeRaw(sql, params) {
|
||||
return run(sql, params)
|
||||
},
|
||||
executeValues(sql, params) {
|
||||
return run(sql, params, true)
|
||||
},
|
||||
executeUnprepared(sql, params, transformRows) {
|
||||
return this.execute(sql, params, transformRows)
|
||||
},
|
||||
executeStream() {
|
||||
return Stream.die("executeStream not implemented")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
const semaphore = yield* Effect.makeSemaphore(1)
|
||||
const connection = yield* makeConnection
|
||||
|
||||
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
||||
const transactionAcquirer = Effect.uninterruptibleMask((restore) => {
|
||||
const fiber = Fiber.getCurrent()!
|
||||
const scope = ServiceMap.unsafeGet(fiber.services, Scope.Scope)
|
||||
return Effect.as(
|
||||
Effect.tap(
|
||||
restore(semaphore.take(1)),
|
||||
() => Scope.addFinalizer(scope, semaphore.release(1))
|
||||
),
|
||||
connection
|
||||
)
|
||||
})
|
||||
|
||||
return Object.assign(
|
||||
(yield* Client.make({
|
||||
acquirer,
|
||||
compiler,
|
||||
transactionAcquirer,
|
||||
spanAttributes: [
|
||||
...(options.spanAttributes ? Object.entries(options.spanAttributes) : []),
|
||||
[ATTR_DB_SYSTEM_NAME, "sqlite"]
|
||||
],
|
||||
transformRows
|
||||
})) as SqliteClient,
|
||||
{
|
||||
[TypeId]: TypeId,
|
||||
config: options
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
/**
|
||||
* @category layers
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export const layerConfig = (
|
||||
config: Config.Wrap<SqliteClientConfig>
|
||||
): Layer.Layer<SqliteClient | Client.SqlClient, Config.ConfigError> =>
|
||||
Layer.effectServices(
|
||||
Config.unwrap(config).asEffect().pipe(
|
||||
Effect.flatMap(make),
|
||||
Effect.map((client) =>
|
||||
ServiceMap.make(SqliteClient, client).pipe(
|
||||
ServiceMap.add(Client.SqlClient, client)
|
||||
)
|
||||
)
|
||||
)
|
||||
).pipe(Layer.provide(Reactivity.layer))
|
||||
|
||||
/**
|
||||
* @category layers
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export const layer = (
|
||||
config: SqliteClientConfig
|
||||
): Layer.Layer<SqliteClient | Client.SqlClient> =>
|
||||
Layer.effectServices(
|
||||
Effect.map(make(config), (client) =>
|
||||
ServiceMap.make(SqliteClient, client).pipe(
|
||||
ServiceMap.add(Client.SqlClient, client)
|
||||
))
|
||||
).pipe(Layer.provide(Reactivity.layer))
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
import type * as Effect from "effect/Effect"
|
||||
import * as Layer from "effect/Layer"
|
||||
import * as Migrator from "effect/unstable/sql/Migrator"
|
||||
import type * as Client from "effect/unstable/sql/SqlClient"
|
||||
import type { SqlError } from "effect/unstable/sql/SqlError"
|
||||
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export * from "effect/unstable/sql/Migrator"
|
||||
|
||||
/**
|
||||
* @category constructor
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export const run: <R>(
|
||||
options: Migrator.MigratorOptions<R>
|
||||
) => Effect.Effect<
|
||||
ReadonlyArray<readonly [id: number, name: string]>,
|
||||
SqlError | Migrator.MigrationError,
|
||||
Client.SqlClient | R
|
||||
> = Migrator.make({})
|
||||
|
||||
/**
|
||||
* @category constructor
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export const layer = <R>(
|
||||
options: Migrator.MigratorOptions<R>
|
||||
): Layer.Layer<never, SqlError | Migrator.MigrationError, R | Client.SqlClient> => Layer.effectDiscard(run(options))
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export * as SqliteClient from "./SqliteClient.ts"
|
||||
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
export * as SqliteMigrator from "./SqliteMigrator.ts"
|
||||
@@ -0,0 +1,3 @@
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@@ -0,0 +1,6 @@
|
||||
import { describe, it } from "@effect/vitest"
|
||||
import { Effect } from "effect"
|
||||
|
||||
describe("Client", () => {
|
||||
it.effect("should work", () => Effect.void)
|
||||
})
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/tsconfig",
|
||||
"extends": "./tsconfig.src.json",
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": ".tsbuildinfo/build.tsbuildinfo",
|
||||
"outDir": "dist",
|
||||
"stripInternal": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/tsconfig",
|
||||
"extends": "../../../tsconfig.base.jsonc",
|
||||
"include": [],
|
||||
"references": [{ "path": "tsconfig.src.json" }, { "path": "tsconfig.test.json" }]
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/tsconfig",
|
||||
"extends": "../../../tsconfig.base.jsonc",
|
||||
"include": ["src"],
|
||||
"references": [{ "path": "../../effect/tsconfig.src.json" }],
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": ".tsbuildinfo/src.tsbuildinfo",
|
||||
"outDir": ".tsbuildinfo/src",
|
||||
"rootDir": "src",
|
||||
"types": ["node"],
|
||||
"moduleResolution": "nodenext"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/tsconfig",
|
||||
"extends": "../../../tsconfig.base.jsonc",
|
||||
"include": ["test/**/*.ts", "test/**/*.json"],
|
||||
"references": [{ "path": "tsconfig.src.json" }],
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": ".tsbuildinfo/test.tsbuildinfo",
|
||||
"outDir": ".tsbuildinfo/test",
|
||||
"rootDir": "test",
|
||||
"noEmit": true,
|
||||
"erasableSyntaxOnly": false,
|
||||
"resolveJsonModule": true,
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@effect/sql-react-native": ["src/index.ts"],
|
||||
"@effect/sql-react-native/*": ["src/*/index.ts", "src/*.ts"]
|
||||
},
|
||||
"plugins": [
|
||||
{
|
||||
"name": "@effect/language-service",
|
||||
"namespaceImportPackages": []
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { mergeConfig, type ViteUserConfig } from "vitest/config"
|
||||
import shared from "../../../vitest.shared.ts"
|
||||
|
||||
const config: ViteUserConfig = {}
|
||||
|
||||
export default mergeConfig(shared, config)
|
||||
Reference in New Issue
Block a user