androidjs-file-repovych
v1.1.0
Published
File repo implements repovych interface for androidjs frontend
Downloads
237
Readme
Androidjs-file-repovych
Realization of repovych repository interface for androidjs file storage on frontend.
Usage
- Copy file
./backend/main.jsto your androidjs backend script - on frontend make repo and use it by instruction
Example of usage
- Copy file
./backend/main.jsto your androidjs backend script - On frontend:
import { make, property, valid } from "class-model";
import { AndroidjsFileRepovych, backendLogOutput, makeFilePath } from ".";
import { IRepovich } from "repovych";
import { ConsoleLoggych, HtmlElemLoggych } from "loggych";
//class of data to store with class-model validation
class MyClass
{
@property(null,
valid.number(),
make.int()
)
id: number;
@property(null,
valid.string(),
make.string()
)
val: string;
}
//description of query
interface MyQuery {
ids: number[];
}
async function run() {
try {
//see html at ./test-repo-app/views/index.html
const input = document.getElementById("my-input") as HTMLInputElement;
const submitBtn = document.getElementById("my-btn") as HTMLButtonElement;
const outConsole = document.getElementById("my-console");
const errConsole = document.getElementById("my-err");
// making logger
const log = HtmlElemLoggych.createDefault(errConsole);
backendLogOutput(eval("front"), log.addContext("backend"));
//making repo
const repo: IRepovich<MyClass, Partial<MyQuery>, {id: number}> = new AndroidjsFileRepovych(
makeFilePath("my-file.json"), //always use makeFilePath function
eval("front"),
() => new MyClass(),
(data, q: MyQuery) => {
//data filtering
if(q.ids) {
data = data.filter(v => q.ids.includes(v.id));
}
return data;
},
["id"], //id fields
log.addContext("frontend") //logger
);
submitBtn.onclick = () => {
const val = input.value;
repo.save([{id: Math.ceil(Math.random() * 999999), val: val}])
.catch(e => {log.log({err: e});})
.then(() => repo.query({}))
.catch(e => {
log.log({err: e});
return e;
})
.then(data => {
outConsole.innerHTML = JSON.stringify(data);
})
.catch(e => {
log.log({err: e});
});
}
return Promise.resolve();
} catch(e) {
return Promise.reject(e);
}
}
run();
//build this file with browserify and connect to androidjs project
//see result mobile app on ./test-repo-app/dist/test-repo-app.apkApi
//after requiring "androidjs" to your page
//use eval("front") to access it
export interface Front {
on: (msg: string, f: (...data: any[]) => void) => void;
send: (msg: string, ...data: any[]) => void;
}
//hepler function to making full android filepath to internal storage file
export declare function makeFilePath(filename: string): string;
export type QueryAllReq = {
id: string;
type: "get-all";
filepath: string;
req: any;
};
//You should run app after initialization all repositories
export declare function run(front: Front): void;
//Androifjs file repo for frontend
export declare class AndroidjsFileRepovych<T extends Id, Q extends {}, Id extends {}> implements IRepovich<T, Q, Id> {
constructor(
filepath: string, //use makeFilePath function to make correct filepath
front: Front, //use eval("front") to access front
newModel: () => T, //making instance of class-model class
filterFn: (data: T[], q: Q) => T[], //data-filtering function
idFields: (keyof Id)[], //id fields
log: ILoggych
);
//Special androidjs-file-repovych commands and queries
queryAll(q?: any): Promise<T[]>;
saveAll(data: T[]): Promise<T[]>;
//Standart repovych commands and queries
query(q: Q): Promise<T[]>;
save(d: T[]): Promise<void>;
delete(ids: Id[]): Promise<void>;
queryDelete(q: Q): Promise<void>;
}
export declare class AndroidjsFileRepovych2<T extends Id, Q extends {}, Id extends {}> implements IRepovich2<T, Q, Id> {
constructor(
filepath: string, //use makeFilePath function to make correct filepath
front: Front, //use eval("front") to access front
newModel: () => T, //making instance of class-model class
filterFn: (data: T[], q: Q) => T[], //data-filtering function
idFields: (keyof Id)[], //id fields
log: ILoggych
);
//Special androidjs-file-repovych commands and queries
queryAll(q?: any): Promise<T[]>;
saveAll(data: T[]): Promise<T[]>;
//Standart repovych commands and queries
query(q: Q): Promise<T[]>;
save(d: T[]): Promise<void>;
delete(ids: Id[]): Promise<void>;
queryDelete(q: Q): Promise<void>;
queryIds(q: Q): Promise<Id[]>;
reset(d: T[]): Promise<void>;
count(q: Q): Promise<number>;
}
//connect any loggych to this command to log backend events
export declare function backendLogOutput(front: Front, log: ILoggych): void;Possible problems
- On typescript use
eval("front")for access to front global variable. - Check, that androidjs.js file have been linked before your script using this repovisch.
- On bundling and es5 compiling possible names conflinct with "front variable". For resolve this replace all
frontvariable names on_front, expect"front"(in quotes). - Dont forget start running with
run(front)command after repos initiated.
Author
Anatoly Starodubtsev [email protected]
License
MIT
