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

feat: support recursive file search #17

Merged
merged 4 commits into from
Sep 12, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,47 @@ generator client {
provider = "prisma-client-js"
}

generator prismaThirdPartyGenerator {
provider = "prisma-includes-generator"
seperateRelationFields = true
}

enum Status {
PENDING
LIVE
DELETED
REMOVED
}

model Post {
id String @id @default(uuid()) @database.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
status Status
text String
author User @relation(fields: [authorId], references: [email])
authorId String
}

model User {
id String @id @default(uuid()) @database.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
posts Post[]
auth Auth?
friends User[] @relation(name: "friends")
friendRelations User[] @relation(name: "friends")
email String
fullName String

@@id([email])
}

model Post {
id String @id @default(uuid()) @database.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
status Status
text String
author User @relation(fields: [authorId], references: [id])
authorId String
model Auth {
id String @id @default(uuid()) @database.Uuid
hash String
salt String
user User @relation(fields: [userId], references: [email])
userId String
}

```
2 changes: 2 additions & 0 deletions example/models/User.model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { createModel } from "../../dist";
import DateTimeMixin from "../mixins/DateTime.mixin";
import AuthModel from "./auth/Auth.model";
import PostModel from "./Post.model";

export default createModel((UserModel) => {
UserModel
.mixin(DateTimeMixin)
.relation("posts", PostModel, { list: true })
.relation("auth", AuthModel, { optional: true })
.relation("friends", UserModel, { list: true, name: "friends" })
.relation("friendRelations", UserModel, { list: true, name: "friends" })
.string("email")
Expand Down
12 changes: 12 additions & 0 deletions example/models/auth/Auth.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createModel } from "../../../dist";
import UUIDMixin from "../../mixins/UUID.mixin";
import UserModel from "../../models/User.model";

export default createModel((AuthModel) => {
AuthModel
.mixin(UUIDMixin)
.string("hash")
.string("salt")
.relation("user", UserModel, { fields: ["userId"], references: ["email"] })
.string("userId", { unique: true });
});
25 changes: 17 additions & 8 deletions example/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ enum Status {
REMOVED
}

model Post {
id String @id @default(uuid()) @database.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
status Status
text String
author User @relation(fields: [authorId], references: [email])
authorId String
}

model User {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
posts Post[]
auth Auth?
friends User[] @relation(name: "friends")
friendRelations User[] @relation(name: "friends")
email String
Expand All @@ -31,12 +42,10 @@ model User {
@@id([email])
}

model Post {
id String @id @default(uuid()) @database.Uuid
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
status Status
text String
author User @relation(fields: [authorId], references: [email])
authorId String
model Auth {
id String @id @default(uuid()) @database.Uuid
hash String
salt String
user User @relation(fields: [userId], references: [email])
userId String @unique
}
53 changes: 42 additions & 11 deletions lib/util/import.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import { readdir } from "fs/promises";
import { readdir, stat } from "fs/promises";
import { join } from "path";

export const importAllFiles = async (basePath: string, folderName: string) => {
const isDirectory = async (fullPath: string): Promise<boolean> => {
return (await stat(fullPath)).isDirectory();
};

async function asyncFilter<T>(
arr: T[],
predicate: (value: T) => Promise<boolean>
): Promise<T[]> {
const results = await Promise.all(arr.map(predicate));

return arr.filter((_, index) => results[index]);
}

const getAllFilesRecursively = async (
basePath: string,
folderName: string
): Promise<string[]> => {
const directoryPath = join(basePath, folderName);
return readdir(directoryPath)
.then((fileNames) => {
const promises = fileNames.map(
(fileName) => import(join(basePath, folderName, fileName))
);

return Promise.all(promises);
})
.catch(() => void 0);

const fileNames = await readdir(directoryPath);
const directories = await asyncFilter(fileNames, async (fileName) =>
isDirectory(join(directoryPath, fileName))
);
const files = fileNames
.filter((fileName) => !directories.includes(fileName))
.map((file) => join(directoryPath, file));

const filesInDirectories = (
await Promise.all(
directories.map((directory) =>
getAllFilesRecursively(directoryPath, directory)
)
)
).flat();

return [...filesInDirectories, ...files];
};

export const importAllFiles = async (basePath: string, folderName: string) => {
return getAllFilesRecursively(basePath, folderName)
.then((files) => Promise.all(files.map((fileName) => import(fileName))))
.catch(console.error);
};