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

chore: enable prettier eslint plugin #316

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -9,8 +9,9 @@ module.exports = {
plugins: ['ember', '@typescript-eslint'],
extends: [
'airbnb-base',
'plugin:@typescript-eslint/recommended',
'eslint:recommended',
'plugin:ember/recommended',
'plugin:prettier/recommended',
],
env: {
browser: true,
@@ -49,6 +50,15 @@ module.exports = {
'func-names': 'off',
},
overrides: [
// ts files
{
files: ['**/*.ts'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {},
},
// node files
{
files: [
@@ -72,7 +82,6 @@ module.exports = {
},
extends: ['plugin:n/recommended'],
rules: {
'@typescript-eslint/no-var-requires': 'off',
'global-require': 'off',
'prefer-object-spread': 'off',
'prefer-rest-params': 'off',
@@ -86,8 +95,6 @@ module.exports = {
extends: ['plugin:qunit/recommended'],
rules: {
'prefer-arrow-callback': 'off',
'func-names': 'off',
'@typescript-eslint/no-empty-function': 'off',
'qunit/require-expect': 'off',
},
},
4 changes: 2 additions & 2 deletions addon/-private/flatten-doc-snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DocumentSnapshot } from 'firebase/firestore';

export default function flattenDocSnapshot(docSnapshot: DocumentSnapshot): {
id: string,
[key: string]: unknown,
id: string;
[key: string]: unknown;
} {
const { id } = docSnapshot;
const data = docSnapshot.data() || {};
217 changes: 154 additions & 63 deletions addon/adapters/cloud-firestore-modular.ts
Original file line number Diff line number Diff line change
@@ -44,7 +44,8 @@ interface Snapshot extends DS.Snapshot {
adapterOptions: AdapterOption;
}

interface SnapshotRecordArray extends DS.SnapshotRecordArray<keyof ModelRegistry> {
interface SnapshotRecordArray
extends DS.SnapshotRecordArray<keyof ModelRegistry> {
adapterOptions: AdapterOption;
}

@@ -57,10 +58,10 @@ interface HasManyRelationshipMeta {
key: string;
type: string;
options: {
isRealtime?: boolean,
isRealtime?: boolean;

buildReference?(db: Firestore, record: unknown): CollectionReference,
filter?(db: CollectionReference | Query, record: unknown): Query,
buildReference?(db: Firestore, record: unknown): CollectionReference;
filter?(db: CollectionReference | Query, record: unknown): Query;
};
}

@@ -98,22 +99,31 @@ export default class CloudFirestoreAdapter extends Adapter {
snapshot: Snapshot,
): RSVP.Promise<unknown> {
return new RSVP.Promise((resolve, reject) => {
const collectionRef = this.buildCollectionRef(type.modelName, snapshot.adapterOptions);
const collectionRef = this.buildCollectionRef(
type.modelName,
snapshot.adapterOptions,
);
const docRef = doc(collectionRef, snapshot.id);
const batch = this.buildWriteBatch(docRef, snapshot);

batch.commit().then(() => {
const data = this.serialize(snapshot, { includeId: true });

resolve(data);

if (snapshot.adapterOptions?.isRealtime && !this.isFastBoot) {
// Setup realtime listener for record
this.firestoreDataManager.findRecordRealtime(type.modelName, docRef);
}
}).catch((e) => {
reject(e);
});
batch
.commit()
.then(() => {
const data = this.serialize(snapshot, { includeId: true });

resolve(data);

if (snapshot.adapterOptions?.isRealtime && !this.isFastBoot) {
// Setup realtime listener for record
this.firestoreDataManager.findRecordRealtime(
type.modelName,
docRef,
);
}
})
.catch((e) => {
reject(e);
});
});
}

@@ -124,18 +134,24 @@ export default class CloudFirestoreAdapter extends Adapter {
): RSVP.Promise<unknown> {
return new RSVP.Promise((resolve, reject) => {
const db = getFirestore();
const collectionRef = this.buildCollectionRef(type.modelName, snapshot.adapterOptions);
const collectionRef = this.buildCollectionRef(
type.modelName,
snapshot.adapterOptions,
);
const docRef = doc(collectionRef, snapshot.id);
const batch = writeBatch(db);

batch.delete(docRef);
this.addIncludeToWriteBatch(batch, snapshot.adapterOptions);

batch.commit().then(() => {
resolve();
}).catch((e) => {
reject(e);
});
batch
.commit()
.then(() => {
resolve();
})
.catch((e) => {
reject(e);
});
});
}

@@ -147,16 +163,27 @@ export default class CloudFirestoreAdapter extends Adapter {
): RSVP.Promise<unknown> {
return new RSVP.Promise(async (resolve, reject) => {
try {
const colRef = this.buildCollectionRef(type.modelName, snapshot.adapterOptions);
const colRef = this.buildCollectionRef(
type.modelName,
snapshot.adapterOptions,
);
const docRef = doc(colRef, id);
const docSnapshot = snapshot.adapterOptions?.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.findRecordRealtime(type.modelName, docRef)
: await getDoc(docRef);
const docSnapshot =
snapshot.adapterOptions?.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.findRecordRealtime(
type.modelName,
docRef,
)
: await getDoc(docRef);

if (docSnapshot.exists()) {
resolve(flattenDocSnapshot(docSnapshot));
} else {
reject(new AdapterRecordNotFoundError(`Record ${id} for model type ${type.modelName} doesn't exist`));
reject(
new AdapterRecordNotFoundError(
`Record ${id} for model type ${type.modelName} doesn't exist`,
),
);
}
} catch (error) {
reject(error);
@@ -173,12 +200,21 @@ export default class CloudFirestoreAdapter extends Adapter {
return new RSVP.Promise(async (resolve, reject) => {
try {
const db = getFirestore();
const colRef = collection(db, buildCollectionName(type.modelName as string));
const querySnapshot = snapshotRecordArray?.adapterOptions?.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.findAllRealtime(type.modelName, colRef)
: await getDocs(colRef);

const result = querySnapshot.docs.map((docSnapshot) => flattenDocSnapshot(docSnapshot));
const colRef = collection(
db,
buildCollectionName(type.modelName as string),
);
const querySnapshot =
snapshotRecordArray?.adapterOptions?.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.findAllRealtime(
type.modelName,
colRef,
)
: await getDocs(colRef);

const result = querySnapshot.docs.map((docSnapshot) =>
flattenDocSnapshot(docSnapshot),
);

resolve(result);
} catch (error) {
@@ -204,11 +240,17 @@ export default class CloudFirestoreAdapter extends Adapter {
referenceKeyName: this.referenceKeyName,
queryId: queryOption.queryId,
};
const docSnapshots = queryOption.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.queryRealtime(config)
: await this.firestoreDataManager.queryWithReferenceTo(queryRef, this.referenceKeyName);

const result = docSnapshots.map((docSnapshot) => (flattenDocSnapshot(docSnapshot)));
const docSnapshots =
queryOption.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.queryRealtime(config)
: await this.firestoreDataManager.queryWithReferenceTo(
queryRef,
this.referenceKeyName,
);

const result = docSnapshots.map((docSnapshot) =>
flattenDocSnapshot(docSnapshot),
);

resolve(result);
} catch (error) {
@@ -233,14 +275,22 @@ export default class CloudFirestoreAdapter extends Adapter {
const db = getFirestore();
const docRef = doc(db, urlNodes.join('/'), id);
const modelName = relationship.type;
const docSnapshot = relationship.options.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.findRecordRealtime(modelName, docRef)
: await getDoc(docRef);
const docSnapshot =
relationship.options.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.findRecordRealtime(
modelName,
docRef,
)
: await getDoc(docRef);

if (docSnapshot.exists()) {
resolve(flattenDocSnapshot(docSnapshot));
} else {
reject(new AdapterRecordNotFoundError(`Record ${id} for model type ${modelName} doesn't exist`));
reject(
new AdapterRecordNotFoundError(
`Record ${id} for model type ${modelName} doesn't exist`,
),
);
}
} catch (error) {
reject(error);
@@ -256,19 +306,30 @@ export default class CloudFirestoreAdapter extends Adapter {
): RSVP.Promise<unknown> {
return new RSVP.Promise(async (resolve, reject) => {
try {
const queryRef = this.buildHasManyCollectionRef(store, snapshot, url, relationship);
const queryRef = this.buildHasManyCollectionRef(
store,
snapshot,
url,
relationship,
);
const config = {
queryRef,
modelName: snapshot.modelName,
id: snapshot.id,
field: relationship.key,
referenceKeyName: this.referenceKeyName,
};
const documentSnapshots = relationship.options.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.findHasManyRealtime(config)
: await this.firestoreDataManager.queryWithReferenceTo(queryRef, this.referenceKeyName);

const result = documentSnapshots.map((docSnapshot) => (flattenDocSnapshot(docSnapshot)));
const documentSnapshots =
relationship.options.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.findHasManyRealtime(config)
: await this.firestoreDataManager.queryWithReferenceTo(
queryRef,
this.referenceKeyName,
);

const result = documentSnapshots.map((docSnapshot) =>
flattenDocSnapshot(docSnapshot),
);

resolve(result);
} catch (error) {
@@ -283,8 +344,10 @@ export default class CloudFirestoreAdapter extends Adapter {
): CollectionReference {
const db = getFirestore();

return adapterOptions?.buildReference?.(db)
|| collection(db, buildCollectionName(modelName as string));
return (
adapterOptions?.buildReference?.(db) ||
collection(db, buildCollectionName(modelName as string))
);
}

private addDocRefToWriteBatch(
@@ -297,13 +360,19 @@ export default class CloudFirestoreAdapter extends Adapter {
batch.set(docRef, data, { merge: true });
}

private addIncludeToWriteBatch(batch: WriteBatch, adapterOptions?: AdapterOption): void {
private addIncludeToWriteBatch(
batch: WriteBatch,
adapterOptions?: AdapterOption,
): void {
const db = getFirestore();

adapterOptions?.include?.(batch, db);
}

private buildWriteBatch(docRef: DocumentReference, snapshot: Snapshot): WriteBatch {
private buildWriteBatch(
docRef: DocumentReference,
snapshot: Snapshot,
): WriteBatch {
const db = getFirestore();
const batch = writeBatch(db);

@@ -322,27 +391,49 @@ export default class CloudFirestoreAdapter extends Adapter {
const db = getFirestore();

if (relationship.options.buildReference) {
const collectionRef = relationship.options.buildReference(db, snapshot.record);

return relationship.options.filter?.(collectionRef, snapshot.record) || collectionRef;
const collectionRef = relationship.options.buildReference(
db,
snapshot.record,
);

return (
relationship.options.filter?.(collectionRef, snapshot.record) ||
collectionRef
);
}

const modelClass = store.modelFor(snapshot.modelName);
const cardinality = modelClass.determineRelationshipType(relationship, store);
const cardinality = modelClass.determineRelationshipType(
relationship,
store,
);

if (cardinality === 'manyToOne') {
const inverse = modelClass.inverseFor(relationship.key, store);
const snapshotCollectionName = buildCollectionName(snapshot.modelName.toString());
const snapshotDocRef = doc(db, `${snapshotCollectionName}/${snapshot.id}`);
const snapshotCollectionName = buildCollectionName(
snapshot.modelName.toString(),
);
const snapshotDocRef = doc(
db,
`${snapshotCollectionName}/${snapshot.id}`,
);
const collectionRef = collection(db, url);
const queryRef = query(collectionRef, where(inverse.name, '==', snapshotDocRef));

return relationship.options.filter?.(queryRef, snapshot.record) || queryRef;
const queryRef = query(
collectionRef,
where(inverse.name, '==', snapshotDocRef),
);

return (
relationship.options.filter?.(queryRef, snapshot.record) || queryRef
);
}

const collectionRef = collection(db, url);

return relationship.options.filter?.(collectionRef, snapshot.record) || collectionRef;
return (
relationship.options.filter?.(collectionRef, snapshot.record) ||
collectionRef
);
}
}

Loading