@peerbit/document
Advanced tools
Comparing version
@@ -38,2 +38,3 @@ import { type AbstractType } from "@dao-xyz/borsh"; | ||
canPerform?: CanPerform<T>; | ||
strictHistory?: boolean; | ||
id?: (obj: any) => indexerTypes.IdPrimitive; | ||
@@ -61,2 +62,3 @@ index?: { | ||
private domain?; | ||
private strictHistory; | ||
canOpen?: (program: T) => Promise<boolean> | boolean; | ||
@@ -97,2 +99,3 @@ compatibility: 6 | 7 | undefined; | ||
operation: PutOperation; | ||
unique?: boolean; | ||
}): Promise<void>; | ||
@@ -99,0 +102,0 @@ count(properties: { |
@@ -35,2 +35,3 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
domain; | ||
strictHistory; | ||
canOpen; | ||
@@ -73,2 +74,3 @@ compatibility; | ||
this.compatibility = options.compatibility; | ||
this.strictHistory = options.strictHistory ?? false; | ||
await this._index.open({ | ||
@@ -235,13 +237,19 @@ log: this.log, | ||
else { | ||
if (entry.meta.next.length !== 1) { | ||
return false; | ||
if (this.strictHistory) { | ||
// make sure that the next pointer exist and points to the existing documents | ||
if (entry.meta.next.length !== 1) { | ||
return false; | ||
} | ||
const prevEntry = await this.log.log.entryIndex.get(existingDocument.context.head); | ||
if (!prevEntry) { | ||
logger.error("Failed to find previous entry for document edit: " + | ||
entry.hash); | ||
return false; | ||
} | ||
const referenceHistoryCorrectly = await pointsToHistory(prevEntry); | ||
return referenceHistoryCorrectly ? putOperation : false; | ||
} | ||
const prevEntry = await this.log.log.entryIndex.get(existingDocument.context.head); | ||
if (!prevEntry) { | ||
logger.error("Failed to find previous entry for document edit: " + | ||
entry.hash); | ||
return false; | ||
else { | ||
return putOperation; | ||
} | ||
const referenceHistoryCorrectly = await pointsToHistory(prevEntry); | ||
return referenceHistoryCorrectly ? putOperation : false; | ||
} | ||
@@ -410,2 +418,17 @@ } | ||
} | ||
// if no casual ordering is used, use timestamps to order docs | ||
let existing = reference?.unique | ||
? null | ||
: (await this._index.index.get(key)) || null; | ||
if (!this.strictHistory && existing) { | ||
// if immutable use oldest, else use newest | ||
let shouldIgnoreChange = this.immutable | ||
? existing.value.__context.modified < | ||
item.meta.clock.timestamp.wallTime | ||
: existing.value.__context.modified > | ||
item.meta.clock.timestamp.wallTime; | ||
if (shouldIgnoreChange) { | ||
continue; | ||
} | ||
} | ||
// Program specific | ||
@@ -416,3 +439,3 @@ if (value instanceof Program) { | ||
} | ||
const context = await this._index.put(value, key, item); | ||
const context = await this._index.put(value, key, item, existing); | ||
documentsChanged.added.push(coerceWithContext(value, context)); | ||
@@ -419,0 +442,0 @@ modified.add(key.primitive); |
@@ -105,3 +105,3 @@ import { type AbstractType } from "@dao-xyz/borsh"; | ||
getFromGid(gid: string): Promise<indexerTypes.IndexedResult<WithContext<I>>>; | ||
put(value: T, id: indexerTypes.IdKey, entry: Entry<Operation>): Promise<types.Context>; | ||
put(value: T, id: indexerTypes.IdKey, entry: Entry<Operation>, existing: indexerTypes.IndexedResult<WithContext<I>> | null | undefined): Promise<types.Context>; | ||
putWithContext(value: T, id: indexerTypes.IdKey, context: types.Context): Promise<types.Context>; | ||
@@ -108,0 +108,0 @@ del(key: indexerTypes.IdKey): indexerTypes.IdKey[] | Promise<indexerTypes.IdKey[]>; |
@@ -296,6 +296,6 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
} | ||
async put(value, id, entry) { | ||
const existing = await this.index.get(id); | ||
async put(value, id, entry, existing) { | ||
const existingDefined = existing === undefined ? await this.index.get(id) : existing; | ||
const context = new types.Context({ | ||
created: existing?.value.__context.created || | ||
created: existingDefined?.value.__context.created || | ||
entry.meta.clock.timestamp.wallTime, | ||
@@ -302,0 +302,0 @@ modified: entry.meta.clock.timestamp.wallTime, |
148
package.json
{ | ||
"name": "@peerbit/document", | ||
"version": "9.2.2", | ||
"description": "Document store implementation", | ||
"type": "module", | ||
"sideEffects": false, | ||
"types": "./dist/src/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
], | ||
"src/*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"src", | ||
"dist", | ||
"!dist/e2e", | ||
"!dist/test", | ||
"!**/*.tsbuildinfo" | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./dist/src/index.d.ts", | ||
"import": "./dist/src/index.js" | ||
} | ||
}, | ||
"eslintConfig": { | ||
"extends": "peerbit", | ||
"parserOptions": { | ||
"project": true, | ||
"sourceType": "module" | ||
}, | ||
"ignorePatterns": [ | ||
"!.aegir.js", | ||
"test/ts-use", | ||
"*.d.ts" | ||
] | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"clean": "aegir clean", | ||
"build": "aegir build --no-bundle", | ||
"test": "aegir test --target node", | ||
"lint": "aegir lint" | ||
}, | ||
"author": "dao.xyz", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@dao-xyz/borsh": "^5.2.3", | ||
"@peerbit/program": "5.1.6", | ||
"@peerbit/rpc": "5.0.39", | ||
"@peerbit/shared-log": "10.4.2", | ||
"@peerbit/indexer-interface": "^2.0.6", | ||
"@peerbit/indexer-simple": "^1.1.10", | ||
"@peerbit/indexer-sqlite3": "^1.2.13", | ||
"@peerbit/document-interface": "^2.0.15" | ||
}, | ||
"devDependencies": { | ||
"@peerbit/test-utils": "2.1.29", | ||
"@peerbit/time": "2.0.8", | ||
"@types/pidusage": "^2.0.5", | ||
"pidusage": "^3.0.2" | ||
} | ||
"name": "@peerbit/document", | ||
"version": "9.3.0-821030a", | ||
"description": "Document store implementation", | ||
"type": "module", | ||
"sideEffects": false, | ||
"types": "./dist/src/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
], | ||
"src/*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"src", | ||
"dist", | ||
"!dist/e2e", | ||
"!dist/test", | ||
"!**/*.tsbuildinfo" | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./dist/src/index.d.ts", | ||
"import": "./dist/src/index.js" | ||
} | ||
}, | ||
"eslintConfig": { | ||
"extends": "peerbit", | ||
"parserOptions": { | ||
"project": true, | ||
"sourceType": "module" | ||
}, | ||
"ignorePatterns": [ | ||
"!.aegir.js", | ||
"test/ts-use", | ||
"*.d.ts" | ||
] | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"clean": "aegir clean", | ||
"build": "aegir build --no-bundle", | ||
"test": "aegir test --target node", | ||
"lint": "aegir lint" | ||
}, | ||
"author": "dao.xyz", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@dao-xyz/borsh": "^5.2.3", | ||
"@peerbit/program": "5.1.7-821030a", | ||
"@peerbit/rpc": "5.0.40-821030a", | ||
"@peerbit/shared-log": "10.4.3-821030a", | ||
"@peerbit/indexer-interface": "2.0.7-821030a", | ||
"@peerbit/indexer-simple": "1.1.11-821030a", | ||
"@peerbit/indexer-sqlite3": "1.2.14-821030a", | ||
"@peerbit/document-interface": "2.0.16-821030a" | ||
}, | ||
"devDependencies": { | ||
"@peerbit/test-utils": "2.1.30-821030a", | ||
"@peerbit/time": "2.0.8-821030a", | ||
"@types/pidusage": "^2.0.5", | ||
"pidusage": "^3.0.2" | ||
} | ||
} |
@@ -92,2 +92,3 @@ import { | ||
canPerform?: CanPerform<T>; | ||
strictHistory?: boolean; | ||
id?: (obj: any) => indexerTypes.IdPrimitive; | ||
@@ -130,3 +131,3 @@ index?: { | ||
private domain?: CustomDocumentDomain<InferR<D>>; | ||
private strictHistory: boolean; | ||
canOpen?: (program: T) => Promise<boolean> | boolean; | ||
@@ -186,2 +187,3 @@ | ||
this.compatibility = options.compatibility; | ||
this.strictHistory = options.strictHistory ?? false; | ||
@@ -383,18 +385,24 @@ await this._index.open({ | ||
} else { | ||
if (entry.meta.next.length !== 1) { | ||
return false; | ||
} | ||
if (this.strictHistory) { | ||
// make sure that the next pointer exist and points to the existing documents | ||
if (entry.meta.next.length !== 1) { | ||
return false; | ||
} | ||
const prevEntry = await this.log.log.entryIndex.get( | ||
existingDocument.context.head, | ||
); | ||
if (!prevEntry) { | ||
logger.error( | ||
"Failed to find previous entry for document edit: " + | ||
entry.hash, | ||
const prevEntry = await this.log.log.entryIndex.get( | ||
existingDocument.context.head, | ||
); | ||
return false; | ||
if (!prevEntry) { | ||
logger.error( | ||
"Failed to find previous entry for document edit: " + | ||
entry.hash, | ||
); | ||
return false; | ||
} | ||
const referenceHistoryCorrectly = | ||
await pointsToHistory(prevEntry); | ||
return referenceHistoryCorrectly ? putOperation : false; | ||
} else { | ||
return putOperation; | ||
} | ||
const referenceHistoryCorrectly = await pointsToHistory(prevEntry); | ||
return referenceHistoryCorrectly ? putOperation : false; | ||
} | ||
@@ -514,3 +522,2 @@ } else { | ||
}); | ||
return appended; | ||
@@ -554,3 +561,3 @@ } | ||
change: Change<Operation>, | ||
reference?: { document: T; operation: PutOperation }, | ||
reference?: { document: T; operation: PutOperation; unique?: boolean }, | ||
): Promise<void> { | ||
@@ -613,2 +620,18 @@ const isAppendOperation = | ||
// if no casual ordering is used, use timestamps to order docs | ||
let existing = reference?.unique | ||
? null | ||
: (await this._index.index.get(key)) || null; | ||
if (!this.strictHistory && existing) { | ||
// if immutable use oldest, else use newest | ||
let shouldIgnoreChange = this.immutable | ||
? existing.value.__context.modified < | ||
item.meta.clock.timestamp.wallTime | ||
: existing.value.__context.modified > | ||
item.meta.clock.timestamp.wallTime; | ||
if (shouldIgnoreChange) { | ||
continue; | ||
} | ||
} | ||
// Program specific | ||
@@ -619,3 +642,3 @@ if (value instanceof Program) { | ||
} | ||
const context = await this._index.put(value, key, item); | ||
const context = await this._index.put(value, key, item, existing); | ||
documentsChanged.added.push(coerceWithContext(value, context)); | ||
@@ -622,0 +645,0 @@ |
@@ -218,7 +218,2 @@ import { type AbstractType, field, serialize, variant } from "@dao-xyz/borsh"; | ||
/* | ||
if (!(await this.canRead(message.sender))) { | ||
throw new AccessError(); | ||
} */ | ||
export type CanSearch = ( | ||
@@ -593,7 +588,13 @@ request: types.SearchRequest | types.CollectNextRequest, | ||
} | ||
public async put(value: T, id: indexerTypes.IdKey, entry: Entry<Operation>) { | ||
const existing = await this.index.get(id); | ||
public async put( | ||
value: T, | ||
id: indexerTypes.IdKey, | ||
entry: Entry<Operation>, | ||
existing: indexerTypes.IndexedResult<WithContext<I>> | null | undefined, | ||
) { | ||
const existingDefined = | ||
existing === undefined ? await this.index.get(id) : existing; | ||
const context = new types.Context({ | ||
created: | ||
existing?.value.__context.created || | ||
existingDefined?.value.__context.created || | ||
entry.meta.clock.timestamp.wallTime, | ||
@@ -600,0 +601,0 @@ modified: entry.meta.clock.timestamp.wallTime, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
309349
1.19%5578
0.94%2
100%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated