@fluree/fluree-client
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -6,2 +6,3 @@ import { IFlureeConfig } from '../interfaces/IFlureeConfig'; | ||
import { ContextStatement } from '../types/ContextTypes'; | ||
import { UpsertStatement } from '../types/TransactionTypes'; | ||
import { HistoryQueryInstance } from './HistoryQueryInstance'; | ||
@@ -131,2 +132,34 @@ import { QueryInstance } from './QueryInstance'; | ||
/** | ||
* Creates a new TransactionInstance for upserting with the Fluree database. The TransactionInstance can be used & re-used to build, sign, and send upsert transactions to the Fluree instance. | ||
* | ||
* Upsert is not an API endpoint in Fluree. This method helps to transform an upsert transaction into an insert/where/delete transaction. | ||
* | ||
* Upsert assumes that the facts provided in the transaction should be treated as the true & accurate state of the data after the transaction is processed. | ||
* | ||
* This means that facts in your transaction should be inserted (if new) and should replace existing facts (if they exist on those subjects & properties). | ||
* @param transaction {UpsertStatement} - The upsert transaction to send to the Fluree instance | ||
* @returns TransactionInstance | ||
* @example | ||
* // Existing data: | ||
* // [ | ||
* // { "@id": "freddy", "name": "Freddy" }, | ||
* // { "@id": "alice", "name": "Alice" } | ||
* // ] | ||
* | ||
* await client.upsert([ | ||
* { "@id": "freddy", "name": "Freddy the Yeti" }, | ||
* { "@id": "alice", "age": 25} | ||
* ]).send(); | ||
* | ||
* // New data state after txn: | ||
* // [ | ||
* // { "@id": "freddy", "name": "Freddy the Yeti" }, | ||
* // { "@id": "alice", "name": "Alice", "age": 25 } | ||
* // ] | ||
* | ||
* // Note that if this had been an "insert" freddy would now have two names. | ||
* // Note also that if this had been handled by deleting/insert, alice might have lost her name. | ||
*/ | ||
upsert(transaction: UpsertStatement): TransactionInstance; | ||
/** | ||
* Creates a new HistoryQueryInstance for querying the history of the Fluree database. The HistoryQueryInstance can be used & re-used to build, sign, and send history queries to the Fluree instance. | ||
@@ -133,0 +166,0 @@ * @param query {IFlureeHistoryQuery} - The history query to send to the Fluree instance |
@@ -20,2 +20,3 @@ "use strict"; | ||
const contextHandler_1 = require("../utils/contextHandler"); | ||
const transactionUtils_1 = require("../utils/transactionUtils"); | ||
const FlureeError_1 = require("./FlureeError"); | ||
@@ -201,2 +202,43 @@ const HistoryQueryInstance_1 = require("./HistoryQueryInstance"); | ||
/** | ||
* Creates a new TransactionInstance for upserting with the Fluree database. The TransactionInstance can be used & re-used to build, sign, and send upsert transactions to the Fluree instance. | ||
* | ||
* Upsert is not an API endpoint in Fluree. This method helps to transform an upsert transaction into an insert/where/delete transaction. | ||
* | ||
* Upsert assumes that the facts provided in the transaction should be treated as the true & accurate state of the data after the transaction is processed. | ||
* | ||
* This means that facts in your transaction should be inserted (if new) and should replace existing facts (if they exist on those subjects & properties). | ||
* @param transaction {UpsertStatement} - The upsert transaction to send to the Fluree instance | ||
* @returns TransactionInstance | ||
* @example | ||
* // Existing data: | ||
* // [ | ||
* // { "@id": "freddy", "name": "Freddy" }, | ||
* // { "@id": "alice", "name": "Alice" } | ||
* // ] | ||
* | ||
* await client.upsert([ | ||
* { "@id": "freddy", "name": "Freddy the Yeti" }, | ||
* { "@id": "alice", "age": 25} | ||
* ]).send(); | ||
* | ||
* // New data state after txn: | ||
* // [ | ||
* // { "@id": "freddy", "name": "Freddy the Yeti" }, | ||
* // { "@id": "alice", "name": "Alice", "age": 25 } | ||
* // ] | ||
* | ||
* // Note that if this had been an "insert" freddy would now have two names. | ||
* // Note also that if this had been handled by deleting/insert, alice might have lost her name. | ||
*/ | ||
upsert(transaction) { | ||
if (!this.connected) { | ||
throw new FlureeError_1.FlureeError('You must connect before transacting. Try using .connect().transact() instead'); | ||
} | ||
const transactionContext = transaction['@context']; | ||
const idAlias = (0, contextHandler_1.findIdAlias)((0, contextHandler_1.mergeContexts)(this.config.defaultContext || {}, transactionContext || {})); | ||
const resultTransaction = (0, transactionUtils_1.handleUpsert)(transaction, idAlias); | ||
resultTransaction.ledger = transaction.ledger || this.config.ledger; | ||
return new TransactionInstance_1.TransactionInstance(resultTransaction, this.config); | ||
} | ||
/** | ||
* Creates a new HistoryQueryInstance for querying the history of the Fluree database. The HistoryQueryInstance can be used & re-used to build, sign, and send history queries to the Fluree instance. | ||
@@ -203,0 +245,0 @@ * @param query {IFlureeHistoryQuery} - The history query to send to the Fluree instance |
@@ -0,5 +1,6 @@ | ||
import { ContextStatement } from './ContextTypes'; | ||
type InsertObject = { | ||
[key: string]: string | string[] | InsertStatement; | ||
[key: string]: string | string[] | number | number[] | boolean | boolean[] | InsertStatement; | ||
}; | ||
type InsertArray = Array<InsertObject>; | ||
export type InsertArray = Array<InsertObject>; | ||
type DeleteObject = { | ||
@@ -11,2 +12,6 @@ [key: string]: string | DeleteStatement; | ||
export type DeleteStatement = DeleteObject | DeleteArray; | ||
export type UpsertStatement = { | ||
'@context'?: ContextStatement; | ||
ledger?: string; | ||
} & InsertStatement; | ||
export {}; |
@@ -9,1 +9,9 @@ import { ContextStatement } from '../types/ContextTypes'; | ||
export declare function mergeContexts(context1: ContextStatement, context2: ContextStatement): ContextStatement; | ||
/** | ||
* Find the alias for a given context. This includes searching through context arrays or nested context for any value of "@id" | ||
* For example, if the context is ["https://ns.flur.ee/", { "ex": "https://example.com/", "id": "@id" }] | ||
* Then "id" is serving as an alias for "@id" | ||
* @param context the context to search for an alias | ||
* @returns the alias for the context | ||
*/ | ||
export declare function findIdAlias(context: ContextStatement): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mergeContexts = void 0; | ||
exports.findIdAlias = exports.mergeContexts = void 0; | ||
/** | ||
@@ -32,2 +32,4 @@ * | ||
else { | ||
if (Object.entries(context2).length === 0) | ||
return context1; | ||
return context1.concat([context2]); | ||
@@ -51,2 +53,31 @@ } | ||
exports.mergeContexts = mergeContexts; | ||
/** | ||
* Find the alias for a given context. This includes searching through context arrays or nested context for any value of "@id" | ||
* For example, if the context is ["https://ns.flur.ee/", { "ex": "https://example.com/", "id": "@id" }] | ||
* Then "id" is serving as an alias for "@id" | ||
* @param context the context to search for an alias | ||
* @returns the alias for the context | ||
*/ | ||
function findIdAlias(context) { | ||
if (typeof context === 'string') { | ||
return '@id'; | ||
} | ||
else if (Array.isArray(context)) { | ||
let result = '@id'; | ||
for (const item of context) { | ||
result = findIdAlias(item); | ||
} | ||
return result; | ||
} | ||
else { | ||
let result = '@id'; | ||
for (const key in context) { | ||
if (context[key] === '@id') { | ||
result = key; | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
exports.findIdAlias = findIdAlias; | ||
// /** | ||
@@ -53,0 +84,0 @@ // * Find the alias for a given context. This includes searching through context arrays or nested context for any value of "@context" |
{ | ||
"name": "@fluree/fluree-client", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -670,5 +670,1 @@ # Fluree Client SDK for TypeScript/JavaScript | ||
``` | ||
``` | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86913
38
1795
670