Compare commits

...

2 Commits

Author SHA1 Message Date
Anselm
ad40b883eb Pre-release 2024-09-02 17:29:47 +01:00
Anselm
1c64ae1bba First sketch of creating and finding unique CoMaps 2024-09-02 17:28:03 +01:00
44 changed files with 287 additions and 66 deletions

View File

@@ -0,0 +1,6 @@
---
"jazz-tools": patch
"cojson": patch
---
First sketch of API for creating and finding unique CoValues

View File

@@ -1,27 +1,28 @@
{ {
"mode": "pre", "mode": "pre",
"tag": "new-auth", "tag": "unique",
"initialVersions": { "initialVersions": {
"jazz-example-chat": "0.0.81", "jazz-example-chat": "0.0.82-new-auth.1",
"jazz-example-chat-clerk": "0.0.79", "jazz-example-chat-clerk": "0.0.80-new-auth.1",
"jazz-inspector": "0.0.59", "jazz-inspector": "0.0.59",
"jazz-example-pets": "0.0.99", "jazz-example-pets": "0.0.100-new-auth.1",
"jazz-example-todo": "0.0.98", "jazz-example-todo": "0.0.99-new-auth.1",
"cojson": "0.7.34", "cojson": "0.7.34",
"cojson-storage-indexeddb": "0.7.34", "cojson-storage-indexeddb": "0.7.34",
"cojson-storage-sqlite": "0.7.34", "cojson-storage-sqlite": "0.7.34",
"cojson-transport-ws": "0.7.34", "cojson-transport-ws": "0.7.34",
"hash-slash": "0.2.0", "hash-slash": "0.2.0",
"jazz-browser": "0.7.34", "jazz-browser": "0.7.35-new-auth.0",
"jazz-browser-auth-clerk": "0.7.32", "jazz-browser-auth-clerk": "0.7.33-new-auth.0",
"jazz-browser-media-images": "0.7.34", "jazz-browser-media-images": "0.7.35-new-auth.0",
"jazz-nodejs": "0.7.34", "jazz-nodejs": "0.7.35-new-auth.0",
"jazz-react": "0.7.34", "jazz-react": "0.7.35-new-auth.1",
"jazz-react-auth-clerk": "0.7.32", "jazz-react-auth-clerk": "0.7.33-new-auth.1",
"jazz-run": "0.7.34", "jazz-run": "0.7.35-new-auth.0",
"jazz-tools": "0.7.34" "jazz-tools": "0.7.35-new-auth.0"
}, },
"changesets": [ "changesets": [
"dirty-plants-sniff",
"small-students-buy", "small-students-buy",
"smart-mice-camp", "smart-mice-camp",
"twelve-lobsters-pull" "twelve-lobsters-pull"

View File

@@ -1,5 +1,15 @@
# jazz-example-chat # jazz-example-chat
## 0.0.80-unique.2
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- cojson@0.7.35-unique.2
- jazz-react@0.7.35-unique.2
- jazz-react-auth-clerk@0.7.33-unique.2
## 0.0.80-new-auth.1 ## 0.0.80-new-auth.1
### Patch Changes ### Patch Changes

View File

@@ -1,7 +1,7 @@
{ {
"name": "jazz-example-chat-clerk", "name": "jazz-example-chat-clerk",
"private": true, "private": true,
"version": "0.0.80-new-auth.1", "version": "0.0.80-unique.2",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",

View File

@@ -1,5 +1,14 @@
# jazz-example-chat # jazz-example-chat
## 0.0.82-unique.2
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- cojson@0.7.35-unique.2
- jazz-react@0.7.35-unique.2
## 0.0.82-new-auth.1 ## 0.0.82-new-auth.1
### Patch Changes ### Patch Changes

View File

@@ -1,7 +1,7 @@
{ {
"name": "jazz-example-chat", "name": "jazz-example-chat",
"private": true, "private": true,
"version": "0.0.82-new-auth.1", "version": "0.0.82-unique.2",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",

View File

@@ -1,5 +1,13 @@
# jazz-example-chat # jazz-example-chat
## 0.0.60-unique.0
### Patch Changes
- Updated dependencies
- cojson@0.7.35-unique.2
- cojson-transport-ws@0.7.35-unique.2
## 0.0.59 ## 0.0.59
### Patch Changes ### Patch Changes

View File

@@ -1,7 +1,7 @@
{ {
"name": "jazz-inspector", "name": "jazz-inspector",
"private": true, "private": true,
"version": "0.0.59", "version": "0.0.60-unique.0",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",

View File

@@ -1,5 +1,14 @@
# jazz-example-pets # jazz-example-pets
## 0.0.100-unique.2
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- jazz-browser-media-images@0.7.35-unique.2
- jazz-react@0.7.35-unique.2
## 0.0.100-new-auth.1 ## 0.0.100-new-auth.1
### Patch Changes ### Patch Changes

View File

@@ -1,7 +1,7 @@
{ {
"name": "jazz-example-pets", "name": "jazz-example-pets",
"private": true, "private": true,
"version": "0.0.100-new-auth.1", "version": "0.0.100-unique.2",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",

View File

@@ -1,5 +1,13 @@
# jazz-example-todo # jazz-example-todo
## 0.0.99-unique.2
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- jazz-react@0.7.35-unique.2
## 0.0.99-new-auth.1 ## 0.0.99-new-auth.1
### Patch Changes ### Patch Changes

View File

@@ -1,7 +1,7 @@
{ {
"name": "jazz-example-todo", "name": "jazz-example-todo",
"private": true, "private": true,
"version": "0.0.99-new-auth.1", "version": "0.0.99-unique.2",
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",

View File

@@ -1,5 +1,12 @@
# cojson-storage-indexeddb # cojson-storage-indexeddb
## 0.7.35-unique.2
### Patch Changes
- Updated dependencies
- cojson@0.7.35-unique.2
## 0.7.34 ## 0.7.34
### Patch Changes ### Patch Changes

View File

@@ -1,6 +1,6 @@
{ {
"name": "cojson-storage-indexeddb", "name": "cojson-storage-indexeddb",
"version": "0.7.34", "version": "0.7.35-unique.2",
"main": "dist/index.js", "main": "dist/index.js",
"type": "module", "type": "module",
"types": "src/index.ts", "types": "src/index.ts",

View File

@@ -1,5 +1,12 @@
# cojson-storage-sqlite # cojson-storage-sqlite
## 0.7.35-unique.2
### Patch Changes
- Updated dependencies
- cojson@0.7.35-unique.2
## 0.7.34 ## 0.7.34
### Patch Changes ### Patch Changes

View File

@@ -1,7 +1,7 @@
{ {
"name": "cojson-storage-sqlite", "name": "cojson-storage-sqlite",
"type": "module", "type": "module",
"version": "0.7.34", "version": "0.7.35-unique.2",
"main": "dist/index.js", "main": "dist/index.js",
"types": "src/index.ts", "types": "src/index.ts",
"license": "MIT", "license": "MIT",

View File

@@ -1,5 +1,12 @@
# cojson-transport-nodejs-ws # cojson-transport-nodejs-ws
## 0.7.35-unique.2
### Patch Changes
- Updated dependencies
- cojson@0.7.35-unique.2
## 0.7.34 ## 0.7.34
### Patch Changes ### Patch Changes

View File

@@ -1,7 +1,7 @@
{ {
"name": "cojson-transport-ws", "name": "cojson-transport-ws",
"type": "module", "type": "module",
"version": "0.7.34", "version": "0.7.35-unique.2",
"main": "dist/index.js", "main": "dist/index.js",
"types": "src/index.ts", "types": "src/index.ts",
"license": "MIT", "license": "MIT",

View File

@@ -1,5 +1,11 @@
# cojson # cojson
## 0.7.35-unique.2
### Patch Changes
- First sketch of API for creating and finding unique CoValues
## 0.7.34 ## 0.7.34
### Patch Changes ### Patch Changes

View File

@@ -5,7 +5,7 @@
"types": "src/index.ts", "types": "src/index.ts",
"type": "module", "type": "module",
"license": "MIT", "license": "MIT",
"version": "0.7.34", "version": "0.7.35-unique.2",
"devDependencies": { "devDependencies": {
"@types/jest": "^29.5.3", "@types/jest": "^29.5.3",
"@typescript-eslint/eslint-plugin": "^6.2.1", "@typescript-eslint/eslint-plugin": "^6.2.1",

View File

@@ -41,9 +41,9 @@ export type CoValueHeader = {
type: AnyRawCoValue["type"]; type: AnyRawCoValue["type"];
ruleset: RulesetDef; ruleset: RulesetDef;
meta: JsonObject | null; meta: JsonObject | null;
createdAt: `2${string}` | null; } & CoValueUniqueness;
uniqueness: `z${string}` | null;
}; export type CoValueUniqueness = {uniqueness: JsonValue, createdAt?: `2${string}` | null}
export function idforHeader( export function idforHeader(
header: CoValueHeader, header: CoValueHeader,

View File

@@ -1,4 +1,4 @@
import { CoValueCore, CoValueHeader } from "../coValueCore.js"; import { CoValueCore, CoValueHeader, CoValueUniqueness } from "../coValueCore.js";
import { CoID, RawCoValue } from "../coValue.js"; import { CoID, RawCoValue } from "../coValue.js";
import { import {
AgentSecret, AgentSecret,
@@ -92,8 +92,8 @@ export class RawControlledAccount<Meta extends AccountMeta = AccountMeta>
* Creates a new group (with the current account as the group's first admin). * Creates a new group (with the current account as the group's first admin).
* @category 1. High-level * @category 1. High-level
*/ */
createGroup() { createGroup(uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique()) {
return this.core.node.createGroup(); return this.core.node.createGroup(uniqueness);
} }
async acceptInvite<T extends RawCoValue>( async acceptInvite<T extends RawCoValue>(

View File

@@ -8,6 +8,7 @@ import { AgentID, isAgentID } from "../ids.js";
import { RawAccount, RawAccountID, ControlledAccountOrAgent } from "./account.js"; import { RawAccount, RawAccountID, ControlledAccountOrAgent } from "./account.js";
import { Role } from "../permissions.js"; import { Role } from "../permissions.js";
import { base58 } from "@scure/base"; import { base58 } from "@scure/base";
import { CoValueUniqueness } from "../coValueCore.js";
export const EVERYONE = "everyone" as const; export const EVERYONE = "everyone" as const;
export type Everyone = "everyone"; export type Everyone = "everyone";
@@ -255,6 +256,7 @@ export class RawGroup<
init?: M["_shape"], init?: M["_shape"],
meta?: M["headerMeta"], meta?: M["headerMeta"],
initPrivacy: "trusting" | "private" = "private", initPrivacy: "trusting" | "private" = "private",
uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique()
): M { ): M {
const map = this.core.node const map = this.core.node
.createCoValue({ .createCoValue({
@@ -264,7 +266,7 @@ export class RawGroup<
group: this.id, group: this.id,
}, },
meta: meta || null, meta: meta || null,
...this.core.crypto.createdNowUnique(), ...uniqueness
}) })
.getCurrentContent() as M; .getCurrentContent() as M;
@@ -287,6 +289,7 @@ export class RawGroup<
init?: L["_item"][], init?: L["_item"][],
meta?: L["headerMeta"], meta?: L["headerMeta"],
initPrivacy: "trusting" | "private" = "private", initPrivacy: "trusting" | "private" = "private",
uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique()
): L { ): L {
const list = this.core.node const list = this.core.node
.createCoValue({ .createCoValue({
@@ -296,7 +299,7 @@ export class RawGroup<
group: this.id, group: this.id,
}, },
meta: meta || null, meta: meta || null,
...this.core.crypto.createdNowUnique(), ...uniqueness
}) })
.getCurrentContent() as L; .getCurrentContent() as L;
@@ -310,7 +313,7 @@ export class RawGroup<
} }
/** @category 3. Value creation */ /** @category 3. Value creation */
createStream<C extends RawCoStream>(meta?: C["headerMeta"]): C { createStream<C extends RawCoStream>(meta?: C["headerMeta"], uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique()): C {
return this.core.node return this.core.node
.createCoValue({ .createCoValue({
type: "costream", type: "costream",
@@ -319,7 +322,7 @@ export class RawGroup<
group: this.id, group: this.id,
}, },
meta: meta || null, meta: meta || null,
...this.core.crypto.createdNowUnique(), ...uniqueness
}) })
.getCurrentContent() as C; .getCurrentContent() as C;
} }
@@ -327,6 +330,7 @@ export class RawGroup<
/** @category 3. Value creation */ /** @category 3. Value creation */
createBinaryStream<C extends RawBinaryCoStream>( createBinaryStream<C extends RawBinaryCoStream>(
meta: C["headerMeta"] = { type: "binary" }, meta: C["headerMeta"] = { type: "binary" },
uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique()
): C { ): C {
return this.core.node return this.core.node
.createCoValue({ .createCoValue({
@@ -336,7 +340,7 @@ export class RawGroup<
group: this.id, group: this.id,
}, },
meta: meta, meta: meta,
...this.core.crypto.createdNowUnique(), ...uniqueness
}) })
.getCurrentContent() as C; .getCurrentContent() as C;
} }

View File

@@ -1,5 +1,6 @@
import { import {
CoValueCore, CoValueCore,
type CoValueUniqueness,
newRandomSessionID, newRandomSessionID,
MAX_RECOMMENDED_TX_SIZE, MAX_RECOMMENDED_TX_SIZE,
idforHeader, idforHeader,
@@ -130,6 +131,7 @@ export type {
OutgoingSyncQueue, OutgoingSyncQueue,
DisconnectedError, DisconnectedError,
PingTimeoutError, PingTimeoutError,
CoValueUniqueness
}; };
// eslint-disable-next-line @typescript-eslint/no-namespace // eslint-disable-next-line @typescript-eslint/no-namespace

View File

@@ -2,6 +2,7 @@ import { AgentSecret, CryptoProvider } from "./crypto/crypto.js";
import { import {
CoValueCore, CoValueCore,
CoValueHeader, CoValueHeader,
CoValueUniqueness,
newRandomSessionID, newRandomSessionID,
} from "./coValueCore.js"; } from "./coValueCore.js";
import { import {
@@ -606,12 +607,12 @@ export class LocalNode {
/** /**
* @deprecated use Account.createGroup() instead * @deprecated use Account.createGroup() instead
*/ */
createGroup(): RawGroup { createGroup(uniqueness: CoValueUniqueness = this.crypto.createdNowUnique()): RawGroup {
const groupCoValue = this.createCoValue({ const groupCoValue = this.createCoValue({
type: "comap", type: "comap",
ruleset: { type: "group", initialAdmin: this.account.id }, ruleset: { type: "group", initialAdmin: this.account.id },
meta: null, meta: null,
...this.crypto.createdNowUnique(), ...uniqueness
}); });
const group = expectGroup(groupCoValue.getCurrentContent()); const group = expectGroup(groupCoValue.getCurrentContent());

View File

@@ -1,5 +1,14 @@
# jazz-browser-media-images # jazz-browser-media-images
## 0.7.33-unique.1
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- cojson@0.7.35-unique.2
- jazz-browser@0.7.35-unique.2
## 0.7.33-new-auth.0 ## 0.7.33-new-auth.0
### Patch Changes ### Patch Changes

View File

@@ -1,6 +1,6 @@
{ {
"name": "jazz-browser-auth-clerk", "name": "jazz-browser-auth-clerk",
"version": "0.7.33-new-auth.0", "version": "0.7.33-unique.1",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",
"types": "src/index.ts", "types": "src/index.ts",

View File

@@ -1,5 +1,13 @@
# jazz-browser-media-images # jazz-browser-media-images
## 0.7.35-unique.2
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- jazz-browser@0.7.35-unique.2
## 0.7.35-new-auth.0 ## 0.7.35-new-auth.0
### Patch Changes ### Patch Changes

View File

@@ -1,6 +1,6 @@
{ {
"name": "jazz-browser-media-images", "name": "jazz-browser-media-images",
"version": "0.7.35-new-auth.0", "version": "0.7.35-unique.2",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",
"types": "src/index.ts", "types": "src/index.ts",

View File

@@ -1,5 +1,15 @@
# jazz-browser # jazz-browser
## 0.7.35-unique.2
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- cojson@0.7.35-unique.2
- cojson-storage-indexeddb@0.7.35-unique.2
- cojson-transport-ws@0.7.35-unique.2
## 0.7.35-new-auth.0 ## 0.7.35-new-auth.0
### Patch Changes ### Patch Changes

View File

@@ -1,6 +1,6 @@
{ {
"name": "jazz-browser", "name": "jazz-browser",
"version": "0.7.35-new-auth.0", "version": "0.7.35-unique.2",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",
"types": "src/index.ts", "types": "src/index.ts",

View File

@@ -1,5 +1,14 @@
# jazz-autosub # jazz-autosub
## 0.7.35-unique.2
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- cojson@0.7.35-unique.2
- cojson-transport-ws@0.7.35-unique.2
## 0.7.35-new-auth.0 ## 0.7.35-new-auth.0
### Patch Changes ### Patch Changes

View File

@@ -5,7 +5,7 @@
"types": "src/index.ts", "types": "src/index.ts",
"type": "module", "type": "module",
"license": "MIT", "license": "MIT",
"version": "0.7.35-new-auth.0", "version": "0.7.35-unique.2",
"dependencies": { "dependencies": {
"cojson": "workspace:*", "cojson": "workspace:*",
"cojson-transport-ws": "workspace:*", "cojson-transport-ws": "workspace:*",

View File

@@ -1,5 +1,15 @@
# jazz-browser-media-images # jazz-browser-media-images
## 0.7.33-unique.2
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- cojson@0.7.35-unique.2
- jazz-browser-auth-clerk@0.7.33-unique.1
- jazz-react@0.7.35-unique.2
## 0.7.33-new-auth.1 ## 0.7.33-new-auth.1
### Patch Changes ### Patch Changes

View File

@@ -1,6 +1,6 @@
{ {
"name": "jazz-react-auth-clerk", "name": "jazz-react-auth-clerk",
"version": "0.7.33-new-auth.1", "version": "0.7.33-unique.2",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",
"types": "src/index.tsx", "types": "src/index.tsx",

View File

@@ -1,5 +1,14 @@
# jazz-react # jazz-react
## 0.7.35-unique.2
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- cojson@0.7.35-unique.2
- jazz-browser@0.7.35-unique.2
## 0.7.35-new-auth.1 ## 0.7.35-new-auth.1
### Patch Changes ### Patch Changes

View File

@@ -1,6 +1,6 @@
{ {
"name": "jazz-react", "name": "jazz-react",
"version": "0.7.35-new-auth.1", "version": "0.7.35-unique.2",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",
"types": "src/index.ts", "types": "src/index.ts",

View File

@@ -1,5 +1,14 @@
# jazz-autosub # jazz-autosub
## 0.7.35-unique.2
### Patch Changes
- Updated dependencies
- jazz-tools@0.7.35-unique.2
- cojson@0.7.35-unique.2
- cojson-transport-ws@0.7.35-unique.2
## 0.7.35-new-auth.0 ## 0.7.35-new-auth.0
### Patch Changes ### Patch Changes

View File

@@ -3,7 +3,7 @@
"bin": "./dist/index.js", "bin": "./dist/index.js",
"type": "module", "type": "module",
"license": "MIT", "license": "MIT",
"version": "0.7.35-new-auth.0", "version": "0.7.35-unique.2",
"scripts": { "scripts": {
"lint": "eslint . --ext ts,tsx", "lint": "eslint . --ext ts,tsx",
"format": "prettier --write './src/**/*.{ts,tsx}'", "format": "prettier --write './src/**/*.{ts,tsx}'",

View File

@@ -1,5 +1,13 @@
# jazz-autosub # jazz-autosub
## 0.7.35-unique.2
### Patch Changes
- First sketch of API for creating and finding unique CoValues
- Updated dependencies
- cojson@0.7.35-unique.2
## 0.7.35-new-auth.0 ## 0.7.35-new-auth.0
### Patch Changes ### Patch Changes

View File

@@ -5,7 +5,7 @@
"types": "./src/index.ts", "types": "./src/index.ts",
"type": "module", "type": "module",
"license": "MIT", "license": "MIT",
"version": "0.7.35-new-auth.0", "version": "0.7.35-unique.2",
"dependencies": { "dependencies": {
"cojson": "workspace:*", "cojson": "workspace:*",
"fast-check": "^3.17.2" "fast-check": "^3.17.2"

View File

@@ -1,4 +1,9 @@
import type { JsonValue, RawCoMap } from "cojson"; import {
cojsonInternals,
type CoValueUniqueness,
type JsonValue,
type RawCoMap,
} from "cojson";
import type { import type {
CoValue, CoValue,
Schema, Schema,
@@ -35,8 +40,10 @@ type CoMapEdit<V> = {
}; };
export type Simplify<A> = { export type Simplify<A> = {
[K in keyof A]: A[K] [K in keyof A]: A[K];
} extends infer B ? B : never } extends infer B
? B
: never;
/** /**
* CoMaps are collaborative versions of plain objects, mapping string-like keys to values. * CoMaps are collaborative versions of plain objects, mapping string-like keys to values.
@@ -237,10 +244,19 @@ export class CoMap extends CoValueBase implements CoValue {
static create<M extends CoMap>( static create<M extends CoMap>(
this: CoValueClass<M>, this: CoValueClass<M>,
init: Simplify<CoMapInit<M>>, init: Simplify<CoMapInit<M>>,
options: { owner: Account | Group }, options: {
owner: Account | Group;
unique?: CoValueUniqueness["uniqueness"];
},
) { ) {
const instance = new this(); const instance = new this();
const raw = instance.rawFromInit(init, options.owner); const raw = instance.rawFromInit(
init,
options.owner,
options.unique === undefined
? undefined
: { uniqueness: options.unique },
);
Object.defineProperties(instance, { Object.defineProperties(instance, {
id: { id: {
value: raw.id, value: raw.id,
@@ -293,6 +309,7 @@ export class CoMap extends CoValueBase implements CoValue {
rawFromInit<Fields extends object = Record<string, any>>( rawFromInit<Fields extends object = Record<string, any>>(
init: Simplify<CoMapInit<Fields>> | undefined, init: Simplify<CoMapInit<Fields>> | undefined,
owner: Account | Group, owner: Account | Group,
uniqueness?: CoValueUniqueness,
) { ) {
const rawOwner = owner._raw; const rawOwner = owner._raw;
@@ -326,7 +343,7 @@ export class CoMap extends CoValueBase implements CoValue {
} }
} }
return rawOwner.createMap(rawInit); return rawOwner.createMap(rawInit, null, "private", uniqueness);
} }
/** /**
@@ -426,6 +443,27 @@ export class CoMap extends CoValueBase implements CoValue {
return subscribeToCoValue<M, Depth>(this, id, as, depth, listener); return subscribeToCoValue<M, Depth>(this, id, as, depth, listener);
} }
static findUnique<M extends CoMap>(
this: CoValueClass<M>,
unique: CoValueUniqueness['uniqueness'],
ownerID: ID<Account> | ID<Group>,
as: Account | Group,
) {
const header = {
type: "comap" as const,
ruleset: {
type: "ownedByGroup" as const,
group: ownerID,
},
meta: null,
uniqueness: unique,
};
return cojsonInternals.idforHeader(
header,
as._raw.core.crypto,
) as ID<M>;
}
/** /**
* Given an already loaded `CoMap`, ensure that the specified fields are loaded to the specified depth. * Given an already loaded `CoMap`, ensure that the specified fields are loaded to the specified depth.
* *

View File

@@ -11,6 +11,7 @@ export type {
AgentID, AgentID,
SyncMessage, SyncMessage,
CryptoProvider, CryptoProvider,
CoValueUniqueness,
} from "cojson"; } from "cojson";
export type { ID, CoValue } from "./internal.js"; export type { ID, CoValue } from "./internal.js";

View File

@@ -11,32 +11,32 @@ import {
createJazzContext, createJazzContext,
fixedCredentialsAuth, fixedCredentialsAuth,
} from "../index.js"; } from "../index.js";
import { randomSessionProvider } from "../internal.js"; import { Group, randomSessionProvider } from "../internal.js";
const Crypto = await WasmCrypto.create(); const Crypto = await WasmCrypto.create();
class TestMap extends CoMap {
color = co.string;
_height = co.number;
birthday = co.encoded(Encoders.Date);
name? = co.string;
nullable = co.optional.encoded<string | undefined>({
encode: (value: string | undefined) => value || null,
decode: (value: unknown) => (value as string) || undefined,
});
optionalDate = co.optional.encoded(Encoders.Date);
get roughColor() {
return this.color + "ish";
}
}
describe("Simple CoMap operations", async () => { describe("Simple CoMap operations", async () => {
const me = await Account.create({ const me = await Account.create({
creationProps: { name: "Hermes Puggington" }, creationProps: { name: "Hermes Puggington" },
crypto: Crypto, crypto: Crypto,
}); });
class TestMap extends CoMap {
color = co.string;
_height = co.number;
birthday = co.encoded(Encoders.Date);
name? = co.string;
nullable = co.optional.encoded<string | undefined>({
encode: (value: string | undefined) => value || null,
decode: (value: unknown) => (value as string) || undefined,
});
optionalDate = co.optional.encoded(Encoders.Date);
get roughColor() {
return this.color + "ish";
}
}
console.log("TestMap schema", TestMap.prototype._schema); console.log("TestMap schema", TestMap.prototype._schema);
const birthday = new Date(); const birthday = new Date();
@@ -738,3 +738,28 @@ describe("CoMap applyDiff", async () => {
expect((map as any).invalidField).toBeUndefined(); expect((map as any).invalidField).toBeUndefined();
}); });
}); });
describe("Creating and finding unique CoMaps", async () => {
test("Creating and finding unique CoMaps", async () => {
const me = await Account.create({
creationProps: { name: "Tester McTesterson" },
crypto: Crypto,
});
const group = await Group.create({
owner: me,
});
const alice = TestMap.create({
name: "Alice",
_height: 100,
birthday: new Date("1990-01-01"),
color: "red",
}, { owner: group, unique: { name: "Alice" } });
const foundAlice = TestMap.findUnique({ name: "Alice" }, group.id, me);
expect(foundAlice).toEqual(alice.id);
});
});