Как мне сохранить и извлечь объекты машинописи, используя pouchdb? - PullRequest
0 голосов
/ 29 января 2019

Я чрезвычайно новичок в машинописи и в webdev в целом и собираюсь использовать pouchdb в проекте машинописи для сохранения моих объектов.У меня проблемы с поиском правильного способа сделать это, и документации довольно мало.

У меня есть объекты машинописи, которые происходят из базового класса Document с обязательными полями _id и _rev.Я на правильном пути здесь?удаленно закрыть?

Вот моя попытка создать базовый класс Document, который пахнет чем-то, что должно идти в базе данных PouchDB -

import PouchDB from 'pouchdb';

// Base class for all objects which are persisted in database
export class Document {

    readonly type: string;
    readonly _id: string;
    private _rev?: string; //set by database when document is inserted

    constructor(type: string, id_suffix?: string) {
        this.type = type;
        let unique_id: string = uuid();

        if (id_suffix === undefined) {
            this._id = '${type}_${unique_id}'
        }
        else {
            this._id = '${type}_${id_suffix}_${unique_id}'
        }
    }
}

Я могу вставить его в базу данных, по-видимому

let db = new PouchDB('my-database');
let mydoc = Document('mydoc');

db.put(t)

let output = db.get(t._id); //Promise<PouchDB.Core.IdMeta & PouchDB.Core.GetMeta>

Может ли кто-нибудь помочь мне вернуть мой предмет?

1 Ответ

0 голосов
/ 29 января 2019

Это похоже на работу ...

import PouchDB from 'pouchdb';

// Base class for all objects which are persisted in database
export class Document {

    readonly type: string;
    readonly _id: string;
    private _rev?: string; //set by database when document is inserted

    constructor(type: string, id_suffix?: string) {
        this.type = type;
        let unique_id: string = uuid();

        if (id_suffix === undefined) {
            this._id = '${type}_${unique_id}'
        }
        else {
            this._id = '${type}_${id_suffix}_${unique_id}'
        }
    }
}

db.put(t);

let output = db.get<Document>(t._id).then(function (doc){
    let x: Document = doc;
    return x;
})
...