Notebooks

Notebooks are how notes are organized in Inkdrop and are commonly used to separate notes by category, project, or purpose.

The notebook model

The notebook model contains information about your notebook collections, such as their name, the number of notes they contain, associated parent notebook, creation and modification timestamps, and other related metadata.

Properties

  • Name
    _id
    Type
    string
    Description

    The unique notebook ID which should start with book: and the remains are randomly generated string.

  • Name
    _rev
    Type
    string
    Description

    This is a CouchDB specific field. The current MVCC-token/revision of this document (mandatory and immutable).

  • Name
    count
    Type
    number
    Description

    It indicates the number of notes in the notebook.

  • Name
    createdAt
    Type
    number
    Description

    The date time when the notebook was created, represented with Unix timestamps in milliseconds.

  • Name
    name
    Type
    string
    Description

    The notebook name.

  • Name
    parentBookId
    Type
    nullable string
    Description

    The ID of the parent notebook.

  • Name
    updatedAt
    Type
    number
    Description

    The date time when the notebook was last updated, represented with Unix timestamps in milliseconds.

The notebook object

{
  "_id": "book:Bk5Ivk0T",
  "_rev": "1-ba9bc6e7e45264bb3d6a8f3c266e9df0",
  "updatedAt": 1475373619554,
  "createdAt": 1475373619554,
  "name": "Ideas",
  "count": 8,
  "parentBookId": "book:ak5I0k0a"
}

The notebook class

The Book class is a wrapper class of the notebook, providing various methods for its operations such as loading, validation, saving, and serialization.

Methods

  • Name
    static loadWithId(bookId: string)
    Type
    Description

    Loads a notebook by its ID from the local database and returns a new instance of the Book.

  • Name
    constructor(initialValues: Partial<Book>)
    Type
    Description

    Initializes a new Book instance with the given initial values. If no values are provided, it assigns default values.

  • Name
    validate()
    Type
    Description

    Validates the current notebook instance based on the predefined validation rules. If valid, returns an empty array. If not, returns the list of validation errors.

  • Name
    save()
    Type
    Description

    Validates and then saves the notebook to the local database. Throws an error if the validation fails or if there's a problem with the saving process.

  • Name
    toObject()
    Type
    Description

    Converts the notebook instance into a plain JavaScript object.

  • Name
    toJson()
    Type
    Description

    Serializes the notebook instance into a JSON string.

Import the class

const { Book } = require('inkdrop').models

Load a notebook from the database

const book = await Book.loadWithId('book:9dc6a7a7')

Create a new notebook

const book = new Book({
  name: 'My Notebook'
})

Change its name

book.name = 'Updated Notebook'

Save the changes

await book.save()

Convert the notebook into a plain object

const object = book.toObject()

Convert the notebook into JSON

const json = book.toJson()

get(docId, options)

Retrieve a notebook

Retrieve a notebook with the given ID.

Parameters

  • Name
    docId
    Type
    string
    Required
    Description

    The notebook ID

  • Name
    options
    Type
    object
    Description

    Options. See PouchDB's documentation for available options.

Returns

Returns a notebook if a valid notebook ID was provided.

Request

const db = inkdrop.main.dataStore.getLocalDB()
const book = await db.books.get('book:Bk5Ivk0T')

Response

{
  "_id": "book:Bk5Ivk0T",
  "_rev": "1-ba9bc6e7e45264bb3d6a8f3c266e9df0",
  "updatedAt": 1475373619554,
  "createdAt": 1475373619554,
  "name": "Ideas",
  "count": 8,
  "parentBookId": "book:ak5I0k0a"
}

all()

List all notebooks

Retrieve a list of your notebooks.

Parameters

No parameters

Returns

Returns a list of notebooks.

Request

const db = inkdrop.main.dataStore.getLocalDB()
const list = await db.books.all()

Response

[
  {
    updatedAt: 1475549348850,
    createdAt: 1475549348850,
    count: 0,
    name: 'Inbox',
    parentBookId: null,
    _id: 'book:SkTsOceR',
    _rev: '2-aa25f8bfe83cfcd6910fbb78c8e45eeb'
  },
  ...
]

createId(): string

Create a notebook ID

Creates new notebook ID which is String

Parameters

No parameters

Returns

Returns a new notebook ID.

Request

const db = inkdrop.main.dataStore.getLocalDB()
const bookId = await db.books.createId()

Response

"book:ak0s1ce6"

validateDocId(): boolean

Validate a notebook ID

Tests if the given docId is a valid notebook ID

Parameters

  • Name
    docId
    Type
    string
    Required
    Description

    A document ID to validate

Returns

Returns true if valid

Request

const db = inkdrop.main.dataStore.getLocalDB()
const isValid = await db.books.validateDocId('book:ak0s1ce6')

Response

true

put(doc)

Create or update a notebook

Create a new notebook or update an existing note. If the notebook already exists, you must specify its revision _rev, otherwise a conflict will occur.

Parameters

  • Name
    doc
    Type
    object
    Required
    Description

    A Book data to store

Returns

It throws an InvalidDataError if the given doc was invalid. The response contains the id of the document, the new rev, and an ok to reassure you that everything is okay.

Request

const db = inkdrop.main.dataStore.getLocalDB()
const result = await db.books.put({
  _id: 'book:9IoGtoCuv',
  updatedAt: 1475549348850,
  createdAt: 1475549348850,
  name: 'New Notebook',
  count: 0,
  parentBookId: null
})

Response

{
  "ok": true,
  "id": "book:9IoGtoCuv",
  "rev": "1-A6157A5EA545C99B00FF904EEF05FD9F"
}

remove(docId)

Remove a notebook

Deletes a notebook with the given notebook ID.

Parameters

  • Name
    docId
    Type
    string
    Required
    Description

    A notebook ID to delete.

Returns

The response contains the id of the document, the new rev, and an ok to reassure you that everything is okay.

Request

const db = inkdrop.main.dataStore.getLocalDB()
const result = await db.books.remove("book:9IoGtoCuv")

Response

{
  "ok": true,
  "id": "book:9IoGtoCuv",
  "rev": "2-9af304be281790604d1d8a4b0f4c9adb"
}

countAll()

Count number of notebooks

Counts a number of notebooks in the database.

Parameters

No parameters.

Returns

Returns a number of notebooks stored in the database.

Request

const db = inkdrop.main.dataStore.getLocalDB()
const num = await db.books.countAll()

Response

5

findWithName(name)

Lookup a notebook by name

Retrieves a notebook with the given name.

Parameters

  • Name
    name
    Type
    string
    Required
    Description

    A notebook name to find.

Returns

Returns a notebook if found otherwise undefined.

Request

const db = inkdrop.main.dataStore.getLocalDB()
const book = await db.books.findWithName('Inbox')

Response

{
  updatedAt: 1475549348850,
  createdAt: 1475549348850,
  count: 0,
  name: 'Inbox',
  parentBookId: null,
  _id: 'book:SkTsOceR',
  _rev: '2-aa25f8bfe83cfcd6910fbb78c8e45eeb'
}
Can you help us improve the docs? 🙏

The source of these docs is here on GitHub. If you see a way these docs can be improved, please fork us!