Skip to content

Commit

Permalink
initial addition of createdBy, update @mapeo/schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomás Ciccola committed Sep 19, 2023
1 parent ca3b6c7 commit acaa223
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
8 changes: 5 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"@fastify/type-provider-typebox": "^3.3.0",
"@hyperswarm/secret-stream": "^6.1.2",
"@mapeo/crypto": "^1.0.0-alpha.8",
"@mapeo/schema": "^3.0.0-next.8",
"@mapeo/schema": "^3.0.0-next.9",
"@mapeo/sqlite-indexer": "^1.0.0-alpha.6",
"@sinclair/typebox": "^0.29.6",
"b4a": "^1.6.3",
Expand Down
18 changes: 12 additions & 6 deletions src/datatype/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class DataType {
docId,
createdAt: nowDateString,
updatedAt: nowDateString,
// createdBy,
links: [],
}

Expand Down Expand Up @@ -150,13 +151,14 @@ export class DataType {
*/
async update(versionId, value) {
const links = Array.isArray(versionId) ? versionId : [versionId]
const { docId, createdAt } = await this.#validateLinks(links)
const { docId, createdAt, createdBy } = await this.#validateLinks(links)
/** @type {any} */
const doc = {
...value,
docId,
createdAt,
updatedAt: new Date().toISOString(),
createdBy,
links,
}
await this.#dataStore.write(doc)
Expand All @@ -169,12 +171,13 @@ export class DataType {
*/
async delete(versionId) {
const links = Array.isArray(versionId) ? versionId : [versionId]
const { docId, createdAt } = await this.#validateLinks(links)
const { docId, createdAt, createdBy } = await this.#validateLinks(links)
/** @type {any} */
const doc = {
docId,
createdAt,
updatedAt: new Date().toISOString(),
createdBy,
links,
schemaName: this.#schemaName,
deleted: true,
Expand All @@ -199,19 +202,22 @@ export class DataType {
* Throws if any of these conditions fail, otherwise returns the validated
* docId and createAt datetime
* @param {string[]} links
* @returns {Promise<{ docId: MapeoDoc['docId'], createdAt: MapeoDoc['createdAt'] }>}
* @returns {Promise<{ docId: MapeoDoc['docId'], createdAt: MapeoDoc['createdAt'], createdBy: MapeoDoc['createdBy'] }>}
*/
async #validateLinks(links) {
const prevDocs = await Promise.all(
links.map((versionId) => this.getByVersionId(versionId))
)
const { docId, createdAt } = prevDocs[0]
const { docId, createdAt, createdBy } = prevDocs[0]
const areLinksValid = prevDocs.every(
(doc) => doc.docId === docId && doc.schemaName === this.#schemaName
(doc) =>
doc.docId === docId &&
doc.schemaName === this.#schemaName &&
doc.createdBy === createdBy
)
if (!areLinksValid) {
throw new Error('Updated docs must have the same docId and schemaName')
}
return { docId, createdAt }
return { docId, createdAt, createdBy }
}
}

0 comments on commit acaa223

Please sign in to comment.