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

Revert v0.9.0 and use converting Date <-> Timestamp #3

Merged
merged 9 commits into from Nov 14, 2018
Merged
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
22 changes: 11 additions & 11 deletions lib/index.d.ts
@@ -1,31 +1,31 @@
import * as admin from 'firebase-admin';
import * as FirebaseFirestore from '@google-cloud/firestore';
declare type Partial<T> = {
[P in keyof T]?: T[P];
};
export declare const initialize: (_firestore: FirebaseFirestore.Firestore) => void;
export declare class Snapshot<T extends Timestamps> {
ref: admin.firestore.DocumentReference;
ref: FirebaseFirestore.DocumentReference;
data: T;
constructor(ref: admin.firestore.DocumentReference, data: T);
constructor(snapshot: admin.firestore.DocumentSnapshot);
constructor(ref: FirebaseFirestore.DocumentReference, data: T);
constructor(snapshot: FirebaseFirestore.DocumentSnapshot);
readonly firestoreURL: string | undefined;
private setCreatedDate;
refresh(): Promise<void>;
save(): Promise<FirebaseFirestore.WriteResult>;
saveWithBatch(batch: admin.firestore.WriteBatch): void;
saveWithBatch(batch: FirebaseFirestore.WriteBatch): void;
saveReferenceCollection<S extends Timestamps>(collectionName: string, snapshot: Snapshot<S>): Promise<FirebaseFirestore.WriteResult>;
saveReferenceCollectionWithBatch<S extends Timestamps>(batch: admin.firestore.WriteBatch, collectionName: string, snapshot: Snapshot<S>): void;
saveReferenceCollectionWithBatch<S extends Timestamps>(batch: FirebaseFirestore.WriteBatch, collectionName: string, snapshot: Snapshot<S>): void;
saveNestedCollection<S extends Timestamps>(collectionName: string, snapshot: Snapshot<S>): Promise<FirebaseFirestore.WriteResult>;
saveNestedCollectionWithBatch<S extends Timestamps>(batch: admin.firestore.WriteBatch, collectionName: string, snapshot: Snapshot<S>): void;
saveNestedCollectionWithBatch<S extends Timestamps>(batch: FirebaseFirestore.WriteBatch, collectionName: string, snapshot: Snapshot<S>): void;
fetchNestedCollections<S extends Timestamps>(collectionName: string): Promise<Snapshot<S>[]>;
update(data: Partial<T>): Promise<FirebaseFirestore.WriteResult>;
updateWithBatch(batch: admin.firestore.WriteBatch, data: Partial<T>): void;
updateWithBatch(batch: FirebaseFirestore.WriteBatch, data: Partial<T>): void;
delete(): Promise<FirebaseFirestore.WriteResult>;
deleteWithBatch(batch: admin.firestore.WriteBatch): void;
deleteWithBatch(batch: FirebaseFirestore.WriteBatch): void;
}
export interface Timestamps {
createdAt?: admin.firestore.Timestamp;
updatedAt?: admin.firestore.Timestamp;
createdAt?: Date;
updatedAt?: Date;
}
export declare const makeNotSavedSnapshot: <T extends Timestamps>(path: string, data: T, id?: string | undefined) => Snapshot<T>;
export declare const fetch: <T extends Timestamps>(pathOrDocumentReference: string | FirebaseFirestore.DocumentReference, id?: string | undefined) => Promise<Snapshot<T>>;
Expand Down
60 changes: 46 additions & 14 deletions lib/index.js
Expand Up @@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const admin = require("firebase-admin");
const FirebaseFirestore = require("@google-cloud/firestore");
let firestore;
exports.initialize = (_firestore) => {
firestore = _firestore;
Expand All @@ -17,7 +17,7 @@ class Snapshot {
constructor(a, b) {
if (b === null || b === undefined) {
this.ref = a.ref;
this.data = a.data();
this.data = convertToOutput(a.data());
}
else {
this.ref = a;
Expand All @@ -32,8 +32,8 @@ class Snapshot {
return undefined;
}
setCreatedDate() {
this.data.createdAt = admin.firestore.Timestamp.now();
this.data.updatedAt = admin.firestore.Timestamp.now();
this.data.createdAt = new Date();
this.data.updatedAt = new Date();
}
refresh() {
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -42,27 +42,27 @@ class Snapshot {
}
save() {
this.setCreatedDate();
return this.ref.create(this.data);
return this.ref.create(converToInput(this.data));
}
saveWithBatch(batch) {
this.setCreatedDate();
batch.create(this.ref, this.data);
batch.create(this.ref, converToInput(this.data));
}
saveReferenceCollection(collectionName, snapshot) {
const rc = this.ref.collection(collectionName).doc(snapshot.ref.id);
return rc.create({ createdAt: admin.firestore.Timestamp.now(), updatedAt: admin.firestore.Timestamp.now() });
return rc.create(converToInput({ createdAt: new Date(), updatedAt: new Date() }));
}
saveReferenceCollectionWithBatch(batch, collectionName, snapshot) {
const rc = this.ref.collection(collectionName).doc(snapshot.ref.id);
batch.create(rc, { createdAt: admin.firestore.Timestamp.now(), updatedAt: admin.firestore.Timestamp.now() });
batch.create(rc, converToInput({ createdAt: new Date(), updatedAt: new Date() }));
}
saveNestedCollection(collectionName, snapshot) {
const rc = this.ref.collection(collectionName).doc(snapshot.ref.id);
return rc.create(snapshot.data);
return rc.create(converToInput(snapshot.data));
}
saveNestedCollectionWithBatch(batch, collectionName, snapshot) {
const rc = this.ref.collection(collectionName).doc(snapshot.ref.id);
batch.create(rc, snapshot.data);
batch.create(rc, converToInput(snapshot.data));
}
fetchNestedCollections(collectionName) {
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -74,18 +74,18 @@ class Snapshot {
});
}
update(data) {
data.updatedAt = admin.firestore.Timestamp.now();
data.updatedAt = new Date();
Object.keys(data).forEach(key => {
this.data[key] = data[key];
});
return this.ref.update(data);
return this.ref.update(converToInput(data));
}
updateWithBatch(batch, data) {
data.updatedAt = admin.firestore.Timestamp.now();
data.updatedAt = new Date();
Object.keys(data).forEach(key => {
this.data[key] = data[key];
});
batch.update(this.ref, data);
batch.update(this.ref, converToInput(data));
}
delete() {
return this.ref.delete();
Expand Down Expand Up @@ -116,3 +116,35 @@ exports.fetch = (pathOrDocumentReference, id) => __awaiter(this, void 0, void 0,
}
return new Snapshot(ds);
});
const converToInput = (data) => {
let result = {};
for (let attr in data) {
if (data[attr] instanceof Date) {
if (!data[attr]) {
continue;
}
const date = data[attr];
result[attr] = FirebaseFirestore.Timestamp.fromDate(date);
}
else {
result[attr] = data[attr];
}
}
return result;
};
const convertToOutput = (data) => {
let result = {};
for (let attr in data) {
if (data[attr] instanceof FirebaseFirestore.Timestamp) {
if (!data[attr]) {
continue;
}
const date = data[attr];
result[attr] = date.toDate();
}
else {
result[attr] = data[attr];
}
}
return result;
};
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -27,6 +27,7 @@
},
"homepage": "https://github.com/starhoshi/tart#readme",
"devDependencies": {
"@google-cloud/firestore": "^0.18.0",
"@types/jest": "^23.1.1",
"firebase-admin": "^6.1.0",
"jest": "^23.1.0",
Expand All @@ -45,4 +46,4 @@
"json"
]
}
}
}
92 changes: 64 additions & 28 deletions src/index.ts
@@ -1,23 +1,23 @@
import * as admin from 'firebase-admin'
import * as FirebaseFirestore from '@google-cloud/firestore'

type Partial<T> = { [P in keyof T]?: T[P]; }

let firestore: admin.firestore.Firestore
let firestore: FirebaseFirestore.Firestore

export const initialize = (_firestore: admin.firestore.Firestore) => {
export const initialize = (_firestore: FirebaseFirestore.Firestore) => {
firestore = _firestore
}

export class Snapshot<T extends Timestamps> {
ref: admin.firestore.DocumentReference
ref: FirebaseFirestore.DocumentReference
data: T

constructor(ref: admin.firestore.DocumentReference, data: T)
constructor(snapshot: admin.firestore.DocumentSnapshot)
constructor(ref: FirebaseFirestore.DocumentReference, data: T)
constructor(snapshot: FirebaseFirestore.DocumentSnapshot)
constructor(a: any, b?: any) {
if (b === null || b === undefined) {
this.ref = a.ref
this.data = a.data() as T
this.data = convertToOutput(a.data() as T)
} else {
this.ref = a
this.data = b
Expand All @@ -33,8 +33,8 @@ export class Snapshot<T extends Timestamps> {
}

private setCreatedDate() {
this.data.createdAt = admin.firestore.Timestamp.now()
this.data.updatedAt = admin.firestore.Timestamp.now()
this.data.createdAt = new Date()
this.data.updatedAt = new Date()
}

async refresh() {
Expand All @@ -43,32 +43,32 @@ export class Snapshot<T extends Timestamps> {

save() {
this.setCreatedDate()
return this.ref.create(this.data)
return this.ref.create(converToInput(this.data))
}

saveWithBatch(batch: admin.firestore.WriteBatch) {
saveWithBatch(batch: FirebaseFirestore.WriteBatch) {
this.setCreatedDate()
batch.create(this.ref, this.data)
batch.create(this.ref, converToInput(this.data))
}

saveReferenceCollection<S extends Timestamps>(collectionName: string, snapshot: Snapshot<S>) {
const rc = this.ref.collection(collectionName).doc(snapshot.ref.id)
return rc.create({ createdAt: admin.firestore.Timestamp.now(), updatedAt: admin.firestore.Timestamp.now() })
return rc.create(converToInput({ createdAt: new Date(), updatedAt: new Date() }))
}

saveReferenceCollectionWithBatch<S extends Timestamps>(batch: admin.firestore.WriteBatch, collectionName: string, snapshot: Snapshot<S>) {
saveReferenceCollectionWithBatch<S extends Timestamps>(batch: FirebaseFirestore.WriteBatch, collectionName: string, snapshot: Snapshot<S>) {
const rc = this.ref.collection(collectionName).doc(snapshot.ref.id)
batch.create(rc, { createdAt: admin.firestore.Timestamp.now(), updatedAt: admin.firestore.Timestamp.now() })
batch.create(rc, converToInput({ createdAt: new Date(), updatedAt: new Date() }))
}

saveNestedCollection<S extends Timestamps>(collectionName: string, snapshot: Snapshot<S>) {
const rc = this.ref.collection(collectionName).doc(snapshot.ref.id)
return rc.create(snapshot.data)
return rc.create(converToInput(snapshot.data))
}

saveNestedCollectionWithBatch<S extends Timestamps>(batch: admin.firestore.WriteBatch, collectionName: string, snapshot: Snapshot<S>) {
saveNestedCollectionWithBatch<S extends Timestamps>(batch: FirebaseFirestore.WriteBatch, collectionName: string, snapshot: Snapshot<S>) {
const rc = this.ref.collection(collectionName).doc(snapshot.ref.id)
batch.create(rc, snapshot.data)
batch.create(rc, converToInput(snapshot.data))
}

async fetchNestedCollections<S extends Timestamps>(collectionName: string) {
Expand All @@ -80,33 +80,33 @@ export class Snapshot<T extends Timestamps> {
}

update(data: Partial<T>) {
data.updatedAt = admin.firestore.Timestamp.now()
data.updatedAt = new Date()
Object.keys(data).forEach(key => {
this.data[key] = data[key]
})
return this.ref.update(data)
return this.ref.update(converToInput(data))
}

updateWithBatch(batch: admin.firestore.WriteBatch, data: Partial<T>) {
data.updatedAt = admin.firestore.Timestamp.now()
updateWithBatch(batch: FirebaseFirestore.WriteBatch, data: Partial<T>) {
data.updatedAt = new Date()
Object.keys(data).forEach(key => {
this.data[key] = data[key]
})
batch.update(this.ref, data)
batch.update(this.ref, converToInput(data))
}

delete() {
return this.ref.delete()
}

deleteWithBatch(batch: admin.firestore.WriteBatch) {
deleteWithBatch(batch: FirebaseFirestore.WriteBatch) {
batch.delete(this.ref)
}
}

export interface Timestamps {
createdAt?: admin.firestore.Timestamp
updatedAt?: admin.firestore.Timestamp
createdAt?: Date
updatedAt?: Date
}

export const makeNotSavedSnapshot = <T extends Timestamps>(path: string, data: T, id?: string) => {
Expand All @@ -117,12 +117,12 @@ export const makeNotSavedSnapshot = <T extends Timestamps>(path: string, data: T
return new Snapshot<T>(ref, data)
}

export const fetch = async <T extends Timestamps>(pathOrDocumentReference: string | admin.firestore.DocumentReference, id?: string) => {
export const fetch = async <T extends Timestamps>(pathOrDocumentReference: string | FirebaseFirestore.DocumentReference, id?: string) => {
let docPath: string = ''
if (typeof pathOrDocumentReference === 'string') {
docPath = `${pathOrDocumentReference}/${id}`
} else {
docPath = (pathOrDocumentReference as admin.firestore.DocumentReference).path
docPath = (pathOrDocumentReference as FirebaseFirestore.DocumentReference).path
}

const ds = await firestore.doc(docPath).get()
Expand All @@ -131,3 +131,39 @@ export const fetch = async <T extends Timestamps>(pathOrDocumentReference: strin
}
return new Snapshot<T>(ds)
}

const converToInput = <T extends Timestamps>(data: T) => {
let result: any = {}

for (let attr in data) {
if (data[attr] instanceof Date) {
if (!data[attr]) {
continue
}
const date = data[attr] as any as Date
result[attr] = FirebaseFirestore.Timestamp.fromDate(date)
} else {
result[attr] = data[attr]
}
}

return result
}

const convertToOutput = <T extends Timestamps>(data: T) => {
let result: any = {}

for (let attr in data) {
if (data[attr] instanceof FirebaseFirestore.Timestamp) {
if (!data[attr]) {
continue
}
const date = data[attr] as any as FirebaseFirestore.Timestamp
result[attr] = date.toDate()
} else {
result[attr] = data[attr]
}
}

return result
}