Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a BinaryModel for big json
- Loading branch information
Showing
6 changed files
with
189 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./models/binarymodel"; | ||
export * from "./stores/migration"; |
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,45 @@ | ||
import { suite, test } from "@testdeck/mocha"; | ||
import { FileBinary } from "@webda/core"; | ||
import { WebdaSimpleTest } from "@webda/core/lib/test"; | ||
import * as assert from "assert"; | ||
import { BinaryModel } from "./binarymodel"; | ||
|
||
interface TestData { | ||
test: string; | ||
array: number[]; | ||
} | ||
@suite | ||
class BinaryModelTest extends WebdaSimpleTest { | ||
@test | ||
async binary() { | ||
await this.registerService(new FileBinary(this.webda, "file", { folder: "/tmp", models: { "*": ["*"] } })) | ||
.resolve() | ||
.init(); | ||
let model = new BinaryModel().load({}, true); | ||
// @ts-ignore | ||
model.uuid = "test"; | ||
assert.strictEqual(model.needsUpload(), false); | ||
model.data = { | ||
test: "OK" | ||
}; | ||
assert.strictEqual(model.needsUpload(), true); | ||
await model.save(); | ||
assert.strictEqual(model.needsUpload(), false); | ||
await model.save(); | ||
model.data.test = "OK2"; | ||
assert.strictEqual(model.needsUpload(), true); | ||
await model.save(); | ||
let stored = <BinaryModel<TestData>>await BinaryModel.ref("test").get(); | ||
await stored.loadData(); | ||
// Call it twice to check that it is not downloaded twice | ||
await stored.loadData(); | ||
await stored.loadData(true); | ||
// @ts-ignore | ||
assert.strictEqual(stored.data.test, "OK2"); | ||
|
||
stored.data.array = [1, 2, 3]; | ||
stored.data.array.push(4); | ||
delete stored.data.array[1]; | ||
delete stored.data.test; | ||
} | ||
} |
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,83 @@ | ||
import { Binary, Core, CoreModel, MemoryBinaryFile, NotEnumerable } from "@webda/core"; | ||
|
||
export class BinaryModel<T = any> extends CoreModel { | ||
@NotEnumerable | ||
__data: T; | ||
|
||
__binary: Binary; | ||
|
||
@NotEnumerable | ||
__dataUpdated: boolean = false; | ||
|
||
public get data(): T { | ||
const subProxier = prop => { | ||
return { | ||
set: (target: this, p: string | symbol, value) => { | ||
this.__dataUpdated = true; | ||
target[p] = value; | ||
return true; | ||
}, | ||
get: (target: this, p: string | symbol) => { | ||
if (Array.isArray(target[p]) || target[p] instanceof Object) { | ||
return new Proxy(target[p], subProxier(prop)); | ||
} | ||
return target[p]; | ||
}, | ||
deleteProperty: (t, property) => { | ||
delete t[property]; | ||
this.__dataUpdated = true; | ||
return true; | ||
} | ||
}; | ||
}; | ||
const proxier = { | ||
deleteProperty: (t, property) => { | ||
delete t[property]; | ||
this.__dataUpdated = true; | ||
return true; | ||
}, | ||
set: (target: this, p: string | symbol, value) => { | ||
this.__dataUpdated = true; | ||
target[p] = value; | ||
return true; | ||
}, | ||
get: (target: this, p: string | symbol) => { | ||
if (Array.isArray(target[p]) || target[p] instanceof Object) { | ||
return new Proxy(target[p], subProxier(p)); | ||
} | ||
return target[p]; | ||
} | ||
}; | ||
return new Proxy(this.__data, proxier); | ||
} | ||
|
||
public set data(data: T) { | ||
this.__data = data; | ||
this.__dataUpdated = true; | ||
} | ||
|
||
needsUpload(): boolean { | ||
return this.__dataUpdated; | ||
} | ||
|
||
async loadData(force: boolean = false) { | ||
if (this.__data && !force) { | ||
return; | ||
} | ||
this.__data = JSON.parse((await this.__binary.getAsBuffer()).toString()); | ||
} | ||
|
||
async save() { | ||
await super.save(); | ||
await this.updateBinary(); | ||
return this; | ||
} | ||
|
||
async updateBinary() { | ||
if (!this.__dataUpdated) { | ||
return; | ||
} | ||
await this.__binary.upload(new MemoryBinaryFile(JSON.stringify(this.__data))); | ||
this.__dataUpdated = false; | ||
} | ||
} |
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