Skip to content

Commit

Permalink
Merge branch 'main' into chore/add-ttl-and-trig
Browse files Browse the repository at this point in the history
  • Loading branch information
rorlic authored Feb 8, 2024
2 parents 0cfaef1 + 4349e48 commit 458f88d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ENV MEMBER_TYPE=
ENV DATABASE_NAME=
ENV COLLECTION_NAME=
ENV MEMORY=
ENV NO_STORAGE=
# install signal-handler wrapper
RUN apt-get -y install dumb-init
# set start command
Expand All @@ -38,4 +39,4 @@ RUN npm install -g npm@${NPM_TAG}
ENV NODE_ENV production
RUN npm ci --omit=dev
USER node
CMD ["sh", "-c", "node ./server.js --host=0.0.0.0 --port=80 --memory=${MEMORY} --silent=${SILENT} --member-type=${MEMBER_TYPE} --connection-uri=${CONNECTION_URI} --database-name=${DATABASE_NAME} --collection-name=${COLLECTION_NAME}"]
CMD ["sh", "-c", "node ./server.js --host=0.0.0.0 --port=80 --no-storage=${NO_STORAGE} --memory=${MEMORY} --silent=${SILENT} --member-type=${MEMBER_TYPE} --connection-uri=${CONNECTION_URI} --database-name=${DATABASE_NAME} --collection-name=${COLLECTION_NAME}"]
4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 21 additions & 9 deletions src/member-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,30 @@ export class MemberController {
}

public async postMember(member: Member, quads: Quad[]) {
const ids = quads.filter(x => x.predicate.value === ns.rdf.type && x.object.value === this._memberType);
if (!quads) {
throw new Error('Quads cannot be undefined');
}

if (ids && ids.length === 1 && ids[0]) {
const id = ids[0].subject.value;
const exists = await this._storage.exists(id);
if (exists) {
console.warn(`[WARNING] overriding id '${id}'`);
const ids = quads.filter(x => x.predicate.value === ns.rdf.type && x.object.value === this._memberType);

switch (ids.length) {
case 1: {
const id = ids[0]?.subject.value || '';
const exists = await this._storage.exists(id);
if (exists) {
console.warn(`[WARNING] overriding id '${id}'`);
}
return this._storage.insertOrUpdate(id, member);
}
case 0: {
console.warn('[WARNING] missing id:\n', member.body);
break;
}
default: {
console.warn('[WARNING] missing unique id:\n', ids);
break;
}
return this._storage.insertOrUpdate(id, member);
}

console.warn('[WARNING] missing unique id:\n', ids);
return undefined;
}

Expand Down
54 changes: 54 additions & 0 deletions src/no-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Member } from "member";
import { IStorage } from "./storage";

export class NoStorage implements IStorage {
private _count = 0;

get storageTypeName(): string {
return 'None';
}

get memberTypeName(): string {
return '(none)';
}

initialize(): Promise<void> {
return new Promise(resolve => { resolve();});
}

terminate(): Promise<void> {
return new Promise(resolve => { resolve();});
}

count(): Promise<number> {
return new Promise(resolve => resolve(this._count));
}

lastIds(_: number): Promise<string[]> {
return new Promise(resolve => resolve([]));
}

member(_: string): Promise<Member | undefined> {
return new Promise(resolve => resolve(undefined));
}

exists(_: string): Promise<boolean> {
return new Promise(resolve => resolve(false));
}

insertOrUpdate(id: string, _: Member): Promise<string> {
return new Promise(resolve => {
this._count += 1;
resolve(id);
});
}

deleteAll(): Promise<number> {
return new Promise(resolve => {
const count = this._count;
this._count = 0;
resolve(count);
});
}

}
14 changes: 11 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import minimist from 'minimist'
import { Parser, Quad } from 'n3';
import { MongoStorage } from './mongo-storage';
import { MemoryStorage } from './memory-storage';
import { IStorage } from 'storage';
import { IStorage } from './storage';
import { NoStorage } from './no-storage';

interface ParsedMember extends Member{
quads: Quad[];
Expand All @@ -31,8 +32,15 @@ if (!memberType) {
}

const useMemoryStorage: boolean = (/true/i).test(args['memory']);
const storage: IStorage = useMemoryStorage
? new MemoryStorage(collectionName)
const useNoStorage: boolean = (/true/i).test(args['no-storage']);

if(useMemoryStorage && useNoStorage) {
exitWithError('you cannot specify to use both "memory" and "no-storage".');
}

const storage: IStorage =
useMemoryStorage ? new MemoryStorage(collectionName)
: useNoStorage ? new NoStorage()
: new MongoStorage(connectionUri, databaseName, collectionName);

if(!silent) {
Expand Down

0 comments on commit 458f88d

Please sign in to comment.