Implementing NoSql with Anson

This is a way of persisting unstructured data with a relational database. For general idea of NoSql & pros and cons, see [1].

  • Save

To persist a structured object in to database, e.g. to insert / update a db field, text type for sqlite, simply set an instance of sub-class of AnDbField with nv().

T_PhotoCSS anson = new T_PhotoCSS(w, h);

st.insert("a_funcs")
    .nv("funcId", "a01")
    .nv("funcName", anson)
    .nv("uri", ExprPart.constStr(null))
    .commit(sqls);

// The object anson is serialized to a string value.
assertEquals(
"insert into a_funcs  (funcId, funcName, uri) values ('a01', '{\"type\": \"io.odysz.transact.sql.parts.T_PhotoCSS\", \"size\": [4, 3]}\n', null)",
sqls.get(0));

See semantic-transact test case, AnsonFieldTest.

  • Load

To load an instance of Anson from db text at server side (java), use AnResultSet#<T>getAnson(). This function will deserialize the instance.

AnResultset rs = ((AnResultset) st.select("b_alarms")
        .col("remarks")
        .whereEq("typeId", "02-photo")
        .rs(s0)          // commit query
        .rs(0))          // results[0]
        .nxt();          // row 1

T_PhotoCSS anson = rs.<T_PhotoCSS>getAnson("remarks");
assertEquals(16, anson.w());
assertEquals( 9, anson.h());

See Semantic-DA test case, method ‎DASemantextTest::testAnsonField.

To convert a JSON string to object at client in typescript with tslint support, use Protocol.registerFactory().

class Profiles extends AnsonBody {
    home: string;
    maxUsers: number;
    servtype: number;

    constructor (obj: { servtype: number; maxUsers: number; home: string }) {
        super( { type: 'io.oz.album.tier.Profiles' } );
        this.home = obj.home;
        this.maxUsers = obj.maxUsers;
        this.servtype = obj.servtype;
    }
}

Protocol.registerBody('io.oz.album.tier.Profiles', (jsonBd) => { return new Profiles(jsonBd); });

See Anclient test case, admin-tier.ts class Profiles.

Reference

[1] IBM, What is a NoSQL database?, Retrieved on Aug 2, 2023.