Skip to content

Commit

Permalink
feat: jsr migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Oct 2, 2024
1 parent b9d1ec0 commit 9331364
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 27 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Publish
on:
workflow_dispatch:

jobs:
publish:
runs-on: ubuntu-latest

permissions:
contents: read
id-token: write

steps:
- uses: actions/checkout@v4

- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: canary

- name: Publish package
run: deno publish --allow-slow-types
17 changes: 17 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
{
"name": "@danet/database",
"version": "0.0.1",
"exports": {
"./mongodb/module.ts": "./mongodb/module.ts",
"./mongodb/repository.ts": "./mongodb/repository.ts",
"./mongodb/service.ts": "./mongodb/service.ts",
"./repository.ts": "./repository.ts",
"./kv/module.ts": "./kv/module.ts",
"./kv/repository.ts": "./kv/repository.ts",
"./kv/service.ts": "./kv/service.ts",
"./import_map.json": "./import_map.json",
"./deno.json": "./deno.json"
},
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"lock": false,
"importMap": "./import_map.json"
}
3 changes: 2 additions & 1 deletion import_map.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"imports": {
"danet/": "https://deno.land/x/danet/"
"@danet/core": "jsr:@danet/core@2",
"@db/mongo": "jsr:@db/mongo"
}
}
2 changes: 1 addition & 1 deletion kv/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from 'danet/mod.ts';
import { Module } from '@danet/core';
import { KvService } from "./service.ts";

@Module({
Expand Down
10 changes: 5 additions & 5 deletions kv/repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { KvService } from "./service.ts";
import { Repository } from "../repository.ts";
import type { KvService } from "./service.ts";
import type { Repository } from "../repository.ts";

export abstract class KvRepository<T extends { _id: string }>
implements Repository<T> {
Expand All @@ -19,7 +19,7 @@ export abstract class KvRepository<T extends { _id: string }>
return items;
}

async getById(id: string) {
async getById(id: string): Promise<T|undefined> {
if (!id) {
return undefined;
}
Expand Down Expand Up @@ -60,7 +60,7 @@ export abstract class KvRepository<T extends { _id: string }>
await transaction
.commit();
}
async updateOne(itemId: string, item: T) {
async updateOne(itemId: string, item: T): Promise<T> {
const itemInDb = await this.kv.client().get([this.collectionName, itemId]);
const itemToInsert = {
...itemInDb,
Expand All @@ -76,7 +76,7 @@ export abstract class KvRepository<T extends { _id: string }>
if (!transactionResult.ok) {
throw new Error("Could update entity");
}
return item;
return itemToInsert;
}

async deleteAll() {
Expand Down
6 changes: 3 additions & 3 deletions kv/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from 'danet/mod.ts';
import { OnAppBootstrap, OnAppClose } from 'danet/src/hook/interfaces.ts';
import { Injectable } from '@danet/core';
import type { OnAppBootstrap, OnAppClose } from '@danet/core/hook';

@Injectable()
export class KvService implements OnAppBootstrap, OnAppClose {
Expand All @@ -11,7 +11,7 @@ export class KvService implements OnAppBootstrap, OnAppClose {
this.kvClient = await Deno.openKv();
}

client() {
client(): Deno.Kv {
return this.kvClient;
}

Expand Down
2 changes: 1 addition & 1 deletion mongodb/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from 'danet/mod.ts';
import { Module } from '@danet/core';
import { MongodbService } from './service.ts';

@Module({
Expand Down
23 changes: 10 additions & 13 deletions mongodb/repository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Repository } from '../repository.ts';
import { Filter, ObjectId } from 'mongo/mod.ts';
import { MongodbService } from './service.ts';
import type { Repository } from '../repository.ts';
import { type Filter, ObjectId, type Document, type Collection } from '@db/mongo';
import type { MongodbService } from './service.ts';

interface Document {
_id: string | ObjectId;
}

export abstract class MongodbRepository<T extends Document>
export abstract class MongodbRepository<T extends Document & { _id: string | ObjectId}>
implements Repository<T> {
constructor(
protected dbService: MongodbService,
Expand All @@ -20,7 +17,7 @@ export abstract class MongodbRepository<T extends Document>
.toArray()).map((obj) => ({ ...obj, _id: obj._id.toString() }));
}

async getOne(filter: Filter<T>) {
async getOne(filter: Filter<T>): Promise<T> {
const obj = await this.dbService.getCollection<T>(this.collectionName)
.findOne(filter);
if (!obj) return undefined;
Expand All @@ -30,7 +27,7 @@ export abstract class MongodbRepository<T extends Document>
};
}

async getById(id: string) {
async getById(id: string): Promise<T> {
const obj = await this.dbService.getCollection<T>(this.collectionName)
.findOne({
_id: new ObjectId(id),
Expand All @@ -42,15 +39,15 @@ export abstract class MongodbRepository<T extends Document>
};
}

async create(obj: T) {
async create(obj: T): Promise<T> {
obj._id = new ObjectId();
await this.dbService.getCollection<T>(
this.collectionName,
).insertOne(obj);
return obj;
}

async updateOne(objId: string, obj: Partial<T>) {
async updateOne(objId: string, obj: Partial<T>): Promise<ReturnType<Collection<T>["updateOne"]>> {
const _id = new ObjectId(objId);
const updated = await this.dbService.getCollection<T>(this.collectionName)
.updateOne(
Expand All @@ -62,13 +59,13 @@ export abstract class MongodbRepository<T extends Document>
return updated;
}

async deleteOne(objId: string) {
deleteOne(objId: string): Promise<number> {
return this.dbService.getCollection<T>(this.collectionName).deleteOne({
_id: new ObjectId(objId),
});
}

async deleteAll() {
deleteAll(): Promise<number> {
return this.dbService.getCollection<T>(this.collectionName).deleteMany({});
}
}
6 changes: 3 additions & 3 deletions mongodb/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from 'danet/mod.ts';
import { OnAppBootstrap, OnAppClose } from 'danet/src/hook/interfaces.ts';
import { Collection, Database, Document, MongoClient } from 'mongo/mod.ts';
import { Injectable } from '@danet/core';
import type { OnAppBootstrap, OnAppClose } from '@danet/core/hook';
import { type Collection, type Database, type Document, MongoClient } from '@db/mongo';

@Injectable()
export class MongodbService implements OnAppBootstrap, OnAppClose {
Expand Down

0 comments on commit 9331364

Please sign in to comment.