// -*- mode: typescript; indent-tabs-mode: nil; js-basic-offset: 4 -*- // // This file is part of Genie // // Copyright 2021 The Board of Trustees of the Leland Stanford Junior University // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Author: Giovanni Campagna import * as Tp from 'thingpedia'; import { SQLiteDatabase, SQLitePlatform } from './sqlite'; import { DatabaseProxy, DatabaseProxyConfig } from './dbproxy'; export interface AbstractRow { uniqueId : string; } export interface AppRow { uniqueId : string; code : string; state : string; name : string; description : string; } export interface ChannelRow { uniqueId : string; value : string; } export interface DeviceRow { uniqueId : string; state : string; } export interface ConversationRow { uniqueId : string; conversationId : string; previousId : string|null; dialogueId : string; context : string|null; agent : string|null; agentTimestamp : string|null; agentTarget : string|null; intermediateContext : string|null; user : string; userTimestamp : string|null; userTarget : string; vote : string|null; comment : string|null; } export interface ConversationStateRow { uniqueId : string; dialogueState : string|null; lastMessageId : number; recording : boolean; } export interface ConversationHistoryRow { uniqueId : string; conversationId : string; messageId : number; message : string } export interface LocalTables { app : AppRow; channel : ChannelRow; conversation : ConversationRow; conversation_state : ConversationStateRow; conversation_history : ConversationHistoryRow; } export interface SyncTables { device : DeviceRow; } export interface SearchParams { filter : Array<{ k : keyof RowType, o : '=' | '>=' | '<=' | '<' | '>', v : string|number|boolean }>; sort : [keyof RowType, 'asc' | 'desc']; limit : number; } export interface LocalTable { name : string; getAll() : Promise; getOne(uniqueId : string) : Promise; getBy(field : keyof RowType, value : string) : Promise; search(search : SearchParams) : Promise; insertOne(uniqueId : string, row : Omit) : Promise; deleteOne(uniqueId : string) : Promise; } export type SyncRecord = { [K in keyof RowType] : RowType[K]|null } & { uniqueId : string; lastModified : number }; export interface SyncAtReply { lastModified : number; ourChanges : Array>; done : boolean[]; } export interface SyncTable{ name : string; fields : ReadonlyArray; getAll() : Promise; getOne(uniqueId : string) : Promise; insertOne(uniqueId : string, row : Omit) : Promise; deleteOne(uniqueId : string) : Promise; getRaw() : Promise>>; getChangesAfter(lastModified : number) : Promise>>; handleChanges(changes : Array>) : Promise; syncAt(lastModified : number, pushedChanges : Array>) : Promise>; replaceAll(data : Array>) : Promise; insertIfRecent(uniqueId : string, lastModified : number, row : Omit) : Promise; deleteIfRecent(uniqueId : string, lastModified : number) : Promise; } export interface AbstractDatabase { ensureSchema() : Promise; getLocalTable(name : T) : LocalTable; getSyncTable(name : T) : SyncTable; } export function createDB(platform : Tp.BasePlatform) : AbstractDatabase { // use database proxy if it's configured in the platform if (platform.hasCapability('database-proxy')) return new DatabaseProxy(platform.getCapability('database-proxy') as DatabaseProxyConfig); const sqliteplatform = platform as SQLitePlatform; return new SQLiteDatabase(sqliteplatform); }