Skip to content

Commit

Permalink
code refactoring for types and client handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aydrian committed Jul 26, 2023
1 parent f8493d7 commit 1570a35
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 54 deletions.
11 changes: 10 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ module.exports = {
"prettier/prettier": ["error", {}],
"@typescript-eslint/explicit-function-return-type": "off",
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error"
"simple-import-sort/exports": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"warn", // or "error"
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_"
}
]
}
};
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Aydrian Howard

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# i18next Prisma backend plugin

## 📝 License

Copyright © 2023 [Aydrian Howard](https://itsaydrian.com). <br />
This project is [MIT](./LICENSE) licensed.
110 changes: 57 additions & 53 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,90 @@
import { PrismaClient } from "@prisma/client";
import type { ModuleType, Services } from "i18next";
import type {
BackendModule,
InitOptions,
ModuleType,
MultiReadCallback,
ReadCallback,
Services
} from "i18next";

export interface PrismaBackendOptions {
client: PrismaClient;
}
export type BackendOptions = {
client?: PrismaClient;
};

class Backend {
static type: ModuleType;
class Backend implements BackendModule<BackendOptions> {
static type: ModuleType = "backend";
type: "backend";

backendOptions!: PrismaBackendOptions;
client!: PrismaClient;
i18nextOptions!: object;
services!: Services;
MODNAME: string;
client?: PrismaClient;

constructor(
services: Services,
backendOptions: PrismaBackendOptions,
i18nextOptions = {}
backendOptions: BackendOptions,
i18nextOptions: InitOptions = {}
) {
this.MODNAME = "i18next-prisma-backend";
this.init(services, backendOptions, i18nextOptions);
}

getClient() {
return this.client || new PrismaClient();
}

init(
services: Services,
backendOptions: PrismaBackendOptions,
i18nextOptions = {}
_services: Services,
backendOptions: BackendOptions,
_i18nextOptions: InitOptions
) {
// console.log({
// backendOptions: backendOptions ? Object.keys(backendOptions) : {},
// blah: "init",
// i18nextOptions
// });
console.log("i18next Prisma: init");
this.services = services;
this.backendOptions = backendOptions;
this.i18nextOptions = i18nextOptions;
console.log("i18next Prisma: init", {
backendOptions: backendOptions ? Object.keys(backendOptions) : {}
});

if (backendOptions?.client) {
console.log("i18next Prisma: existing Prisma Client");
this.client = backendOptions.client;
} else {
console.log("i18next Prisma: new Prisma Client");
this.client = new PrismaClient();
}
this.client = backendOptions?.client;
}

read(
language: string,
namespace: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback: (err: any, result: any) => void
) {
this.readTranslations(language)
read(language: string, namespace: string, callback: ReadCallback) {
this.readTranslations(language, namespace)
.then((resources) => callback(null, resources))
.catch((err) => {
return callback(err, null);
});
}

async readTranslations(lang: string) {
const result = await this.client.i18n.findMany({
select: { translation: true, word: true },
where: { lang }
async readTranslations(language: string, namespace: string) {
const client = this.getClient();
const result = await client.i18n.findMany({
select: { translation: true, key: true },
where: { language, namespace }
});
const resources = result.reduce((obj, { translation, word }) => {
const resources = result.reduce((obj, { translation, key }) => {
return {
...obj,
[word]: translation
[key]: translation
};
}, {});
return resources;
}

// async create(
// languages: readonly string[],
// namespace: string,
// key: string,
// fallbackValue: string
// ) {
// }
}
readMulti(
_languages: readonly string[],
_namespaces: readonly string[],
_callback: MultiReadCallback
) {
/* return multiple resources - useful eg. for bundling loading in one xhr request */
throw new Error("Not Implemented Yet");
}

Backend.type = "backend";
create(
_languages: readonly string[],
_namespace: string,
_key: string,
_fallbackValue: string
) {
/* save the missing translation */
throw new Error("Not Implemented Yet");
}
}

export default Backend;

0 comments on commit 1570a35

Please sign in to comment.