-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract loader and update logic for deterministic streams
- Loading branch information
Showing
37 changed files
with
2,236 additions
and
3,914 deletions.
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,6 @@ | ||
{ | ||
"extends": ["3box", "3box/jest", "3box/typescript"], | ||
"parserOptions": { | ||
"project": ["tsconfig.lint.json"] | ||
} | ||
} |
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,4 @@ | ||
*.log | ||
.DS_Store | ||
node_modules | ||
dist |
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 @@ | ||
"eslint-config-3box/prettier.config" |
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,10 @@ | ||
# @composedb/loader | ||
|
||
## 0.7.0 | ||
|
||
### Minor Changes | ||
|
||
- Add support for models using the new `set` account relation | ||
- Add support for the `shouldIndex` metadata flag | ||
- Add support for immutable fields | ||
- Add support for optional relation fields |
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,5 @@ | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. |
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,19 @@ | ||
The MIT License (MIT) | ||
|
||
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. |
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,11 @@ | ||
# ComposeDB loader | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install @composedb/loader | ||
``` | ||
|
||
## License | ||
|
||
Apache-2.0 OR MIT |
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,71 @@ | ||
{ | ||
"name": "@composedb/loader", | ||
"version": "0.7.0", | ||
"author": "3Box Labs", | ||
"license": "(Apache-2.0 OR MIT)", | ||
"homepage": "https://github.com/ceramicstudio/js-composedb#readme", | ||
"keywords": [ | ||
"ceramic", | ||
"composedb", | ||
"loader" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ceramicstudio/js-composedb", | ||
"directory": "packages/loader" | ||
}, | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=20" | ||
}, | ||
"sideEffects": false, | ||
"scripts": { | ||
"build:clean": "del dist", | ||
"build:js": "swc src -d ./dist --config-file ../../.swcrc", | ||
"build:types": "tsc --emitDeclarationOnly --skipLibCheck", | ||
"build": "pnpm build:clean && pnpm build:types && pnpm build:js", | ||
"lint": "eslint src test --fix", | ||
"test": "node --experimental-vm-modules ../../node_modules/jest/bin/jest.js", | ||
"test:ci": "pnpm run test --ci --coverage", | ||
"prepare": "pnpm build", | ||
"prepublishOnly": "package-check" | ||
}, | ||
"dependencies": { | ||
"@ceramicnetwork/stream-model-instance": "^3.1.0", | ||
"@ceramicnetwork/streamid": "^4.1.0", | ||
"@ipld/dag-cbor": "^9.1.0", | ||
"dataloader": "^2.2.2", | ||
"multiformats": "^13.0.1" | ||
}, | ||
"devDependencies": { | ||
"@ceramicnetwork/common": "^4.1.0", | ||
"@composedb/devtools": "workspace:^", | ||
"@composedb/test-schemas": "workspace:^", | ||
"@composedb/types": "workspace:^", | ||
"jest-environment-composedb": "workspace:^" | ||
}, | ||
"jest": { | ||
"extensionsToTreatAsEsm": [ | ||
".ts" | ||
], | ||
"moduleNameMapper": { | ||
"^(\\.{1,2}/.*)\\.js$": "$1" | ||
}, | ||
"transform": { | ||
"^.+\\.(t|j)s$": [ | ||
"@swc/jest", | ||
{ | ||
"root": "../.." | ||
} | ||
] | ||
} | ||
} | ||
} |
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,52 @@ | ||
import type { GenesisCommit, GenesisHeader } from '@ceramicnetwork/common' | ||
import { ModelInstanceDocument } from '@ceramicnetwork/stream-model-instance' | ||
import { StreamID } from '@ceramicnetwork/streamid' | ||
import * as codec from '@ipld/dag-cbor' | ||
import { CID } from 'multiformats/cid' | ||
import { sha256 } from 'multiformats/hashes/sha2' | ||
|
||
import type { LoadKey } from './types.js' | ||
|
||
export type GenesisMetadata = { | ||
controller: string | ||
model: StreamID | string | ||
unique?: Array<string> | ||
} | ||
|
||
/** | ||
* Create a deterministic genesis commit matching the logic unsed internally by ModelInstanceDocument. | ||
*/ | ||
export function createDeterministicGenesis(meta: GenesisMetadata): GenesisCommit { | ||
const model = meta.model instanceof StreamID ? meta.model : StreamID.fromString(meta.model) | ||
const header: GenesisHeader = { controllers: [meta.controller], model: model.bytes, sep: 'model' } | ||
if (Array.isArray(meta.unique)) { | ||
header.unique = new TextEncoder().encode(meta.unique.join('|')) | ||
} | ||
return { header, data: null } | ||
} | ||
|
||
/** | ||
* Create a StreamID from a GenesisCommit. | ||
*/ | ||
export async function createGenesisID(genesis: GenesisCommit): Promise<StreamID> { | ||
const bytes = codec.encode(genesis) | ||
const digest = await sha256.digest(bytes) | ||
const cid = CID.createV1(codec.code, digest) | ||
return new StreamID(ModelInstanceDocument.STREAM_TYPE_ID, cid) | ||
} | ||
|
||
/** | ||
* Create a LoadKey for a deterministic stream. | ||
*/ | ||
export async function createDeterministicKey(meta: GenesisMetadata): Promise<LoadKey> { | ||
const genesis = createDeterministicGenesis(meta) | ||
return { id: await createGenesisID(genesis), genesis } | ||
} | ||
|
||
/** | ||
* Get the cache key for a deterministic stream. | ||
*/ | ||
export function getDeterministicCacheKey(meta: GenesisMetadata): string { | ||
const model = typeof meta.model === 'string' ? meta.model : meta.model.toString() | ||
return [meta.controller, model].concat(meta.unique ?? []).join('|') | ||
} |
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,19 @@ | ||
/** | ||
* ComposeDB loader module, handling streams loading and caching. | ||
* | ||
* ## Installation | ||
* | ||
* ```sh | ||
* npm install @composedb/loader | ||
* ``` | ||
* | ||
* @module loader | ||
*/ | ||
export { | ||
type CreateOptions, | ||
DocumentLoader, | ||
type DocumentLoaderParams, | ||
type UpdateDocOptions, | ||
type UpdateOptions, | ||
} from './loader.js' | ||
export type { CacheMap, DeterministicKeysCache, DocID, DocumentCache, LoadKey } from './types.js' |
Oops, something went wrong.