-
-
Notifications
You must be signed in to change notification settings - Fork 35
Added modules for IndexedDb #1240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,111 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import * as Config from "effect/Config"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import * as Effect from "effect/Effect"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import * as Layer from "effect/Layer"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import * as Schema from "effect/Schema"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import * as SchemaIssue from "effect/SchemaIssue"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import * as ServiceMap from "effect/ServiceMap"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @category type ids | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const TypeId: unique symbol = Symbol.for( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "@effect/platform-browser/IndexedDb", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @category type ids | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export type TypeId = typeof TypeId; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @category models | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface IndexedDb { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readonly [TypeId]: TypeId; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readonly indexedDB: globalThis.IDBFactory; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readonly IDBKeyRange: typeof globalThis.IDBKeyRange; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @category tag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const IndexedDb: ServiceMap.Service<IndexedDb, IndexedDb> = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ServiceMap.Service<IndexedDb, IndexedDb>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "@effect/platform-browser/IndexedDb", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @category constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const make = (impl: Omit<IndexedDb, TypeId>): IndexedDb => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IndexedDb.of({ ...impl, [TypeId]: TypeId }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Instance of IndexedDb from the `window` object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @category constructors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const layerWindow: Layer.Layer<IndexedDb, Config.ConfigError> = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Layer.effect( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IndexedDb, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Effect.suspend(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (window.indexedDB && window.IDBKeyRange) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Effect.succeed( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| make({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| indexedDB: window.indexedDB, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IDBKeyRange: window.IDBKeyRange, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Effect.fail( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new Config.ConfigError( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new Schema.SchemaError( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new SchemaIssue.MissingKey({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message: "window.indexedDB is not available", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Schema for `autoIncrement` key path (`number`). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @category schemas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const AutoIncrement = Schema.Number.annotate({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| identifier: "AutoIncrement", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| title: "autoIncrement", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: "Defines a valid autoIncrement key path for the IndexedDb table", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+88
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the standard (IndexedDB API 3.0), the
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 2 ** 53 })) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** @internal */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const IDBFlatKey = Schema.Union([ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Schema.String, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Schema.Number, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Schema.Date, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Schema.declare( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (input): input is BufferSource => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input instanceof ArrayBuffer || ArrayBuffer.isView(input), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Schema for `IDBValidKey` (`number | string | Date | BufferSource | IDBValidKey[]`). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @since 1.0.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @category schemas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const IDBValidKey = Schema.Union([IDBFlatKey, Schema.Array(IDBFlatKey)]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+94
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use Schema.Finite for finite numbers. I think it excludes nan too
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would avoid using suspend here and keep it as it was. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No suspend is fine as it's functionally equivalent.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK if infinite is permitted then the nan check is fine. suspend works, but you end up with a better schema ast without it. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on other Effect modules, it may make more sense to expose effectful service functions rather than the built-in interface. For example, as a rough sketch:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would keep the service lean and then potentially expose another service that further extends it with effectful methods. Keeps it easily injectable for tests etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The effectful service could be added at a later date.