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

Adding transaction support #758

Merged
merged 13 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion __test__/handler-remove-many.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('Test Document Remove Many', () => {
const Cat = model('Cat', CatSchema);
const metadata = getModelMetadata(Cat);
try {
await removeCallback('dummy_id', metadata);
await removeCallback('dummy_id', metadata, {}, {});
} catch (err) {
const error = err as StatusExecution;
const dnf = new DocumentNotFoundError();
Expand Down
10 changes: 9 additions & 1 deletion __test__/testData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ottoman, SearchConsistency } from '../src';
import { LogicalWhereExpr, Ottoman, SearchConsistency, FindOptions, ModelTypes } from '../src';

export const bucketName = 'travel-sample';
export const username = 'Administrator';
Expand All @@ -18,4 +18,12 @@ export const startInTest = async (ottoman: Ottoman): Promise<boolean> => {
return true;
};

export const cleanUp = <T = any>(
model: ModelTypes<T>,
query: LogicalWhereExpr<T> = { _type: model.collectionName },
options: FindOptions = { consistency: SearchConsistency.LOCAL },
) => {
return model.removeMany(query, options);
};

export const consistency = { consistency: SearchConsistency.LOCAL };
43 changes: 43 additions & 0 deletions __test__/transaction-indexes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Schema, model, getDefaultInstance, SearchConsistency } from '../src';
import { delay, startInTest } from './testData';

test('Testing indexes', async () => {
const UserSchema = new Schema({
name: String,
email: String,
card: {
cardNumber: String,
zipCode: String,
},
roles: [{ name: String }],
});

UserSchema.index.findN1qlByName = { by: 'name', options: { limit: 4, select: 'name' } };

const User = model('TransactionUser7', UserSchema);
const ottoman = getDefaultInstance();
await startInTest(ottoman);

const userData = {
name: `index`,
email: '[email protected]',
card: { cardNumber: '424242425252', zipCode: '42424' },
roles: [{ name: 'admin' }],
};

try {
await ottoman.$transactions(async (ctx) => {
await User.create(userData, { transactionContext: ctx });

await delay(2500);

const usersN1ql = await User.findN1qlByName(userData.name, { transactionContext: ctx });
expect(usersN1ql.rows[0].name).toBe(userData.name);
});
} catch (e) {
console.log(e);
}

const usersN1ql = await User.findN1qlByName(userData.name, { consistency: SearchConsistency.LOCAL });
expect(usersN1ql.rows[0].name).toBe(userData.name);
});
Loading
Loading