npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cavinpabua/frappe-connector

v1.1.0

Published

TypeScript svelte library for Frappe Framework backend.

Downloads

696

Readme

frappe-connector

TypeScript svelte library for Frappe Framework backend.

Installation

If you're seeing this, you've probably already done this step. Congrats!

npm install @cavinpabua/frappe-connector

or

yarn add @cavinpabua/frappe-connector

Initialising the library

import { Frappe } from '@cavinpabua/frappe-connector';
// Using username and password
const frappe = new Frappe({
    url: 'https://frappe.cloud',
    username: '',
    password: ''
});
await frappe.login() // this is needed since we need to login to get the session-id for user

// Using apiKey and secretKey
const frappe = new Frappe({
    url: 'https://frappe.cloud',
    apiKey: '',
    secretKey: ''
});

Database

Initialise the database library

const db = frappe.db();

Fetch a single document using document name

db.getDoc('DocType', 'document_name')
  .then((doc) => console.log(doc))
  .catch((error) => console.error(error));

Fetch document list

db.getDocList('DocType')
  .then((docs) => console.log(docs))
  .catch((error) => console.error(error));

You can also add more options to filter the list

db.getDocList('DocType', {
  /** Fields to be fetched */
  fields: ['name', 'creation'],
  /** Filters to be applied - SQL AND operation */
  filters: [['creation', '>', '2021-10-09']],
  /** Filters to be applied - SQL OR operation */
  orFilters: [],
  /** Fetch from nth document in filtered and sorted list. Used for pagination  */
  limit_start: 5,
  /** Number of documents to be fetched. Default is 20  */
  limit: 10,
  /** Sort results by field and order  */
  orderBy: {
    field: 'creation',
    order: 'desc',
  },
  /** Group the results by particular field */
  groupBy: 'name',
  /** Fetch documents as a dictionary */
  asDict: false,
})
  .then((docs) => console.log(docs))
  .catch((error) => console.error(error));

Get count of document with filters

const filters = [['creation', '>', '2021-10-09']];
const useCache = true; /** Default is false - Optional **/
const debug = false; /** Default is false - Optional **/

db.getCount('DocType', filters, cache, debug)
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

Get Last Doc (Latest document to be inserted)

db.getLastDoc('My Doctype')
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

Optionally, you can have your own filter for the last doc

db.getLastDoc('My Doctype', {
    filters?: Filter<T>[];
    orFilters?: Filter<T>[];
    orderBy?: {
        field: keyof T | (string & Record<never, never>);
        order?: 'asc' | 'desc';
    };
})
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

Create Document

const params = {
  doctype: "My Doctype",
  field_1: "test",
  field_2: "test"
}

db.createDoc(params)
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

Create Documents in Bulk

const doc1 = {
  doctype: "My Doctype",
  field_1: "test",
  field_2: "test"
}
const doc2 = {
  doctype: "My Doctype",
  field_1: "test",
  field_2: "test"
}
const arrayOfDocs = [doc1, doc2]

db.createManyDocs(arrayOfDocs)
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

Update a Document

const values = {
  field_1: "updates this field",
  field_2: "updates this field"
}

db.updateDoc("My Doctype", "doc_name", {
  ...values
})
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

Update Documents in bulk

const doc1 = {
  doctype: "My Doctype",
  field_1: "test",
  field_2: "test"
}
const doc2 = {
  doctype: "My Doctype",
  field_1: "test",
  field_2: "test"
}
const arrayOfDocs = [doc1, doc2]

db.updateManyDocs(arrayOfDocs)
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

Delete a Document

db.deleteDoc("My Doctype", "doc_to_delete")
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

Submit a Document if submittable

db.submitDoc("My Doctype", "name_of_doc_to_submit")
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

Cancel a submitted Document

db.cancelDoc("My Doctype", "name_of_doc_to_cancel")
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

Rename a Document

db.renameDoc("My Doctype", "old_name", "new_name")
  .then((count) => console.log(count))
  .catch((error) => console.error(error));

FrappeUpload

const file = frappe.file();

Uploading a file with onProgress

const myFile; //Your File object

const fileArgs = {
  "isPrivate": true,
  /** Folder the file exists in (optional) */
  "folder": "Home",
  /** File URL (optional) */
  "file_url": "",
  /** Doctype associated with the file (optional) */
  "doctype": "User",
  /** Docname associated with the file (mandatory if doctype is present) */
  "docname": "Administrator",
  /** Field in the document **/
  "fieldname": "image"
}

file.uploadFile(
            myFile,
            fileArgs,
            /** Progress Indicator callback function **/
            (completedBytes, totalBytes) => console.log(Math.round((completedBytes / totalBytes) * 100), " completed")
        )
        .then(() => console.log("File Upload complete"))
        .catch(e => console.error(e))

Axios instance

If you want to use the axios instance for any requests you want. This instance will have the credentials initiated in the Frappe constructor.

const instance: AxiosInstance = frappe.client()

You can use it like a regular axios instance

// Like a normal Axios GET method
const searchParams = {
  doctype: 'Users',
  txt: 'Cavin',
};
instance
  .get('frappe.desk.search_link', searchParams)
  .then((result) => console.log(result))
  .catch((error) => console.error(error));