Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to firebase-admin for nodejs #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: 2
jobs:
build:
docker:
- image: circleci/node:10
working_directory: ~/repo

steps:
- checkout

# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run: yarn install

- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}

# run lint!
- run: yarn lint
# run tests!
- run: yarn test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ firebase.config.js*
npm-debug.log*
yarn-error.log*
dist/
*secret.json*
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@vuga/base-firestore",
"private": false,
"version": "0.0.3",
"description": "Universal Firebase Firestore CRUD library, works on both clients and servers",
"version": "0.0.6",
"description": "Firebase Firestore CRUD/Query/Pagination library for Node",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand All @@ -13,10 +13,9 @@
"scripts": {
"lint": "tslint \"src/**/*.ts\" --project tsconfig.json",
"build": "rimraf dist && tsc",
"test-spec": "mocha src/**/*test.ts -R spec --require ts-node/register --exit",
"test": "npm run test-spec",
"prepare": "npm run build",
"prepublishOnly": "npm run build && npm test"
"test": "mocha src/**/*test.ts -R spec --require ts-node/register --exit",
"prepare": "yarn build",
"prepublishOnly": "yarn build && yarn test"
},
"engines": {
"node": ">=4.2.0"
Expand Down Expand Up @@ -59,7 +58,7 @@
"typescript": ">=2.0"
},
"dependencies": {
"lodash": "^4.17.15",
"firebase": "^6.6.0"
"firebase-admin": "^8.7.0",
"lodash": "^4.17.15"
}
}
15 changes: 11 additions & 4 deletions src/_init.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import * as firebase from 'firebase';
import 'firebase/firestore';
import * as admin from 'firebase-admin';
import config from './config';

firebase.initializeApp(config);
const db = firebase.firestore();
const { projectId } = config;

admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: `https://${projectId}.firebaseio.com`
});

const db = admin.firestore();
// const storage = admin.storage();
// const firebaseAuth = admin.auth;

export { db };
10 changes: 8 additions & 2 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { firestore } from 'firebase';
import { firestore } from 'firebase-admin';
export const getTimeStamp = () => {
return firestore.FieldValue.serverTimestamp();
};
};

/**
* convert JavaScript objects with custom prototypes to a plain object.
* @param obj
*/
export const serializeObj = (obj: any) => JSON.parse(JSON.stringify(obj));
6 changes: 0 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
require('dotenv').config();
const config = {
apiKey: process.env.FIREBASE_API_KEY || "",
authDomain: process.env.FIREBASE_AUTH_DOMAIN || "",
databaseURL: process.env.FIREBASE_DATABASE_URL || "",
projectId: process.env.FIREBASE_PROJECT_ID || "",
storageBucket: process.env.FIREBASE_STORAGE_BUCKET || "",
messagingSenderId: process.env.FIREBASE_MSG_SENDER_ID || "",
appId: process.env.FIREBASE_APP_ID || ""
}
export default config;
31 changes: 20 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { firestore } from 'firebase'
import * as admin from 'firebase-admin'
import isEmpty from 'lodash/isEmpty';
import { getTimeStamp } from './_utils';

namespace firestore {
export interface Firestore extends admin.firestore.Firestore { }
export interface CollectionReference extends admin.firestore.CollectionReference { }
export interface DocumentData extends admin.firestore.DocumentData { }
};

interface IField {
name: string;
operator?: any;
Expand Down Expand Up @@ -116,7 +122,7 @@ export default class BaseFireStore {
return { data: nextData, total: totalPosts };
} catch (error) {
this.logger(error);
return Promise.reject(error);
return null
}
}

Expand Down Expand Up @@ -148,7 +154,7 @@ export default class BaseFireStore {
}
} catch (error) {
this.logger(error);
return Promise.reject(error);
return null
}
}

Expand Down Expand Up @@ -197,7 +203,7 @@ export default class BaseFireStore {
}
} catch (error) {
this.logger(error);
return Promise.reject(error);
return null
}
}

Expand All @@ -222,7 +228,7 @@ export default class BaseFireStore {
return data;
} catch (error) {
this.logger(error);
return Promise.reject(error);
return null
}
}

Expand All @@ -236,11 +242,14 @@ export default class BaseFireStore {
.doc(id)
.get()
.then((item: any) => {
if (!item.data()) {
throw new Error(`Couldn't find item by id ${id}`);
}
return { ...item.data(), id };
});
} catch (error) {
this.logger(error);
return Promise.reject(error);
return null
}
}

Expand All @@ -258,7 +267,7 @@ export default class BaseFireStore {
});
} catch (error) {
this.logger(error);
return Promise.reject(error);
return null
}
}

Expand All @@ -274,7 +283,7 @@ export default class BaseFireStore {
}
catch (error) {
this.logger(error);
return Promise.reject(error);
return null
}
}

Expand Down Expand Up @@ -311,7 +320,7 @@ export default class BaseFireStore {

} catch (error) {
this.logger(error);
return Promise.reject(error);
return null
}
}

Expand All @@ -330,7 +339,7 @@ export default class BaseFireStore {
return post;
} catch (error) {
this.logger(error);
return Promise.reject(error);
return null
}
}

Expand All @@ -352,7 +361,7 @@ export default class BaseFireStore {
return post;
} catch (error) {
this.logger(error);
return Promise.reject(error);
return null
}
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"sourceMap": true,
"inlineSources": true,
"esModuleInterop": true,
"types": ["node", "mocha"]
"types": ["node", "mocha"],
"strictNullChecks": false
},
"include": [
"src/**/*"
Expand Down