googlesheets-raghbir
v1.0.2
Published
`@raghbir/googlesheetsorm` is an ORM (Object-Relational Mapping) library for Google Sheets. It allows you to interact with Google Sheets as if they were a database, providing methods to create, read, update, and delete rows in a sheet.
Downloads
8
Readme
@raghbir/googlesheetsorm
@raghbir/googlesheetsorm
is an ORM (Object-Relational Mapping) library for Google Sheets. It allows you to interact with Google Sheets as if they were a database, providing methods to create, read, update, and delete rows in a sheet.
Installation
To install the package, run:
npm install @raghbir/googlesheetsorm
Usage
1. Setting up Google API Credentials
Before using the library, you need to set up Google API credentials. You can create a project in the Google Cloud Console and enable the Google Sheets API. Download the credentials file and keep it ready.
2. Initializing the Library
First, import the GoogleDB
class and initialize it with the required options:
const { GoogleDB } = require('@raghbir/googlesheetsorm');
const db = new GoogleDB({
auth: {
// Replace with your actual credentials
CLIENT_ID: 'your-client-id',
CLIENT_SECRET: 'your-client-secret',
REDIRECT_URI: 'your-redirect-uri',
refreshToken: 'your-refresh-token'
},
spreadSheetLink: 'https://docs.google.com/spreadsheets/d/your-spreadsheet-id/edit#gid=0',
spreadSheetName: 'Sheet1',
schema: {
name: { type: 'string', required: true },
age: { type: 'number' },
isActive: { type: 'boolean' }
}
});
3. Creating a Record
To create a new record in the Google Sheet:
async function createRecord() {
const record = await db.create({ name: 'John Doe', age: 30, isActive: true });
console.log('Record created:', record);
}
createRecord();
4. Finding Records
To find records matching a query:
async function findRecords() {
const records = await db.find({ isActive: true });
console.log('Records found:', records);
}
findRecords();
5. Finding a Single Record
To find a single record matching a query:
async function findOneRecord() {
const record = await db.findOne({ name: 'John Doe' });
console.log('Record found:', record);
}
findOneRecord();
6. Updating a Record
To update a record matching a query:
async function updateRecord() {
const updatedRecord = await db.update({ name: 'John Doe' }, { age: 31 });
console.log('Record updated:', updatedRecord);
}
updateRecord();
7. Deleting a Record
To delete a record matching a query:
async function deleteRecord() {
const deleteResponse = await db.delete({ name: 'John Doe' });
console.log('Record deleted:', deleteResponse);
}
deleteRecord();
API Reference
GoogleDB
Constructor
new GoogleDB(options: GoogleDBOptions)
- options: An object with the following properties:
auth
: Authentication credentials for Google API.spreadSheetLink
: The link to your Google Spreadsheet.spreadSheetName
: The name of the sheet to use.schema
: The schema definition for the sheet.
Methods
create(data: { [key: string]: any }): Promise<{ [key: string]: any }>
- Creates a new record in the Google Sheet.
- data: The data to insert.
- Returns a promise that resolves to the created record.
find(query: { [key: string]: any }): Promise<any[]>
- Finds records matching the query.
- query: The query to filter records.
- Returns a promise that resolves to an array of matching records.
findOne(query: { [key: string]: any }): Promise
- Finds a single record matching the query.
- query: The query to filter records.
- Returns a promise that resolves to the matching record or null.
update(query: { [key: string]: any }, newData: { [key: string]: any }): Promise<{ [key: string]: any }>
- Updates records matching the query.
- query: The query to filter records.
- newData: The new data to update.
- Returns a promise that resolves to the updated record.
delete(query: { [key: string]: any }): Promise<sheets_v4.Schema$UpdateValuesResponse>
- Deletes records matching the query.
- query: The query to filter records.
- Returns a promise that resolves to the update response.
This documentation should help users understand how to install and use your npm package. Make sure to include any additional information or examples specific to your package's features and usage.